2022-06-04 07:51:56 +03:00
|
|
|
/**
|
|
|
|
* @typedef {object} PathItem
|
|
|
|
* @property {"Dir"|"SymlinkDir"|"File"|"SymlinkFile"} path_type
|
|
|
|
* @property {string} name
|
|
|
|
* @property {number} mtime
|
|
|
|
* @property {number} size
|
|
|
|
*/
|
|
|
|
|
2022-08-23 09:24:42 +03:00
|
|
|
/**
|
2023-02-21 03:13:29 +03:00
|
|
|
* @typedef {object} DATA
|
2022-08-23 09:24:42 +03:00
|
|
|
* @property {string} href
|
|
|
|
* @property {string} uri_prefix
|
2023-11-28 02:14:53 +03:00
|
|
|
* @property {"Index" | "Edit" | "View"} kind
|
2022-08-23 09:24:42 +03:00
|
|
|
* @property {PathItem[]} paths
|
|
|
|
* @property {boolean} allow_upload
|
|
|
|
* @property {boolean} allow_delete
|
|
|
|
* @property {boolean} allow_search
|
2022-12-10 06:09:42 +03:00
|
|
|
* @property {boolean} allow_archive
|
2023-02-21 07:42:40 +03:00
|
|
|
* @property {boolean} auth
|
|
|
|
* @property {string} user
|
2022-08-23 09:24:42 +03:00
|
|
|
* @property {boolean} dir_exists
|
2023-02-20 17:50:24 +03:00
|
|
|
* @property {string} editable
|
|
|
|
*/
|
|
|
|
|
2023-06-01 16:32:22 +03:00
|
|
|
var DUFS_MAX_UPLOADINGS = 1;
|
|
|
|
|
2022-08-23 09:24:42 +03:00
|
|
|
/**
|
|
|
|
* @type {DATA} DATA
|
|
|
|
*/
|
|
|
|
var DATA;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {PARAMS}
|
|
|
|
* @typedef {object} PARAMS
|
|
|
|
* @property {string} q
|
|
|
|
* @property {string} sort
|
|
|
|
* @property {string} order
|
|
|
|
*/
|
|
|
|
const PARAMS = Object.fromEntries(new URLSearchParams(window.location.search).entries());
|
|
|
|
|
2023-02-22 05:04:40 +03:00
|
|
|
const IFRAME_FORMATS = [
|
|
|
|
".pdf",
|
|
|
|
".jpg", ".jpeg", ".png", ".gif", ".bmp", ".svg",
|
|
|
|
".mp4", ".mov", ".avi", ".wmv", ".flv", ".webm",
|
|
|
|
".mp3", ".ogg", ".wav", ".m4a",
|
|
|
|
];
|
|
|
|
|
2022-08-23 09:24:42 +03:00
|
|
|
const dirEmptyNote = PARAMS.q ? 'No results' : DATA.dir_exists ? 'Empty folder' : 'Folder will be created when a file is uploaded';
|
2022-06-04 07:51:56 +03:00
|
|
|
|
2023-02-21 03:13:29 +03:00
|
|
|
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>`,
|
|
|
|
symlinkDir: `<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-1zM1 3h5v1H1V3zm6 9v-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.73C4.86 8.43 5.82 8 7.01 8V6l4 3-4 3H7z"></path></svg>`,
|
|
|
|
file: `<svg height="16" viewBox="0 0 12 16" width="12"><path fill-rule="evenodd" d="M6 5H2V4h4v1zM2 8h7V7H2v1zm0 2h7V9H2v1zm0 2h7v-1H2v1zm10-7.5V14c0 .55-.45 1-1 1H1c-.55 0-1-.45-1-1V2c0-.55.45-1 1-1h7.5L12 4.5zM11 5L8 2H1v12h10V5z"></path></svg>`,
|
|
|
|
download: `<svg width="16" height="16" viewBox="0 0 16 16"><path d="M.5 9.9a.5.5 0 0 1 .5.5v2.5a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2.5a.5.5 0 0 1 1 0v2.5a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2v-2.5a.5.5 0 0 1 .5-.5z"/><path d="M7.646 11.854a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 10.293V1.5a.5.5 0 0 0-1 0v8.793L5.354 8.146a.5.5 0 1 0-.708.708l3 3z"/></svg>`,
|
|
|
|
move: `<svg width="16" height="16" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M1.5 1.5A.5.5 0 0 0 1 2v4.8a2.5 2.5 0 0 0 2.5 2.5h9.793l-3.347 3.346a.5.5 0 0 0 .708.708l4.2-4.2a.5.5 0 0 0 0-.708l-4-4a.5.5 0 0 0-.708.708L13.293 8.3H3.5A1.5 1.5 0 0 1 2 6.8V2a.5.5 0 0 0-.5-.5z"/></svg>`,
|
2023-02-22 05:37:54 +03:00
|
|
|
edit: `<svg width="16" height="16" viewBox="0 0 16 16"><path d="M12.146.146a.5.5 0 0 1 .708 0l3 3a.5.5 0 0 1 0 .708l-10 10a.5.5 0 0 1-.168.11l-5 2a.5.5 0 0 1-.65-.65l2-5a.5.5 0 0 1 .11-.168l10-10zM11.207 2.5 13.5 4.793 14.793 3.5 12.5 1.207 11.207 2.5zm1.586 3L10.5 3.207 4 9.707V10h.5a.5.5 0 0 1 .5.5v.5h.5a.5.5 0 0 1 .5.5v.5h.293l6.5-6.5zm-9.761 5.175-.106.106-1.528 3.821 3.821-1.528.106-.106A.5.5 0 0 1 5 12.5V12h-.5a.5.5 0 0 1-.5-.5V11h-.5a.5.5 0 0 1-.468-.325z"/></svg>`,
|
2023-11-28 02:14:53 +03:00
|
|
|
delete: `<svg width="16" height="16" viewBox="0 0 16 16"><path d="M6.854 7.146a.5.5 0 1 0-.708.708L7.293 9l-1.147 1.146a.5.5 0 0 0 .708.708L8 9.707l1.146 1.147a.5.5 0 0 0 .708-.708L8.707 9l1.147-1.146a.5.5 0 0 0-.708-.708L8 8.293 6.854 7.146z"/><path d="M14 14V4.5L9.5 0H4a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2zM9.5 3A1.5 1.5 0 0 0 11 4.5h2V14a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1h5.5v2z"/></svg>`,
|
|
|
|
view: `<svg width="16" height="16" viewBox="0 0 16 16"><path d="M4 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2zm0 1h8a1 1 0 0 1 1 1v12a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1"/></svg>`,
|
2023-02-21 03:13:29 +03:00
|
|
|
}
|
|
|
|
|
2022-06-03 02:18:12 +03:00
|
|
|
/**
|
|
|
|
* @type Element
|
|
|
|
*/
|
2022-06-11 03:51:17 +03:00
|
|
|
let $pathsTable;
|
2022-08-23 09:24:42 +03:00
|
|
|
/**
|
|
|
|
* @type Element
|
|
|
|
*/
|
|
|
|
let $pathsTableHead;
|
2022-06-11 03:51:17 +03:00
|
|
|
/**
|
|
|
|
* @type Element
|
|
|
|
*/
|
|
|
|
let $pathsTableBody;
|
|
|
|
/**
|
|
|
|
* @type Element
|
|
|
|
*/
|
|
|
|
let $uploadersTable;
|
|
|
|
/**
|
|
|
|
* @type Element
|
|
|
|
*/
|
|
|
|
let $emptyFolder;
|
2022-07-04 15:12:35 +03:00
|
|
|
/**
|
|
|
|
* @type Element
|
|
|
|
*/
|
2023-02-20 17:50:24 +03:00
|
|
|
let $editor;
|
2023-02-21 07:42:40 +03:00
|
|
|
/**
|
|
|
|
* @type Element
|
|
|
|
*/
|
|
|
|
let $userBtn;
|
2023-11-03 09:55:07 +03:00
|
|
|
/**
|
|
|
|
* @type Element
|
|
|
|
*/
|
|
|
|
let $userName;
|
2023-02-20 17:50:24 +03:00
|
|
|
|
|
|
|
function ready() {
|
|
|
|
$pathsTable = document.querySelector(".paths-table")
|
|
|
|
$pathsTableHead = document.querySelector(".paths-table thead");
|
|
|
|
$pathsTableBody = document.querySelector(".paths-table tbody");
|
|
|
|
$uploadersTable = document.querySelector(".uploaders-table");
|
|
|
|
$emptyFolder = document.querySelector(".empty-folder");
|
|
|
|
$editor = document.querySelector(".editor");
|
2023-02-21 07:42:40 +03:00
|
|
|
$userBtn = document.querySelector(".user-btn");
|
2023-11-03 09:55:07 +03:00
|
|
|
$userName = document.querySelector(".user-name");
|
2023-02-20 17:50:24 +03:00
|
|
|
|
|
|
|
addBreadcrumb(DATA.href, DATA.uri_prefix);
|
|
|
|
|
|
|
|
if (DATA.kind == "Index") {
|
2023-02-21 03:13:29 +03:00
|
|
|
document.title = `Index of ${DATA.href} - Dufs`;
|
2023-02-20 17:50:24 +03:00
|
|
|
document.querySelector(".index-page").classList.remove("hidden");
|
|
|
|
|
2023-02-22 05:04:40 +03:00
|
|
|
setupIndexPage();
|
2023-02-20 17:50:24 +03:00
|
|
|
|
|
|
|
} else if (DATA.kind == "Edit") {
|
2023-06-01 02:35:41 +03:00
|
|
|
document.title = `Edit ${DATA.href} - Dufs`;
|
2023-02-21 03:45:52 +03:00
|
|
|
document.querySelector(".editor-page").classList.remove("hidden");;
|
|
|
|
|
2023-11-28 02:14:53 +03:00
|
|
|
setupEditorPage();
|
|
|
|
} else if (DATA.kind == "View") {
|
|
|
|
document.title = `View ${DATA.href} - Dufs`;
|
|
|
|
document.querySelector(".editor-page").classList.remove("hidden");;
|
|
|
|
|
|
|
|
setupEditorPage();
|
2023-02-20 17:50:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-30 06:47:57 +03:00
|
|
|
|
|
|
|
class Uploader {
|
2022-06-04 07:51:56 +03:00
|
|
|
/**
|
2023-12-11 13:28:11 +03:00
|
|
|
*
|
|
|
|
* @param {File} file
|
|
|
|
* @param {string[]} dirs
|
2022-06-04 07:51:56 +03:00
|
|
|
*/
|
2022-05-31 11:43:42 +03:00
|
|
|
constructor(file, dirs) {
|
2022-06-30 05:00:42 +03:00
|
|
|
/**
|
|
|
|
* @type Element
|
|
|
|
*/
|
|
|
|
this.$uploadStatus = null
|
|
|
|
this.uploaded = 0;
|
|
|
|
this.lastUptime = 0;
|
2022-05-31 11:43:42 +03:00
|
|
|
this.name = [...dirs, file.name].join("/");
|
|
|
|
this.idx = Uploader.globalIdx++;
|
2022-05-30 06:47:57 +03:00
|
|
|
this.file = file;
|
|
|
|
}
|
|
|
|
|
|
|
|
upload() {
|
2022-07-06 14:17:30 +03:00
|
|
|
const { idx, name } = this;
|
2023-02-20 17:50:24 +03:00
|
|
|
const url = newUrl(name);
|
2022-06-21 14:23:34 +03:00
|
|
|
const encodedName = encodedStr(name);
|
2022-06-03 01:49:55 +03:00
|
|
|
$uploadersTable.insertAdjacentHTML("beforeend", `
|
|
|
|
<tr id="upload${idx}" class="uploader">
|
2022-06-19 17:16:43 +03:00
|
|
|
<td class="path cell-icon">
|
2023-02-20 17:50:24 +03:00
|
|
|
${getPathSvg()}
|
2022-06-19 17:16:43 +03:00
|
|
|
</td>
|
2022-06-03 01:49:55 +03:00
|
|
|
<td class="path cell-name">
|
2022-07-30 02:53:27 +03:00
|
|
|
<a href="${url}">${encodedName}</a>
|
2022-06-03 01:49:55 +03:00
|
|
|
</td>
|
2022-06-11 07:34:03 +03:00
|
|
|
<td class="cell-status upload-status" id="uploadStatus${idx}"></td>
|
2022-06-03 01:49:55 +03:00
|
|
|
</tr>`);
|
|
|
|
$uploadersTable.classList.remove("hidden");
|
2022-06-11 03:51:17 +03:00
|
|
|
$emptyFolder.classList.add("hidden");
|
2022-06-03 01:49:55 +03:00
|
|
|
this.$uploadStatus = document.getElementById(`uploadStatus${idx}`);
|
2022-07-06 14:17:30 +03:00
|
|
|
this.$uploadStatus.innerHTML = '-';
|
|
|
|
Uploader.queues.push(this);
|
|
|
|
Uploader.runQueue();
|
|
|
|
}
|
2022-05-30 06:47:57 +03:00
|
|
|
|
2022-07-06 14:17:30 +03:00
|
|
|
ajax() {
|
2023-02-20 17:50:24 +03:00
|
|
|
const url = newUrl(this.name);
|
2022-07-06 14:17:30 +03:00
|
|
|
this.lastUptime = Date.now();
|
2022-05-31 00:49:42 +03:00
|
|
|
const ajax = new XMLHttpRequest();
|
2022-05-30 06:47:57 +03:00
|
|
|
ajax.upload.addEventListener("progress", e => this.progress(e), false);
|
2022-05-31 00:49:42 +03:00
|
|
|
ajax.addEventListener("readystatechange", () => {
|
2023-02-20 12:23:31 +03:00
|
|
|
if (ajax.readyState === 4) {
|
2022-06-04 07:51:56 +03:00
|
|
|
if (ajax.status >= 200 && ajax.status < 300) {
|
2022-05-31 00:49:42 +03:00
|
|
|
this.complete();
|
|
|
|
} else {
|
|
|
|
this.fail();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
ajax.addEventListener("error", () => this.fail(), false);
|
|
|
|
ajax.addEventListener("abort", () => this.fail(), false);
|
2022-05-30 06:47:57 +03:00
|
|
|
ajax.open("PUT", url);
|
2022-07-06 14:17:30 +03:00
|
|
|
ajax.send(this.file);
|
2022-05-30 06:47:57 +03:00
|
|
|
}
|
2023-02-20 12:23:31 +03:00
|
|
|
|
2022-05-30 06:47:57 +03:00
|
|
|
|
|
|
|
progress(event) {
|
2022-07-05 04:16:21 +03:00
|
|
|
const now = Date.now();
|
|
|
|
const speed = (event.loaded - this.uploaded) / (now - this.lastUptime) * 1000;
|
|
|
|
const [speedValue, speedUnit] = formatSize(speed);
|
2023-11-26 05:23:37 +03:00
|
|
|
const speedText = `${speedValue} ${speedUnit}/s`;
|
2022-06-11 07:34:03 +03:00
|
|
|
const progress = formatPercent((event.loaded / event.total) * 100);
|
|
|
|
const duration = formatDuration((event.total - event.loaded) / speed)
|
2023-11-26 05:23:37 +03:00
|
|
|
this.$uploadStatus.innerHTML = `<span>${speedText}</span><span>${progress} ${duration}</span>`;
|
2022-06-11 07:34:03 +03:00
|
|
|
this.uploaded = event.loaded;
|
|
|
|
this.lastUptime = now;
|
2022-05-30 06:47:57 +03:00
|
|
|
}
|
|
|
|
|
2022-05-31 00:49:42 +03:00
|
|
|
complete() {
|
2022-06-03 01:49:55 +03:00
|
|
|
this.$uploadStatus.innerHTML = `✓`;
|
2023-06-01 16:32:22 +03:00
|
|
|
Uploader.runnings--;
|
2022-07-06 14:17:30 +03:00
|
|
|
Uploader.runQueue();
|
2022-05-30 06:47:57 +03:00
|
|
|
}
|
|
|
|
|
2022-05-31 00:49:42 +03:00
|
|
|
fail() {
|
2022-06-03 01:49:55 +03:00
|
|
|
this.$uploadStatus.innerHTML = `✗`;
|
2023-06-01 16:32:22 +03:00
|
|
|
Uploader.runnings--;
|
2022-07-06 14:17:30 +03:00
|
|
|
Uploader.runQueue();
|
2022-05-30 06:47:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-30 05:00:42 +03:00
|
|
|
Uploader.globalIdx = 0;
|
|
|
|
|
2022-08-03 03:51:12 +03:00
|
|
|
Uploader.runnings = 0;
|
2022-07-06 14:17:30 +03:00
|
|
|
|
2023-02-21 07:42:40 +03:00
|
|
|
Uploader.auth = false;
|
|
|
|
|
2022-07-06 14:17:30 +03:00
|
|
|
/**
|
|
|
|
* @type Uploader[]
|
|
|
|
*/
|
|
|
|
Uploader.queues = [];
|
|
|
|
|
|
|
|
|
2023-02-21 07:42:40 +03:00
|
|
|
Uploader.runQueue = async () => {
|
2023-06-01 16:32:22 +03:00
|
|
|
if (Uploader.runnings >= DUFS_MAX_UPLOADINGS) return;
|
|
|
|
if (Uploader.queues.length == 0) return;
|
|
|
|
Uploader.runnings++;
|
2022-07-06 14:17:30 +03:00
|
|
|
let uploader = Uploader.queues.shift();
|
2023-02-21 07:42:40 +03:00
|
|
|
if (!Uploader.auth) {
|
|
|
|
Uploader.auth = true;
|
2023-06-02 14:35:30 +03:00
|
|
|
try {
|
|
|
|
await checkAuth()
|
|
|
|
} catch {
|
|
|
|
Uploader.auth = false;
|
|
|
|
}
|
2023-02-21 07:42:40 +03:00
|
|
|
}
|
2022-07-06 14:17:30 +03:00
|
|
|
uploader.ajax();
|
|
|
|
}
|
|
|
|
|
2022-06-04 07:51:56 +03:00
|
|
|
/**
|
2022-06-05 01:12:37 +03:00
|
|
|
* Add breadcrumb
|
2023-12-11 13:28:11 +03:00
|
|
|
* @param {string} href
|
2022-06-19 16:23:19 +03:00
|
|
|
* @param {string} uri_prefix
|
2022-06-04 07:51:56 +03:00
|
|
|
*/
|
2022-06-19 16:23:19 +03:00
|
|
|
function addBreadcrumb(href, uri_prefix) {
|
2022-05-31 00:49:42 +03:00
|
|
|
const $breadcrumb = document.querySelector(".breadcrumb");
|
2022-06-19 16:23:19 +03:00
|
|
|
let parts = [];
|
|
|
|
if (href === "/") {
|
|
|
|
parts = [""];
|
|
|
|
} else {
|
|
|
|
parts = href.split("/");
|
|
|
|
}
|
2022-05-31 00:49:42 +03:00
|
|
|
const len = parts.length;
|
2022-06-19 17:22:49 +03:00
|
|
|
let path = uri_prefix;
|
2022-05-31 00:49:42 +03:00
|
|
|
for (let i = 0; i < len; i++) {
|
|
|
|
const name = parts[i];
|
2022-05-30 06:47:57 +03:00
|
|
|
if (i > 0) {
|
2022-06-19 17:22:49 +03:00
|
|
|
if (!path.endsWith("/")) {
|
2023-02-20 12:23:31 +03:00
|
|
|
path += "/";
|
2022-06-19 17:22:49 +03:00
|
|
|
}
|
2022-07-30 02:53:27 +03:00
|
|
|
path += encodeURIComponent(name);
|
2022-05-30 06:47:57 +03:00
|
|
|
}
|
2022-06-21 14:23:34 +03:00
|
|
|
const encodedName = encodedStr(name);
|
2022-06-19 16:23:19 +03:00
|
|
|
if (i === 0) {
|
2023-11-15 14:44:44 +03:00
|
|
|
$breadcrumb.insertAdjacentHTML("beforeend", `<a href="${path}" title="Root"><svg width="16" height="16" viewBox="0 0 16 16"><path d="M6.5 14.5v-3.505c0-.245.25-.495.5-.495h2c.25 0 .5.25.5.5v3.5a.5.5 0 0 0 .5.5h4a.5.5 0 0 0 .5-.5v-7a.5.5 0 0 0-.146-.354L13 5.793V2.5a.5.5 0 0 0-.5-.5h-1a.5.5 0 0 0-.5.5v1.293L8.354 1.146a.5.5 0 0 0-.708 0l-6 6A.5.5 0 0 0 1.5 7.5v7a.5.5 0 0 0 .5.5h4a.5.5 0 0 0 .5-.5z"/></svg></a>`);
|
2022-06-19 16:23:19 +03:00
|
|
|
} else if (i === len - 1) {
|
2022-06-21 14:23:34 +03:00
|
|
|
$breadcrumb.insertAdjacentHTML("beforeend", `<b>${encodedName}</b>`);
|
2022-05-30 06:47:57 +03:00
|
|
|
} else {
|
2022-07-30 02:53:27 +03:00
|
|
|
$breadcrumb.insertAdjacentHTML("beforeend", `<a href="${path}">${encodedName}</a>`);
|
2022-06-19 16:23:19 +03:00
|
|
|
}
|
|
|
|
if (i !== len - 1) {
|
|
|
|
$breadcrumb.insertAdjacentHTML("beforeend", `<span class="separator">/</span>`);
|
2022-05-30 06:47:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-22 05:04:40 +03:00
|
|
|
function setupIndexPage() {
|
|
|
|
if (DATA.allow_archive) {
|
|
|
|
const $download = document.querySelector(".download");
|
|
|
|
$download.href = baseUrl() + "?zip";
|
2023-06-01 02:35:41 +03:00
|
|
|
$download.title = "Download folder as a .zip file";
|
2023-02-22 05:04:40 +03:00
|
|
|
$download.classList.remove("hidden");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DATA.allow_upload) {
|
|
|
|
setupDropzone();
|
|
|
|
setupUploadFile();
|
|
|
|
setupNewFolder();
|
|
|
|
setupNewFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DATA.auth) {
|
|
|
|
setupAuth();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DATA.allow_search) {
|
|
|
|
setupSearch()
|
|
|
|
}
|
|
|
|
|
|
|
|
renderPathsTableHead();
|
|
|
|
renderPathsTableBody();
|
|
|
|
}
|
|
|
|
|
2022-08-23 09:24:42 +03:00
|
|
|
/**
|
|
|
|
* Render path table thead
|
|
|
|
*/
|
|
|
|
function renderPathsTableHead() {
|
|
|
|
const headerItems = [
|
|
|
|
{
|
|
|
|
name: "name",
|
|
|
|
props: `colspan="2"`,
|
|
|
|
text: "Name",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "mtime",
|
|
|
|
props: ``,
|
|
|
|
text: "Last Modified",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "size",
|
|
|
|
props: ``,
|
|
|
|
text: "Size",
|
|
|
|
}
|
|
|
|
];
|
|
|
|
$pathsTableHead.insertAdjacentHTML("beforeend", `
|
|
|
|
<tr>
|
|
|
|
${headerItems.map(item => {
|
2023-02-20 12:23:31 +03:00
|
|
|
let svg = `<svg width="12" height="12" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M11.5 15a.5.5 0 0 0 .5-.5V2.707l3.146 3.147a.5.5 0 0 0 .708-.708l-4-4a.5.5 0 0 0-.708 0l-4 4a.5.5 0 1 0 .708.708L11 2.707V14.5a.5.5 0 0 0 .5.5zm-7-14a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L4 13.293V1.5a.5.5 0 0 1 .5-.5z"/></svg>`;
|
2023-09-06 18:25:04 +03:00
|
|
|
let order = "desc";
|
2023-02-20 12:23:31 +03:00
|
|
|
if (PARAMS.sort === item.name) {
|
2023-09-06 18:25:04 +03:00
|
|
|
if (PARAMS.order === "desc") {
|
|
|
|
order = "asc";
|
2023-02-20 12:23:31 +03:00
|
|
|
svg = `<svg width="12" height="12" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v11.793l3.146-3.147a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 .708-.708L7.5 13.293V1.5A.5.5 0 0 1 8 1z"/></svg>`
|
2023-09-06 18:25:04 +03:00
|
|
|
} else {
|
|
|
|
svg = `<svg width="12" height="12" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M8 15a.5.5 0 0 0 .5-.5V2.707l3.146 3.147a.5.5 0 0 0 .708-.708l-4-4a.5.5 0 0 0-.708 0l-4 4a.5.5 0 1 0 .708.708L7.5 2.707V14.5a.5.5 0 0 0 .5.5z"/></svg>`
|
2023-02-20 12:23:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
const qs = new URLSearchParams({ ...PARAMS, order, sort: item.name }).toString();
|
|
|
|
const icon = `<span>${svg}</span>`
|
|
|
|
return `<th class="cell-${item.name}" ${item.props}><a href="?${qs}">${item.text}${icon}</a></th>`
|
|
|
|
}).join("\n")}
|
2022-08-23 09:24:42 +03:00
|
|
|
<th class="cell-actions">Actions</th>
|
|
|
|
</tr>
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render path table tbody
|
|
|
|
*/
|
|
|
|
function renderPathsTableBody() {
|
|
|
|
if (DATA.paths && DATA.paths.length > 0) {
|
|
|
|
const len = DATA.paths.length;
|
|
|
|
if (len > 0) {
|
|
|
|
$pathsTable.classList.remove("hidden");
|
|
|
|
}
|
|
|
|
for (let i = 0; i < len; i++) {
|
|
|
|
addPath(DATA.paths[i], i);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$emptyFolder.textContent = dirEmptyNote;
|
|
|
|
$emptyFolder.classList.remove("hidden");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-04 07:51:56 +03:00
|
|
|
/**
|
|
|
|
* Add pathitem
|
2023-12-11 13:28:11 +03:00
|
|
|
* @param {PathItem} file
|
|
|
|
* @param {number} index
|
2022-06-04 07:51:56 +03:00
|
|
|
*/
|
2022-05-30 06:47:57 +03:00
|
|
|
function addPath(file, index) {
|
2022-06-21 14:23:34 +03:00
|
|
|
const encodedName = encodedStr(file.name);
|
2023-02-20 17:50:24 +03:00
|
|
|
let url = newUrl(file.name)
|
2022-05-31 00:49:42 +03:00
|
|
|
let actionDelete = "";
|
|
|
|
let actionDownload = "";
|
2022-07-04 16:20:00 +03:00
|
|
|
let actionMove = "";
|
2023-02-21 03:13:29 +03:00
|
|
|
let actionEdit = "";
|
2023-11-28 02:14:53 +03:00
|
|
|
let actionView = "";
|
2023-02-21 03:13:29 +03:00
|
|
|
let isDir = file.path_type.endsWith("Dir");
|
|
|
|
if (isDir) {
|
2022-06-17 14:01:17 +03:00
|
|
|
url += "/";
|
2022-12-10 06:09:42 +03:00
|
|
|
if (DATA.allow_archive) {
|
|
|
|
actionDownload = `
|
|
|
|
<div class="action-btn">
|
2023-02-21 03:13:29 +03:00
|
|
|
<a href="${url}?zip" title="Download folder as a .zip file">${ICONS.download}</a>
|
2022-12-10 06:09:42 +03:00
|
|
|
</div>`;
|
|
|
|
}
|
2022-05-30 06:47:57 +03:00
|
|
|
} else {
|
|
|
|
actionDownload = `
|
|
|
|
<div class="action-btn" >
|
2023-02-21 03:13:29 +03:00
|
|
|
<a href="${url}" title="Download file" download>${ICONS.download}</a>
|
2022-05-30 06:47:57 +03:00
|
|
|
</div>`;
|
|
|
|
}
|
2022-05-31 03:38:30 +03:00
|
|
|
if (DATA.allow_delete) {
|
2022-07-05 04:16:21 +03:00
|
|
|
if (DATA.allow_upload) {
|
2023-06-01 15:33:21 +03:00
|
|
|
actionMove = `<div onclick="movePath(${index})" class="action-btn" id="moveBtn${index}" title="Move to new path">${ICONS.move}</div>`;
|
|
|
|
if (!isDir) {
|
2023-02-21 03:13:29 +03:00
|
|
|
actionEdit = `<a class="action-btn" title="Edit file" target="_blank" href="${url}?edit">${ICONS.edit}</a>`;
|
|
|
|
}
|
2022-07-05 04:16:21 +03:00
|
|
|
}
|
2022-05-30 06:47:57 +03:00
|
|
|
actionDelete = `
|
2023-02-21 03:13:29 +03:00
|
|
|
<div onclick="deletePath(${index})" class="action-btn" id="deleteBtn${index}" title="Delete">${ICONS.delete}</div>`;
|
2022-05-30 06:47:57 +03:00
|
|
|
}
|
2023-11-28 02:14:53 +03:00
|
|
|
if (!actionEdit && !isDir) {
|
|
|
|
actionView = `<a class="action-btn" title="View file" target="_blank" href="${url}?view">${ICONS.view}</a>`;
|
|
|
|
}
|
2022-05-31 00:49:42 +03:00
|
|
|
let actionCell = `
|
2022-05-30 06:47:57 +03:00
|
|
|
<td class="cell-actions">
|
|
|
|
${actionDownload}
|
2023-11-28 02:14:53 +03:00
|
|
|
${actionView}
|
2022-07-04 16:20:00 +03:00
|
|
|
${actionMove}
|
2023-02-22 05:04:40 +03:00
|
|
|
${actionDelete}
|
2023-06-01 15:33:21 +03:00
|
|
|
${actionEdit}
|
2022-05-30 06:47:57 +03:00
|
|
|
</td>`
|
|
|
|
|
2022-06-03 01:49:55 +03:00
|
|
|
$pathsTableBody.insertAdjacentHTML("beforeend", `
|
2022-05-30 06:47:57 +03:00
|
|
|
<tr id="addPath${index}">
|
2022-06-19 17:04:59 +03:00
|
|
|
<td class="path cell-icon">
|
2023-02-20 17:50:24 +03:00
|
|
|
${getPathSvg(file.path_type)}
|
2022-06-19 17:04:59 +03:00
|
|
|
</td>
|
2022-06-03 01:49:55 +03:00
|
|
|
<td class="path cell-name">
|
2023-02-22 05:04:40 +03:00
|
|
|
<a href="${url}" ${isDir ? "" : `target="_blank"`}>${encodedName}</a>
|
2022-06-03 01:49:55 +03:00
|
|
|
</td>
|
|
|
|
<td class="cell-mtime">${formatMtime(file.mtime)}</td>
|
2022-06-11 07:34:03 +03:00
|
|
|
<td class="cell-size">${formatSize(file.size).join(" ")}</td>
|
2022-06-03 01:49:55 +03:00
|
|
|
${actionCell}
|
2022-05-30 06:47:57 +03:00
|
|
|
</tr>`)
|
|
|
|
}
|
|
|
|
|
2023-02-22 05:04:40 +03:00
|
|
|
function setupDropzone() {
|
2023-02-20 12:23:31 +03:00
|
|
|
["drag", "dragstart", "dragend", "dragover", "dragenter", "dragleave", "drop"].forEach(name => {
|
|
|
|
document.addEventListener(name, e => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2022-05-31 11:43:42 +03:00
|
|
|
});
|
2023-02-20 12:23:31 +03:00
|
|
|
});
|
2023-02-21 07:42:40 +03:00
|
|
|
document.addEventListener("drop", async e => {
|
2023-02-20 12:23:31 +03:00
|
|
|
if (!e.dataTransfer.items[0].webkitGetAsEntry) {
|
|
|
|
const files = e.dataTransfer.files.filter(v => v.size > 0);
|
|
|
|
for (const file of files) {
|
|
|
|
new Uploader(file, []).upload();
|
2022-05-31 11:43:42 +03:00
|
|
|
}
|
2023-02-20 12:23:31 +03:00
|
|
|
} else {
|
|
|
|
const entries = [];
|
|
|
|
const len = e.dataTransfer.items.length;
|
|
|
|
for (let i = 0; i < len; i++) {
|
|
|
|
entries.push(e.dataTransfer.items[i].webkitGetAsEntry());
|
|
|
|
}
|
|
|
|
addFileEntries(entries, [])
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-21 07:42:40 +03:00
|
|
|
function setupAuth() {
|
|
|
|
if (DATA.user) {
|
|
|
|
$userBtn.classList.remove("hidden");
|
2023-11-03 09:55:07 +03:00
|
|
|
$userName.textContent = DATA.user;
|
2023-02-21 07:42:40 +03:00
|
|
|
} else {
|
|
|
|
const $loginBtn = document.querySelector(".login-btn");
|
|
|
|
$loginBtn.classList.remove("hidden");
|
2023-06-02 14:35:30 +03:00
|
|
|
$loginBtn.addEventListener("click", async () => {
|
|
|
|
try {
|
|
|
|
await checkAuth()
|
2023-06-03 05:09:02 +03:00
|
|
|
location.reload();
|
2023-06-02 14:35:30 +03:00
|
|
|
} catch (err) {
|
|
|
|
alert(err.message);
|
|
|
|
}
|
|
|
|
});
|
2023-02-21 07:42:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-20 12:23:31 +03:00
|
|
|
function setupSearch() {
|
2023-02-20 17:50:24 +03:00
|
|
|
const $searchbar = document.querySelector(".searchbar");
|
2023-02-20 12:23:31 +03:00
|
|
|
$searchbar.classList.remove("hidden");
|
|
|
|
$searchbar.addEventListener("submit", event => {
|
|
|
|
event.preventDefault();
|
|
|
|
const formData = new FormData($searchbar);
|
|
|
|
const q = formData.get("q");
|
2023-02-20 17:50:24 +03:00
|
|
|
let href = baseUrl();
|
2023-02-20 12:23:31 +03:00
|
|
|
if (q) {
|
|
|
|
href += "?q=" + q;
|
|
|
|
}
|
|
|
|
location.href = href;
|
|
|
|
});
|
|
|
|
if (PARAMS.q) {
|
|
|
|
document.getElementById('search').value = PARAMS.q;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-22 05:04:40 +03:00
|
|
|
function setupUploadFile() {
|
2023-02-20 12:23:31 +03:00
|
|
|
document.querySelector(".upload-file").classList.remove("hidden");
|
2023-02-21 07:42:40 +03:00
|
|
|
document.getElementById("file").addEventListener("change", async e => {
|
2023-02-20 12:23:31 +03:00
|
|
|
const files = e.target.files;
|
|
|
|
for (let file of files) {
|
|
|
|
new Uploader(file, []).upload();
|
|
|
|
}
|
|
|
|
});
|
2022-05-31 11:43:42 +03:00
|
|
|
}
|
|
|
|
|
2023-02-22 05:04:40 +03:00
|
|
|
function setupNewFolder() {
|
|
|
|
const $newFolder = document.querySelector(".new-folder");
|
|
|
|
$newFolder.classList.remove("hidden");
|
|
|
|
$newFolder.addEventListener("click", () => {
|
|
|
|
const name = prompt("Enter folder name");
|
|
|
|
if (name) createFolder(name);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-21 03:45:52 +03:00
|
|
|
function setupNewFile() {
|
|
|
|
const $newFile = document.querySelector(".new-file");
|
|
|
|
$newFile.classList.remove("hidden");
|
|
|
|
$newFile.addEventListener("click", () => {
|
|
|
|
const name = prompt("Enter file name");
|
|
|
|
if (name) createFile(name);
|
|
|
|
});
|
|
|
|
}
|
2023-02-20 17:50:24 +03:00
|
|
|
|
2023-11-28 02:14:53 +03:00
|
|
|
async function setupEditorPage() {
|
2023-02-22 05:04:40 +03:00
|
|
|
const url = baseUrl();
|
|
|
|
|
|
|
|
const $download = document.querySelector(".download");
|
2023-02-20 17:50:24 +03:00
|
|
|
$download.classList.remove("hidden");
|
2023-02-22 05:04:40 +03:00
|
|
|
$download.href = url;
|
|
|
|
|
2023-11-28 02:14:53 +03:00
|
|
|
if (DATA.kind == "Edit") {
|
|
|
|
const $moveFile = document.querySelector(".move-file");
|
|
|
|
$moveFile.classList.remove("hidden");
|
|
|
|
$moveFile.addEventListener("click", async () => {
|
|
|
|
const query = location.href.slice(url.length);
|
|
|
|
const newFileUrl = await doMovePath(url);
|
|
|
|
if (newFileUrl) {
|
|
|
|
location.href = newFileUrl + query;
|
|
|
|
}
|
2023-02-22 05:04:40 +03:00
|
|
|
});
|
2023-11-28 02:14:53 +03:00
|
|
|
|
|
|
|
const $deleteFile = document.querySelector(".delete-file");
|
|
|
|
$deleteFile.classList.remove("hidden");
|
|
|
|
$deleteFile.addEventListener("click", async () => {
|
|
|
|
const url = baseUrl();
|
|
|
|
const name = baseName(url);
|
|
|
|
await doDeletePath(name, url, () => {
|
|
|
|
location.href = location.href.split("/").slice(0, -1).join("/");
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
|
|
|
const $saveBtn = document.querySelector(".save-btn");
|
|
|
|
$saveBtn.classList.remove("hidden");
|
|
|
|
$saveBtn.addEventListener("click", saveChange);
|
|
|
|
} else if (DATA.kind == "View") {
|
|
|
|
$editor.readonly = true;
|
|
|
|
}
|
2023-02-20 17:50:24 +03:00
|
|
|
|
|
|
|
if (!DATA.editable) {
|
|
|
|
const $notEditable = document.querySelector(".not-editable");
|
2023-02-22 05:04:40 +03:00
|
|
|
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>`)
|
|
|
|
} else {
|
|
|
|
$notEditable.classList.remove("hidden");
|
2023-06-01 02:35:41 +03:00
|
|
|
$notEditable.textContent = "Cannot edit because file is too large or binary.";
|
2023-02-22 05:04:40 +03:00
|
|
|
}
|
2023-02-20 17:50:24 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$editor.classList.remove("hidden");
|
|
|
|
try {
|
|
|
|
const res = await fetch(baseUrl());
|
2023-02-21 07:42:40 +03:00
|
|
|
await assertResOK(res);
|
2023-03-17 06:22:21 +03:00
|
|
|
const encoding = getEncoding(res.headers.get("content-type"));
|
|
|
|
if (encoding === "utf-8") {
|
|
|
|
$editor.value = await res.text();
|
|
|
|
} else {
|
|
|
|
const bytes = await res.arrayBuffer();
|
|
|
|
const dataView = new DataView(bytes)
|
|
|
|
const decoder = new TextDecoder(encoding)
|
|
|
|
$editor.value = decoder.decode(dataView);
|
|
|
|
}
|
2023-02-20 17:50:24 +03:00
|
|
|
} catch (err) {
|
|
|
|
alert(`Failed get file, ${err.message}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-22 05:04:40 +03:00
|
|
|
/**
|
|
|
|
* Delete path
|
2023-12-11 13:28:11 +03:00
|
|
|
* @param {number} index
|
|
|
|
* @returns
|
2023-02-22 05:04:40 +03:00
|
|
|
*/
|
|
|
|
async function deletePath(index) {
|
|
|
|
const file = DATA.paths[index];
|
|
|
|
if (!file) return;
|
|
|
|
await doDeletePath(file.name, newUrl(file.name), () => {
|
|
|
|
document.getElementById(`addPath${index}`)?.remove();
|
|
|
|
DATA.paths[index] = null;
|
|
|
|
if (!DATA.paths.find(v => !!v)) {
|
|
|
|
$pathsTable.classList.add("hidden");
|
|
|
|
$emptyFolder.textContent = dirEmptyNote;
|
|
|
|
$emptyFolder.classList.remove("hidden");
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function doDeletePath(name, url, cb) {
|
|
|
|
if (!confirm(`Delete \`${name}\`?`)) return;
|
|
|
|
try {
|
2023-06-01 17:22:36 +03:00
|
|
|
await checkAuth();
|
2023-02-22 05:04:40 +03:00
|
|
|
const res = await fetch(url, {
|
|
|
|
method: "DELETE",
|
|
|
|
});
|
|
|
|
await assertResOK(res);
|
|
|
|
cb();
|
|
|
|
} catch (err) {
|
|
|
|
alert(`Cannot delete \`${file.name}\`, ${err.message}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move path
|
2023-12-11 13:28:11 +03:00
|
|
|
* @param {number} index
|
|
|
|
* @returns
|
2023-02-22 05:04:40 +03:00
|
|
|
*/
|
|
|
|
async function movePath(index) {
|
|
|
|
const file = DATA.paths[index];
|
|
|
|
if (!file) return;
|
|
|
|
const fileUrl = newUrl(file.name);
|
|
|
|
const newFileUrl = await doMovePath(fileUrl);
|
|
|
|
if (newFileUrl) {
|
|
|
|
location.href = newFileUrl.split("/").slice(0, -1).join("/");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function doMovePath(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)
|
|
|
|
if (!newPath) return;
|
|
|
|
if (!newPath.startsWith("/")) newPath = "/" + newPath;
|
|
|
|
if (filePath === newPath) return;
|
|
|
|
const newFileUrl = fileUrlObj.origin + prefix + newPath.split("/").map(encodeURIComponent).join("/");
|
|
|
|
|
|
|
|
try {
|
2023-06-01 17:22:36 +03:00
|
|
|
await checkAuth();
|
2023-02-22 05:04:40 +03:00
|
|
|
const res1 = await fetch(newFileUrl, {
|
|
|
|
method: "HEAD",
|
|
|
|
});
|
|
|
|
if (res1.status === 200) {
|
2023-06-01 02:35:41 +03:00
|
|
|
if (!confirm("Override existing file?")) {
|
2023-02-22 05:04:40 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const res2 = await fetch(fileUrl, {
|
|
|
|
method: "MOVE",
|
|
|
|
headers: {
|
|
|
|
"Destination": newFileUrl,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await assertResOK(res2);
|
|
|
|
return newFileUrl;
|
|
|
|
} catch (err) {
|
|
|
|
alert(`Cannot move \`${filePath}\` to \`${newPath}\`, ${err.message}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-20 17:50:24 +03:00
|
|
|
/**
|
|
|
|
* Save editor change
|
|
|
|
*/
|
|
|
|
async function saveChange() {
|
|
|
|
try {
|
|
|
|
await fetch(baseUrl(), {
|
|
|
|
method: "PUT",
|
|
|
|
body: $editor.value,
|
|
|
|
});
|
2023-04-01 11:10:34 +03:00
|
|
|
location.reload();
|
2023-02-20 17:50:24 +03:00
|
|
|
} catch (err) {
|
|
|
|
alert(`Failed to save file, ${err.message}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-02 14:35:30 +03:00
|
|
|
async function checkAuth() {
|
2023-02-21 07:42:40 +03:00
|
|
|
if (!DATA.auth) return;
|
2023-06-02 14:35:30 +03:00
|
|
|
const res = await fetch(baseUrl(), {
|
|
|
|
method: "WRITEABLE",
|
|
|
|
});
|
|
|
|
await assertResOK(res);
|
|
|
|
document.querySelector(".login-btn").classList.add("hidden");
|
|
|
|
$userBtn.classList.remove("hidden");
|
2023-11-03 09:55:07 +03:00
|
|
|
$userName.textContent = "";
|
2023-02-21 07:42:40 +03:00
|
|
|
}
|
|
|
|
|
2022-07-04 15:12:35 +03:00
|
|
|
/**
|
|
|
|
* Create a folder
|
2023-12-11 13:28:11 +03:00
|
|
|
* @param {string} name
|
2022-07-04 15:12:35 +03:00
|
|
|
*/
|
|
|
|
async function createFolder(name) {
|
2023-02-20 17:50:24 +03:00
|
|
|
const url = newUrl(name);
|
2022-07-04 15:12:35 +03:00
|
|
|
try {
|
2023-06-01 17:22:36 +03:00
|
|
|
await checkAuth();
|
2022-07-04 15:12:35 +03:00
|
|
|
const res = await fetch(url, {
|
|
|
|
method: "MKCOL",
|
|
|
|
});
|
2023-02-21 07:42:40 +03:00
|
|
|
await assertResOK(res);
|
2023-02-20 17:50:24 +03:00
|
|
|
location.href = url;
|
2022-07-04 15:12:35 +03:00
|
|
|
} catch (err) {
|
|
|
|
alert(`Cannot create folder \`${name}\`, ${err.message}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 03:45:52 +03:00
|
|
|
async function createFile(name) {
|
|
|
|
const url = newUrl(name);
|
|
|
|
try {
|
2023-06-01 17:22:36 +03:00
|
|
|
await checkAuth();
|
2023-02-21 03:45:52 +03:00
|
|
|
const res = await fetch(url, {
|
|
|
|
method: "PUT",
|
|
|
|
body: "",
|
|
|
|
});
|
2023-02-21 07:42:40 +03:00
|
|
|
await assertResOK(res);
|
2023-02-21 03:45:52 +03:00
|
|
|
location.href = url + "?edit";
|
|
|
|
} catch (err) {
|
|
|
|
alert(`Cannot create file \`${name}\`, ${err.message}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 11:43:42 +03:00
|
|
|
async function addFileEntries(entries, dirs) {
|
|
|
|
for (const entry of entries) {
|
|
|
|
if (entry.isFile) {
|
|
|
|
entry.file(file => {
|
|
|
|
new Uploader(file, dirs).upload();
|
|
|
|
});
|
|
|
|
} else if (entry.isDirectory) {
|
2023-12-11 13:28:11 +03:00
|
|
|
const dirReader = entry.createReader();
|
|
|
|
|
|
|
|
const successCallback = entries => {
|
|
|
|
if (entries.length > 0) {
|
|
|
|
addFileEntries(entries, [...dirs, entry.name]);
|
|
|
|
dirReader.readEntries(successCallback);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
dirReader.readEntries(successCallback);
|
2022-05-31 11:43:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-20 17:50:24 +03:00
|
|
|
function newUrl(name) {
|
|
|
|
let url = baseUrl();
|
2023-02-20 12:23:31 +03:00
|
|
|
if (!url.endsWith("/")) url += "/";
|
|
|
|
url += name.split("/").map(encodeURIComponent).join("/");
|
|
|
|
return url;
|
2022-05-30 06:47:57 +03:00
|
|
|
}
|
|
|
|
|
2023-02-20 17:50:24 +03:00
|
|
|
function baseUrl() {
|
|
|
|
return location.href.split('?')[0];
|
|
|
|
}
|
|
|
|
|
2023-02-22 05:04:40 +03:00
|
|
|
function baseName(url) {
|
|
|
|
return decodeURIComponent(url.split("/").filter(v => v.length > 0).slice(-1)[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
function extName(filename) {
|
|
|
|
const dotIndex = filename.lastIndexOf('.');
|
|
|
|
|
|
|
|
if (dotIndex === -1 || dotIndex === 0 || dotIndex === filename.length - 1) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return filename.substring(dotIndex);
|
|
|
|
}
|
|
|
|
|
2023-02-20 17:50:24 +03:00
|
|
|
function getPathSvg(path_type) {
|
2022-06-04 08:13:37 +03:00
|
|
|
switch (path_type) {
|
2022-05-30 06:47:57 +03:00
|
|
|
case "Dir":
|
2023-02-21 03:13:29 +03:00
|
|
|
return ICONS.dir;
|
2022-06-30 05:00:42 +03:00
|
|
|
case "SymlinkFile":
|
2023-02-21 03:13:29 +03:00
|
|
|
return ICONS.symlinkFile;
|
2022-05-30 06:47:57 +03:00
|
|
|
case "SymlinkDir":
|
2023-02-21 03:13:29 +03:00
|
|
|
return ICONS.symlinkDir;
|
2022-05-30 06:47:57 +03:00
|
|
|
default:
|
2023-02-21 03:13:29 +03:00
|
|
|
return ICONS.file;
|
2022-05-30 06:47:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatMtime(mtime) {
|
|
|
|
if (!mtime) return ""
|
2022-05-31 00:49:42 +03:00
|
|
|
const date = new Date(mtime);
|
|
|
|
const year = date.getFullYear();
|
|
|
|
const month = padZero(date.getMonth() + 1, 2);
|
|
|
|
const day = padZero(date.getDate(), 2);
|
|
|
|
const hours = padZero(date.getHours(), 2);
|
|
|
|
const minutes = padZero(date.getMinutes(), 2);
|
2022-06-19 17:04:59 +03:00
|
|
|
return `${year}-${month}-${day} ${hours}:${minutes}`;
|
2022-05-30 06:47:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function padZero(value, size) {
|
|
|
|
return ("0".repeat(size) + value).slice(-1 * size)
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatSize(size) {
|
2023-02-21 03:13:29 +03:00
|
|
|
if (size == null) return []
|
2022-05-31 00:49:42 +03:00
|
|
|
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
|
2023-02-21 03:13:29 +03:00
|
|
|
if (size == 0) return [0, "B"];
|
2022-05-31 00:49:42 +03:00
|
|
|
const i = parseInt(Math.floor(Math.log(size) / Math.log(1024)));
|
2023-05-18 07:01:02 +03:00
|
|
|
ratio = 1
|
|
|
|
if (i >= 3) {
|
|
|
|
ratio = 100
|
|
|
|
}
|
|
|
|
return [Math.round(size * ratio / Math.pow(1024, i), 2) / ratio, sizes[i]];
|
2022-06-11 07:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function formatDuration(seconds) {
|
|
|
|
seconds = Math.ceil(seconds);
|
2022-07-05 04:16:21 +03:00
|
|
|
const h = Math.floor(seconds / 3600);
|
|
|
|
const m = Math.floor((seconds - h * 3600) / 60);
|
|
|
|
const s = seconds - h * 3600 - m * 60
|
2022-06-11 07:34:03 +03:00
|
|
|
return `${padZero(h, 2)}:${padZero(m, 2)}:${padZero(s, 2)}`;
|
|
|
|
}
|
|
|
|
|
2022-12-10 04:18:54 +03:00
|
|
|
function formatPercent(percent) {
|
|
|
|
if (percent > 10) {
|
|
|
|
return percent.toFixed(1) + "%";
|
2022-06-11 07:34:03 +03:00
|
|
|
} else {
|
2022-12-10 04:18:54 +03:00
|
|
|
return percent.toFixed(2) + "%";
|
2022-06-11 07:34:03 +03:00
|
|
|
}
|
2022-05-30 06:47:57 +03:00
|
|
|
}
|
|
|
|
|
2022-06-21 14:23:34 +03:00
|
|
|
function encodedStr(rawStr) {
|
2023-02-20 12:23:31 +03:00
|
|
|
return rawStr.replace(/[\u00A0-\u9999<>\&]/g, function (i) {
|
|
|
|
return '&#' + i.charCodeAt(0) + ';';
|
2022-06-21 14:23:34 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-21 07:42:40 +03:00
|
|
|
async function assertResOK(res) {
|
2023-02-20 17:50:24 +03:00
|
|
|
if (!(res.status >= 200 && res.status < 300)) {
|
2023-06-02 14:35:30 +03:00
|
|
|
throw new Error(await res.text() || `Invalid status ${res.status}`);
|
2022-05-30 06:47:57 +03:00
|
|
|
}
|
2022-06-03 01:49:55 +03:00
|
|
|
}
|
2023-03-17 06:22:21 +03:00
|
|
|
|
|
|
|
function getEncoding(contentType) {
|
|
|
|
const charset = contentType?.split(";")[1];
|
|
|
|
if (/charset/i.test(charset)) {
|
|
|
|
let encoding = charset.split("=")[1];
|
|
|
|
if (encoding) {
|
|
|
|
return encoding.toLowerCase()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 'utf-8'
|
2023-05-18 07:01:02 +03:00
|
|
|
}
|