From a9bb4b8b65df521a2f6fab31356167d4cf7699e8 Mon Sep 17 00:00:00 2001 From: Sergio Gutierrez Villalba Date: Tue, 26 Sep 2023 13:56:40 +0200 Subject: [PATCH] feat(file): check existence EP --- src/app/routes/storage.ts | 41 ++++++++++++++++++++++++++++++++++++ src/app/services/files.js | 44 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/src/app/routes/storage.ts b/src/app/routes/storage.ts index 296cd564..abee64be 100644 --- a/src/app/routes/storage.ts +++ b/src/app/routes/storage.ts @@ -99,6 +99,42 @@ export class StorageController { } } + public async checkFileExistence(req: Request, res: Response) { + const { behalfUser } = req as SharedRequest; + const { file } = req.body as { file: { name: string; folderId: number; type: string } }; + + if ( + !file || + !file.name || + !file.folderId || + !file.type + ) { + this.logger.error( + `Missing body params to check the file existence for user ${ + behalfUser.email + }: ${JSON.stringify(file, null, 2)}`, + ); + return res.status(400).json({ error: 'Missing information to check file existence' }); + } + + try { + const result = await this.services.Files.CheckFileExistence(behalfUser, file); + + if (!result.exists) { + return res.status(404).send(); + } + + res.status(200).json(result.file); + } catch (err) { + this.logger.error( + `[FILE/CHECK-EXISTENCE] ERROR: ${ + (err as Error).message + }, BODY ${JSON.stringify(file)}, STACK: ${(err as Error).stack} USER: ${behalfUser.email}`, + ); + res.status(500).send({ error: 'Internal Server Error' }); + } + } + public async createFolder(req: Request, res: Response): Promise { const { folderName, parentFolderId } = req.body; const { behalfUser: user } = req as any; @@ -746,6 +782,11 @@ export default (router: Router, service: any) => { resourceSharingAdapter.UploadFile, controller.createFile.bind(controller) ); + router.post('/storage/file/exists', + passportAuth, + sharedAdapter, + controller.checkFileExistence.bind(controller) + ); router.post('/storage/thumbnail', passportAuth, sharedAdapter, diff --git a/src/app/services/files.js b/src/app/services/files.js index 1b3311d9..42fa0e38 100644 --- a/src/app/services/files.js +++ b/src/app/services/files.js @@ -13,6 +13,49 @@ const { Op } = sequelize; module.exports = (Model, App) => { const log = App.logger; + const CheckFileExistence = async (user, file) => { + const maybeAlreadyExistentFile = await Model.file.findOne({ + where: { + name: { [Op.eq]: file.name }, + folder_id: { [Op.eq]: file.folderId }, + type: { [Op.eq]: file.type }, + userId: { [Op.eq]: user.id }, + status: { [Op.eq]: 'EXISTS' }, + }, + }); + + const fileExists = !!maybeAlreadyExistentFile; + + if (!fileExists) { + return { exists: false, file: null }; + } + + const fileInfo = { + name: file.name, + plainName: file.plain_name, + type: file.type, + size: file.size, + folderId: file.folder_id, + fileId: file.fileId, + bucket: file.bucket, + userId: user.id, + uuid: v4(), + modificationTime: file.modificationTime || new Date(), + }; + + try { + fileInfo.plainName = fileInfo.plainName ?? AesUtil.decrypt(file.name, file.fileId); + } catch { + // eslint-disable-next-line no-empty + } + + if (file.date) { + fileInfo.createdAt = file.date; + } + + return { exists: true, file: fileInfo }; + }; + const CreateFile = async (user, file) => { const folder = await Model.folder.findOne({ where: { @@ -405,6 +448,7 @@ module.exports = (Model, App) => { return { Name: 'Files', CreateFile, + CheckFileExistence, Delete, DeleteFile, UpdateMetadata,