diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index 7c5502a064..83b6f9a661 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -355,8 +355,8 @@ App::post('/v1/storage/buckets/:bucketId/files') ->inject('mode') ->inject('deviceFiles') ->inject('deviceLocal') - ->inject('project') - ->action(function (string $bucketId, string $fileId, mixed $file, ?array $read, ?array $write, Request $request, Response $response, Database $dbForProject, Document $user, Audit $audits, Stats $usage, Event $events, string $mode, Device $deviceFiles, Device $deviceLocal, Document $project) { + ->inject('deletes') + ->action(function (string $bucketId, string $fileId, mixed $file, ?array $read, ?array $write, Request $request, Response $response, Database $dbForProject, Document $user, Audit $audits, Stats $usage, Event $events, string $mode, Device $deviceFiles, Device $deviceLocal, Delete $deletes) { $bucket = Authorization::skip(fn () => $dbForProject->getDocument('buckets', $bucketId)); if ( @@ -655,6 +655,11 @@ App::post('/v1/storage/buckets/:bucketId/files') ->setContext('bucket', $bucket) ; + $deletes + ->setType(DELETE_TYPE_CACHE_BY_RESOURCE) + ->setResource('file/' . $file->getId()) + ; + $metadata = null; // was causing leaks as it was passed by reference $response->setStatusCode(Response::STATUS_CODE_CREATED); @@ -1419,7 +1424,6 @@ App::delete('/v1/storage/buckets/:bucketId/files/:fileId') if ($deviceDeleted) { $deletes - ->setType(DELETE_TYPE_CACHE_BY_RESOURCE) ->setType(DELETE_TYPE_CACHE_BY_RESOURCE) ->setResource('file/' . $fileId) ; diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 1635f0a4a9..d633b1e572 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -133,7 +133,6 @@ App::init() $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)); @@ -345,15 +344,15 @@ App::shutdown() $resource = $parseLabel($pattern); } - $key = md5($request->getURI() . implode('*', $request->getParams())); + $key = md5($request->getURI() . implode('*', $request->getParams())); - $data = json_encode([ - 'content-type' => $response->getContentType(), - 'payload' => base64_encode($data['payload']), - ]) ; + $data = json_encode([ + 'content-type' => $response->getContentType(), + 'payload' => base64_encode($data['payload']), + ]) ; - $signature = md5($data); - $cacheLog = $dbForProject->getDocument('cache', $key); + $signature = md5($data); + $cacheLog = $dbForProject->getDocument('cache', $key); if ($cacheLog->isEmpty()) { Authorization::skip(fn () => $dbForProject->createDocument('cache', new Document([ '$id' => $key, @@ -366,16 +365,20 @@ App::shutdown() Authorization::skip(fn () => $dbForProject->updateDocument('cache', $cacheLog->getId(), $cacheLog)); } - $cache = new Cache(new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId())); + if ($signature !== $cacheLog->getAttribute('signature')) { + $cache = new Cache( + new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId()) + ); $cache->save($key, $data); + } } } if ( - App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled' - && $project->getId() - && $mode !== APP_MODE_ADMIN // TODO: add check to make sure user is admin - && !empty($route->getLabel('sdk.namespace', null)) + App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled' + && $project->getId() + && $mode !== APP_MODE_ADMIN // TODO: add check to make sure user is admin + && !empty($route->getLabel('sdk.namespace', null)) ) { // Don't calculate console usage on admin mode $usage ->setParam('networkRequestSize', $request->getSize() + $usage->getParam('storage')) diff --git a/tests/e2e/Services/Storage/StorageBase.php b/tests/e2e/Services/Storage/StorageBase.php index 4937eddec2..a0264ec7c1 100644 --- a/tests/e2e/Services/Storage/StorageBase.php +++ b/tests/e2e/Services/Storage/StorageBase.php @@ -490,7 +490,7 @@ trait StorageBase $this->assertEquals(204, $file['headers']['status-code']); $this->assertEmpty($file['body']); - sleep(10); + sleep(1); //upload again using the same ID $file = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucketId . '/files', array_merge([ 'content-type' => 'multipart/form-data',