Fix errors in directory upload

This commit is contained in:
2023-07-06 18:12:42 +02:00
parent c617997017
commit 150eba5581
3 changed files with 33 additions and 11 deletions

View File

@@ -13,7 +13,7 @@ import { fs_get_node, fs_mkdirall } from "../FilesystemAPI"
import { fs_path_url, fs_split_path } from "../FilesystemUtil" import { fs_path_url, fs_split_path } from "../FilesystemUtil"
// code and an error message // code and an error message
export const upload_file = async (file, bucket, path, on_progress, on_success, on_error) => { export const upload_file = async (file, path, on_progress, on_success, on_error) => {
// Check the file size limit. For free accounts it's 20 GB // Check the file size limit. For free accounts it's 20 GB
if (window.user.subscription.file_size_limit === 0) { if (window.user.subscription.file_size_limit === 0) {
window.user.subscription.file_size_limit = 20e9 window.user.subscription.file_size_limit = 20e9
@@ -29,7 +29,7 @@ export const upload_file = async (file, bucket, path, on_progress, on_success, o
// Check if the parent directory exists // Check if the parent directory exists
try { try {
await ensure_parent_dir(bucket, path) await ensure_parent_dir(path)
} catch (err) { } catch (err) {
if (err.value && err.message) { if (err.value && err.message) {
on_error(err.value, err.message) on_error(err.value, err.message)
@@ -39,10 +39,10 @@ export const upload_file = async (file, bucket, path, on_progress, on_success, o
return return
} }
console.log("Uploading file to ", fs_path_url(bucket, path)) console.log("Uploading file to ", fs_path_url(path))
let xhr = new XMLHttpRequest(); let xhr = new XMLHttpRequest();
xhr.open("PUT", fs_path_url(bucket, path), true); xhr.open("PUT", fs_path_url(path), true);
xhr.timeout = 86400000; // 24 hours, to account for slow connections xhr.timeout = 86400000; // 24 hours, to account for slow connections
xhr.upload.addEventListener("progress", evt => { xhr.upload.addEventListener("progress", evt => {
@@ -85,24 +85,48 @@ export const upload_file = async (file, bucket, path, on_progress, on_success, o
xhr.send(file); xhr.send(file);
} }
const ensure_parent_dir = async (bucket, path) => { let created_dirs = new Map()
const ensure_parent_dir = async path => {
let parent = fs_split_path(path).parent let parent = fs_split_path(path).parent
if (created_dirs.has(parent)) {
// We have already checked this directory
return
}
console.debug("Checking if parent directory exists", parent) console.debug("Checking if parent directory exists", parent)
try { try {
let node = await fs_get_node(bucket, parent) let node = await fs_get_node(parent)
if (node.path[node.base_index].type !== "dir") { if (node.path[node.base_index].type !== "dir") {
throw "Path " + path + " is not a directory" throw "Path " + path + " is not a directory"
} }
} catch (err) { } catch (err) {
if (err.value && err.value === "path_not_found") { if (err.value && err.value === "path_not_found") {
// Directory does not exist. Create it // Directory does not exist. Create it
await fs_mkdirall(bucket, parent) await create_parent_dir(parent)
console.debug("Created parent directory", parent) console.debug("Created parent directory", parent)
} else { } else {
throw err throw err
} }
} }
} }
const create_parent_dir = async path => {
try {
await fs_mkdirall(path)
} catch (err) {
// This function can run concurrently, so it's possible the directory
// was already created before this runs. In that case we just return
if (err.value === "node_already_exists") {
console.debug("Parent dir", path, "already existed during creation")
return
} else {
throw err
}
}
// Mark the directory as created.
created_dirs.set(path, null)
}

View File

@@ -19,7 +19,6 @@ let error_message = ""
export const start = () => { export const start = () => {
upload_file( upload_file(
job.file, job.file,
job.bucket,
job.path, job.path,
(prog_loaded, prog_total) => { (prog_loaded, prog_total) => {
loaded = prog_loaded loaded = prog_loaded
@@ -30,7 +29,7 @@ export const start = () => {
dispatch("finished") dispatch("finished")
}, },
(code, message) => { (code, message) => {
console.log("error", code, message) console.log("Upload error", code, message)
error_code = code error_code = code
error_message = message error_message = message
job.status = "error" job.status = "error"

View File

@@ -56,7 +56,6 @@ export const upload_file = async file => {
upload_queue.push({ upload_queue.push({
task_id: task_id_counter, task_id: task_id_counter,
file: file, file: file,
bucket: fs_state.root.id,
path: path, path: path,
component: null, component: null,
status: "queued", status: "queued",