1
0
Fork 0
mirror of synced 2024-06-27 02:31:04 +12:00

response changes

This commit is contained in:
shimon 2022-07-26 15:50:33 +03:00
parent 966765dac7
commit d09bf306ce
5 changed files with 36 additions and 38 deletions

View file

@ -47,8 +47,6 @@ $avatarCallback = function (string $type, string $code, int $width, int $height,
$output = (empty($output)) ? $type : $output;
$data = $image->output($output, $quality);
$response
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 30)) . ' GMT')
->addHeader('X-Appwrite-Cache', 'miss')
->setContentType('image/png')
->file($data, 'image/png');
@ -155,8 +153,6 @@ App::get('/v1/avatars/image')
$data = $image->output($output, $quality);
$response
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 30)) . ' GMT')
->addHeader('X-Appwrite-Cache', 'miss')
->setContentType('image/png')
->file($data, 'image/png');
@ -268,9 +264,10 @@ App::get('/v1/avatars/favicon')
if (empty($data) || (\mb_substr($data, 0, 5) === '<html') || \mb_substr($data, 0, 5) === '<!doc') {
throw new Exception('Favicon not found', 404, Exception::AVATAR_ICON_NOT_FOUND);
}
$response->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 30)) . ' GMT')
->addHeader('X-Appwrite-Cache', 'miss')
->file($data, 'image/png');
$response
->setContentType('image/x-icon')
->file($data, 'image/x-icon')
;
}
$fetch = @\file_get_contents($outputHref, false);
@ -285,8 +282,6 @@ App::get('/v1/avatars/favicon')
$data = $image->output($output, $quality);
$response
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 30)) . ' GMT')
->addHeader('X-Appwrite-Cache', 'miss')
->setContentType('image/png')
->file($data, 'image/png');
@ -329,8 +324,7 @@ App::get('/v1/avatars/qr')
$image->crop((int) $size, (int) $size);
$response
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 30)) . ' GMT') // 30 days cache
->addHeader('X-Appwrite-Cache', 'miss')
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT') // 45 days cache
->setContentType('image/png')
->send($image->output('png', 9));
});

View file

@ -959,8 +959,6 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/preview')
$contentType = (\array_key_exists($output, $outputs)) ? $outputs[$output] : $outputs['jpg'];
$response
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 30)) . ' GMT')
->addHeader('X-Appwrite-Cache', 'miss')
->setContentType($contentType)
->file($data, $contentType);

View file

@ -117,28 +117,29 @@ App::init(function (App $utopia, Request $request, Response $response, Document
$useCache = $route->getLabel('cache', false);
if ($useCache) {
$key = md5($request->getURI() . $request->getServer('query_string'));
$cache = new Cache(new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId()));
$data = $cache->load($key, 60 * 60 * 24 * 30 * 1);
$key = md5($request->getURI() . implode('*', $request->getParams()));
$cache = new Cache(new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId()));
$timestamp = 60 * 60 * 24 * 30;
$data = $cache->load($key, $timestamp);
if (!empty($data)) {
$cacheLog = Authorization::skip(fn () => $dbForProject->getDocument('cache', $key));
if ($cacheLog->isEmpty()) {
Authorization::skip(fn () => $dbForProject->createDocument('cache', new Document([
'$id' => $key,
'accessedAt' => time(),
])));
'$id' => $key,
'accessedAt' => time(),
])));
} else {
$cacheLog->setAttribute('accessedAt', time());
Authorization::skip(fn () => $dbForProject->updateDocument('cache', $cacheLog->getId(), $cacheLog));
}
$data = json_decode($data, true);
$expire = \date('D, d M Y H:i:s', \time() + $timestamp) . ' GMT';
$response
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 30)) . ' GMT')
->addHeader('X-Appwrite-Cache', 'hit')
->setContentType($data['content-type'])
->file(base64_decode($data['payload']), $data['content-type'], $data['date']);
->addHeader('Expires', $expire)
->addHeader('X-Appwrite-Cache', 'hit')
->setContentType($data['content-type'])
->file(base64_decode($data['payload']), $data['content-type']);
}
}
}, ['utopia', 'request', 'response', 'project', 'user', 'events', 'audits', 'mails', 'usage', 'deletes', 'database', 'dbForProject', 'mode'], 'api');
@ -268,22 +269,28 @@ App::shutdown(function (App $utopia, Request $request, Response $response, Docum
$useCache = $route->getLabel('cache', false);
if ($useCache) {
$key = md5($request->getURI() . $request->getServer('query_string'));
$cache = new Cache(new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId()));
$data = $response->getPayload();
$key = md5($request->getURI() . implode('*', $request->getParams()));
$cache = new Cache(new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId()));
$data = $response->getPayload();
$timestamp = 60 * 60 * 24 * 30;
if (!empty($data)) {
$cacheLog = $dbForProject->getDocument('cache', $key);
if ($cacheLog->isEmpty()) {
Authorization::skip(fn () => $dbForProject->createDocument('cache', new Document([
'$id' => $key,
'accessedAt' => time(),
])));
'$id' => $key,
'accessedAt' => time(),
])));
} else {
$cacheLog->setAttribute('accessedAt', time());
Authorization::skip(fn () => $dbForProject->updateDocument('cache', $cacheLog->getId(), $cacheLog));
}
$data['payload'] = base64_encode($data['payload']);
$cache->save($key, json_encode($data));
if (!empty($data['payload'])) {
$data['payload'] = base64_encode($data['payload']);
$cache->save($key, json_encode($data));
}
$response
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + $timestamp) . ' GMT')
->addHeader('X-Appwrite-Cache', 'miss');
}
}

View file

@ -147,7 +147,7 @@ class DeletesV1 extends Worker
[new Query('accessedAt', Query::TYPE_LESSER, [$timestamp])],
$dbForProject,
function (Document $document) use ($cache, $projectId) {
$path = APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $projectId . DIRECTORY_SEPARATOR . $document->getId();
$path = APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $projectId . DIRECTORY_SEPARATOR . $document->getId();
if ($cache->purge($document->getId())) {
Console::success('Deleting cache file: ' . $path);

View file

@ -457,10 +457,9 @@ class Response extends SwooleResponse
public function file(string $data, string $contentType): void
{
$this->payload = [
'content-type' => $contentType,
'payload' => $data
];
'content-type' => $contentType,
'payload' => $data
];
$this->send($data);
}
@ -491,7 +490,7 @@ class Response extends SwooleResponse
/**
* @return array
*/
public function getPayload(): string | array
public function getPayload(): array
{
return $this->payload;
}