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

View File

@@ -206,18 +206,16 @@ export default class AnonymizedFile {
}
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) => {
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) {

View File

@@ -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,

View File

@@ -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) {

View File

@@ -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() {