1
0
Fork 0
mirror of synced 2024-06-27 02:31:04 +12:00
This commit is contained in:
shimon 2022-07-24 12:49:51 +03:00
parent 509057f84c
commit f214ead6f6
5 changed files with 53 additions and 19 deletions

View file

@ -2786,6 +2786,8 @@ $collections = [
'$id' => '_key_accessedAt',
'type' => Database::INDEX_KEY,
'attributes' => ['accessedAt'],
'lengths' => [],
'orders' => [],
],
],
],

View file

@ -7,8 +7,6 @@ use Appwrite\Utopia\Response;
use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;
use Utopia\App;
use Utopia\Cache\Adapter\Filesystem;
use Utopia\Cache\Cache;
use Utopia\Config\Config;
use Utopia\Database\Document;
use Utopia\Image\Image;
@ -37,7 +35,7 @@ $avatarCallback = function (string $type, string $code, int $width, int $height,
}
$output = 'png';
$date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT'; // 45 days cache
$date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 30)) . ' GMT'; // 30 days cache
$path = $set[$code];
$type = 'png';
@ -49,7 +47,12 @@ $avatarCallback = function (string $type, string $code, int $width, int $height,
$image->crop((int) $width, (int) $height);
$output = (empty($output)) ? $type : $output;
$data = $image->output($output, $quality);
$response->file($data, 'image/png', $date, 'miss');
$response
->addHeader('Expires', $date)
->addHeader('X-Appwrite-Cache', 'miss')
->setContentType('image/png')
->file($data, 'image/png', $date);
unset($image);
};
@ -130,7 +133,7 @@ App::get('/v1/avatars/image')
$quality = 80;
$output = 'png';
$date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT'; // 45 days cache
$date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 30)) . ' GMT'; // 30 days cache
$type = 'png';
@ -154,7 +157,12 @@ App::get('/v1/avatars/image')
$output = (empty($output)) ? $type : $output;
$data = $image->output($output, $quality);
$response->file($data, 'image/png', $date, 'miss');
$response
->addHeader('Expires', $date)
->addHeader('X-Appwrite-Cache', 'miss')
->setContentType('image/png')
->file($data, 'image/png', $date);
unset($image);
});
@ -178,7 +186,7 @@ App::get('/v1/avatars/favicon')
$height = 56;
$quality = 80;
$output = 'png';
$date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT'; // 45 days cache
$date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 30)) . ' GMT'; // 30 days cache
$type = 'png';
if (!\extension_loaded('imagick')) {
@ -264,8 +272,9 @@ 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->file($data, 'image/png', $date, 'miss');
$response->addHeader('Expires', $date)
->addHeader('X-Appwrite-Cache', 'miss')
->file($data, 'image/png', $date);
}
$fetch = @\file_get_contents($outputHref, false);
@ -279,7 +288,12 @@ App::get('/v1/avatars/favicon')
$output = (empty($output)) ? $type : $output;
$data = $image->output($output, $quality);
$response->file($data, 'image/png', $date, 'miss');
$response
->addHeader('Expires', $date)
->addHeader('X-Appwrite-Cache', 'miss')
->setContentType('image/png')
->file($data, 'image/png', $date);
unset($image);
});
@ -319,7 +333,8 @@ 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 * 45)) . ' GMT') // 45 days cache
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 30)) . ' GMT') // 30 days cache
->addHeader('X-Appwrite-Cache', 'miss')
->setContentType('image/png')
->send($image->output('png', 9));
});

View file

@ -861,7 +861,7 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/preview')
$inputs = Config::getParam('storage-inputs');
$outputs = Config::getParam('storage-outputs');
$fileLogos = Config::getParam('storage-logos');
$date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT'; // 45 days cache
$date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 30)) . ' GMT'; // 30 days cache
if ($bucket->getAttribute('permission') === 'bucket') {
// skip authorization
@ -957,7 +957,13 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/preview')
->setParam('bucketId', $bucketId)
;
$response->file($data, (\array_key_exists($output, $outputs)) ? $outputs[$output] : $outputs['jpg'], $date, 'miss');
$contentType = (\array_key_exists($output, $outputs)) ? $outputs[$output] : $outputs['jpg'];
$response
->addHeader('Expires', $date)
->addHeader('X-Appwrite-Cache', 'miss')
->setContentType($contentType)
->file($data, $contentType, $date);
unset($image);
});

View file

@ -131,12 +131,13 @@ App::init(function (App $utopia, Request $request, Response $response, Document
$cacheLog->setAttribute('accessedAt', time());
Authorization::skip(fn () => $dbForProject->updateDocument('cache', $cacheLog->getId(), $cacheLog));
}
$data = json_decode($data, true);
$response->setContentType($data['content-type'])
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT') // 45 days cache
->addHeader('Expires', $data['date'])
->addHeader('X-Appwrite-Cache', 'hit')
->file(base64_decode($data['payload']));
$response
->addHeader('Expires', $data['date'])
->addHeader('X-Appwrite-Cache', 'hit')
->setContentType($data['content-type'])
->file(base64_decode($data['payload']), $data['content-type'], $data['date']);
}
}
}, ['utopia', 'request', 'response', 'project', 'user', 'events', 'audits', 'mails', 'usage', 'deletes', 'database', 'dbForProject', 'mode'], 'api');

View file

@ -445,7 +445,17 @@ class Response extends SwooleResponse
return $this->payload;
}
public function file($data, $contentType, $date, $state): void
/**
* Set $payload for cache usage and sending file HTTP response.
*
* @param string $data
* @param string $contentType
* @param string $date
*
* @return void
*/
public function file(string $data, string $contentType, string $date): void
{
$this->payload = [
'content-type' => $contentType,