diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index d24cd8577f..f12a99e897 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -359,8 +359,7 @@ App::post('/v1/storage/buckets/:bucketId/files') ->inject('mode') ->inject('deviceFiles') ->inject('deviceLocal') - ->inject('deletes') - ->action(function (string $bucketId, string $fileId, mixed $file, ?array $permissions, Request $request, Response $response, Database $dbForProject, Document $user, Event $events, string $mode, Device $deviceFiles, Device $deviceLocal, Delete $deletes) { + ->action(function (string $bucketId, string $fileId, mixed $file, ?array $permissions, Request $request, Response $response, Database $dbForProject, Document $user, Event $events, string $mode, Device $deviceFiles, Device $deviceLocal) { $bucket = Authorization::skip(fn () => $dbForProject->getDocument('buckets', $bucketId)); @@ -661,11 +660,6 @@ 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 diff --git a/app/workers/deletes.php b/app/workers/deletes.php index f27bc4feb9..8fb23de4e1 100644 --- a/app/workers/deletes.php +++ b/app/workers/deletes.php @@ -114,10 +114,10 @@ class DeletesV1 extends Worker break; case DELETE_TYPE_CACHE_BY_RESOURCE: - $this->deleteCacheByResource($project->getId()); + $this->deleteCacheByResource($project, $this->args['resource']); break; case DELETE_TYPE_CACHE_BY_TIMESTAMP: - $this->deleteCacheByDate(); + $this->deleteCacheByDate($this->args['datetime']); break; default: Console::error('No delete operation for type: ' . $type); @@ -130,31 +130,56 @@ class DeletesV1 extends Worker } /** - * @param string $projectId + * @param Document $project + * @param string $resource + * @throws Exception */ - protected function deleteCacheByResource(string $projectId): void + protected function deleteCacheByResource(Document $project, string $resource): void { - $this->deleteCacheFiles([ - Query::equal('resource', [$this->args['resource']]), - ]); + $projectId = $project->getId(); + $dbForProject = $this->getProjectDB($projectId); + + $cache = new Cache( + new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $projectId) + ); + + $query = [ + Query::equal('resource', [$resource]) + ]; + + $this->deleteByGroup( + 'cache', + $query, + $dbForProject, + function (Document $document) use ($cache, $projectId) { + $path = APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $projectId . DIRECTORY_SEPARATOR . $document->getId(); + + if ($cache->purge($document->getId())) { + Console::success('Deleting cache file: ' . $path); + } else { + Console::error('Failed to delete cache file: ' . $path); + } + } + ); } - protected function deleteCacheByDate(): void + /** + * @param string $datetime + * @throws Exception + */ + protected function deleteCacheByDate(string $datetime): void { - $this->deleteCacheFiles([ - Query::lessThan('accessedAt', $this->args['datetime']), - ]); - } - - protected function deleteCacheFiles($query): void - { - $this->deleteForProjectIds(function (string $projectId) use ($query) { + $this->deleteForProjectIds(function (string $projectId) use ($datetime) { $dbForProject = $this->getProjectDB($projectId); $cache = new Cache( new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $projectId) ); + $query = [ + Query::lessThan('accessedAt', $datetime), + ]; + $this->deleteByGroup( 'cache', $query,