feat: improve response content type header

This commit is contained in:
tdurieux
2023-04-25 17:42:50 +02:00
parent 8ac3a66a30
commit c9acb7b899
4 changed files with 25 additions and 10 deletions
+1
View File
@@ -1,5 +1,6 @@
{ {
"ERRORS": { "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_found": "The repository is not found.",
"repo_not_accessible": "Anonymous GitHub is unable to or is forbidden to access the repository.", "repo_not_accessible": "Anonymous GitHub is unable to or is forbidden to access the repository.",
"repository_expired": "The repository is expired", "repository_expired": "The repository is expired",
+10 -5
View File
@@ -1669,11 +1669,16 @@ angular
$scope.type = "error"; $scope.type = "error";
try { try {
err.data = JSON.parse(err.data); err.data = JSON.parse(err.data);
} catch (ignore) {} if (err.data.error) {
if (err.data.error) { $scope.content = err.data.error;
$scope.content = err.data.error; } else {
} else { $scope.content = err.data;
$scope.content = err.data; }
} catch (ignore) {
console.log(err);
if (err.status == -1) {
$scope.content = "request_error";
}
} }
} }
); );
+13 -4
View File
@@ -5,10 +5,14 @@ import Repository from "./Repository";
import { Tree, TreeElement, TreeFile } from "./types"; import { Tree, TreeElement, TreeFile } from "./types";
import storage from "./storage"; import storage from "./storage";
import config from "../config"; import config from "../config";
import { anonymizePath, AnonymizeTransformer } from "./anonymize-utils"; import {
anonymizePath,
AnonymizeTransformer,
isTextFile,
} from "./anonymize-utils";
import AnonymousError from "./AnonymousError"; import AnonymousError from "./AnonymousError";
import { handleError } from "./routes/route-utils"; import { handleError } from "./routes/route-utils";
import { tryCatch } from "bullmq"; import { lookup } from "mime-types";
/** /**
* Represent a file in a anonymized repository * Represent a file in a anonymized repository
@@ -202,8 +206,13 @@ export default class AnonymizedFile {
} }
async send(res: Response): Promise<void> { async send(res: Response): Promise<void> {
if (this.extension()) { const mime = lookup(this.anonymizedPath);
res.contentType(this.extension()); 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"); res.header("Accept-Ranges", "none");
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
+1 -1
View File
@@ -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 filename = basename(filePath);
const extensions = filename.split(".").reverse(); const extensions = filename.split(".").reverse();
const extension = extensions[0].toLowerCase(); const extension = extensions[0].toLowerCase();