Skip to content

Commit

Permalink
fix file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
VictoriqueMoe committed Mar 19, 2024
1 parent 39ebe32 commit 65fb13c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/controllers/serve/FileServerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("/")
Expand All @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion src/model/rest/FileEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}
}
6 changes: 5 additions & 1 deletion src/model/rest/FileUploadModelResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}
}
12 changes: 11 additions & 1 deletion src/services/FileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ export class FileService {
resourcePath = source.path;
originalFileName = source.originalname;
}
if (originalFileName.startsWith("/")) {
originalFileName = originalFileName.substring(1);
}
return [resourcePath, originalFileName];
}

Expand Down Expand Up @@ -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]);
Expand Down

0 comments on commit 65fb13c

Please sign in to comment.