From 08e0c61e0fd7041d6cded6f4d71fbb410e5615ce Mon Sep 17 00:00:00 2001 From: Sabir Hassan Date: Tue, 19 Jul 2022 22:41:03 +0500 Subject: [PATCH] feat: add api end point for delete folder --- api/public/swagger.yaml | 27 +++++++++++++++++++++++++++ api/src/controllers/drive.ts | 32 ++++++++++++++++++++++++++++++++ api/src/routes/api/drive.ts | 13 +++++++++++++ 3 files changed, 72 insertions(+) diff --git a/api/public/swagger.yaml b/api/public/swagger.yaml index cb785f3..9a104b8 100644 --- a/api/public/swagger.yaml +++ b/api/public/swagger.yaml @@ -963,6 +963,33 @@ paths: schema: type: string example: /Public/somefolder + delete: + operationId: DeleteFolder + responses: + '200': + description: Ok + content: + application/json: + schema: + properties: + status: {type: string} + required: + - status + type: object + summary: 'Delete folder from SASjs Drive' + tags: + - Drive + security: + - + bearerAuth: [] + parameters: + - + in: query + name: _folderPath + required: true + schema: + type: string + example: /Public/somefolder/ /SASjsApi/drive/filetree: get: operationId: GetFileTree diff --git a/api/src/controllers/drive.ts b/api/src/controllers/drive.ts index 3562311..358e276 100644 --- a/api/src/controllers/drive.ts +++ b/api/src/controllers/drive.ts @@ -22,6 +22,7 @@ import { moveFile, createFolder, deleteFile as deleteFileOnSystem, + deleteFolder as deleteFolderOnSystem, folderExists, listFilesInFolder, listSubFoldersInFolder, @@ -140,6 +141,17 @@ export class DriveController { return getFolder(_folderPath) } + /** + * + * @summary Delete folder from SASjs Drive + * @query _folderPath Location of folder + * @example _folderPath "/Public/somefolder/" + */ + @Delete('/folder') + public async deleteFolder(@Query() _folderPath: string) { + return deleteFolder(_folderPath) + } + /** * * @summary Delete file from SASjs Drive @@ -315,6 +327,26 @@ const deleteFile = async (filePath: string) => { return { status: 'success' } } +const deleteFolder = async (folderPath: string) => { + const driveFolderPath = getFilesFolder() + + const folderPathFull = path + .join(getFilesFolder(), folderPath) + .replace(new RegExp('/', 'g'), path.sep) + + if (!folderPathFull.includes(driveFolderPath)) { + throw new Error('Cannot delete file outside drive.') + } + + if (!(await fileExists(folderPathFull))) { + throw new Error('Folder does not exist.') + } + + await deleteFolderOnSystem(folderPathFull) + + return { status: 'success' } +} + const saveFile = async ( filePath: string, multerFile: Express.Multer.File diff --git a/api/src/routes/api/drive.ts b/api/src/routes/api/drive.ts index 6126946..1f59271 100644 --- a/api/src/routes/api/drive.ts +++ b/api/src/routes/api/drive.ts @@ -149,6 +149,19 @@ driveRouter.delete('/file', async (req, res) => { } }) +driveRouter.delete('/folder', async (req, res) => { + const { error: errQ, value: query } = folderParamValidation(req.query) + + if (errQ) return res.status(400).send(errQ.details[0].message) + + try { + const response = await controller.deleteFolder(query._folderPath) + res.send(response) + } catch (err: any) { + res.status(403).send(err.toString()) + } +}) + driveRouter.post( '/file', (...arg) => multerSingle('file', arg),