Fix errors in directory upload
This commit is contained in:
1 parent
c617997017
commit
150eba5581
3 files changed
+33
-11
No files matched your search
@@ -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)
|
||||
}
|
||||
Reference in new issue
Block a user