1
0
mirror of https://github.com/sasjs/server.git synced 2025-12-11 19:44:35 +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: schema:
type: string type: string
example: /Public/somefolder 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: /SASjsApi/drive/filetree:
get: get:
operationId: GetFileTree operationId: GetFileTree

View File

@@ -22,6 +22,7 @@ import {
moveFile, moveFile,
createFolder, createFolder,
deleteFile as deleteFileOnSystem, deleteFile as deleteFileOnSystem,
deleteFolder as deleteFolderOnSystem,
folderExists, folderExists,
listFilesInFolder, listFilesInFolder,
listSubFoldersInFolder, listSubFoldersInFolder,
@@ -140,6 +141,17 @@ export class DriveController {
return getFolder(_folderPath) 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 * @summary Delete file from SASjs Drive
@@ -315,6 +327,26 @@ const deleteFile = async (filePath: string) => {
return { status: 'success' } 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 ( const saveFile = async (
filePath: string, filePath: string,
multerFile: Express.Multer.File 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( driveRouter.post(
'/file', '/file',
(...arg) => multerSingle('file', arg), (...arg) => multerSingle('file', arg),