1
0
Fork 0
mirror of synced 2024-06-01 18:39:57 +12:00
appwrite/app/controllers/api/storage.php

716 lines
30 KiB
PHP
Raw Normal View History

2019-05-09 18:54:39 +12:00
<?php
2020-06-27 17:48:47 +12:00
global $utopia, $request, $response, $user, $audit, $usage, $project, $projectDB;
2019-05-09 18:54:39 +12:00
use Utopia\Exception;
use Utopia\Response;
2019-05-09 18:54:39 +12:00
use Utopia\Validator\ArrayList;
use Utopia\Validator\WhiteList;
use Utopia\Validator\Range;
use Utopia\Validator\Text;
use Utopia\Validator\HexColor;
use Utopia\Cache\Cache;
use Utopia\Cache\Adapter\Filesystem;
use Appwrite\ClamAV\Network;
use Appwrite\Database\Database;
use Appwrite\Database\Validator\UID;
use Appwrite\Storage\Storage;
2020-06-12 07:36:10 +12:00
use Appwrite\Storage\Device\Local;
use Appwrite\Storage\Validator\File;
use Appwrite\Storage\Validator\FileSize;
use Appwrite\Storage\Validator\Upload;
use Appwrite\Storage\Compression\Algorithms\GZIP;
use Appwrite\Resize\Resize;
use Appwrite\OpenSSL\OpenSSL;
2019-05-09 18:54:39 +12:00
$fileLogos = [ // Based on this list @see http://stackoverflow.com/a/4212908/2299554
2020-02-28 12:24:00 +13:00
'default' => __DIR__.'/../../config/files/none.png',
// Video Files
'video/mp4' => __DIR__.'/../../config/files/video.png',
'video/x-flv' => __DIR__.'/../../config/files/video.png',
'application/x-mpegURL' => __DIR__.'/../../config/files/video.png',
'video/MP2T' => __DIR__.'/../../config/files/video.png',
'video/3gpp' => __DIR__.'/../../config/files/video.png',
'video/quicktime' => __DIR__.'/../../config/files/video.png',
'video/x-msvideo' => __DIR__.'/../../config/files/video.png',
'video/x-ms-wmv' => __DIR__.'/../../config/files/video.png',
// // Microsoft Word
2020-02-29 04:04:09 +13:00
'application/msword' => __DIR__.'/../../config/files/word.png',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => __DIR__.'/../../config/files/word.png',
'application/vnd.openxmlformats-officedocument.wordprocessingml.template' => __DIR__.'/../../config/files/word.png',
'application/vnd.ms-word.document.macroEnabled.12' => __DIR__.'/../../config/files/word.png',
2020-02-28 12:24:00 +13:00
// // Microsoft Excel
2020-02-29 04:04:09 +13:00
'application/vnd.ms-excel' => __DIR__.'/../../config/files/excel.png',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => __DIR__.'/../../config/files/excel.png',
'application/vnd.openxmlformats-officedocument.spreadsheetml.template' => __DIR__.'/../../config/files/excel.png',
'application/vnd.ms-excel.sheet.macroEnabled.12' => __DIR__.'/../../config/files/excel.png',
'application/vnd.ms-excel.template.macroEnabled.12' => __DIR__.'/../../config/files/excel.png',
'application/vnd.ms-excel.addin.macroEnabled.12' => __DIR__.'/../../config/files/excel.png',
'application/vnd.ms-excel.sheet.binary.macroEnabled.12' => __DIR__.'/../../config/files/excel.png',
2020-02-28 12:24:00 +13:00
// // Microsoft Power Point
2020-02-29 04:04:09 +13:00
'application/vnd.ms-powerpoint' => __DIR__.'/../../config/files/ppt.png',
'application/vnd.openxmlformats-officedocument.presentationml.presentation' => __DIR__.'/../../config/files/ppt.png',
'application/vnd.openxmlformats-officedocument.presentationml.template' => __DIR__.'/../../config/files/ppt.png',
'application/vnd.openxmlformats-officedocument.presentationml.slideshow' => __DIR__.'/../../config/files/ppt.png',
'application/vnd.ms-powerpoint.addin.macroEnabled.12' => __DIR__.'/../../config/files/ppt.png',
'application/vnd.ms-powerpoint.presentation.macroEnabled.12' => __DIR__.'/../../config/files/ppt.png',
'application/vnd.ms-powerpoint.template.macroEnabled.12' => __DIR__.'/../../config/files/ppt.png',
'application/vnd.ms-powerpoint.slideshow.macroEnabled.12' => __DIR__.'/../../config/files/ppt.png',
2019-05-09 18:54:39 +12:00
// Adobe PDF
2020-02-28 12:24:00 +13:00
'application/pdf' => __DIR__.'/../../config/files/pdf.png',
2019-05-09 18:54:39 +12:00
];
$inputs = [
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
2019-05-09 18:54:39 +12:00
];
$outputs = [
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
'webp' => 'image/webp',
2019-05-09 18:54:39 +12:00
];
$mimes = [
'image/jpeg',
'image/jpeg',
'image/gif',
'image/png',
'image/webp',
2020-02-28 12:24:00 +13:00
// Video Files
'video/mp4',
'video/x-flv',
'application/x-mpegURL',
'video/MP2T',
'video/3gpp',
'video/quicktime',
'video/x-msvideo',
'video/x-ms-wmv',
2019-05-09 18:54:39 +12:00
// Microsoft Word
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
'application/vnd.ms-word.document.macroEnabled.12',
// Microsoft Excel
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
'application/vnd.ms-excel.sheet.macroEnabled.12',
'application/vnd.ms-excel.template.macroEnabled.12',
'application/vnd.ms-excel.addin.macroEnabled.12',
'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
// Microsoft Power Point
'application/vnd.ms-powerpoint',
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'application/vnd.openxmlformats-officedocument.presentationml.template',
'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
'application/vnd.ms-powerpoint.addin.macroEnabled.12',
'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
'application/vnd.ms-powerpoint.template.macroEnabled.12',
'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
// Microsoft Access
'application/vnd.ms-access',
// Adobe PDF
'application/pdf',
];
2020-06-27 00:28:57 +12:00
$utopia->init(function () use ($project) {
Storage::addDevice('local', new Local(APP_STORAGE_UPLOADS.'/app-'.$project->getId()));
}, 'storage');
2020-02-01 11:34:07 +13:00
$utopia->post('/v1/storage/files')
->desc('Create File')
2020-06-26 06:32:12 +12:00
->groups(['api', 'storage'])
2020-02-01 11:34:07 +13:00
->label('scope', 'files.write')
->label('webhook', 'storage.files.create')
->label('sdk.platform', [APP_PLATFORM_CLIENT, APP_PLATFORM_SERVER])
->label('sdk.namespace', 'storage')
->label('sdk.method', 'createFile')
->label('sdk.description', '/docs/references/storage/create-file.md')
->label('sdk.consumes', 'multipart/form-data')
2020-04-11 06:59:14 +12:00
->label('sdk.methodType', 'upload')
2020-02-10 10:37:28 +13:00
->param('file', [], function () { return new File(); }, 'Binary File.', false)
2020-02-01 11:34:07 +13:00
->param('read', [], function () { return new ArrayList(new Text(64)); }, 'An array of strings with read permissions. By default no user is granted with any read permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.')
->param('write', [], function () { return new ArrayList(new Text(64)); }, 'An array of strings with write permissions. By default no user is granted with any write permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.')
// ->param('folderId', '', function () { return new UID(); }, 'Folder to associate files with.', true)
->action(
function ($file, $read, $write, $folderId = '') use ($request, $response, $user, $projectDB, $webhook, $audit, $usage) {
$file = $request->getFiles('file');
2020-02-17 20:16:11 +13:00
$read = (empty($read)) ? ['user:'.$user->getId()] : $read;
$write = (empty($write)) ? ['user:'.$user->getId()] : $write;
2020-02-01 11:34:07 +13:00
/*
* Validators
*/
//$fileType = new FileType(array(FileType::FILE_TYPE_PNG, FileType::FILE_TYPE_GIF, FileType::FILE_TYPE_JPEG));
$fileSize = new FileSize($request->getServer('_APP_STORAGE_LIMIT', 0));
2020-02-01 11:34:07 +13:00
$upload = new Upload();
if (empty($file)) {
throw new Exception('No file sent', 400);
}
// Make sure we handle a single file and multiple files the same way
2020-06-20 23:20:49 +12:00
$file['name'] = (\is_array($file['name']) && isset($file['name'][0])) ? $file['name'][0] : $file['name'];
$file['tmp_name'] = (\is_array($file['tmp_name']) && isset($file['tmp_name'][0])) ? $file['tmp_name'][0] : $file['tmp_name'];
$file['size'] = (\is_array($file['size']) && isset($file['size'][0])) ? $file['size'][0] : $file['size'];
2020-02-01 11:34:07 +13:00
// Check if file type is allowed (feature for project settings?)
//if (!$fileType->isValid($file['tmp_name'])) {
//throw new Exception('File type not allowed', 400);
//}
// Check if file size is exceeding allowed limit
if (!$fileSize->isValid($file['size'])) {
throw new Exception('File size not allowed', 400);
}
/*
* Models
*/
$device = Storage::getDevice('local');
if (!$upload->isValid($file['tmp_name'])) {
throw new Exception('Invalid file', 403);
}
// Save to storage
$size = $device->getFileSize($file['tmp_name']);
2020-06-20 23:20:49 +12:00
$path = $device->getPath(\uniqid().'.'.\pathinfo($file['name'], PATHINFO_EXTENSION));
2020-02-01 11:34:07 +13:00
if (!$device->upload($file['tmp_name'], $path)) { // TODO deprecate 'upload' and replace with 'move'
throw new Exception('Failed moving file', 500);
}
$mimeType = $device->getFileMimeType($path); // Get mime-type before compression and encryption
2020-06-25 08:59:04 +12:00
if ($request->getServer('_APP_STORAGE_ANTIVIRUS') === 'enabled') { // Check if scans are enabled
$antiVirus = new Network('clamav', 3310);
// Check if file size is exceeding allowed limit
if (!$antiVirus->fileScan($path)) {
$device->delete($path);
throw new Exception('Invalid file', 403);
}
2020-02-01 11:34:07 +13:00
}
// Compression
$compressor = new GZIP();
$data = $device->read($path);
$data = $compressor->compress($data);
$key = $request->getServer('_APP_OPENSSL_KEY_V1');
$iv = OpenSSL::randomPseudoBytes(OpenSSL::cipherIVLength(OpenSSL::CIPHER_AES_128_GCM));
$data = OpenSSL::encrypt($data, OpenSSL::CIPHER_AES_128_GCM, $key, 0, $iv, $tag);
2020-06-25 08:59:04 +12:00
if (!$device->write($path, $data)) {
2020-02-01 11:34:07 +13:00
throw new Exception('Failed to save file', 500);
}
$sizeActual = $device->getFileSize($path);
$file = $projectDB->createDocument([
'$collection' => Database::SYSTEM_COLLECTION_FILES,
'$permissions' => [
'read' => $read,
'write' => $write,
],
2020-06-20 23:20:49 +12:00
'dateCreated' => \time(),
2020-02-01 11:34:07 +13:00
'folderId' => $folderId,
'name' => $file['name'],
'path' => $path,
'signature' => $device->getFileHash($path),
'mimeType' => $mimeType,
'sizeOriginal' => $size,
'sizeActual' => $sizeActual,
'algorithm' => $compressor->getName(),
2020-06-20 23:20:49 +12:00
'token' => \bin2hex(\random_bytes(64)),
2020-02-01 11:34:07 +13:00
'comment' => '',
'fileOpenSSLVersion' => '1',
'fileOpenSSLCipher' => OpenSSL::CIPHER_AES_128_GCM,
2020-06-20 23:20:49 +12:00
'fileOpenSSLTag' => \bin2hex($tag),
'fileOpenSSLIV' => \bin2hex($iv),
2020-02-01 11:34:07 +13:00
]);
if (false === $file) {
throw new Exception('Failed saving file to DB', 500);
}
$webhook
->setParam('payload', $file->getArrayCopy())
;
$audit
->setParam('event', 'storage.files.create')
2020-02-17 20:16:11 +13:00
->setParam('resource', 'storage/files/'.$file->getId())
2020-02-01 11:34:07 +13:00
;
$usage
->setParam('storage', $sizeActual)
;
$response
->setStatusCode(Response::STATUS_CODE_CREATED)
->json($file->getArrayCopy())
;
}
);
2019-05-09 18:54:39 +12:00
$utopia->get('/v1/storage/files')
->desc('List Files')
2020-06-26 06:32:12 +12:00
->groups(['api', 'storage'])
->label('scope', 'files.read')
2020-01-27 19:14:14 +13:00
->label('sdk.platform', [APP_PLATFORM_CLIENT, APP_PLATFORM_SERVER])
2019-05-09 18:54:39 +12:00
->label('sdk.namespace', 'storage')
2020-01-31 09:58:49 +13:00
->label('sdk.method', 'listFiles')
2019-10-08 20:09:35 +13:00
->label('sdk.description', '/docs/references/storage/list-files.md')
->param('search', '', function () { return new Text(256); }, 'Search term to filter your list results.', true)
->param('limit', 25, function () { return new Range(0, 100); }, 'Results limit value. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true)
->param('offset', 0, function () { return new Range(0, 2000); }, 'Results offset. The default value is 0. Use this param to manage pagination.', true)
->param('orderType', 'ASC', function () { return new WhiteList(['ASC', 'DESC']); }, 'Order result by ASC or DESC order.', true)
2019-05-09 18:54:39 +12:00
->action(
function ($search, $limit, $offset, $orderType) use ($response, $projectDB) {
2019-05-09 18:54:39 +12:00
$results = $projectDB->getCollection([
'limit' => $limit,
'offset' => $offset,
'orderField' => 'dateCreated',
'orderType' => $orderType,
'orderCast' => 'int',
'search' => $search,
'filters' => [
'$collection='.Database::SYSTEM_COLLECTION_FILES,
2019-05-09 18:54:39 +12:00
],
]);
2020-06-20 23:20:49 +12:00
$results = \array_map(function ($value) { /* @var $value \Database\Document */
2020-02-17 20:16:11 +13:00
return $value->getArrayCopy(['$id', '$permissions', 'name', 'dateCreated', 'signature', 'mimeType', 'sizeOriginal']);
2019-05-09 18:54:39 +12:00
}, $results);
$response->json(['sum' => $projectDB->getSum(), 'files' => $results]);
}
);
$utopia->get('/v1/storage/files/:fileId')
->desc('Get File')
2020-06-26 06:32:12 +12:00
->groups(['api', 'storage'])
->label('scope', 'files.read')
2020-01-27 19:14:14 +13:00
->label('sdk.platform', [APP_PLATFORM_CLIENT, APP_PLATFORM_SERVER])
2019-05-09 18:54:39 +12:00
->label('sdk.namespace', 'storage')
2020-01-31 09:58:49 +13:00
->label('sdk.method', 'getFile')
2019-10-08 20:09:35 +13:00
->label('sdk.description', '/docs/references/storage/get-file.md')
->param('fileId', '', function () { return new UID(); }, 'File unique ID.')
2019-05-09 18:54:39 +12:00
->action(
function ($fileId) use ($response, $projectDB) {
2019-05-09 18:54:39 +12:00
$file = $projectDB->getDocument($fileId);
2020-02-17 20:16:11 +13:00
if (empty($file->getId()) || Database::SYSTEM_COLLECTION_FILES != $file->getCollection()) {
2019-05-09 18:54:39 +12:00
throw new Exception('File not found', 404);
}
2020-02-17 20:16:11 +13:00
$response->json($file->getArrayCopy(['$id', '$permissions', 'name', 'dateCreated', 'signature', 'mimeType', 'sizeOriginal']));
2019-05-09 18:54:39 +12:00
}
);
$utopia->get('/v1/storage/files/:fileId/preview')
2019-08-25 20:10:28 +12:00
->desc('Get File Preview')
2020-06-26 06:32:12 +12:00
->groups(['api', 'storage'])
->label('scope', 'files.read')
2020-01-27 19:14:14 +13:00
->label('sdk.platform', [APP_PLATFORM_CLIENT, APP_PLATFORM_SERVER])
2019-05-09 18:54:39 +12:00
->label('sdk.namespace', 'storage')
2020-01-31 09:58:49 +13:00
->label('sdk.method', 'getFilePreview')
2019-10-08 20:09:35 +13:00
->label('sdk.description', '/docs/references/storage/get-file-preview.md')
2020-01-15 05:16:24 +13:00
->label('sdk.response.type', 'image/*')
2020-04-11 06:59:14 +12:00
->label('sdk.methodType', 'location')
->param('fileId', '', function () { return new UID(); }, 'File unique ID')
2020-02-10 18:54:26 +13:00
->param('width', 0, function () { return new Range(0, 4000); }, 'Resize preview image width, Pass an integer between 0 to 4000.', true)
->param('height', 0, function () { return new Range(0, 4000); }, 'Resize preview image height, Pass an integer between 0 to 4000.', true)
->param('quality', 100, function () { return new Range(0, 100); }, 'Preview image quality. Pass an integer between 0 to 100. Defaults to 100.', true)
->param('background', '', function () { return new HexColor(); }, 'Preview image background color. Only works with transparent images (png). Use a valid HEX color, no # is needed for prefix.', true)
2020-06-20 23:20:49 +12:00
->param('output', null, function () use ($outputs) { return new WhiteList(\array_merge(\array_keys($outputs), [null])); }, 'Output format type (jpeg, jpg, png, gif and webp).', true)
2019-05-09 18:54:39 +12:00
->action(
2020-03-29 01:42:16 +13:00
function ($fileId, $width, $height, $quality, $background, $output) use ($request, $response, $projectDB, $project, $inputs, $outputs, $fileLogos) {
2019-05-09 18:54:39 +12:00
$storage = 'local';
2020-06-20 23:20:49 +12:00
if (!\extension_loaded('imagick')) {
2019-05-09 18:54:39 +12:00
throw new Exception('Imagick extension is missing', 500);
}
if (!Storage::exists($storage)) {
2019-12-28 21:59:56 +13:00
throw new Exception('No such storage device', 400);
2019-05-09 18:54:39 +12:00
}
2020-06-20 23:20:49 +12:00
if ((\strpos($request->getServer('HTTP_ACCEPT'), 'image/webp') === false) && ('webp' == $output)) { // Fallback webp to jpeg when no browser support
2019-05-09 18:54:39 +12:00
$output = 'jpg';
}
2020-06-20 23:20:49 +12:00
$date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)).' GMT'; // 45 days cache
$key = \md5($fileId.$width.$height.$quality.$background.$storage.$output);
2019-05-09 18:54:39 +12:00
$file = $projectDB->getDocument($fileId);
2019-05-09 18:54:39 +12:00
2020-02-17 20:16:11 +13:00
if (empty($file->getId()) || Database::SYSTEM_COLLECTION_FILES != $file->getCollection()) {
2019-05-09 18:54:39 +12:00
throw new Exception('File not found', 404);
}
$path = $file->getAttribute('path');
2020-06-20 23:20:49 +12:00
$type = \strtolower(\pathinfo($path, PATHINFO_EXTENSION));
2020-02-28 12:24:00 +13:00
$algorithm = $file->getAttribute('algorithm');
$cipher = $file->getAttribute('fileOpenSSLCipher');
2020-02-28 12:24:00 +13:00
$mime = $file->getAttribute('mimeType');
2020-06-25 08:59:04 +12:00
if (!\in_array($mime, $inputs)) {
2020-06-20 23:20:49 +12:00
$path = (\array_key_exists($mime, $fileLogos)) ? $fileLogos[$mime] : $fileLogos['default'];
2020-02-28 12:24:00 +13:00
$algorithm = null;
$cipher = null;
2020-02-29 19:24:46 +13:00
$background = (empty($background)) ? 'eceff1' : $background;
2020-06-20 23:20:49 +12:00
$type = \strtolower(\pathinfo($path, PATHINFO_EXTENSION));
$key = \md5($path.$width.$height.$quality.$background.$storage.$output);
2020-02-28 12:24:00 +13:00
}
2019-05-12 16:56:55 +12:00
$compressor = new GZIP();
$device = Storage::getDevice('local');
2019-05-09 18:54:39 +12:00
2020-06-20 23:20:49 +12:00
if (!\file_exists($path)) {
2020-02-28 12:24:00 +13:00
throw new Exception('File not found', 404);
2019-05-09 18:54:39 +12:00
}
2020-02-20 01:41:23 +13:00
$cache = new Cache(new Filesystem(APP_STORAGE_CACHE.'/app-'.$project->getId())); // Limit file number or size
$data = $cache->load($key, 60 * 60 * 24 * 30 * 3 /* 3 months */);
2019-05-09 18:54:39 +12:00
if ($data) {
2019-05-09 18:54:39 +12:00
$output = (empty($output)) ? $type : $output;
$response
2020-06-20 23:20:49 +12:00
->setContentType((\in_array($output, $outputs)) ? $outputs[$output] : $outputs['jpg'])
2019-05-09 18:54:39 +12:00
->addHeader('Expires', $date)
->addHeader('X-Appwrite-Cache', 'hit')
2020-02-17 20:16:11 +13:00
->send($data)
2019-05-09 18:54:39 +12:00
;
2020-02-17 20:16:11 +13:00
return;
2019-05-09 18:54:39 +12:00
}
$source = $device->read($path);
if (!empty($cipher)) { // Decrypt
2019-05-09 18:54:39 +12:00
$source = OpenSSL::decrypt(
$source,
$file->getAttribute('fileOpenSSLCipher'),
$request->getServer('_APP_OPENSSL_KEY_V'.$file->getAttribute('fileOpenSSLVersion')),
2019-05-09 18:54:39 +12:00
0,
2020-06-20 23:20:49 +12:00
\hex2bin($file->getAttribute('fileOpenSSLIV')),
\hex2bin($file->getAttribute('fileOpenSSLTag'))
2019-05-09 18:54:39 +12:00
);
}
if (!empty($algorithm)) {
2019-05-09 18:54:39 +12:00
$source = $compressor->decompress($source);
}
$resize = new Resize($source);
$resize->crop((int) $width, (int) $height);
2019-05-09 18:54:39 +12:00
if (!empty($background)) {
$resize->setBackground('#'.$background);
2019-05-09 18:54:39 +12:00
}
$output = (empty($output)) ? $type : $output;
$response
->setContentType($outputs[$output])
->addHeader('Expires', $date)
->addHeader('X-Appwrite-Cache', 'miss')
2020-02-17 20:16:11 +13:00
->send('')
2019-05-09 18:54:39 +12:00
;
$data = $resize->output($output, $quality);
$cache->save($key, $data);
echo $data;
unset($resize);
}
);
$utopia->get('/v1/storage/files/:fileId/download')
2019-08-25 20:10:28 +12:00
->desc('Get File for Download')
2020-06-26 06:32:12 +12:00
->groups(['api', 'storage'])
->label('scope', 'files.read')
2020-01-27 19:14:14 +13:00
->label('sdk.platform', [APP_PLATFORM_CLIENT, APP_PLATFORM_SERVER])
2019-05-09 18:54:39 +12:00
->label('sdk.namespace', 'storage')
2020-01-31 09:58:49 +13:00
->label('sdk.method', 'getFileDownload')
2019-10-08 20:09:35 +13:00
->label('sdk.description', '/docs/references/storage/get-file-download.md')
2020-01-15 05:16:24 +13:00
->label('sdk.response.type', '*')
2020-04-11 06:59:14 +12:00
->label('sdk.methodType', 'location')
->param('fileId', '', function () { return new UID(); }, 'File unique ID.')
2019-05-09 18:54:39 +12:00
->action(
function ($fileId) use ($response, $request, $projectDB) {
2019-05-09 18:54:39 +12:00
$file = $projectDB->getDocument($fileId);
2020-02-17 20:16:11 +13:00
if (empty($file->getId()) || Database::SYSTEM_COLLECTION_FILES != $file->getCollection()) {
2019-05-09 18:54:39 +12:00
throw new Exception('File not found', 404);
}
$path = $file->getAttribute('path', '');
2020-06-20 23:20:49 +12:00
if (!\file_exists($path)) {
throw new Exception('File not found in '.$path, 404);
2019-05-09 18:54:39 +12:00
}
$compressor = new GZIP();
$device = Storage::getDevice('local');
2019-05-09 18:54:39 +12:00
$source = $device->read($path);
if (!empty($file->getAttribute('fileOpenSSLCipher'))) { // Decrypt
2019-05-09 18:54:39 +12:00
$source = OpenSSL::decrypt(
$source,
$file->getAttribute('fileOpenSSLCipher'),
$request->getServer('_APP_OPENSSL_KEY_V'.$file->getAttribute('fileOpenSSLVersion')),
2019-05-09 18:54:39 +12:00
0,
2020-06-20 23:20:49 +12:00
\hex2bin($file->getAttribute('fileOpenSSLIV')),
\hex2bin($file->getAttribute('fileOpenSSLTag'))
2019-05-09 18:54:39 +12:00
);
}
$source = $compressor->decompress($source);
// Response
$response
->setContentType($file->getAttribute('mimeType'))
->addHeader('Content-Disposition', 'attachment; filename="'.$file->getAttribute('name', '').'"')
2020-06-20 23:20:49 +12:00
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)).' GMT') // 45 days cache
->addHeader('X-Peak', \memory_get_peak_usage())
2019-05-09 18:54:39 +12:00
->send($source)
;
}
);
$utopia->get('/v1/storage/files/:fileId/view')
2019-08-25 20:10:28 +12:00
->desc('Get File for View')
2020-06-26 06:32:12 +12:00
->groups(['api', 'storage'])
->label('scope', 'files.read')
2020-01-27 19:14:14 +13:00
->label('sdk.platform', [APP_PLATFORM_CLIENT, APP_PLATFORM_SERVER])
2019-05-09 18:54:39 +12:00
->label('sdk.namespace', 'storage')
2020-01-31 09:58:49 +13:00
->label('sdk.method', 'getFileView')
2019-10-08 20:09:35 +13:00
->label('sdk.description', '/docs/references/storage/get-file-view.md')
2020-01-15 05:16:24 +13:00
->label('sdk.response.type', '*')
2020-04-11 06:59:14 +12:00
->label('sdk.methodType', 'location')
->param('fileId', '', function () { return new UID(); }, 'File unique ID.')
->param('as', '', function () { return new WhiteList(['pdf', /*'html',*/ 'text']); }, 'Choose a file format to convert your file to. Currently you can only convert word and pdf files to pdf or txt. This option is currently experimental only, use at your own risk.', true)
2019-05-09 18:54:39 +12:00
->action(
function ($fileId, $as) use ($response, $request, $projectDB, $mimes) {
2019-05-09 18:54:39 +12:00
$file = $projectDB->getDocument($fileId);
2020-02-17 20:16:11 +13:00
if (empty($file->getId()) || Database::SYSTEM_COLLECTION_FILES != $file->getCollection()) {
2019-05-09 18:54:39 +12:00
throw new Exception('File not found', 404);
}
$path = $file->getAttribute('path', '');
2020-06-20 23:20:49 +12:00
if (!\file_exists($path)) {
throw new Exception('File not found in '.$path, 404);
2019-05-09 18:54:39 +12:00
}
$compressor = new GZIP();
$device = Storage::getDevice('local');
2019-05-09 18:54:39 +12:00
$contentType = 'text/plain';
2020-06-20 23:20:49 +12:00
if (\in_array($file->getAttribute('mimeType'), $mimes)) {
2019-05-09 18:54:39 +12:00
$contentType = $file->getAttribute('mimeType');
}
$source = $device->read($path);
if (!empty($file->getAttribute('fileOpenSSLCipher'))) { // Decrypt
2019-05-09 18:54:39 +12:00
$source = OpenSSL::decrypt(
$source,
$file->getAttribute('fileOpenSSLCipher'),
$request->getServer('_APP_OPENSSL_KEY_V'.$file->getAttribute('fileOpenSSLVersion')),
2019-05-09 18:54:39 +12:00
0,
2020-06-20 23:20:49 +12:00
\hex2bin($file->getAttribute('fileOpenSSLIV')),
\hex2bin($file->getAttribute('fileOpenSSLTag'))
2019-05-09 18:54:39 +12:00
);
}
$output = $compressor->decompress($source);
$fileName = $file->getAttribute('name', '');
2019-05-09 18:54:39 +12:00
$contentTypes = [
'pdf' => 'application/pdf',
'text' => 'text/plain',
2019-05-09 18:54:39 +12:00
];
2020-06-20 23:20:49 +12:00
$contentType = (\array_key_exists($as, $contentTypes)) ? $contentTypes[$as] : $contentType;
2019-05-09 18:54:39 +12:00
// Response
$response
->setContentType($contentType)
->addHeader('Content-Security-Policy', 'script-src none;')
->addHeader('X-Content-Type-Options', 'nosniff')
->addHeader('Content-Disposition', 'inline; filename="'.$fileName.'"')
2020-06-20 23:20:49 +12:00
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)).' GMT') // 45 days cache
->addHeader('X-Peak', \memory_get_peak_usage())
2019-05-09 18:54:39 +12:00
->send($output)
;
}
);
2019-08-29 00:29:45 +12:00
$utopia->put('/v1/storage/files/:fileId')
->desc('Update File')
2020-06-26 06:32:12 +12:00
->groups(['api', 'storage'])
2019-08-29 00:29:45 +12:00
->label('scope', 'files.write')
->label('webhook', 'storage.files.update')
2020-01-27 19:14:14 +13:00
->label('sdk.platform', [APP_PLATFORM_CLIENT, APP_PLATFORM_SERVER])
2019-08-29 00:29:45 +12:00
->label('sdk.namespace', 'storage')
2020-01-31 09:58:49 +13:00
->label('sdk.method', 'updateFile')
2019-10-08 20:09:35 +13:00
->label('sdk.description', '/docs/references/storage/update-file.md')
->param('fileId', '', function () { return new UID(); }, 'File unique ID.')
2019-11-03 18:02:50 +13:00
->param('read', [], function () { return new ArrayList(new Text(64)); }, 'An array of strings with read permissions. By default no user is granted with any read permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.')
->param('write', [], function () { return new ArrayList(new Text(64)); }, 'An array of strings with write permissions. By default no user is granted with any write permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.')
2019-11-18 09:33:02 +13:00
//->param('folderId', '', function () { return new UID(); }, 'Folder to associate files with.', true)
2019-08-29 00:29:45 +12:00
->action(
function ($fileId, $read, $write, $folderId = '') use ($response, $projectDB, $audit, $webhook) {
2019-08-29 00:29:45 +12:00
$file = $projectDB->getDocument($fileId);
2020-02-17 20:16:11 +13:00
if (empty($file->getId()) || Database::SYSTEM_COLLECTION_FILES != $file->getCollection()) {
2019-08-29 00:29:45 +12:00
throw new Exception('File not found', 404);
}
2020-06-20 23:20:49 +12:00
$file = $projectDB->updateDocument(\array_merge($file->getArrayCopy(), [
2019-08-29 00:29:45 +12:00
'$permissions' => [
'read' => $read,
'write' => $write,
],
'folderId' => $folderId,
]));
if (false === $file) {
2019-08-29 00:29:45 +12:00
throw new Exception('Failed saving file to DB', 500);
}
$webhook
->setParam('payload', $file->getArrayCopy())
;
$audit
->setParam('event', 'storage.files.update')
2020-02-17 20:16:11 +13:00
->setParam('resource', 'storage/files/'.$file->getId())
;
2019-08-29 00:29:45 +12:00
$response->json($file->getArrayCopy());
}
);
2019-05-09 18:54:39 +12:00
$utopia->delete('/v1/storage/files/:fileId')
->desc('Delete File')
2020-06-26 06:32:12 +12:00
->groups(['api', 'storage'])
2019-08-14 23:53:07 +12:00
->label('scope', 'files.write')
->label('webhook', 'storage.files.delete')
2020-01-27 19:14:14 +13:00
->label('sdk.platform', [APP_PLATFORM_CLIENT, APP_PLATFORM_SERVER])
2019-05-09 18:54:39 +12:00
->label('sdk.namespace', 'storage')
2020-01-31 09:58:49 +13:00
->label('sdk.method', 'deleteFile')
2019-10-08 20:09:35 +13:00
->label('sdk.description', '/docs/references/storage/delete-file.md')
->param('fileId', '', function () { return new UID(); }, 'File unique ID.')
2019-05-09 18:54:39 +12:00
->action(
function ($fileId) use ($response, $projectDB, $webhook, $audit, $usage) {
2019-05-09 18:54:39 +12:00
$file = $projectDB->getDocument($fileId);
2020-02-17 20:16:11 +13:00
if (empty($file->getId()) || Database::SYSTEM_COLLECTION_FILES != $file->getCollection()) {
2019-05-09 18:54:39 +12:00
throw new Exception('File not found', 404);
}
$device = Storage::getDevice('local');
if ($device->delete($file->getAttribute('path', ''))) {
if (!$projectDB->deleteDocument($fileId)) {
2019-05-09 18:54:39 +12:00
throw new Exception('Failed to remove file from DB', 500);
}
}
$webhook
->setParam('payload', $file->getArrayCopy())
;
2019-05-09 18:54:39 +12:00
$audit
->setParam('event', 'storage.files.delete')
2020-02-17 20:16:11 +13:00
->setParam('resource', 'storage/files/'.$file->getId())
2019-05-09 18:54:39 +12:00
;
$usage
->setParam('storage', $file->getAttribute('size', 0) * -1)
;
$response->noContent();
}
);
// $utopia->get('/v1/storage/files/:fileId/scan')
// ->desc('Scan Storage')
2020-06-26 06:32:12 +12:00
// ->groups(['api', 'storage'])
// ->label('scope', 'god')
// ->label('sdk.platform', [APP_PLATFORM_CLIENT, APP_PLATFORM_SERVER])
// ->label('sdk.namespace', 'storage')
// ->label('sdk.method', 'getFileScan')
// ->label('sdk.hide', true)
// ->param('fileId', '', function () { return new UID(); }, 'File unique ID.')
// ->param('storage', 'local', function () { return new WhiteList(['local']);})
// ->action(
// function ($fileId, $storage) use ($response, $request, $projectDB) {
// $file = $projectDB->getDocument($fileId);
// if (empty($file->getId()) || Database::SYSTEM_COLLECTION_FILES != $file->getCollection()) {
// throw new Exception('File not found', 404);
// }
// $path = $file->getAttribute('path', '');
// if (!file_exists($path)) {
// throw new Exception('File not found in '.$path, 404);
// }
// $compressor = new GZIP();
// $device = Storage::getDevice($storage);
// $source = $device->read($path);
// if (!empty($file->getAttribute('fileOpenSSLCipher'))) { // Decrypt
// $source = OpenSSL::decrypt(
// $source,
// $file->getAttribute('fileOpenSSLCipher'),
// $request->getServer('_APP_OPENSSL_KEY_V'.$file->getAttribute('fileOpenSSLVersion')),
// 0,
// hex2bin($file->getAttribute('fileOpenSSLIV')),
// hex2bin($file->getAttribute('fileOpenSSLTag'))
// );
// }
// $source = $compressor->decompress($source);
// $antiVirus = new Network('clamav', 3310);
// //var_dump($antiVirus->ping());
// //var_dump($antiVirus->version());
// //var_dump($antiVirus->fileScan('/storage/uploads/app-1/5/9/f/e/59fecaed49645.pdf'));
// //$response->json($antiVirus->continueScan($device->getRoot()));
// }
// );