storage-backend: Added json interface to list transferred files

This commit is contained in:
steckbrief 2017-05-04 16:23:36 +02:00
parent fd7ea5c319
commit a564fd0a7a
3 changed files with 120 additions and 5 deletions

View file

@ -11,7 +11,7 @@
* size
* content_type
* user_jid
* receipient_jid
* recipient_jid
* 403: In case the XMPP Server Key is not valid
* 406:
* File is empty (error code: 1)
@ -50,6 +50,7 @@
include_once(__DIR__.'/lib/functions.common.inc.php');
include_once(__DIR__.'/lib/functions.http.inc.php');
include_once(__DIR__.'/lib/functions.filetransfer.inc.php');
include_once(__DIR__.'/lib/xmpp.util.inc.php');
$method = $_SERVER['REQUEST_METHOD'];
// Load configuration
@ -74,6 +75,10 @@ switch ($method) {
}
switch ($slotType) {
case 'list':
$slots = readSlots($userJid);
$result = ['list' => $slots];
break;
case 'delete':
// Check if all parameters needed for an delete are present - return 400 (bad request) if a parameter is missing / empty
$fileURL = getMandatoryPostParameter('file_url');
@ -104,7 +109,7 @@ switch ($method) {
$filename = rawurlencode(getMandatoryPostParameter('filename'));
$filesize = getMandatoryPostParameter('size');
$mimeType = getOptionalPostParameter('content_type');
$receipientJid = getMandatoryPostParameter('receipient_jid');
$recipientJid = getMandatoryPostParameter('recipient_jid');
// check file name - return 406 (not acceptable) if file contains invalid characters
foreach ($config['invalid_characters_in_filename'] as $invalidCharacter) {
@ -122,7 +127,7 @@ switch ($method) {
}
// generate slot uuid, register slot uuid and expected file size and expected mime type
$slotUUID = generate_uuid();
registerSlot($slotUUID, $filename, $filesize, $mimeType, $userJid, $receipientJid, $config);
registerSlot($slotUUID, $filename, $filesize, $mimeType, $userJid, $recipientJid, $config);
if (!mkdir(getUploadFilePath($slotUUID, $config))) {
sendHttpReturnCodeAndJson(500, "Could not create directory for upload.");
}

View file

@ -16,8 +16,49 @@ function getUploadFilePath($slotUUID, $config, $filename = NULL) {
}
function loadSlotParameters($slotUUID, $config) {
$slotParameters = require(getSlotFilePath($slotUUID, $config));
$slotFilePath = getSlotFilePath($slotUUID, $config);
$slotParameters = require($slotFilePath);
$slotParameters['filename'] = $slotParameters['filename'];
$slotParameters['creation_time'] = filemtime($slotFilePath);
return $slotParameters;
}
}
function readSlots($jid) {
global $config;
$jid = getBareJid($jid);
$slots = array();
if ($handle = opendir($config['slot_registry_dir'])) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && $entry != ".htaccess") {
$slotUUID = $entry;
$params = loadSlotParameters($slotUUID, $config);
$senderBareJid = getBareJid($params['user_jid']);
$recipientBareJid = (array_key_exists('receipient_jid', $params)) ? getBareJid($params['receipient_jid']) : '';
if ($senderBareJid == $jid || $recipientBareJid == $jid) {
$filePath = getUploadFilePath($slotUUID, $config, $params['filename']);
$file = [];
$fileExists = file_exists($filePath);
$file['url'] = "";
$file['sent_time'] = $params['creation_time'];
if ($fileExists) {
$file['url'] = $config['base_url_get'].$slotUUID.'/'.$params['filename'];
}
$file['fileinfo'] = [];
$file['fileinfo']['filename'] = $params['filename'];
$file['fileinfo']['filesize'] = $params['filesize'];
$file['fileinfo']['content_type'] = $params['content_type'];
$file['sender_jid'] = $senderBareJid;
$file['recipient_jid'] = $recipientBareJid;
if (null == $file['receipient_jid']) {
$file['receipient_jid'] = "";
}
$slots[] = $file;
}
}
}
}
return $slots;
}

View file

@ -0,0 +1,69 @@
<?php
/*
* xmpp jid util functions.
*/
function getJidDomain($jid) {
if (null == $jid) {
return null;
}
$atIndex = strpos($jid, '@');
$slashIndex = strpos($jid, '/');
if ($slashIndex !== false) {
if ($slashIndex > $atIndex) {// 'local@domain.foo/resource' and 'local@domain.foo/res@otherres' case
return substr($jid, $atIndex + 1, $slashIndex - $atIndex + 1);
} else {// 'domain.foo/res@otherres' case
return substr($jid, 0, $slashIndex);
}
} else {
return substr($jid, $atIndex + 1);
}
}
function getJidLocalPart($jid) {
if ($jid == null) {
return null;
}
$atIndex = strpos($jid, '@');
if ($atIndex === false || $atIndex == 0) {
return "";
}
$slashIndex = strpos($jid, '/');
if ($slashIndex !== false && $slashIndex < $atIndex) {
return "";
} else {
return substr($jid, 0, $atIndex);
}
}
function getBareJid($jid) {
if ($jid == null) {
return null;
}
$slashIndex = strpos($jid, '/');
if ($slashIndex === false) {
return $jid;
} else if ($slashIndex == 0) {
return "";
} else {
return substr($jid, 0, $slashIndex);
}
}
function getResource($jid) {
if ($jid == null) {
return null;
}
$slashIndex = strpos($jid, '/');
if ($slashIndex + 1 > strlen($jid) || $slashIndex === false) {
return "";
} else {
return substr($jid, $slashIndex + 1);
}
}