Skip to content

Commit

Permalink
fix: make opts.filename from #591 work with opts.keepExtensions (#597)
Browse files Browse the repository at this point in the history
* fix: make opts.filename from #591 work with opts.keepExtensions
* chore: tweaks for failing tests
* fix: extension problems

Signed-off-by: Charlike Mike Reagent <opensource@tunnckocore.com>
  • Loading branch information
tunnckoCore committed Apr 2, 2020
1 parent 37877b4 commit d33bd0c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
11 changes: 10 additions & 1 deletion examples/with-koa2.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ app.on('error', (err) => {

app.use(async (ctx, next) => {
if (ctx.url === '/api/upload' && ctx.method.toLowerCase() === 'post') {
const form = formidable({ multiples: true });
let i = 0;
const form = formidable({
multiples: true,
keepExtensions: true,
// must return absolute path
filename: (part, $self) => {
i += 1;
return `${$self.uploadDir}/sasasa${i}`;
},
});

// not very elegant, but that's for now if you don't want touse `koa-better-body`
// or other middlewares.
Expand Down
43 changes: 31 additions & 12 deletions src/Formidable.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ class IncomingForm extends EventEmitter {
this.uploaddir = dir;
this.uploadDir = dir;

this.options.filename =
typeof this.options.filename === 'function'
? this.options.filename.bind(this)
: this._uploadPath.bind(this);

// initialize with null
[
'error',
Expand All @@ -61,6 +56,18 @@ class IncomingForm extends EventEmitter {
this[key] = null;
});

const hasRename = typeof this.options.filename === 'function';

if (this.options.keepExtensions === true && hasRename) {
this._rename = (part) => {
const resultFilepath = this.options.filename.call(this, part, this);

return this._uploadPath(part, resultFilepath);
};
} else {
this._rename = (part) => this._uploadPath(part);
}

this._flushing = 0;
this._fieldsSize = 0;
this._fileSize = 0;
Expand Down Expand Up @@ -290,7 +297,7 @@ class IncomingForm extends EventEmitter {
this._flushing += 1;

const file = new File({
path: this.options.filename(part, this),
path: this._rename(part),
name: part.filename,
type: part.mime,
hash: this.options.hash,
Expand Down Expand Up @@ -433,17 +440,29 @@ class IncomingForm extends EventEmitter {
filename = filename.replace(/&#([\d]{4});/g, (_, code) =>
String.fromCharCode(code),
);

return filename;
}

_uploadPath(part) {
const name = `${this.uploadDir}${path.sep}${toHexoId()}`;
_getExtension(str) {
const basename = path.basename(str);
const firstDot = basename.indexOf('.');
const lastDot = basename.lastIndexOf('.');
const extname = path.extname(basename).replace(/(\.[a-z0-9]+).*/i, '$1');

if (part && this.options.keepExtensions) {
let ext = path.extname(typeof part === 'string' ? part : part.filename);
ext = ext.replace(/(\.[a-z0-9]+).*/i, '$1');
if (firstDot === lastDot) {
return extname;
}

return basename.slice(firstDot, lastDot) + extname;

This comment has been minimized.

Copy link
@GrosSacASac

GrosSacASac May 19, 2022

Contributor

you skipped the safety check of felix from line 451 of the middle part you added back

}

_uploadPath(part, fp) {
const name = fp || `${this.uploadDir}${path.sep}${toHexoId()}`;

return `${name}${ext}`;
if (part && this.options.keepExtensions) {
const filename = typeof part === 'string' ? part : part.filename;
return `${name}${this._getExtension(filename)}`;
}

return name;
Expand Down

0 comments on commit d33bd0c

Please sign in to comment.