2023-05-17 15:34:56 +02:00
|
|
|
import { fs_path_url } from './FilesystemUtil.js'
|
2023-05-11 19:07:29 +02:00
|
|
|
|
2023-05-11 15:01:29 +02:00
|
|
|
export const fs_mkdir = async (bucket, path, opts = null) => {
|
|
|
|
const form = new FormData()
|
|
|
|
form.append("action", "mkdir")
|
|
|
|
|
|
|
|
if (opts && opts.mode) {
|
|
|
|
form.append("mode", opts.mode)
|
|
|
|
}
|
|
|
|
|
2023-05-17 15:34:56 +02:00
|
|
|
const resp = await fetch(fs_path_url(bucket, path), { method: "POST", body: form });
|
2023-05-11 15:01:29 +02:00
|
|
|
if (resp.status >= 400) {
|
|
|
|
throw new Error(await resp.text())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const fs_get_node = async (bucket, path) => {
|
2023-05-17 15:34:56 +02:00
|
|
|
const resp = await fetch(fs_path_url(bucket, path) + "?stat");
|
2023-05-11 15:01:29 +02:00
|
|
|
if (resp.status >= 400) {
|
2023-05-11 19:07:29 +02:00
|
|
|
throw await resp.text()
|
2023-05-11 15:01:29 +02:00
|
|
|
}
|
|
|
|
return resp.json()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updates a node's parameters. Available options are:
|
|
|
|
// - created, Date object
|
|
|
|
// - modified, Date object
|
|
|
|
// - mode, file mode formatted as octal string
|
|
|
|
// - shared, boolean. If true the node will receive a public ID
|
|
|
|
//
|
|
|
|
// Returns the modified filesystem node object
|
|
|
|
export const fs_update = async (bucket, path, opts) => {
|
|
|
|
const form = new FormData()
|
|
|
|
form.append("action", "update")
|
|
|
|
|
2023-05-11 19:07:29 +02:00
|
|
|
if (opts.created !== undefined) {
|
2023-05-11 15:01:29 +02:00
|
|
|
form.append("created", opts.created.toISOString())
|
|
|
|
}
|
2023-05-11 19:07:29 +02:00
|
|
|
if (opts.modified !== undefined) {
|
2023-05-11 15:01:29 +02:00
|
|
|
form.append("modified", opts.modified.toISOString())
|
|
|
|
}
|
2023-05-11 19:07:29 +02:00
|
|
|
if (opts.mode !== undefined) {
|
2023-05-11 15:01:29 +02:00
|
|
|
form.append("mode", opts.mode)
|
|
|
|
}
|
2023-05-11 19:07:29 +02:00
|
|
|
if (opts.shared !== undefined) {
|
2023-05-11 15:01:29 +02:00
|
|
|
form.append("shared", opts.shared)
|
|
|
|
}
|
2023-05-11 19:07:29 +02:00
|
|
|
if (opts.read_password !== undefined) {
|
|
|
|
form.append("read_password", opts.read_password)
|
|
|
|
}
|
|
|
|
if (opts.write_password !== undefined) {
|
|
|
|
form.append("write_password", opts.write_password)
|
|
|
|
}
|
2023-05-11 15:01:29 +02:00
|
|
|
|
2023-05-17 15:34:56 +02:00
|
|
|
const resp = await fetch(fs_path_url(bucket, path), { method: "POST", body: form })
|
2023-05-11 15:01:29 +02:00
|
|
|
if (resp.status >= 400) {
|
|
|
|
throw new Error(await resp.text())
|
|
|
|
}
|
|
|
|
return resp.json()
|
|
|
|
}
|
|
|
|
|
|
|
|
export const fs_rename = async (bucket, old_path, new_path) => {
|
|
|
|
const form = new FormData()
|
|
|
|
form.append("action", "rename")
|
|
|
|
form.append("target", new_path)
|
|
|
|
|
2023-05-17 15:34:56 +02:00
|
|
|
const resp = await fetch(fs_path_url(bucket, old_path), { method: "POST", body: form })
|
2023-05-11 15:01:29 +02:00
|
|
|
if (resp.status >= 400) {
|
|
|
|
throw new Error(await resp.text())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const fs_delete = async (bucket, path) => {
|
2023-05-17 15:34:56 +02:00
|
|
|
const resp = await fetch(fs_path_url(bucket, path), { method: "DELETE" });
|
2023-05-11 15:01:29 +02:00
|
|
|
if (resp.status >= 400) {
|
|
|
|
throw new Error(await resp.text())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export const fs_delete_all = async (bucket, path) => {
|
2023-05-17 15:34:56 +02:00
|
|
|
const resp = await fetch(fs_path_url(bucket, path) + "?recursive", { method: "DELETE" });
|
2023-05-11 15:01:29 +02:00
|
|
|
if (resp.status >= 400) {
|
|
|
|
throw new Error(await resp.text())
|
|
|
|
}
|
|
|
|
}
|
2023-05-25 17:06:17 +02:00
|
|
|
|
|
|
|
export const fs_search = async (bucket, path, term, limit = 10) => {
|
|
|
|
const resp = await fetch(
|
|
|
|
fs_path_url(bucket, path) +
|
|
|
|
"?search=" + encodeURIComponent(term) +
|
|
|
|
"&limit=" + limit
|
|
|
|
)
|
|
|
|
if (resp.status >= 400) {
|
|
|
|
throw await resp.text()
|
|
|
|
}
|
|
|
|
return resp.json()
|
|
|
|
}
|
2023-05-25 23:07:17 +02:00
|
|
|
|
|
|
|
export const fs_timeseries = async (bucket, path, start, end, interval = 60) => {
|
|
|
|
const resp = await fetch(
|
|
|
|
fs_path_url(bucket, path) +
|
|
|
|
"?timeseries" +
|
|
|
|
"&start=" + start.toISOString() +
|
|
|
|
"&end=" + end.toISOString() +
|
|
|
|
"&interval=" + interval
|
|
|
|
)
|
|
|
|
if (resp.status >= 400) {
|
|
|
|
throw await resp.text()
|
|
|
|
}
|
|
|
|
return resp.json()
|
|
|
|
}
|