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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user