1
0
mirror of https://github.com/sasjs/server.git synced 2025-12-10 19:34:34 +00:00

feat: add api end point for delete folder

This commit is contained in:
2022-07-19 22:41:03 +05:00
parent 3e53f70928
commit 08e0c61e0f
3 changed files with 72 additions and 0 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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),