adds API method to get spec and implementation version of the storage backend

This commit is contained in:
steckbrief 2018-10-17 22:10:42 +02:00
parent bc439cd988
commit 3973a5f737
4 changed files with 64 additions and 14 deletions

View file

@ -14,5 +14,7 @@ return [
'delete_token_validity' => 5 * 60, 'delete_token_validity' => 5 * 60,
// Flag to whether deletion is only allowed by creator or anybody // Flag to whether deletion is only allowed by creator or anybody
'delete_only_by_creator' => true, 'delete_only_by_creator' => true,
// The default count of items returned when a file list is requested
'list_default_limit' => 30,
]; ];
?> ?>

View file

@ -67,8 +67,8 @@ switch ($method) {
case 'POST': case 'POST':
// parse post parameters // parse post parameters
// check if all parameters are present - return 400 (bad request) if a parameter is missing / empty // check if all parameters are present - return 400 (bad request) if a parameter is missing / empty
$xmppServerKey = getMandatoryPostParameter('xmpp_server_key'); $xmppServerKey = getMandatoryPost('xmpp_server_key');
$userJid = getMandatoryPostParameter('user_jid'); $userJid = getMandatoryPost('user_jid');
$slotType = getOptionalPostParameter('slot_type', 'upload'); $slotType = getOptionalPostParameter('slot_type', 'upload');
// Check if xmppServerKey is allowed to request slots // Check if xmppServerKey is allowed to request slots
@ -78,14 +78,16 @@ switch ($method) {
switch ($slotType) { switch ($slotType) {
case 'list': case 'list':
$slots = readSlots($userJid); $limit = getOptionalPostParameter('limit', $config['list_default_limit']);
$result = ['list' => $slots]; $offset = getOptionalPostParameter('offset', 0);
$files = listFiles($userJid, $limit, $offset);
$result = ['list' => $files];
break; break;
case 'upload': case 'upload':
default: default:
// Check if all parameters needed for an upload are present - return 400 (bad request) if a parameter is missing / empty // Check if all parameters needed for an upload are present - return 400 (bad request) if a parameter is missing / empty
$filename = rawurlencode(getMandatoryPostParameter('filename')); $filename = rawurlencode(getMandatoryPost('filename'));
$filesize = getMandatoryPostParameter('size'); $filesize = getMandatoryPost('size');
$mimeType = getOptionalPostParameter('content_type'); $mimeType = getOptionalPostParameter('content_type');
$recipientJid = getOptionalPostParameter('recipient_jid', 'Unknown'); // Optional for backwards compatibility (xep-0363 v0.1) $recipientJid = getOptionalPostParameter('recipient_jid', 'Unknown'); // Optional for backwards compatibility (xep-0363 v0.1)
@ -143,7 +145,8 @@ switch ($method) {
sendHttpReturnCodeAndJson(403, "Uploaded file size differs from requested slot size."); sendHttpReturnCodeAndJson(403, "Uploaded file size differs from requested slot size.");
} }
// check actual mime type with registered mime type // check actual mime type with registered mime type
if (!is_null($slotParameters['content_type']) && !empty($slotParameters['content_type']) && mime_content_type($uploadFilePath) != $slotParameters['content_type']) { $uploadedContentType = mime_content_type($uploadFilePath);
if (!is_null($slotParameters['content_type']) && !empty($slotParameters['content_type']) && $uploadedContentType != $slotParameters['content_type']) {
unlink($uploadFilePath); unlink($uploadFilePath);
sendHttpReturnCodeAndJson(403, "Uploaded file content type differs from requested slot content type."); sendHttpReturnCodeAndJson(403, "Uploaded file content type differs from requested slot content type.");
} }
@ -193,6 +196,17 @@ switch ($method) {
sendHttpReturnCodeAndJson(500, "Could not delete file."); sendHttpReturnCodeAndJson(500, "Could not delete file.");
} }
break; break;
case 'GET':
$actionParameter = getMandatoryGet('action');
switch ($actionParameter) {
case 'version':
echo json_encode(require_once(__DIR__.'/lib/version.inc.php'));
break;
default:
sendHttpReturnCodeAndJson(403, "Access not allowed.");
}
break;
default: default:
sendHttpReturnCodeAndJson(403, "Access not allowed."); sendHttpReturnCodeAndJson(403, "Access not allowed.");
break; break;
@ -212,12 +226,12 @@ function checkFilenameParameter($filename, $slotParameters) {
return $slotParameters['filename'] == $filename; return $slotParameters['filename'] == $filename;
} }
function getMandatoryPostParameter($parameterName) { function getMandatoryPost($parameterName) {
$parameter = $_POST[$parameterName]; return getMandatoryPostParameter($parameterName, ['msg' => 'Missing parameter.', 'err_code' => 4, 'parameters' => ['missing_parameter' => $parameterName]], true);
if (!isset($parameter) || is_null($parameter) || empty($parameter)) { }
sendHttpReturnCodeAndJson(400, ['msg' => 'Missing parameter.', 'err_code' => 4, 'parameters' => ['missing_parameter' => $parameterName]]);
} function getMandatoryGet($parameterName) {
return $parameter; return getMandatoryGetParameter($parameterName, ['msg' => 'Missing parameter.', 'err_code' => 4, 'parameters' => ['missing_parameter' => $parameterName]], true);
} }
function getUUIDFromUri($uri) { function getUUIDFromUri($uri) {

View file

@ -24,7 +24,35 @@ function loadSlotParameters($slotUUID, $config) {
return $slotParameters; return $slotParameters;
} }
function readSlots($jid) { function listFiles($jid, $limit = -1, $offset = 0) {
// Read complete set of existing slots per jid (unsorted)
$slots = readSlots($jid, $limit, $offset);
// Sort ascending by timestamp
usort($slots, function($a, $b) {
return $a['sent_time'] - $b['sent_time'];
});
// Select requested slot subset
$offsetCounter = 0;
$resultSet = array();
foreach ($slots as $slot) {
if (0 < $offset && $offsetCounter < $offset) {
$offsetCounter++;
continue;
}
$resultSet[] = $slot;
if (0 < $limit && $limit == count($resultSet)) {
break;
}
}
return ['count' => count($slots),
'hasMore' => $offset + count($resultSet) < count($slots),
'files' => $resultSet];
}
function readSlots($jid, $limit = -1, $offset = 0) {
global $config; global $config;
$jid = getBareJid($jid); $jid = getBareJid($jid);
@ -60,5 +88,6 @@ function readSlots($jid) {
} }
} }
} }
return $slots; return $slots;
} }

View file

@ -0,0 +1,5 @@
<?php
return [
'spec' => '0.3',
'impl' => '0.3-dev'
];