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

chore(drive): post new file route added

This commit is contained in:
Saad Jutt
2021-11-13 16:39:20 +05:00
parent 04a8626570
commit a4ac5dc280
3 changed files with 92 additions and 0 deletions

View File

@@ -106,6 +106,24 @@ export class DriveController {
return getFile(filePath)
}
/**
* @summary Create a file in SASjs Drive
*
*/
@Example<UpdateFileResponse>({
status: 'success'
})
@Response<UpdateFileResponse>(400, 'File already exists', {
status: 'failure',
message: 'File request failed.'
})
@Post('/file')
public async saveFile(
@Body() body: FilePayload
): Promise<UpdateFileResponse> {
return saveFile(body)
}
/**
* @summary Modify a file in SASjs Drive
*
@@ -174,6 +192,29 @@ const getFile = async (filePath: string): Promise<GetFileResponse> => {
}
}
const saveFile = async (body: FilePayload): Promise<GetFileResponse> => {
const { filePath, fileContent } = body
try {
const filePathFull = path
.join(getTmpFilesFolderPath(), filePath)
.replace(new RegExp('/', 'g'), path.sep)
if (await fileExists(filePathFull)) {
throw 'DriveController: File already exists.'
}
await createFile(filePathFull, fileContent)
return { status: 'success' }
} catch (err: any) {
throw {
code: 400,
status: 'failure',
message: 'File request failed.',
error: typeof err === 'object' ? err.toString() : err
}
}
}
const updateFile = async (body: FilePayload): Promise<GetFileResponse> => {
const { filePath, fileContent } = body
try {