Skip to content

Commit

Permalink
feat: buffer filename
Browse files Browse the repository at this point in the history
  • Loading branch information
elrrrrrrr committed May 7, 2024
1 parent bc21ec3 commit be4de7c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ export class HttpClient extends EventEmitter {
requestOptions.method = 'POST';
}
const formData = new FormData();
const uploadFiles: [string, string | Readable | Buffer][] = [];
const uploadFiles: [string, string | Readable | Buffer, string?][] = [];
if (Array.isArray(args.files)) {
for (const [ index, file ] of args.files.entries()) {
const field = index === 0 ? 'file' : `file${index}`;
Expand All @@ -452,7 +452,8 @@ export class HttpClient extends EventEmitter {
uploadFiles.push([ 'file', args.files ]);
} else if (typeof args.files === 'object') {
for (const field in args.files) {
uploadFiles.push([ field, args.files[field] ]);
// set custom fileName
uploadFiles.push([ field, args.files[field], field ]);
}
}
// set normal fields first
Expand All @@ -461,7 +462,7 @@ export class HttpClient extends EventEmitter {
formData.append(field, args.data[field]);
}
}
for (const [ index, [ field, file ]] of uploadFiles.entries()) {
for (const [ index, [ field, file, customFileName ]] of uploadFiles.entries()) {
if (typeof file === 'string') {
// FIXME: support non-ascii filename
// const fileName = encodeURIComponent(basename(file));
Expand All @@ -470,7 +471,7 @@ export class HttpClient extends EventEmitter {
const fileReadable = createReadStream(file);
formData.append(field, new BlobFromStream(fileReadable, mime.lookup(fileName) || ''), fileName);
} else if (Buffer.isBuffer(file)) {
formData.append(field, new Blob([ file ]), `bufferfile${index}`);
formData.append(field, new Blob([ file ]), customFileName || `bufferfile${index}`);
} else if (file instanceof Readable || isReadable(file as any)) {
const fileName = getFileName(file) || `streamfile${index}`;
formData.append(field, new BlobFromStream(file, mime.lookup(fileName) || ''), fileName);
Expand Down
33 changes: 32 additions & 1 deletion test/options.files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe('options.files.test.ts', () => {
// console.log(response.data);
assert.equal(response.data.method, 'POST');
assert.match(response.data.headers['content-type'], /^multipart\/form-data;/);
assert.equal(response.data.files.hello.filename, 'bufferfile0');
assert.equal(response.data.files.hello.filename, 'hello');
assert.equal(response.data.files.hello.mimeType, 'application/octet-stream');
assert.equal(response.data.files.hello.encoding, '7bit');
assert.equal(response.data.files.hello.size, 11);
Expand Down Expand Up @@ -268,4 +268,35 @@ describe('options.files.test.ts', () => {
assert.equal(response.data.form.hello, 'hello world,😄😓');
assert.equal(response.data.form.foo, 'bar');
});

it('should support custom fileName when use files:object', async () => {
const stat = await fs.stat(__filename);
const rawData = JSON.stringify({ a: 1 });
const response = await urllib.request(`${_url}multipart`, {
files: {
'buffer.js': Buffer.from(rawData),
uploadfile: __filename,
},
data: {
hello: 'hello world,😄😓',
foo: 'bar',
},
dataType: 'json',
});
assert.equal(response.status, 200);
// console.log(response.data);
assert.equal(response.data.method, 'POST');
assert.match(response.data.headers['content-type'], /^multipart\/form-data;/);

assert.equal(response.data.files.uploadfile.filename, 'options.files.test.ts');
assert.equal(response.data.files.uploadfile.mimeType, 'video/mp2t');
assert.equal(response.data.files.uploadfile.size, stat.size);

assert.equal(response.data.files['buffer.js'].filename, 'buffer.js');
assert.equal(response.data.files['buffer.js'].mimeType, 'application/octet-stream');
assert.equal(response.data.files['buffer.js'].size, rawData.length);

assert.equal(response.data.form.hello, 'hello world,😄😓');
assert.equal(response.data.form.foo, 'bar');
});
});

0 comments on commit be4de7c

Please sign in to comment.