From ca04339529c471bba8763c96fdee2a6e885d36e4 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Thu, 2 May 2024 11:49:00 +0100 Subject: [PATCH] feat: list files in folder in webview --- src/core/AnonymizedFile.ts | 8 +++++++- src/server/routes/webview.ts | 35 +++++++++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/core/AnonymizedFile.ts b/src/core/AnonymizedFile.ts index 0f9cbe1..76f759e 100644 --- a/src/core/AnonymizedFile.ts +++ b/src/core/AnonymizedFile.ts @@ -62,6 +62,13 @@ export default class AnonymizedFile { const filename = basename(this.anonymizedPath); if (!this.anonymizedPath.includes(config.ANONYMIZATION_MASK)) { + if (this.anonymizedPath == "") { + return { + name: "", + path: "", + repoId: this.repository.repoId, + }; + } const query: FilterQuery = { repoId: this.repository.repoId, path: fileDir, @@ -284,7 +291,6 @@ export default class AnonymizedFile { ]); // const hostName = new URL(config.STREAMER_ENTRYPOINT).hostname; // const ipHost = await this.cacheableLookup.lookupAsync(hostName); - // console.timeLog("streamer"+ this.anonymizedPath, "got ip"); got .stream(join(config.STREAMER_ENTRYPOINT, "api"), { method: "POST", diff --git a/src/server/routes/webview.ts b/src/server/routes/webview.ts index eae49bf..1946209 100644 --- a/src/server/routes/webview.ts +++ b/src/server/routes/webview.ts @@ -5,6 +5,7 @@ import AnonymizedFile from "../../core/AnonymizedFile"; import AnonymousError from "../../core/AnonymousError"; import * as marked from "marked"; import { streamToString } from "../../core/anonymize-utils"; +import { IFile } from "../../core/model/files/files.types"; const router = express.Router(); @@ -48,7 +49,7 @@ async function webView(req: express.Request, res: express.Response) { indexRepoId + req.params.repoId.length + 1 ); let requestPath = path.join(wRoot, filePath); - if (requestPath.at(0) == "/") { + if (requestPath.at(0) == "/" || requestPath.at(0) == ".") { requestPath = requestPath.substring(1); } @@ -56,12 +57,22 @@ async function webView(req: express.Request, res: express.Response) { repository: repo, anonymizedPath: requestPath, }); - if (filePath == "" && req.headers.accept?.includes("text/html")) { + let info: IFile | null = null; + try { + info = await f.getFileInfo(); + } catch (error) {} + if ( + req.headers.accept?.includes("text/html") && + (filePath == "" || (info && info.size == null)) + ) { + const folderPath = info + ? path.join(info.path, info.name) + : wRoot.substring(1); // look for index file const candidates = await repo.files({ recursive: false, // look for file at the root of the page source - path: wRoot.substring(1), + path: folderPath == "." ? "" : folderPath, }); let bestMatch = null; @@ -79,6 +90,18 @@ async function webView(req: express.Request, res: express.Response) { repository: repo, anonymizedPath: requestPath, }); + } else { + // print list of files in the root repository + const body = `

Content of ${filePath}

${candidates + .map( + (c) => + `${c.name + (c.size == null ? "/" : "")}` + ) + .join("")}
`; + const html = `Content${body}`; + return res.contentType("text/html").send(html); } } @@ -90,9 +113,9 @@ async function webView(req: express.Request, res: express.Response) { } if (f.extension() == "md") { const content = await streamToString(await f.anonymizedContent()); - res - .contentType("text/html") - .send(marked.marked(content, { headerIds: false, mangle: false })); + const body = marked.marked(content, { headerIds: false, mangle: false }); + const html = `Content
${body}
`; + res.contentType("text/html").send(html); } else { f.send(res); }