From 65fb13c58de69a5b99c756a8fd0d4315701b7e5e Mon Sep 17 00:00:00 2001 From: VictoriqueMoe Date: Tue, 19 Mar 2024 21:51:40 +0000 Subject: [PATCH] fix file paths --- src/controllers/serve/FileServerController.ts | 5 +++-- src/model/rest/FileEntry.ts | 6 +++++- src/model/rest/FileUploadModelResponse.ts | 6 +++++- src/services/FileService.ts | 12 +++++++++++- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/controllers/serve/FileServerController.ts b/src/controllers/serve/FileServerController.ts index 201bc3d..56aa722 100644 --- a/src/controllers/serve/FileServerController.ts +++ b/src/controllers/serve/FileServerController.ts @@ -5,6 +5,7 @@ import * as Path from "node:path"; import { FileService } from "../../services/FileService.js"; import { FileProtectedException } from "../../model/exceptions/FileProtectedException.js"; import { MimeService } from "../../services/MimeService.js"; +import type { Response } from "express"; @Hidden() @Controller("/") @@ -14,9 +15,9 @@ export class FileServerController { @Inject() private mimeService: MimeService, ) {} - @Get("/:t/:file?") + @Get("/:t/:file(*)?") public async getFile( - @Res() res: Res, + @Res() res: Response, @PathParams("t") resource: string, @HeaderParams("x-password") password?: string, @PathParams("file") requestedFileName?: string, diff --git a/src/model/rest/FileEntry.ts b/src/model/rest/FileEntry.ts index bffb405..152330d 100644 --- a/src/model/rest/FileEntry.ts +++ b/src/model/rest/FileEntry.ts @@ -68,6 +68,10 @@ export class FileEntry { if (fileUploadModel.settings?.hideFilename || !fileUploadModel.originalFileName) { return `${baseUrl}/f/${fileUploadModel.fullFileNameOnSystem}`; } - return `${baseUrl}/f/${fileUploadModel.fileName}/${fileUploadModel.originalFileName}`; + let { originalFileName } = fileUploadModel; + if (originalFileName.startsWith("/")) { + originalFileName = originalFileName.substring(1); + } + return `${baseUrl}/f/${fileUploadModel.fileName}/${originalFileName}`; } } diff --git a/src/model/rest/FileUploadModelResponse.ts b/src/model/rest/FileUploadModelResponse.ts index 7319caf..d610a5e 100644 --- a/src/model/rest/FileUploadModelResponse.ts +++ b/src/model/rest/FileUploadModelResponse.ts @@ -43,6 +43,10 @@ export class FileUploadModelResponse { if (fileUploadModel.settings?.hideFilename || !fileUploadModel.originalFileName) { return `${baseUrl}/f/${fileUploadModel.fullFileNameOnSystem}`; } - return `${baseUrl}/f/${fileUploadModel.fileName}/${fileUploadModel.originalFileName}`; + let { originalFileName } = fileUploadModel; + if (originalFileName.startsWith("/")) { + originalFileName = originalFileName.substring(1); + } + return `${baseUrl}/f/${fileUploadModel.fileName}/${originalFileName}`; } } diff --git a/src/services/FileService.ts b/src/services/FileService.ts index ba6154f..e1ed045 100644 --- a/src/services/FileService.ts +++ b/src/services/FileService.ts @@ -113,6 +113,9 @@ export class FileService { resourcePath = source.path; originalFileName = source.originalname; } + if (originalFileName.startsWith("/")) { + originalFileName = originalFileName.substring(1); + } return [resourcePath, originalFileName]; } @@ -166,7 +169,14 @@ export class FileService { ): Promise<[Buffer, FileUploadModel]> { const entry = await this.repo.getEntryFileName(fileNameOnSystem); const resource = requestedFileName ?? fileNameOnSystem; - if (entry === null || (requestedFileName && entry.originalFileName !== requestedFileName) || entry.hasExpired) { + if (entry === null) { + throw new NotFound(`resource ${resource} is not found`); + } + let { originalFileName } = entry; + if (originalFileName.startsWith("/")) { + originalFileName = originalFileName.substring(1); + } + if ((requestedFileName && originalFileName !== requestedFileName) || entry.hasExpired) { throw new NotFound(`resource ${resource} is not found`); } return Promise.all([this.encryptionService.decrypt(entry, password), entry]);