filebrowser/frontend/src/utils/upload.js

130 lines
2.7 KiB
JavaScript
Raw Normal View History

2024-07-30 17:45:27 +00:00
import { state } from "@/store";
2021-03-21 11:51:58 +00:00
import url from "@/utils/url";
2020-07-07 19:58:07 +00:00
export function checkConflict(files, items) {
2021-03-21 11:51:58 +00:00
if (typeof items === "undefined" || items === null) {
items = [];
2020-07-07 19:58:07 +00:00
}
2021-03-21 11:51:58 +00:00
let folder_upload = files[0].fullPath !== undefined;
2020-07-07 19:58:07 +00:00
2021-03-21 11:51:58 +00:00
let conflict = false;
2020-07-07 19:58:07 +00:00
for (let i = 0; i < files.length; i++) {
2021-03-21 11:51:58 +00:00
let file = files[i];
let name = file.name;
2020-07-07 19:58:07 +00:00
if (folder_upload) {
2021-03-21 11:51:58 +00:00
let dirs = file.fullPath.split("/");
2020-07-07 19:58:07 +00:00
if (dirs.length > 1) {
2021-03-21 11:51:58 +00:00
name = dirs[0];
2020-07-07 19:58:07 +00:00
}
}
let res = items.findIndex(function hasConflict(element) {
2021-03-21 11:51:58 +00:00
return element.name === this;
}, name);
2020-07-07 19:58:07 +00:00
if (res >= 0) {
2021-03-21 11:51:58 +00:00
conflict = true;
break;
2020-07-07 19:58:07 +00:00
}
}
2021-03-21 11:51:58 +00:00
return conflict;
2020-07-07 19:58:07 +00:00
}
export function scanFiles(dt) {
return new Promise((resolve) => {
2021-03-21 11:51:58 +00:00
let reading = 0;
const contents = [];
2020-07-07 19:58:07 +00:00
if (dt.items !== undefined) {
for (let item of dt.items) {
2021-03-21 11:51:58 +00:00
if (
item.kind === "file" &&
typeof item.webkitGetAsEntry === "function"
) {
const entry = item.webkitGetAsEntry();
readEntry(entry);
2020-07-07 19:58:07 +00:00
}
}
} else {
2021-03-21 11:51:58 +00:00
resolve(dt.files);
2020-07-07 19:58:07 +00:00
}
function readEntry(entry, directory = "") {
if (entry.isFile) {
2021-03-21 11:51:58 +00:00
reading++;
entry.file((file) => {
reading--;
2020-07-07 19:58:07 +00:00
2021-03-21 11:51:58 +00:00
file.fullPath = `${directory}${file.name}`;
contents.push(file);
2020-07-07 19:58:07 +00:00
if (reading === 0) {
2021-03-21 11:51:58 +00:00
resolve(contents);
2020-07-07 19:58:07 +00:00
}
2021-03-21 11:51:58 +00:00
});
2020-07-07 19:58:07 +00:00
} else if (entry.isDirectory) {
const dir = {
isDir: true,
2020-07-09 15:16:04 +00:00
size: 0,
2021-03-21 11:51:58 +00:00
fullPath: `${directory}${entry.name}`,
2022-05-02 15:03:02 +00:00
name: entry.name,
2021-03-21 11:51:58 +00:00
};
2020-07-07 19:58:07 +00:00
2021-03-21 11:51:58 +00:00
contents.push(dir);
2020-07-07 19:58:07 +00:00
2021-03-21 11:51:58 +00:00
readReaderContent(entry.createReader(), `${directory}${entry.name}`);
2020-07-07 19:58:07 +00:00
}
}
function readReaderContent(reader, directory) {
2021-03-21 11:51:58 +00:00
reading++;
2020-07-07 19:58:07 +00:00
reader.readEntries(function (entries) {
2021-03-21 11:51:58 +00:00
reading--;
2020-07-07 19:58:07 +00:00
if (entries.length > 0) {
for (const entry of entries) {
2021-03-21 11:51:58 +00:00
readEntry(entry, `${directory}/`);
2020-07-07 19:58:07 +00:00
}
2021-03-21 11:51:58 +00:00
readReaderContent(reader, `${directory}/`);
2020-07-07 19:58:07 +00:00
}
if (reading === 0) {
2021-03-21 11:51:58 +00:00
resolve(contents);
2020-07-07 19:58:07 +00:00
}
2021-03-21 11:51:58 +00:00
});
2020-07-07 19:58:07 +00:00
}
2021-03-21 11:51:58 +00:00
});
2020-07-07 19:58:07 +00:00
}
2020-09-29 14:04:03 +00:00
export function handleFiles(files, base, overwrite = false) {
2020-07-07 19:58:07 +00:00
for (let i = 0; i < files.length; i++) {
2024-07-30 17:45:27 +00:00
let id = state.upload.id;
2021-03-21 11:51:58 +00:00
let path = base;
let file = files[i];
2020-07-07 19:58:07 +00:00
2020-09-29 14:04:03 +00:00
if (file.fullPath !== undefined) {
2021-03-21 11:51:58 +00:00
path += url.encodePath(file.fullPath);
2020-09-29 14:04:03 +00:00
} else {
2021-03-21 11:51:58 +00:00
path += url.encodeRFC5987ValueChars(file.name);
2020-09-29 14:04:03 +00:00
}
2020-07-07 19:58:07 +00:00
2020-07-09 15:16:04 +00:00
if (file.isDir) {
2021-03-21 11:51:58 +00:00
path += "/";
2020-07-07 19:58:07 +00:00
}
2020-07-09 15:16:04 +00:00
const item = {
id,
2020-09-29 14:04:03 +00:00
path,
2020-07-09 15:16:04 +00:00
file,
2021-03-21 11:51:58 +00:00
overwrite,
...(!file.isDir && { type: file.type }),
2021-03-21 11:51:58 +00:00
};
2020-07-07 19:58:07 +00:00
2024-07-30 17:45:27 +00:00
state.dispatch("upload/upload", item);
2020-07-07 19:58:07 +00:00
}
2021-03-21 11:51:58 +00:00
}