mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-02-13 10:52:53 +00:00
fix: improve reqs on non-existing files
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user