filebrowser/frontend/src/api/pub.js

62 lines
1.3 KiB
JavaScript
Raw Normal View History

2021-03-21 11:51:58 +00:00
import { fetchURL, removePrefix } from "./utils";
import { baseURL } from "@/utils/constants";
2021-03-03 17:46:37 +00:00
2021-03-21 11:51:58 +00:00
export async function fetch(url, password = "") {
url = removePrefix(url);
2021-03-12 15:15:56 +00:00
const res = await fetchURL(`/api/public/share${url}`, {
headers: { "X-SHARE-PASSWORD": encodeURIComponent(password) },
2021-03-21 11:51:58 +00:00
});
2021-03-12 15:15:56 +00:00
if (res.status === 200) {
2021-03-21 11:51:58 +00:00
let data = await res.json();
data.url = `/share${url}`;
2021-03-12 15:15:56 +00:00
if (data.isDir) {
2021-03-21 11:51:58 +00:00
if (!data.url.endsWith("/")) data.url += "/";
2021-03-12 15:15:56 +00:00
data.items = data.items.map((item, index) => {
2021-03-21 11:51:58 +00:00
item.index = index;
item.url = `${data.url}${encodeURIComponent(item.name)}`;
2021-03-12 15:15:56 +00:00
if (item.isDir) {
2021-03-21 11:51:58 +00:00
item.url += "/";
2021-03-12 15:15:56 +00:00
}
2021-03-21 11:51:58 +00:00
return item;
});
2021-03-12 15:15:56 +00:00
}
2021-03-21 11:51:58 +00:00
return data;
2021-03-12 15:15:56 +00:00
} else {
2021-03-21 11:51:58 +00:00
throw new Error(res.status);
2021-03-12 15:15:56 +00:00
}
2021-03-03 17:46:37 +00:00
}
export function download(format, hash, token, ...files) {
2021-03-21 11:51:58 +00:00
let url = `${baseURL}/api/public/dl/${hash}`;
2021-03-03 17:46:37 +00:00
if (files.length === 1) {
2021-03-21 11:51:58 +00:00
url += encodeURIComponent(files[0]) + "?";
2021-03-03 17:46:37 +00:00
} else {
2021-03-21 11:51:58 +00:00
let arg = "";
2021-03-03 17:46:37 +00:00
for (let file of files) {
2021-03-21 11:51:58 +00:00
arg += encodeURIComponent(file) + ",";
2021-03-03 17:46:37 +00:00
}
2021-03-21 11:51:58 +00:00
arg = arg.substring(0, arg.length - 1);
arg = encodeURIComponent(arg);
url += `/?files=${arg}&`;
2021-03-03 17:46:37 +00:00
}
if (format) {
2021-03-21 11:51:58 +00:00
url += `algo=${format}&`;
2021-03-03 17:46:37 +00:00
}
if (token) {
2021-03-21 11:51:58 +00:00
url += `token=${token}&`;
2021-03-03 17:46:37 +00:00
}
2021-03-21 11:51:58 +00:00
window.open(url);
}