Skip to content

Commit

Permalink
Improve files handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentaTomas committed Jul 30, 2023
1 parent 748f18e commit 1f5b762
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
11 changes: 11 additions & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ paths:
schema:
type: string
description: Contents of the file as a string
'404':
content:
application/json:
schema:
properties:
reason:
type: string
required:
- reason
type: object
description: Response to send if the file is not found
summary: Read the contents of a file at the given path
put:
operationId: WriteFile
Expand Down
1 change: 1 addition & 0 deletions src/generated/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export function RegisterRoutes(app: Router) {
const args = {
env: {"default":"Nodejs","in":"query","name":"env","ref":"Environment"},
path: {"in":"query","name":"path","required":true,"dataType":"string"},
notFoundResponse: {"in":"res","name":"404","required":true,"dataType":"nestedObjectLiteral","nestedProperties":{"reason":{"dataType":"string","required":true}}},
conversationID: {"in":"header","name":"openai-conversation-id","dataType":"string"},
};

Expand Down
19 changes: 15 additions & 4 deletions src/plugin/filesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
Query,
Route,
Consumes,
TsoaResponse,
Res,
Body,
Produces,
} from 'tsoa'
Expand All @@ -22,24 +24,33 @@ export class FilesystemController extends Controller {
*
* @param env Environment where to read the file from
* @param path Path to the file to read
* @param notFoundResponse Response to send if the file is not found
* @returns Contents of the file as a string
*/
@Get()
@Produces(textPlainMIME)
public async readFile(
@Query() env: Environment = defaultEnvironment,
@Query() path: string,
@Res() notFoundResponse: TsoaResponse<404, { reason: string }>,
@Header(openAIConversationIDHeader) conversationID?: string,
): Promise<string> {
const sessionID = getUserSessionID(conversationID, env)
const session = await CachedSession.findOrStartSession({ sessionID, envID: env })

this.setHeader('Content-Type', textPlainMIME)

return await session
.session
.filesystem!
.read(path)
try {
return await session
.session
.filesystem!
.read(path)
} catch (err) {
console.error(err)
return notFoundResponse(404, {
reason: `File on path "${path}" not found`,
})
}
}

/**
Expand Down

0 comments on commit 1f5b762

Please sign in to comment.