Skip to content

Commit b15dc31

Browse files
authored
Merge pull request #406 from AtkinsSJ/fs-upload-data-type-error
Explicitly reject invalid types for puter.fs.upload() and .write() payloads
2 parents 1f6a209 + 03fe3b6 commit b15dc31

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

packages/puter-js/src/modules/FileSystem/operations/upload.js

+18-25
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@ const upload = async function(items, dirPath, options = {}){
1515
}
1616
}
1717

18+
const error = (e) => {
19+
// if error callback is provided, call it
20+
if(options.error && typeof options.error === 'function')
21+
options.error(e);
22+
return reject(e);
23+
};
24+
1825
// xhr object to be used for the upload
1926
let xhr = new XMLHttpRequest();
2027

2128
// Can not write to root
22-
if(dirPath === '/'){
23-
// if error callback is provided, call it
24-
if(options.error && typeof options.error === 'function')
25-
options.error('Can not upload to root directory.');
26-
return reject('Can not upload to root directory.');
27-
}
29+
if(dirPath === '/')
30+
return error('Can not upload to root directory.');
2831

2932
// If dirPath is not provided or it's not starting with a slash, it means it's a relative path
3033
// in that case, we need to prepend the app's root directory to it
@@ -119,7 +122,11 @@ const upload = async function(items, dirPath, options = {}){
119122
entries[i].filepath = entries[i].name;
120123
entries[i].fullPath = entries[i].name;
121124
}
122-
}
125+
}
126+
// Anything else is invalid
127+
else {
128+
return error({ code: 'field_invalid', message: 'upload() items parameter is an invalid type' });
129+
}
123130

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

146153
// Continue only if there are actually any files/directories to upload
147154
if(dirs.length === 0 && files.length === 0){
148-
if(options.error && typeof options.error === 'function'){
149-
options.error({code: 'EMPTY_UPLOAD', message: 'No files or directories to upload.'});
150-
}
151-
return reject({code: 'EMPTY_UPLOAD', message: 'No files or directories to upload.'});
155+
return error({code: 'EMPTY_UPLOAD', message: 'No files or directories to upload.'});
152156
}
153157

154158
// Check storage capacity.
@@ -163,10 +167,7 @@ const upload = async function(items, dirPath, options = {}){
163167
try{
164168
storage = await this.space();
165169
if(storage.capacity - storage.used < total_size){
166-
if(options.error && typeof options.error === 'function'){
167-
options.error({code: 'NOT_ENOUGH_SPACE', message: 'Not enough storage space available.'});
168-
}
169-
return reject({code: 'NOT_ENOUGH_SPACE', message: 'Not enough storage space available.'});
170+
return error({code: 'NOT_ENOUGH_SPACE', message: 'Not enough storage space available.'});
170171
}
171172
}catch(e){
172173
// Ignored
@@ -368,18 +369,10 @@ const upload = async function(items, dirPath, options = {}){
368369
break;
369370
}
370371
}
371-
// if error callback is provided, call it
372-
if(options.error && typeof options.error === 'function'){
373-
options.error(failed_operation);
374-
}
375-
return reject(failed_operation);
372+
return error(failed_operation);
376373
}
377374

378-
// if error callback is provided, call it
379-
if(options.error && typeof options.error === 'function'){
380-
options.error(resp);
381-
}
382-
return reject(resp);
375+
return error(resp);
383376
}
384377
// Success
385378
else{

packages/puter-js/src/modules/FileSystem/operations/write.js

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ const write = async function (targetPath, data, options = {}) {
4646
if(!data)
4747
data = new File([data ?? ''], filename);
4848

49+
// data should be a File now. If it's not, it's an unsupported type
50+
if (!(data instanceof File)) {
51+
throw new Error({ code: 'field_invalid', message: 'write() data parameter is an invalid type' });
52+
}
53+
4954
// perform upload
5055
return this.upload(data, parent, options);
5156
}

0 commit comments

Comments
 (0)