mirror of
https://github.com/HeyPuter/puter
synced 2024-11-15 06:15:47 +00:00
refactor: Reduce boilerplate for returning errors from puter.fs.upload()
This commit is contained in:
parent
c55b1e4680
commit
a9c89cef19
@ -15,16 +15,19 @@ const upload = async function(items, dirPath, options = {}){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const error = (e) => {
|
||||||
|
// if error callback is provided, call it
|
||||||
|
if(options.error && typeof options.error === 'function')
|
||||||
|
options.error(e);
|
||||||
|
return reject(e);
|
||||||
|
};
|
||||||
|
|
||||||
// xhr object to be used for the upload
|
// xhr object to be used for the upload
|
||||||
let xhr = new XMLHttpRequest();
|
let xhr = new XMLHttpRequest();
|
||||||
|
|
||||||
// Can not write to root
|
// Can not write to root
|
||||||
if(dirPath === '/'){
|
if(dirPath === '/')
|
||||||
// if error callback is provided, call it
|
return error('Can not upload to root directory.');
|
||||||
if(options.error && typeof options.error === 'function')
|
|
||||||
options.error('Can not upload to root directory.');
|
|
||||||
return reject('Can not upload to root directory.');
|
|
||||||
}
|
|
||||||
|
|
||||||
// If dirPath is not provided or it's not starting with a slash, it means it's a relative path
|
// If dirPath is not provided or it's not starting with a slash, it means it's a relative path
|
||||||
// in that case, we need to prepend the app's root directory to it
|
// in that case, we need to prepend the app's root directory to it
|
||||||
@ -145,10 +148,7 @@ const upload = async function(items, dirPath, options = {}){
|
|||||||
|
|
||||||
// Continue only if there are actually any files/directories to upload
|
// Continue only if there are actually any files/directories to upload
|
||||||
if(dirs.length === 0 && files.length === 0){
|
if(dirs.length === 0 && files.length === 0){
|
||||||
if(options.error && typeof options.error === 'function'){
|
return error({code: 'EMPTY_UPLOAD', message: 'No files or directories to upload.'});
|
||||||
options.error({code: 'EMPTY_UPLOAD', message: 'No files or directories to upload.'});
|
|
||||||
}
|
|
||||||
return reject({code: 'EMPTY_UPLOAD', message: 'No files or directories to upload.'});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check storage capacity.
|
// Check storage capacity.
|
||||||
@ -163,10 +163,7 @@ const upload = async function(items, dirPath, options = {}){
|
|||||||
try{
|
try{
|
||||||
storage = await this.space();
|
storage = await this.space();
|
||||||
if(storage.capacity - storage.used < total_size){
|
if(storage.capacity - storage.used < total_size){
|
||||||
if(options.error && typeof options.error === 'function'){
|
return error({code: 'NOT_ENOUGH_SPACE', message: 'Not enough storage space available.'});
|
||||||
options.error({code: 'NOT_ENOUGH_SPACE', message: 'Not enough storage space available.'});
|
|
||||||
}
|
|
||||||
return reject({code: 'NOT_ENOUGH_SPACE', message: 'Not enough storage space available.'});
|
|
||||||
}
|
}
|
||||||
}catch(e){
|
}catch(e){
|
||||||
// Ignored
|
// Ignored
|
||||||
@ -368,18 +365,10 @@ const upload = async function(items, dirPath, options = {}){
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// if error callback is provided, call it
|
return error(failed_operation);
|
||||||
if(options.error && typeof options.error === 'function'){
|
|
||||||
options.error(failed_operation);
|
|
||||||
}
|
|
||||||
return reject(failed_operation);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// if error callback is provided, call it
|
return error(resp);
|
||||||
if(options.error && typeof options.error === 'function'){
|
|
||||||
options.error(resp);
|
|
||||||
}
|
|
||||||
return reject(resp);
|
|
||||||
}
|
}
|
||||||
// Success
|
// Success
|
||||||
else{
|
else{
|
||||||
|
Loading…
Reference in New Issue
Block a user