Enhance file download handling and web interface. Update the download handler to support inline previews based on query parameters. Introduce a preview button for supported file types in the web interface, along with corresponding styles for action buttons. This improves user experience by allowing direct file previews without downloads.
This commit is contained in:
+27
-1
@@ -61,6 +61,20 @@
|
||||
return `${value.toFixed(value >= 10 ? 0 : 1)} ${units[i]}`;
|
||||
}
|
||||
|
||||
// ponytail: extension allowlist; browsers that can't render still get a tab (download/blank)
|
||||
const previewExt = new Set([
|
||||
"png", "jpg", "jpeg", "gif", "webp", "svg", "bmp", "ico",
|
||||
"pdf",
|
||||
"txt", "log", "csv", "json", "xml", "html", "htm", "css", "js", "md",
|
||||
"mp4", "webm", "ogg", "mp3", "wav",
|
||||
]);
|
||||
|
||||
function isPreviewable(filename) {
|
||||
const i = String(filename).lastIndexOf(".");
|
||||
if (i < 0) return false;
|
||||
return previewExt.has(String(filename).slice(i + 1).toLowerCase());
|
||||
}
|
||||
|
||||
function joinPath(base, name) {
|
||||
if (!base) return name;
|
||||
if (/[\\/]$/.test(base)) return base + name;
|
||||
@@ -178,15 +192,27 @@
|
||||
const modified = document.createElement("td");
|
||||
modified.textContent = entry.modified_time ? new Date(entry.modified_time).toLocaleString() : "";
|
||||
const action = document.createElement("td");
|
||||
action.className = "actions";
|
||||
const full = joinPath(data.path, entry.name);
|
||||
const del = document.createElement("button");
|
||||
del.type = "button";
|
||||
del.textContent = "Delete";
|
||||
const full = joinPath(data.path, entry.name);
|
||||
del.addEventListener("click", (event) => {
|
||||
event.stopPropagation();
|
||||
deleteFile(full, entry.name).catch((err) => showError(err.message));
|
||||
});
|
||||
action.append(del);
|
||||
if (entry.type === "file" && isPreviewable(entry.name)) {
|
||||
const preview = document.createElement("button");
|
||||
preview.type = "button";
|
||||
preview.textContent = "Preview";
|
||||
preview.addEventListener("click", (event) => {
|
||||
event.stopPropagation();
|
||||
const url = `/api/v1/download?path=${encodeURIComponent(full)}&inline=1`;
|
||||
window.open(url, "_blank", "noopener");
|
||||
});
|
||||
action.append(preview);
|
||||
}
|
||||
tr.append(name, type, size, modified, action);
|
||||
fileRows.append(tr);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user