fix: improve reqs on non-existing files

This commit is contained in:
tdurieux
2023-05-01 22:45:18 +02:00
parent a23f089a8a
commit 7d8b087a5d
4 changed files with 25 additions and 17 deletions
+7 -9
View File
@@ -206,18 +206,16 @@ export default class AnonymizedFile {
} }
async send(res: Response): Promise<void> { async send(res: Response): Promise<void> {
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) => { return new Promise(async (resolve, reject) => {
try { try {
const content = await this.content(); 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 { try {
const fileInfo = await storage.fileInfo(this.originalCachePath); const fileInfo = await storage.fileInfo(this.originalCachePath);
if (fileInfo.size) { if (fileInfo.size) {
+12 -4
View File
@@ -8,11 +8,19 @@ export const router = express.Router();
router.get( router.get(
"/:repoId/file/:path*", "/:repoId/file/:path*",
async (req: express.Request, res: express.Response) => { async (req: express.Request, res: express.Response) => {
let anonymizedPath = req.params.path; const anonymizedPath = new URL(
if (req.params[0]) { req.url,
anonymizedPath += req.params[0]; `${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, { const repo = await getRepo(req, res, {
nocheck: false, nocheck: false,
+2
View File
@@ -107,6 +107,8 @@ export function handleError(
let status = 500; let status = 500;
if (error.httpStatus) { if (error.httpStatus) {
status = error.httpStatus; status = error.httpStatus;
} else if (error.$metadata?.httpStatusCode) {
status = error.$metadata.httpStatusCode;
} else if (message && message.indexOf("not_found") > -1) { } else if (message && message.indexOf("not_found") > -1) {
status = 404; status = 404;
} else if (message && message.indexOf("not_connected") > -1) { } else if (message && message.indexOf("not_connected") > -1) {
+4 -4
View File
@@ -135,10 +135,10 @@ export default class GitHubDownload extends GitHubBase implements SourceBase {
if (await storage.exists(file.originalCachePath)) { if (await storage.exists(file.originalCachePath)) {
return storage.read(file.originalCachePath); return storage.read(file.originalCachePath);
} }
await this.download(); throw new AnonymousError("file_not_found", {
// update the file list httpStatus: 404,
await this.repository.files({ force: true }); object: file,
return storage.read(file.originalCachePath); });
} }
async getFiles() { async getFiles() {