Fix errors in directory upload
This commit is contained in:
@@ -13,7 +13,7 @@ import { fs_get_node, fs_mkdirall } from "../FilesystemAPI"
|
||||
import { fs_path_url, fs_split_path } from "../FilesystemUtil"
|
||||
|
||||
// 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
|
||||
if (window.user.subscription.file_size_limit === 0) {
|
||||
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
|
||||
try {
|
||||
await ensure_parent_dir(bucket, path)
|
||||
await ensure_parent_dir(path)
|
||||
} catch (err) {
|
||||
if (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
|
||||
}
|
||||
|
||||
console.log("Uploading file to ", fs_path_url(bucket, path))
|
||||
console.log("Uploading file to ", fs_path_url(path))
|
||||
|
||||
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.upload.addEventListener("progress", evt => {
|
||||
@@ -85,24 +85,48 @@ export const upload_file = async (file, bucket, path, on_progress, on_success, o
|
||||
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
|
||||
|
||||
if (created_dirs.has(parent)) {
|
||||
// We have already checked this directory
|
||||
return
|
||||
}
|
||||
|
||||
console.debug("Checking if parent directory exists", parent)
|
||||
|
||||
try {
|
||||
let node = await fs_get_node(bucket, parent)
|
||||
let node = await fs_get_node(parent)
|
||||
if (node.path[node.base_index].type !== "dir") {
|
||||
throw "Path " + path + " is not a directory"
|
||||
}
|
||||
} catch (err) {
|
||||
if (err.value && err.value === "path_not_found") {
|
||||
// Directory does not exist. Create it
|
||||
await fs_mkdirall(bucket, parent)
|
||||
|
||||
await create_parent_dir(parent)
|
||||
console.debug("Created parent directory", parent)
|
||||
} else {
|
||||
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)
|
||||
}
|
||||
|
@@ -19,7 +19,6 @@ let error_message = ""
|
||||
export const start = () => {
|
||||
upload_file(
|
||||
job.file,
|
||||
job.bucket,
|
||||
job.path,
|
||||
(prog_loaded, prog_total) => {
|
||||
loaded = prog_loaded
|
||||
@@ -30,7 +29,7 @@ export const start = () => {
|
||||
dispatch("finished")
|
||||
},
|
||||
(code, message) => {
|
||||
console.log("error", code, message)
|
||||
console.log("Upload error", code, message)
|
||||
error_code = code
|
||||
error_message = message
|
||||
job.status = "error"
|
||||
|
@@ -56,7 +56,6 @@ export const upload_file = async file => {
|
||||
upload_queue.push({
|
||||
task_id: task_id_counter,
|
||||
file: file,
|
||||
bucket: fs_state.root.id,
|
||||
path: path,
|
||||
component: null,
|
||||
status: "queued",
|
||||
|
Reference in New Issue
Block a user