filebrowser/frontend/src/api/pub.js

61 lines
1.2 KiB
JavaScript
Raw Normal View History

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