diff --git a/app/config/collections/common.php b/app/config/collections/common.php index a613e8f3ab..f68400e226 100644 --- a/app/config/collections/common.php +++ b/app/config/collections/common.php @@ -2548,6 +2548,17 @@ return [ 'array' => false, 'filters' => [], ], + [ + '$id' => 'transformedAt', + 'type' => Database::VAR_DATETIME, + 'format' => '', + 'size' => 0, + 'signed' => false, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => ['datetime'], + ], [ '$id' => ID::custom('search'), 'type' => Database::VAR_STRING, @@ -2617,6 +2628,13 @@ return [ 'lengths' => [], 'orders' => [Database::ORDER_ASC], ], + [ + '$id' => ID::custom('_key_transformedAt'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['transformedAt'], + 'lengths' => [], + 'orders' => [], + ] ] ], ]; diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index b6a07c356d..b5ddc94c9d 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -21,6 +21,7 @@ use Appwrite\Utopia\Response; use Utopia\App; use Utopia\Config\Config; use Utopia\Database\Database; +use Utopia\Database\DateTime; use Utopia\Database\Document; use Utopia\Database\Exception\Duplicate as DuplicateException; use Utopia\Database\Exception\NotFound as NotFoundException; @@ -1075,6 +1076,12 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/preview') ->addMetric(str_replace('{bucketInternalId}', $bucket->getInternalId(), METRIC_BUCKET_ID_FILES_TRANSFORMATIONS), 1) ; + $transformedAt = $file->getAttribute('transformedAt', ''); + if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_PROJECT_ACCESS)) > $transformedAt) { + $file->setAttribute('transformedAt', DateTime::now()); + Authorization::skip(fn () => $dbForProject->updateDocument('bucket_' . $file->getAttribute('bucketInternalId'), $file->getId(), $file)); + } + $response ->addHeader('Cache-Control', 'private, max-age=2592000') // 30 days ->setContentType($contentType) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index e2845521f8..12b65fe6cd 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -607,6 +607,12 @@ App::init() if ($file->isEmpty()) { throw new Exception(Exception::STORAGE_FILE_NOT_FOUND); } + + $transformedAt = $file->getAttribute('transformedAt', ''); + if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_PROJECT_ACCESS)) > $transformedAt) { + $file->setAttribute('transformedAt', DateTime::now()); + Authorization::skip(fn () => $dbForProject->updateDocument('bucket_' . $file->getAttribute('bucketInternalId'), $file->getId(), $file)); + } } $response diff --git a/app/init.php b/app/init.php index f812ef094c..e912ffd8f4 100644 --- a/app/init.php +++ b/app/init.php @@ -122,6 +122,7 @@ const APP_LIMIT_LIST_DEFAULT = 25; // Default maximum number of items to return const APP_KEY_ACCESS = 24 * 60 * 60; // 24 hours const APP_USER_ACCESS = 24 * 60 * 60; // 24 hours const APP_PROJECT_ACCESS = 24 * 60 * 60; // 24 hours +const APP_FILE_ACCESS = 24 * 60 * 60; // 24 hours const APP_CACHE_UPDATE = 24 * 60 * 60; // 24 hours const APP_CACHE_BUSTER = 4318; const APP_VERSION_STABLE = '1.6.1';