*/
var DATA;
+/**
+ * @type {string}
+ */
+var DIR_EMPTY_NOTE;
+
/**
* @type {PARAMS}
* @typedef {object} PARAMS
".mp3", ".ogg", ".wav", ".m4a",
];
-const dirEmptyNote = PARAMS.q ? 'No results' : DATA.dir_exists ? 'Empty folder' : 'Folder will be created when a file is uploaded';
-
const ICONS = {
dir: `<svg height="16" viewBox="0 0 14 16" width="14"><path fill-rule="evenodd" d="M13 4H7V3c0-.66-.31-1-1-1H1c-.55 0-1 .45-1 1v10c0 .55.45 1 1 1h12c.55 0 1-.45 1-1V5c0-.55-.45-1-1-1zM6 4H1V3h5v1z"></path></svg>`,
symlinkFile: `<svg height="16" viewBox="0 0 12 16" width="12"><path fill-rule="evenodd" d="M8.5 1H1c-.55 0-1 .45-1 1v12c0 .55.45 1 1 1h10c.55 0 1-.45 1-1V4.5L8.5 1zM11 14H1V2h7l3 3v9zM6 4.5l4 3-4 3v-2c-.98-.02-1.84.22-2.55.7-.71.48-1.19 1.25-1.45 2.3.02-1.64.39-2.88 1.13-3.73.73-.84 1.69-1.27 2.88-1.27v-2H6z"></path></svg>`,
*/
let $userName;
-function ready() {
- $pathsTable = document.querySelector(".paths-table")
+// Produce table when window loads
+window.addEventListener("DOMContentLoaded", async () => {
+ const $indexData = document.getElementById('index-data');
+ if (!$indexData) {
+ alert("No data");
+ return;
+ }
+
+ DATA = JSON.parse($indexData.innerHTML);
+ DIR_EMPTY_NOTE = PARAMS.q ? 'No results' : DATA.dir_exists ? 'Empty folder' : 'Folder will be created when a file is uploaded';
+
+ await ready();
+});
+
+async function ready() {
+ $pathsTable = document.querySelector(".paths-table");
$pathsTableHead = document.querySelector(".paths-table thead");
$pathsTableBody = document.querySelector(".paths-table tbody");
$uploadersTable = document.querySelector(".uploaders-table");
addBreadcrumb(DATA.href, DATA.uri_prefix);
- if (DATA.kind == "Index") {
+ if (DATA.kind === "Index") {
document.title = `Index of ${DATA.href} - Dufs`;
document.querySelector(".index-page").classList.remove("hidden");
- setupIndexPage();
- } else if (DATA.kind == "Edit") {
+ await setupIndexPage();
+ } else if (DATA.kind === "Edit") {
document.title = `Edit ${DATA.href} - Dufs`;
- document.querySelector(".editor-page").classList.remove("hidden");;
+ document.querySelector(".editor-page").classList.remove("hidden");
- setupEditorPage();
- } else if (DATA.kind == "View") {
+ await setupEditorPage();
+ } else if (DATA.kind === "View") {
document.title = `View ${DATA.href} - Dufs`;
- document.querySelector(".editor-page").classList.remove("hidden");;
+ document.querySelector(".editor-page").classList.remove("hidden");
- setupEditorPage();
+ await setupEditorPage();
}
}
-
class Uploader {
/**
*
uploadOffset = parseInt(value) || 0;
}
this.uploadOffset = uploadOffset;
- this.ajax()
+ this.ajax();
}
progress(event) {
const [speedValue, speedUnit] = formatSize(speed);
const speedText = `${speedValue} ${speedUnit}/s`;
const progress = formatPercent(((event.loaded + this.uploadOffset) / this.file.size) * 100);
- const duration = formatDuration((event.total - event.loaded) / speed)
+ const duration = formatDuration((event.total - event.loaded) / speed);
this.$uploadStatus.innerHTML = `<span style="width: 80px;">${speedText}</span><span>${progress} ${duration}</span>`;
this.uploaded = event.loaded;
this.lastUptime = now;
if (!Uploader.auth) {
Uploader.auth = true;
try {
- await checkAuth()
+ await checkAuth();
} catch {
Uploader.auth = false;
}
}
}
-function setupIndexPage() {
+async function setupIndexPage() {
if (DATA.allow_archive) {
const $download = document.querySelector(".download");
$download.href = baseUrl() + "?zip";
}
if (DATA.auth) {
- setupAuth();
+ await setupAuth();
}
if (DATA.allow_search) {
- setupSearch()
+ setupSearch();
}
renderPathsTableHead();
addPath(DATA.paths[i], i);
}
} else {
- $emptyFolder.textContent = dirEmptyNote;
+ $emptyFolder.textContent = DIR_EMPTY_NOTE;
$emptyFolder.classList.remove("hidden");
}
}
*/
function addPath(file, index) {
const encodedName = encodedStr(file.name);
- let url = newUrl(file.name)
+ let url = newUrl(file.name);
let actionDelete = "";
let actionDownload = "";
let actionMove = "";
${actionMove}
${actionDelete}
${actionEdit}
- </td>`
+ </td>`;
$pathsTableBody.insertAdjacentHTML("beforeend", `
<tr id="addPath${index}">
<td class="cell-mtime">${formatMtime(file.mtime)}</td>
<td class="cell-size">${formatSize(file.size).join(" ")}</td>
${actionCell}
-</tr>`)
+</tr>`);
}
function setupDropzone() {
});
document.addEventListener("drop", async e => {
if (!e.dataTransfer.items[0].webkitGetAsEntry) {
- const files = e.dataTransfer.files.filter(v => v.size > 0);
+ const files = Array.from(e.dataTransfer.files).filter(v => v.size > 0);
for (const file of files) {
new Uploader(file, []).upload();
}
for (let i = 0; i < len; i++) {
entries.push(e.dataTransfer.items[i].webkitGetAsEntry());
}
- addFileEntries(entries, [])
+ addFileEntries(entries, []);
}
});
}
-function setupAuth() {
+async function setupAuth() {
if (DATA.user) {
$userBtn.classList.remove("hidden");
$userName.textContent = DATA.user;
$loginBtn.classList.remove("hidden");
$loginBtn.addEventListener("click", async () => {
try {
- await checkAuth()
+ await checkAuth();
location.reload();
} catch (err) {
alert(err.message);
await doDeletePath(name, url, () => {
location.href = location.href.split("/").slice(0, -1).join("/");
});
- })
+ });
const $saveBtn = document.querySelector(".save-btn");
$saveBtn.classList.remove("hidden");
const url = baseUrl();
const ext = extName(baseName(url));
if (IFRAME_FORMATS.find(v => v === ext)) {
- $notEditable.insertAdjacentHTML("afterend", `<iframe src="${url}" sandbox width="100%" height="${window.innerHeight - 100}px"></iframe>`)
+ $notEditable.insertAdjacentHTML("afterend", `<iframe src="${url}" sandbox width="100%" height="${window.innerHeight - 100}px"></iframe>`);
} else {
$notEditable.classList.remove("hidden");
$notEditable.textContent = "Cannot edit because file is too large or binary.";
$editor.value = await res.text();
} else {
const bytes = await res.arrayBuffer();
- const dataView = new DataView(bytes)
- const decoder = new TextDecoder(encoding)
+ const dataView = new DataView(bytes);
+ const decoder = new TextDecoder(encoding);
$editor.value = decoder.decode(dataView);
}
} catch (err) {
DATA.paths[index] = null;
if (!DATA.paths.find(v => !!v)) {
$pathsTable.classList.add("hidden");
- $emptyFolder.textContent = dirEmptyNote;
+ $emptyFolder.textContent = DIR_EMPTY_NOTE;
$emptyFolder.classList.remove("hidden");
}
- })
+ });
}
async function doDeletePath(name, url, cb) {
}
async function doMovePath(fileUrl) {
- const fileUrlObj = new URL(fileUrl)
+ const fileUrlObj = new URL(fileUrl);
const prefix = DATA.uri_prefix.slice(0, -1);
const filePath = decodeURIComponent(fileUrlObj.pathname.slice(prefix.length));
- let newPath = prompt("Enter new path", filePath)
+ let newPath = prompt("Enter new path", filePath);
if (!newPath) return;
if (!newPath.startsWith("/")) newPath = "/" + newPath;
if (filePath === newPath) return;
}
function baseName(url) {
- return decodeURIComponent(url.split("/").filter(v => v.length > 0).slice(-1)[0])
+ return decodeURIComponent(url.split("/").filter(v => v.length > 0).slice(-1)[0]);
}
function extName(filename) {
}
function formatMtime(mtime) {
- if (!mtime) return ""
+ if (!mtime) return "";
const date = new Date(mtime);
const year = date.getFullYear();
const month = padZero(date.getMonth() + 1, 2);
}
function padZero(value, size) {
- return ("0".repeat(size) + value).slice(-1 * size)
+ return ("0".repeat(size) + value).slice(-1 * size);
}
function formatSize(size) {
- if (size == null) return [0, "B"]
+ if (size == null) return [0, "B"];
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
if (size == 0) return [0, "B"];
const i = parseInt(Math.floor(Math.log(size) / Math.log(1024)));
- ratio = 1
+ let ratio = 1;
if (i >= 3) {
- ratio = 100
+ ratio = 100;
}
return [Math.round(size * ratio / Math.pow(1024, i), 2) / ratio, sizes[i]];
}
seconds = Math.ceil(seconds);
const h = Math.floor(seconds / 3600);
const m = Math.floor((seconds - h * 3600) / 60);
- const s = seconds - h * 3600 - m * 60
+ const s = seconds - h * 3600 - m * 60;
return `${padZero(h, 2)}:${padZero(m, 2)}:${padZero(s, 2)}`;
}
if (/charset/i.test(charset)) {
let encoding = charset.split("=")[1];
if (encoding) {
- return encoding.toLowerCase()
+ return encoding.toLowerCase();
}
}
- return 'utf-8'
+ return 'utf-8';
}