2022-05-02 13:47:22 +00:00
|
|
|
import { fetchURL, removePrefix, createURL } from "./utils";
|
2021-03-21 11:51:58 +00:00
|
|
|
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}`, {
|
2022-02-21 19:47:28 +00:00
|
|
|
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);
|
|
|
|
}
|
2022-05-02 13:47:22 +00:00
|
|
|
|
|
|
|
export function getDownloadURL(share, inline = false) {
|
|
|
|
const params = {
|
|
|
|
...(inline && { inline: "true" }),
|
|
|
|
...(share.token && { token: share.token }),
|
|
|
|
};
|
|
|
|
|
|
|
|
return createURL("api/public/dl/" + share.hash + share.path, params, false);
|
|
|
|
}
|