Files
filebrowser/frontend/src/api/pub.ts
T

76 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-05-02 13:47:22 +00:00
import { fetchURL, removePrefix, createURL } from "./utils";
2021-03-21 12:51:58 +01:00
import { baseURL } from "@/utils/constants";
2021-03-03 17:46:37 +00:00
2024-04-01 17:18:22 +02:00
export async function fetch(url: string, password: string = "") {
2021-03-21 12:51:58 +01:00
url = removePrefix(url);
2021-03-12 15:15:56 +00:00
2022-05-04 12:58:19 +00:00
const res = await fetchURL(
`/api/public/share${url}`,
{
headers: { "X-SHARE-PASSWORD": encodeURIComponent(password) },
},
false
);
2021-03-12 15:15:56 +00:00
2024-04-01 17:18:22 +02:00
const data = (await res.json()) as Resource;
2022-05-04 12:11:36 +00:00
data.url = `/share${url}`;
2021-03-12 15:15:56 +00:00
2022-05-04 12:11:36 +00:00
if (data.isDir) {
if (!data.url.endsWith("/")) data.url += "/";
2024-04-01 17:18:22 +02:00
data.items = data.items.map((item: any, index: any) => {
2022-05-04 12:11:36 +00:00
item.index = index;
item.url = `${data.url}${encodeURIComponent(item.name)}`;
if (item.isDir) {
item.url += "/";
}
return item;
});
2021-03-12 15:15:56 +00:00
}
2022-05-04 12:11:36 +00:00
return data;
2021-03-03 17:46:37 +00:00
}
2024-04-01 17:18:22 +02:00
export function download(
format: DownloadFormat,
hash: string,
token: string,
...files: string[]
) {
2021-03-21 12:51:58 +01:00
let url = `${baseURL}/api/public/dl/${hash}`;
2021-03-03 17:46:37 +00:00
if (files.length === 1) {
url += files[0] + "?";
2021-03-03 17:46:37 +00:00
} else {
2021-03-21 12:51:58 +01:00
let arg = "";
2021-03-03 17:46:37 +00:00
2024-04-01 17:18:22 +02:00
for (const file of files) {
arg += file + ",";
2021-03-03 17:46:37 +00:00
}
2021-03-21 12:51:58 +01: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 12:51:58 +01:00
url += `algo=${format}&`;
2021-03-03 17:46:37 +00:00
}
if (token) {
2021-03-21 12:51:58 +01:00
url += `token=${token}&`;
2021-03-03 17:46:37 +00:00
}
2021-03-21 12:51:58 +01:00
window.open(url);
}
2022-05-02 13:47:22 +00:00
2024-04-01 17:18:22 +02:00
export function getDownloadURL(res: Resource, inline = false) {
2022-05-02 13:47:22 +00:00
const params = {
...(inline && { inline: "true" }),
2024-04-01 17:18:22 +02:00
...(res.token && { token: res.token }),
2022-05-02 13:47:22 +00:00
};
return createURL("api/public/dl/" + res.hash + res.path, params);
2022-05-02 13:47:22 +00:00
}