Skip to content

Commit

Permalink
Merge pull request #406 from AtkinsSJ/fs-upload-data-type-error
Browse files Browse the repository at this point in the history
Explicitly reject invalid types for puter.fs.upload() and .write() payloads
  • Loading branch information
KernelDeimos authored May 22, 2024
2 parents 1f6a209 + 03fe3b6 commit b15dc31
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
43 changes: 18 additions & 25 deletions packages/puter-js/src/modules/FileSystem/operations/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
let xhr = new XMLHttpRequest();

// Can not write to root
if(dirPath === '/'){
// if error callback is provided, call it
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 === '/')
return error('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
// in that case, we need to prepend the app's root directory to it
Expand Down Expand Up @@ -119,7 +122,11 @@ const upload = async function(items, dirPath, options = {}){
entries[i].filepath = entries[i].name;
entries[i].fullPath = entries[i].name;
}
}
}
// Anything else is invalid
else {
return error({ code: 'field_invalid', message: 'upload() items parameter is an invalid type' });
}

// Will hold directories and files to be uploaded
let dirs = [];
Expand All @@ -145,10 +152,7 @@ const upload = async function(items, dirPath, options = {}){

// Continue only if there are actually any files/directories to upload
if(dirs.length === 0 && files.length === 0){
if(options.error && typeof options.error === 'function'){
options.error({code: 'EMPTY_UPLOAD', message: 'No files or directories to upload.'});
}
return reject({code: 'EMPTY_UPLOAD', message: 'No files or directories to upload.'});
return error({code: 'EMPTY_UPLOAD', message: 'No files or directories to upload.'});
}

// Check storage capacity.
Expand All @@ -163,10 +167,7 @@ const upload = async function(items, dirPath, options = {}){
try{
storage = await this.space();
if(storage.capacity - storage.used < total_size){
if(options.error && typeof options.error === 'function'){
options.error({code: 'NOT_ENOUGH_SPACE', message: 'Not enough storage space available.'});
}
return reject({code: 'NOT_ENOUGH_SPACE', message: 'Not enough storage space available.'});
return error({code: 'NOT_ENOUGH_SPACE', message: 'Not enough storage space available.'});
}
}catch(e){
// Ignored
Expand Down Expand Up @@ -368,18 +369,10 @@ const upload = async function(items, dirPath, options = {}){
break;
}
}
// if error callback is provided, call it
if(options.error && typeof options.error === 'function'){
options.error(failed_operation);
}
return reject(failed_operation);
return error(failed_operation);
}

// if error callback is provided, call it
if(options.error && typeof options.error === 'function'){
options.error(resp);
}
return reject(resp);
return error(resp);
}
// Success
else{
Expand Down
5 changes: 5 additions & 0 deletions packages/puter-js/src/modules/FileSystem/operations/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ const write = async function (targetPath, data, options = {}) {
if(!data)
data = new File([data ?? ''], filename);

// data should be a File now. If it's not, it's an unsupported type
if (!(data instanceof File)) {
throw new Error({ code: 'field_invalid', message: 'write() data parameter is an invalid type' });
}

// perform upload
return this.upload(data, parent, options);
}
Expand Down

0 comments on commit b15dc31

Please sign in to comment.