mirror of
https://github.com/sasjs/server.git
synced 2025-12-10 11:24:35 +00:00
chore(drive): post new file route added
This commit is contained in:
@@ -580,6 +580,40 @@ paths:
|
||||
schema:
|
||||
type: string
|
||||
example: /Public/somefolder/some.file
|
||||
post:
|
||||
operationId: SaveFile
|
||||
responses:
|
||||
'200':
|
||||
description: Ok
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/UpdateFileResponse'
|
||||
examples:
|
||||
'Example 1':
|
||||
value: {status: success}
|
||||
'400':
|
||||
description: 'File already exists'
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/UpdateFileResponse'
|
||||
examples:
|
||||
'Example 1':
|
||||
value: {status: failure, message: 'File request failed.'}
|
||||
summary: 'Create a file in SASjs Drive'
|
||||
tags:
|
||||
- Drive
|
||||
security:
|
||||
-
|
||||
bearerAuth: []
|
||||
parameters: []
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/FilePayload'
|
||||
patch:
|
||||
operationId: UpdateFile
|
||||
responses:
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -35,6 +35,23 @@ driveRouter.get('/file', async (req, res) => {
|
||||
}
|
||||
})
|
||||
|
||||
driveRouter.post('/file', async (req, res) => {
|
||||
const { error, value: body } = updateFileDriveValidation(req.body)
|
||||
if (error) return res.status(400).send(error.details[0].message)
|
||||
|
||||
const controller = new DriveController()
|
||||
try {
|
||||
const response = await controller.saveFile(body)
|
||||
res.send(response)
|
||||
} catch (err: any) {
|
||||
const statusCode = err.code
|
||||
|
||||
delete err.code
|
||||
|
||||
res.status(statusCode).send(err)
|
||||
}
|
||||
})
|
||||
|
||||
driveRouter.patch('/file', async (req, res) => {
|
||||
const { error, value: body } = updateFileDriveValidation(req.body)
|
||||
if (error) return res.status(400).send(error.details[0].message)
|
||||
|
||||
Reference in New Issue
Block a user