feat: list files in folder in webview

This commit is contained in:
tdurieux
2024-05-02 11:49:00 +01:00
parent ed11e9db36
commit ca04339529
2 changed files with 36 additions and 7 deletions

View File

@@ -62,6 +62,13 @@ export default class AnonymizedFile {
const filename = basename(this.anonymizedPath); const filename = basename(this.anonymizedPath);
if (!this.anonymizedPath.includes(config.ANONYMIZATION_MASK)) { if (!this.anonymizedPath.includes(config.ANONYMIZATION_MASK)) {
if (this.anonymizedPath == "") {
return {
name: "",
path: "",
repoId: this.repository.repoId,
};
}
const query: FilterQuery<IFile> = { const query: FilterQuery<IFile> = {
repoId: this.repository.repoId, repoId: this.repository.repoId,
path: fileDir, path: fileDir,
@@ -284,7 +291,6 @@ export default class AnonymizedFile {
]); ]);
// const hostName = new URL(config.STREAMER_ENTRYPOINT).hostname; // const hostName = new URL(config.STREAMER_ENTRYPOINT).hostname;
// const ipHost = await this.cacheableLookup.lookupAsync(hostName); // const ipHost = await this.cacheableLookup.lookupAsync(hostName);
// console.timeLog("streamer"+ this.anonymizedPath, "got ip");
got got
.stream(join(config.STREAMER_ENTRYPOINT, "api"), { .stream(join(config.STREAMER_ENTRYPOINT, "api"), {
method: "POST", method: "POST",

View File

@@ -5,6 +5,7 @@ import AnonymizedFile from "../../core/AnonymizedFile";
import AnonymousError from "../../core/AnonymousError"; import AnonymousError from "../../core/AnonymousError";
import * as marked from "marked"; import * as marked from "marked";
import { streamToString } from "../../core/anonymize-utils"; import { streamToString } from "../../core/anonymize-utils";
import { IFile } from "../../core/model/files/files.types";
const router = express.Router(); const router = express.Router();
@@ -48,7 +49,7 @@ async function webView(req: express.Request, res: express.Response) {
indexRepoId + req.params.repoId.length + 1 indexRepoId + req.params.repoId.length + 1
); );
let requestPath = path.join(wRoot, filePath); let requestPath = path.join(wRoot, filePath);
if (requestPath.at(0) == "/") { if (requestPath.at(0) == "/" || requestPath.at(0) == ".") {
requestPath = requestPath.substring(1); requestPath = requestPath.substring(1);
} }
@@ -56,12 +57,22 @@ async function webView(req: express.Request, res: express.Response) {
repository: repo, repository: repo,
anonymizedPath: requestPath, 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 // look for index file
const candidates = await repo.files({ const candidates = await repo.files({
recursive: false, recursive: false,
// look for file at the root of the page source // look for file at the root of the page source
path: wRoot.substring(1), path: folderPath == "." ? "" : folderPath,
}); });
let bestMatch = null; let bestMatch = null;
@@ -79,6 +90,18 @@ async function webView(req: express.Request, res: express.Response) {
repository: repo, repository: repo,
anonymizedPath: requestPath, anonymizedPath: requestPath,
}); });
} else {
// print list of files in the root repository
const body = `<div class="container p-3"><h2>Content of ${filePath}</h2><div class="list-group">${candidates
.map(
(c) =>
`<a class="list-group-item list-group-item-action" href="${
c.name + (c.size == null ? "/" : "")
}">${c.name + (c.size == null ? "/" : "")}</a>`
)
.join("")}</div></div>`;
const html = `<!DOCTYPE html><html><head><title>Content</title></head><link rel="stylesheet" href="/css/all.min.css" /><body>${body}</body></html>`;
return res.contentType("text/html").send(html);
} }
} }
@@ -90,9 +113,9 @@ async function webView(req: express.Request, res: express.Response) {
} }
if (f.extension() == "md") { if (f.extension() == "md") {
const content = await streamToString(await f.anonymizedContent()); const content = await streamToString(await f.anonymizedContent());
res const body = marked.marked(content, { headerIds: false, mangle: false });
.contentType("text/html") const html = `<!DOCTYPE html><html><head><title>Content</title></head><link rel="stylesheet" href="/css/all.min.css" /><body><div class="container p-3 file-content markdown-body">${body}<div></body></html>`;
.send(marked.marked(content, { headerIds: false, mangle: false })); res.contentType("text/html").send(html);
} else { } else {
f.send(res); f.send(res);
} }