diff --git a/src/AnonymizedFile.ts b/src/AnonymizedFile.ts index 182913e..6e0c2bd 100644 --- a/src/AnonymizedFile.ts +++ b/src/AnonymizedFile.ts @@ -206,18 +206,16 @@ export default class AnonymizedFile { } async send(res: Response): Promise { - const mime = lookup(this.anonymizedPath); - if (mime && this.extension() != "ts") { - res.contentType(mime); - } else if (isTextFile(this.anonymizedPath)) { - res.contentType("text/plain"); - } else { - res.contentType("application/octet-stream"); - } - res.header("Accept-Ranges", "none"); return new Promise(async (resolve, reject) => { try { const content = await this.content(); + const mime = lookup(this.anonymizedPath); + if (mime && this.extension() != "ts") { + res.contentType(mime); + } else if (isTextFile(this.anonymizedPath)) { + res.contentType("text/plain"); + } + res.header("Accept-Ranges", "none"); try { const fileInfo = await storage.fileInfo(this.originalCachePath); if (fileInfo.size) { diff --git a/src/routes/file.ts b/src/routes/file.ts index a05b4cf..a8116d2 100644 --- a/src/routes/file.ts +++ b/src/routes/file.ts @@ -8,11 +8,19 @@ export const router = express.Router(); router.get( "/:repoId/file/:path*", async (req: express.Request, res: express.Response) => { - let anonymizedPath = req.params.path; - if (req.params[0]) { - anonymizedPath += req.params[0]; + const anonymizedPath = new URL( + req.url, + `${req.protocol}://${req.hostname}` + ).pathname.replace(`/${req.params.repoId}/file/`, ""); + if (anonymizedPath.endsWith("/")) { + return handleError( + new AnonymousError("folder_not_supported", { + httpStatus: 404, + object: anonymizedPath, + }), + res + ); } - anonymizedPath = anonymizedPath; const repo = await getRepo(req, res, { nocheck: false, diff --git a/src/routes/route-utils.ts b/src/routes/route-utils.ts index 6265b60..28533e8 100644 --- a/src/routes/route-utils.ts +++ b/src/routes/route-utils.ts @@ -107,6 +107,8 @@ export function handleError( let status = 500; if (error.httpStatus) { status = error.httpStatus; + } else if (error.$metadata?.httpStatusCode) { + status = error.$metadata.httpStatusCode; } else if (message && message.indexOf("not_found") > -1) { status = 404; } else if (message && message.indexOf("not_connected") > -1) { diff --git a/src/source/GitHubDownload.ts b/src/source/GitHubDownload.ts index a544652..4bf8904 100644 --- a/src/source/GitHubDownload.ts +++ b/src/source/GitHubDownload.ts @@ -135,10 +135,10 @@ export default class GitHubDownload extends GitHubBase implements SourceBase { if (await storage.exists(file.originalCachePath)) { return storage.read(file.originalCachePath); } - await this.download(); - // update the file list - await this.repository.files({ force: true }); - return storage.read(file.originalCachePath); + throw new AnonymousError("file_not_found", { + httpStatus: 404, + object: file, + }); } async getFiles() {