saving file output to cache instead of decoding data to json
This commit is contained in:
parent
bbc9bb823d
commit
a6768bdc32
3 changed files with 36 additions and 21 deletions
|
@ -3206,6 +3206,28 @@ $collections = [
|
||||||
'array' => false,
|
'array' => false,
|
||||||
'filters' => [],
|
'filters' => [],
|
||||||
],
|
],
|
||||||
|
[
|
||||||
|
'$id' => 'resourceType',
|
||||||
|
'type' => Database::VAR_STRING,
|
||||||
|
'format' => '',
|
||||||
|
'size' => 255,
|
||||||
|
'signed' => true,
|
||||||
|
'required' => false,
|
||||||
|
'default' => null,
|
||||||
|
'array' => false,
|
||||||
|
'filters' => [],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'$id' => ID::custom('mimeType'),
|
||||||
|
'type' => Database::VAR_STRING,
|
||||||
|
'format' => '',
|
||||||
|
'size' => 255, // https://tools.ietf.org/html/rfc4288#section-4.2
|
||||||
|
'signed' => true,
|
||||||
|
'required' => false,
|
||||||
|
'default' => null,
|
||||||
|
'array' => false,
|
||||||
|
'filters' => [],
|
||||||
|
],
|
||||||
[
|
[
|
||||||
'$id' => 'accessedAt',
|
'$id' => 'accessedAt',
|
||||||
'type' => Database::VAR_DATETIME,
|
'type' => Database::VAR_DATETIME,
|
||||||
|
|
|
@ -945,6 +945,8 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/preview')
|
||||||
|
|
||||||
$data = $image->output($output, $quality);
|
$data = $image->output($output, $quality);
|
||||||
|
|
||||||
|
unset($image);
|
||||||
|
|
||||||
$contentType = (\array_key_exists($output, $outputs)) ? $outputs[$output] : $outputs['jpg'];
|
$contentType = (\array_key_exists($output, $outputs)) ? $outputs[$output] : $outputs['jpg'];
|
||||||
|
|
||||||
$response
|
$response
|
||||||
|
@ -952,8 +954,6 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/preview')
|
||||||
->setContentType($contentType)
|
->setContentType($contentType)
|
||||||
->file($data)
|
->file($data)
|
||||||
;
|
;
|
||||||
|
|
||||||
unset($image);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
App::get('/v1/storage/buckets/:bucketId/files/:fileId/download')
|
App::get('/v1/storage/buckets/:bucketId/files/:fileId/download')
|
||||||
|
|
|
@ -195,28 +195,25 @@ App::init()
|
||||||
$database->setProject($project);
|
$database->setProject($project);
|
||||||
|
|
||||||
$dbForProject->on(Database::EVENT_DOCUMENT_CREATE, fn ($event, Document $document) => $databaseListener($event, $document, $usage));
|
$dbForProject->on(Database::EVENT_DOCUMENT_CREATE, fn ($event, Document $document) => $databaseListener($event, $document, $usage));
|
||||||
|
|
||||||
$dbForProject->on(Database::EVENT_DOCUMENT_DELETE, fn ($event, Document $document) => $databaseListener($event, $document, $usage));
|
$dbForProject->on(Database::EVENT_DOCUMENT_DELETE, fn ($event, Document $document) => $databaseListener($event, $document, $usage));
|
||||||
|
|
||||||
$useCache = $route->getLabel('cache', false);
|
$useCache = $route->getLabel('cache', false);
|
||||||
|
|
||||||
if ($useCache) {
|
if ($useCache) {
|
||||||
$key = md5($request->getURI() . implode('*', $request->getParams())) . '*' . APP_CACHE_BUSTER;
|
$key = md5($request->getURI() . implode('*', $request->getParams())) . '*' . APP_CACHE_BUSTER;
|
||||||
|
$cacheLog = $dbForProject->getDocument('cache', $key);
|
||||||
$cache = new Cache(
|
$cache = new Cache(
|
||||||
new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId())
|
new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId())
|
||||||
);
|
);
|
||||||
$timestamp = 60 * 60 * 24 * 30;
|
$timestamp = 60 * 60 * 24 * 30;
|
||||||
$data = $cache->load($key, $timestamp);
|
$data = $cache->load($key, $timestamp);
|
||||||
|
|
||||||
if (!empty($data)) {
|
if (!empty($data) && !empty($cacheLog)) {
|
||||||
$data = json_decode($data, true);
|
$parts = explode('/', $cacheLog->getAttribute('resourceType'));
|
||||||
$parts = explode('/', $data['resourceType']);
|
|
||||||
$type = $parts[0] ?? null;
|
$type = $parts[0] ?? null;
|
||||||
|
|
||||||
if ($type === 'bucket') {
|
if ($type === 'bucket') {
|
||||||
$bucketId = $parts[1] ?? null;
|
$bucketId = $parts[1] ?? null;
|
||||||
|
$bucket = Authorization::skip(fn () => $dbForProject->getDocument('buckets', $bucketId));
|
||||||
$bucket = Authorization::skip(fn () => $dbForProject->getDocument('buckets', $bucketId));
|
|
||||||
|
|
||||||
if ($bucket->isEmpty() || (!$bucket->getAttribute('enabled') && $mode !== APP_MODE_ADMIN)) {
|
if ($bucket->isEmpty() || (!$bucket->getAttribute('enabled') && $mode !== APP_MODE_ADMIN)) {
|
||||||
throw new Exception(Exception::STORAGE_BUCKET_NOT_FOUND);
|
throw new Exception(Exception::STORAGE_BUCKET_NOT_FOUND);
|
||||||
|
@ -225,11 +222,12 @@ App::init()
|
||||||
$fileSecurity = $bucket->getAttribute('fileSecurity', false);
|
$fileSecurity = $bucket->getAttribute('fileSecurity', false);
|
||||||
$validator = new Authorization(Database::PERMISSION_READ);
|
$validator = new Authorization(Database::PERMISSION_READ);
|
||||||
$valid = $validator->isValid($bucket->getRead());
|
$valid = $validator->isValid($bucket->getRead());
|
||||||
|
|
||||||
if (!$fileSecurity && !$valid) {
|
if (!$fileSecurity && !$valid) {
|
||||||
throw new Exception(Exception::USER_UNAUTHORIZED);
|
throw new Exception(Exception::USER_UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
|
|
||||||
$parts = explode('/', $data['resource']);
|
$parts = explode('/', $cacheLog->getAttribute('resource'));
|
||||||
$fileId = $parts[1] ?? null;
|
$fileId = $parts[1] ?? null;
|
||||||
|
|
||||||
if ($fileSecurity && !$valid) {
|
if ($fileSecurity && !$valid) {
|
||||||
|
@ -246,8 +244,8 @@ App::init()
|
||||||
$response
|
$response
|
||||||
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + $timestamp) . ' GMT')
|
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + $timestamp) . ' GMT')
|
||||||
->addHeader('X-Appwrite-Cache', 'hit')
|
->addHeader('X-Appwrite-Cache', 'hit')
|
||||||
->setContentType($data['contentType'])
|
->setContentType($cacheLog->getAttribute('mimeType'))
|
||||||
->send(base64_decode($data['payload']))
|
->send($data)
|
||||||
;
|
;
|
||||||
|
|
||||||
$route->setIsActive(false);
|
$route->setIsActive(false);
|
||||||
|
@ -475,7 +473,6 @@ App::shutdown()
|
||||||
if ($useCache) {
|
if ($useCache) {
|
||||||
$resource = $resourceType = null;
|
$resource = $resourceType = null;
|
||||||
$data = $response->getPayload();
|
$data = $response->getPayload();
|
||||||
|
|
||||||
if (!empty($data['payload'])) {
|
if (!empty($data['payload'])) {
|
||||||
$pattern = $route->getLabel('cache.resource', null);
|
$pattern = $route->getLabel('cache.resource', null);
|
||||||
if (!empty($pattern)) {
|
if (!empty($pattern)) {
|
||||||
|
@ -488,14 +485,8 @@ App::shutdown()
|
||||||
}
|
}
|
||||||
|
|
||||||
$key = md5($request->getURI() . implode('*', $request->getParams())) . '*' . APP_CACHE_BUSTER;
|
$key = md5($request->getURI() . implode('*', $request->getParams())) . '*' . APP_CACHE_BUSTER;
|
||||||
$data = json_encode([
|
|
||||||
'resourceType' => $resourceType,
|
|
||||||
'resource' => $resource,
|
|
||||||
'contentType' => $response->getContentType(),
|
|
||||||
'payload' => base64_encode($data['payload']),
|
|
||||||
]) ;
|
|
||||||
|
|
||||||
$signature = md5($data);
|
$signature = md5($data['payload']);
|
||||||
$cacheLog = $dbForProject->getDocument('cache', $key);
|
$cacheLog = $dbForProject->getDocument('cache', $key);
|
||||||
$accessedAt = $cacheLog->getAttribute('accessedAt', '');
|
$accessedAt = $cacheLog->getAttribute('accessedAt', '');
|
||||||
$now = DateTime::now();
|
$now = DateTime::now();
|
||||||
|
@ -503,6 +494,8 @@ App::shutdown()
|
||||||
Authorization::skip(fn () => $dbForProject->createDocument('cache', new Document([
|
Authorization::skip(fn () => $dbForProject->createDocument('cache', new Document([
|
||||||
'$id' => $key,
|
'$id' => $key,
|
||||||
'resource' => $resource,
|
'resource' => $resource,
|
||||||
|
'resourceType' => $resourceType,
|
||||||
|
'mimeType' => $response->getContentType(),
|
||||||
'accessedAt' => $now,
|
'accessedAt' => $now,
|
||||||
'signature' => $signature,
|
'signature' => $signature,
|
||||||
])));
|
])));
|
||||||
|
@ -515,7 +508,7 @@ App::shutdown()
|
||||||
$cache = new Cache(
|
$cache = new Cache(
|
||||||
new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId())
|
new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId())
|
||||||
);
|
);
|
||||||
$cache->save($key, $data);
|
$cache->save($key, $data['payload']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue