From 220cc86809ff9f1bf7a74341426c652acfb38c91 Mon Sep 17 00:00:00 2001 From: rijkvanzanten Date: Tue, 4 Aug 2020 18:51:38 -0400 Subject: [PATCH] Small fixes --- api/src/routes/files.ts | 2 +- api/src/services/files.ts | 26 +++++++++++++++----------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/api/src/routes/files.ts b/api/src/routes/files.ts index 588b115cc2..6040cf5356 100644 --- a/api/src/routes/files.ts +++ b/api/src/routes/files.ts @@ -108,7 +108,7 @@ router.get( sanitizeQuery, asyncHandler(async (req, res) => { const service = new FilesService({ accountability: req.accountability }); - const records = service.readByQuery(req.sanitizedQuery); + const records = await service.readByQuery(req.sanitizedQuery); return res.json({ data: records || null }); }) ); diff --git a/api/src/services/files.ts b/api/src/services/files.ts index 12a0cf9d99..5a79c74d3d 100644 --- a/api/src/services/files.ts +++ b/api/src/services/files.ts @@ -23,15 +23,15 @@ export default class FilesService extends ItemsService { if (primaryKey !== undefined) { // If the file you're uploading already exists, we'll consider this upload a replace. In that case, we'll // delete the previously saved file and thumbnails to ensure they're generated fresh - const disk = storage.disk(data.storage); + const disk = storage.disk(payload.storage); for await (const file of disk.flatList(String(primaryKey))) { await disk.delete(file.path); } - await this.update(data, primaryKey); + await this.update(payload, primaryKey); } else { - primaryKey = await this.create(data); + primaryKey = await this.create(payload); } payload.filename_disk = primaryKey + path.extname(payload.filename_download); @@ -66,15 +66,15 @@ export default class FilesService extends ItemsService { }); await storage.disk(data.storage).put(payload.filename_disk, stream.pipe(pipeline)); - - // We do this in a service without accountability. Even if you don't have update permissions to the file, - // we still want to be able to set the extracted values from the file on create - const sudoService = new ItemsService('directus_files'); - await sudoService.update(payload, primaryKey); } else { await storage.disk(data.storage).put(payload.filename_disk, stream); } + // We do this in a service without accountability. Even if you don't have update permissions to the file, + // we still want to be able to set the extracted values from the file on create + const sudoService = new ItemsService('directus_files'); + await sudoService.update(payload, primaryKey); + return primaryKey; } @@ -82,11 +82,15 @@ export default class FilesService extends ItemsService { delete(keys: PrimaryKey[]): Promise; async delete(key: PrimaryKey | PrimaryKey[]): Promise { const keys = Array.isArray(key) ? key : [key]; - - const files = await super.readByKey(keys, { fields: ['storage', 'filename_disk'] }); + const files = await super.readByKey(keys, { fields: ['id', 'storage'] }); for (const file of files) { - await storage.disk(file.storage).delete(file.filename_disk); + const disk = storage.disk(file.storage); + + // Delete file + thumbnails + for await (const { path } of disk.flatList(file.id)) { + await disk.delete(path); + } } await super.delete(keys);