mirror of
https://github.com/sasjs/server.git
synced 2026-01-16 02:10:05 +00:00
fix(api-cdrive-oller): throw erow error when file not found
This commit is contained in:
19
api/src/controllers/Drive.ts
Normal file
19
api/src/controllers/Drive.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import { fileExists, readFile, createFile } from '@sasjs/utils'
|
||||||
|
|
||||||
|
export class DriveController {
|
||||||
|
async readFile(filePath: string) {
|
||||||
|
await this.validateFilePath(filePath)
|
||||||
|
return await readFile(filePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
async updateFile(filePath: string, fileContent: string) {
|
||||||
|
await this.validateFilePath(filePath)
|
||||||
|
return await createFile(filePath, fileContent)
|
||||||
|
}
|
||||||
|
|
||||||
|
private async validateFilePath(filePath: string) {
|
||||||
|
if (!(await fileExists(filePath))) {
|
||||||
|
throw 'DriveController: File does not exists.'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
export * from './deploy'
|
export * from './deploy'
|
||||||
export * from './sasjsDrive'
|
export * from './Drive'
|
||||||
export * from './Session'
|
export * from './Session'
|
||||||
export * from './Execution'
|
export * from './Execution'
|
||||||
export * from './FileUploadController'
|
export * from './FileUploadController'
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
import { fileExists, readFile, createFile } from '@sasjs/utils'
|
|
||||||
|
|
||||||
export class SASjsDriveController {
|
|
||||||
async readFile(filePath: string) {
|
|
||||||
if (await fileExists(filePath)) {
|
|
||||||
return await readFile(filePath)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async updateFile(filePath: string, fileContent: string) {
|
|
||||||
if (await fileExists(filePath)) {
|
|
||||||
return await createFile(filePath, fileContent)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -3,7 +3,7 @@ import path from 'path'
|
|||||||
import {
|
import {
|
||||||
createFileTree,
|
createFileTree,
|
||||||
getTreeExample,
|
getTreeExample,
|
||||||
SASjsDriveController,
|
DriveController,
|
||||||
ExecutionController,
|
ExecutionController,
|
||||||
FileUploadController
|
FileUploadController
|
||||||
} from '../controllers'
|
} from '../controllers'
|
||||||
@@ -55,8 +55,18 @@ router.get('/SASjsApi/files', async (req, res) => {
|
|||||||
const filePath = path
|
const filePath = path
|
||||||
.join(getTmpFilesFolderPath(), req.query.filePath)
|
.join(getTmpFilesFolderPath(), req.query.filePath)
|
||||||
.replace(new RegExp('/', 'g'), path.sep)
|
.replace(new RegExp('/', 'g'), path.sep)
|
||||||
const fileContent = await new SASjsDriveController().readFile(filePath)
|
await new DriveController()
|
||||||
res.status(200).send({ status: 'success', fileContent: fileContent })
|
.readFile(filePath)
|
||||||
|
.then((fileContent) => {
|
||||||
|
res.status(200).send({ status: 'success', fileContent: fileContent })
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
res.status(400).send({
|
||||||
|
status: 'failure',
|
||||||
|
message: 'File request failed.',
|
||||||
|
...(typeof err === 'object' ? err : { details: err })
|
||||||
|
})
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
res.status(400).send({
|
res.status(400).send({
|
||||||
status: 'failure',
|
status: 'failure',
|
||||||
@@ -69,8 +79,18 @@ router.post('/SASjsApi/files', async (req, res) => {
|
|||||||
const filePath = path
|
const filePath = path
|
||||||
.join(getTmpFilesFolderPath(), req.body.filePath)
|
.join(getTmpFilesFolderPath(), req.body.filePath)
|
||||||
.replace(new RegExp('/', 'g'), path.sep)
|
.replace(new RegExp('/', 'g'), path.sep)
|
||||||
await new SASjsDriveController().updateFile(filePath, req.body.fileContent)
|
await new DriveController()
|
||||||
res.status(200).send({ status: 'success' })
|
.updateFile(filePath, req.body.fileContent)
|
||||||
|
.then(() => {
|
||||||
|
res.status(200).send({ status: 'success' })
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
res.status(400).send({
|
||||||
|
status: 'failure',
|
||||||
|
message: 'File request failed.',
|
||||||
|
...(typeof err === 'object' ? err : { details: err })
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
router.get('/SASjsApi/executor', async (req, res) => {
|
router.get('/SASjsApi/executor', async (req, res) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user