1
0
mirror of https://github.com/sasjs/server.git synced 2026-01-08 07:00:04 +00:00

fix: get file instead of it's content

This commit is contained in:
Saad Jutt
2022-03-06 02:33:56 +05:00
parent 95843fa4c7
commit efaf38d303
3 changed files with 25 additions and 71 deletions

View File

@@ -223,18 +223,6 @@ components:
- fileTree - fileTree
type: object type: object
additionalProperties: false additionalProperties: false
GetFileResponse:
properties:
status:
type: string
fileContent:
type: string
message:
type: string
required:
- status
type: object
additionalProperties: false
UpdateFileResponse: UpdateFileResponse:
properties: properties:
status: status:
@@ -610,24 +598,8 @@ paths:
get: get:
operationId: GetFile operationId: GetFile
responses: responses:
'200': '204':
description: Ok description: 'No content'
content:
application/json:
schema:
$ref: '#/components/schemas/GetFileResponse'
examples:
'Example 1':
value: {status: success, fileContent: 'Contents of the File'}
'400':
description: 'Unable to get File'
content:
application/json:
schema:
$ref: '#/components/schemas/GetFileResponse'
examples:
'Example 1':
value: {status: failure, message: 'File request failed.'}
summary: 'Get file from SASjs Drive' summary: 'Get file from SASjs Drive'
tags: tags:
- Drive - Drive

View File

@@ -1,7 +1,8 @@
import path from 'path' import path from 'path'
import { Express } from 'express' import express, { Express } from 'express'
import { import {
Security, Security,
Request,
Route, Route,
Tags, Tags,
Example, Example,
@@ -14,14 +15,7 @@ import {
UploadedFile, UploadedFile,
FormField FormField
} from 'tsoa' } from 'tsoa'
import { import { fileExists, createFile, moveFile, createFolder } from '@sasjs/utils'
fileExists,
readFile,
createFile,
moveFile,
createFolder,
deleteFile
} from '@sasjs/utils'
import { createFileTree, ExecutionController, getTreeExample } from './internal' import { createFileTree, ExecutionController, getTreeExample } from './internal'
import { FileTree, isFileTree, TreeNode } from '../types' import { FileTree, isFileTree, TreeNode } from '../types'
@@ -103,17 +97,12 @@ export class DriveController {
* @query filePath Location of SAS program * @query filePath Location of SAS program
* @example filePath "/Public/somefolder/some.file" * @example filePath "/Public/somefolder/some.file"
*/ */
@Example<GetFileResponse>({
status: 'success',
fileContent: 'Contents of the File'
})
@Response<GetFileResponse>(400, 'Unable to get File', {
status: 'failure',
message: 'File request failed.'
})
@Get('/file') @Get('/file')
public async getFile(@Query() filePath: string): Promise<GetFileResponse> { public async getFile(
return getFile(filePath) @Request() request: express.Request,
@Query() filePath: string
) {
return getFile(request, filePath)
} }
/** /**
@@ -190,24 +179,22 @@ const deploy = async (data: DeployPayload) => {
return successDeployResponse return successDeployResponse
} }
const getFile = async (filePath: string): Promise<GetFileResponse> => { const getFile = async (req: express.Request, filePath: string) => {
try { const driveFilesPath = getTmpFilesFolderPath()
const filePathFull = path const filePathFull = path
.join(getTmpFilesFolderPath(), filePath) .join(getTmpFilesFolderPath(), filePath)
.replace(new RegExp('/', 'g'), path.sep) .replace(new RegExp('/', 'g'), path.sep)
await validateFilePath(filePathFull) if (!filePathFull.includes(driveFilesPath)) {
const fileContent = await readFile(filePathFull) throw new Error('Cannot get file outside drive.')
}
return { status: 'success', fileContent: fileContent } if (!(await fileExists(filePathFull))) {
} catch (err: any) { throw new Error('File does not exist.')
throw {
code: 400,
status: 'failure',
message: 'File request failed.',
error: typeof err === 'object' ? err.toString() : err
}
} }
req.res?.download(filePathFull)
} }
const saveFile = async ( const saveFile = async (

View File

@@ -32,14 +32,9 @@ driveRouter.get('/file', async (req, res) => {
if (error) return res.status(400).send(error.details[0].message) if (error) return res.status(400).send(error.details[0].message)
try { try {
const response = await controller.getFile(query.filePath) await controller.getFile(req, query.filePath)
res.send(response)
} catch (err: any) { } catch (err: any) {
const statusCode = err.code res.status(403).send(err.toString())
delete err.code
res.status(statusCode).send(err)
} }
}) })