diff --git a/public/i18n/locale-en.json b/public/i18n/locale-en.json index 9fad8f7..074cce2 100644 --- a/public/i18n/locale-en.json +++ b/public/i18n/locale-en.json @@ -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", diff --git a/public/script/app.js b/public/script/app.js index 78c3bf3..3c13ce0 100644 --- a/public/script/app.js +++ b/public/script/app.js @@ -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"; + } } } ); diff --git a/src/AnonymizedFile.ts b/src/AnonymizedFile.ts index 8bfe4fa..6dcb10a 100644 --- a/src/AnonymizedFile.ts +++ b/src/AnonymizedFile.ts @@ -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 { - 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) => { diff --git a/src/anonymize-utils.ts b/src/anonymize-utils.ts index cc3db45..617cbbf 100644 --- a/src/anonymize-utils.ts +++ b/src/anonymize-utils.ts @@ -18,7 +18,7 @@ export function streamToString(stream: Readable): Promise { }); } -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();