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-27 15:50:44 +02:00
|
|
|
export const fs_check_response = async resp => {
|
|
|
|
let text = await resp.text()
|
|
|
|
if (resp.status >= 400) {
|
|
|
|
let error
|
|
|
|
try {
|
|
|
|
error = JSON.parse(text)
|
|
|
|
} catch (err) {
|
|
|
|
error = text
|
|
|
|
}
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return JSON.parse(text)
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:51:10 +02:00
|
|
|
export const fs_mkdir = async (path, opts = null) => {
|
2023-05-11 15:01:29 +02:00
|
|
|
const form = new FormData()
|
|
|
|
form.append("action", "mkdir")
|
|
|
|
|
|
|
|
if (opts && opts.mode) {
|
|
|
|
form.append("mode", opts.mode)
|
|
|
|
}
|
|
|
|
|
2023-05-27 15:50:44 +02:00
|
|
|
return await fs_check_response(
|
2023-05-30 15:51:10 +02:00
|
|
|
await fetch(fs_path_url(path), { method: "POST", body: form })
|
2023-05-27 15:50:44 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:51:10 +02:00
|
|
|
export const fs_mkdirall = async (path, opts = null) => {
|
2023-05-27 15:50:44 +02:00
|
|
|
const form = new FormData()
|
|
|
|
form.append("action", "mkdirall")
|
|
|
|
|
|
|
|
if (opts && opts.mode) {
|
|
|
|
form.append("mode", opts.mode)
|
2023-05-11 15:01:29 +02:00
|
|
|
}
|
2023-05-27 15:50:44 +02:00
|
|
|
|
|
|
|
return await fs_check_response(
|
2023-05-30 15:51:10 +02:00
|
|
|
await fetch(fs_path_url(path), { method: "POST", body: form })
|
2023-05-27 15:50:44 +02:00
|
|
|
)
|
2023-05-11 15:01:29 +02:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:51:10 +02:00
|
|
|
export const fs_get_node = async path => {
|
2023-05-27 15:50:44 +02:00
|
|
|
return await fs_check_response(
|
2023-05-30 15:51:10 +02:00
|
|
|
await fetch(fs_path_url(path) + "?stat")
|
2023-05-27 15:50:44 +02:00
|
|
|
)
|
2023-05-11 15:01:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2023-05-30 15:51:10 +02:00
|
|
|
export const fs_update = async (path, opts) => {
|
2023-05-11 15:01:29 +02:00
|
|
|
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-27 15:50:44 +02:00
|
|
|
return await fs_check_response(
|
2023-05-30 15:51:10 +02:00
|
|
|
await fetch(fs_path_url(path), { method: "POST", body: form })
|
2023-05-27 15:50:44 +02:00
|
|
|
)
|
2023-05-11 15:01:29 +02:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:51:10 +02:00
|
|
|
export const fs_rename = async (old_path, new_path) => {
|
2023-05-11 15:01:29 +02:00
|
|
|
const form = new FormData()
|
|
|
|
form.append("action", "rename")
|
|
|
|
form.append("target", new_path)
|
|
|
|
|
2023-05-27 15:50:44 +02:00
|
|
|
return await fs_check_response(
|
2023-05-30 15:51:10 +02:00
|
|
|
await fetch(fs_path_url(old_path), { method: "POST", body: form })
|
2023-05-27 15:50:44 +02:00
|
|
|
)
|
2023-05-11 15:01:29 +02:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:51:10 +02:00
|
|
|
export const fs_delete = async path => {
|
2023-05-27 15:50:44 +02:00
|
|
|
return await fs_check_response(
|
2023-05-30 15:51:10 +02:00
|
|
|
await fetch(fs_path_url(path), { method: "DELETE" })
|
2023-05-27 15:50:44 +02:00
|
|
|
)
|
2023-05-11 15:01:29 +02:00
|
|
|
}
|
2023-05-30 15:51:10 +02:00
|
|
|
export const fs_delete_all = async path => {
|
2023-05-27 15:50:44 +02:00
|
|
|
return await fs_check_response(
|
2023-05-30 15:51:10 +02:00
|
|
|
await fetch(fs_path_url(path) + "?recursive", { method: "DELETE" })
|
2023-05-27 15:50:44 +02:00
|
|
|
)
|
2023-05-11 15:01:29 +02:00
|
|
|
}
|
2023-05-25 17:06:17 +02:00
|
|
|
|
2023-05-30 15:51:10 +02:00
|
|
|
export const fs_search = async (path, term, limit = 10) => {
|
2023-05-27 15:50:44 +02:00
|
|
|
return await fs_check_response(
|
|
|
|
await fetch(
|
2023-05-30 15:51:10 +02:00
|
|
|
fs_path_url(path) +
|
2023-05-27 15:50:44 +02:00
|
|
|
"?search=" + encodeURIComponent(term) +
|
|
|
|
"&limit=" + limit
|
|
|
|
)
|
2023-05-25 17:06:17 +02:00
|
|
|
)
|
|
|
|
}
|
2023-05-25 23:07:17 +02:00
|
|
|
|
2023-05-30 15:51:10 +02:00
|
|
|
export const fs_timeseries = async (path, start, end, interval = 60) => {
|
2023-05-27 15:50:44 +02:00
|
|
|
return await fs_check_response(
|
|
|
|
await fetch(
|
2023-05-30 15:51:10 +02:00
|
|
|
fs_path_url(path) +
|
2023-05-27 15:50:44 +02:00
|
|
|
"?timeseries" +
|
|
|
|
"&start=" + start.toISOString() +
|
|
|
|
"&end=" + end.toISOString() +
|
|
|
|
"&interval=" + interval
|
|
|
|
)
|
2023-05-25 23:07:17 +02:00
|
|
|
)
|
|
|
|
}
|
2024-02-13 13:13:44 +01:00
|
|
|
|
|
|
|
export const fs_import = async (parent_dir_path = "", filelist = []) => {
|
|
|
|
const form = new FormData()
|
|
|
|
form.append("action", "import")
|
|
|
|
form.append("files", JSON.stringify(filelist))
|
|
|
|
|
|
|
|
return await fs_check_response(
|
|
|
|
await fetch(fs_path_url(parent_dir_path), { method: "POST", body: form })
|
|
|
|
)
|
|
|
|
}
|