1
0
mirror of https://github.com/sasjs/server.git synced 2025-12-10 11:24:35 +00:00

fix(api-cdrive-oller): throw erow error when file not found

This commit is contained in:
2021-10-25 05:51:46 +00:00
parent 70a8aaf600
commit 03d1d60660
4 changed files with 45 additions and 21 deletions

View 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.'
}
}
}

View File

@@ -1,5 +1,5 @@
export * from './deploy'
export * from './sasjsDrive'
export * from './Drive'
export * from './Session'
export * from './Execution'
export * from './FileUploadController'

View File

@@ -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)
}
}
}

View File

@@ -3,7 +3,7 @@ import path from 'path'
import {
createFileTree,
getTreeExample,
SASjsDriveController,
DriveController,
ExecutionController,
FileUploadController
} from '../controllers'
@@ -55,8 +55,18 @@ router.get('/SASjsApi/files', async (req, res) => {
const filePath = path
.join(getTmpFilesFolderPath(), req.query.filePath)
.replace(new RegExp('/', 'g'), path.sep)
const fileContent = await new SASjsDriveController().readFile(filePath)
res.status(200).send({ status: 'success', fileContent: fileContent })
await new DriveController()
.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 {
res.status(400).send({
status: 'failure',
@@ -69,8 +79,18 @@ router.post('/SASjsApi/files', async (req, res) => {
const filePath = path
.join(getTmpFilesFolderPath(), req.body.filePath)
.replace(new RegExp('/', 'g'), path.sep)
await new SASjsDriveController().updateFile(filePath, req.body.fileContent)
res.status(200).send({ status: 'success' })
await new DriveController()
.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) => {