mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-02-12 18:32:44 +00:00
feat: improve response content type header
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
{
|
||||
"ERRORS": {
|
||||
"request_error": "Unable to download the file, check your connection or contact the admin.",
|
||||
"repo_not_found": "The repository is not found.",
|
||||
"repo_not_accessible": "Anonymous GitHub is unable to or is forbidden to access the repository.",
|
||||
"repository_expired": "The repository is expired",
|
||||
|
||||
@@ -1669,11 +1669,16 @@ angular
|
||||
$scope.type = "error";
|
||||
try {
|
||||
err.data = JSON.parse(err.data);
|
||||
} catch (ignore) {}
|
||||
if (err.data.error) {
|
||||
$scope.content = err.data.error;
|
||||
} else {
|
||||
$scope.content = err.data;
|
||||
if (err.data.error) {
|
||||
$scope.content = err.data.error;
|
||||
} else {
|
||||
$scope.content = err.data;
|
||||
}
|
||||
} catch (ignore) {
|
||||
console.log(err);
|
||||
if (err.status == -1) {
|
||||
$scope.content = "request_error";
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
@@ -5,10 +5,14 @@ import Repository from "./Repository";
|
||||
import { Tree, TreeElement, TreeFile } from "./types";
|
||||
import storage from "./storage";
|
||||
import config from "../config";
|
||||
import { anonymizePath, AnonymizeTransformer } from "./anonymize-utils";
|
||||
import {
|
||||
anonymizePath,
|
||||
AnonymizeTransformer,
|
||||
isTextFile,
|
||||
} from "./anonymize-utils";
|
||||
import AnonymousError from "./AnonymousError";
|
||||
import { handleError } from "./routes/route-utils";
|
||||
import { tryCatch } from "bullmq";
|
||||
import { lookup } from "mime-types";
|
||||
|
||||
/**
|
||||
* Represent a file in a anonymized repository
|
||||
@@ -202,8 +206,13 @@ export default class AnonymizedFile {
|
||||
}
|
||||
|
||||
async send(res: Response): Promise<void> {
|
||||
if (this.extension()) {
|
||||
res.contentType(this.extension());
|
||||
const mime = lookup(this.anonymizedPath);
|
||||
if (mime) {
|
||||
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) => {
|
||||
|
||||
@@ -18,7 +18,7 @@ export function streamToString(stream: Readable): Promise<string> {
|
||||
});
|
||||
}
|
||||
|
||||
export function isTextFile(filePath: string, content: Buffer) {
|
||||
export function isTextFile(filePath: string, content?: Buffer) {
|
||||
const filename = basename(filePath);
|
||||
const extensions = filename.split(".").reverse();
|
||||
const extension = extensions[0].toLowerCase();
|
||||
|
||||
Reference in New Issue
Block a user