1
0
mirror of https://github.com/sasjs/server.git synced 2026-01-06 06:10:04 +00:00

feat: implemented api for renaming file/folder

This commit is contained in:
2022-07-20 23:45:11 +05:00
parent 48688a6547
commit fdcaba9d56
4 changed files with 144 additions and 3 deletions

View File

@@ -72,6 +72,19 @@ interface AddFolderPayload {
folderPath: string
}
interface RenamePayload {
/**
* Old path of file/folder
* @example "/Public/someFolder"
*/
oldPath: string
/**
* New path of file/folder
* @example "/Public/newFolder"
*/
newPath: string
}
const fileTreeExample = getTreeExample()
const successDeployResponse: DeployResponse = {
@@ -241,6 +254,24 @@ export class DriveController {
return updateFile((_filePath ?? filePath)!, file)
}
/**
* @summary Renames a file/folder in SASjs Drive
*
*/
@Example<FileFolderResponse>({
status: 'success'
})
@Response<FileFolderResponse>(409, 'Folder already exists', {
status: 'failure',
message: 'rename request failed.'
})
@Post('/rename')
public async rename(
@Body() body: RenamePayload
): Promise<FileFolderResponse> {
return rename(body.oldPath, body.newPath)
}
/**
* @summary Fetch file tree within SASjs Drive.
*
@@ -418,6 +449,46 @@ const addFolder = async (folderPath: string): Promise<FileFolderResponse> => {
return { status: 'success' }
}
const rename = async (
oldPath: string,
newPath: string
): Promise<FileFolderResponse> => {
const drivePath = getFilesFolder()
const oldPathFull = path
.join(drivePath, oldPath)
.replace(new RegExp('/', 'g'), path.sep)
const newPathFull = path
.join(drivePath, newPath)
.replace(new RegExp('/', 'g'), path.sep)
if (!oldPathFull.includes(drivePath)) {
throw new Error('Old path is outside drive.')
}
if (!newPathFull.includes(drivePath)) {
throw new Error('New path is outside drive.')
}
if (await folderExists(oldPathFull)) {
if (await folderExists(newPathFull)) {
throw new Error('Folder already exists.')
} else moveFile(oldPathFull, newPathFull)
return { status: 'success' }
}
if (await fileExists(oldPathFull)) {
if (await fileExists(newPathFull)) {
throw new Error('File already exists.')
} else moveFile(oldPath, newPathFull)
return { status: 'success' }
}
throw new Error('No file/folder found for provided path.')
}
const updateFile = async (
filePath: string,
multerFile: Express.Multer.File