formidable
Advanced tools
Comparing version 2.0.0-canary.20200402.1 to 2.0.0-canary.20200402.2
@@ -26,3 +26,4 @@ ### Unreleased (`canary` & `dev` dist-tags) | ||
* fix: exposing file writable stream errors ([#520](https://github.com/node-formidable/node-formidable/pull/520), [#316](https://github.com/node-formidable/node-formidable/pull/316), [#469](https://github.com/node-formidable/node-formidable/pull/469), [#470](https://github.com/node-formidable/node-formidable/pull/470)) | ||
* feat: custom file (re)naming, thru options.filename ([#591](https://github.com/node-formidable/node-formidable/pull/591), [#84](https://github.com/node-formidable/node-formidable/issues/84), [#86](https://github.com/node-formidable/node-formidable/issues/86), [#94](https://github.com/node-formidable/node-formidable/issues/94), [#154](https://github.com/node-formidable/node-formidable/issues/154), [#158](https://github.com/node-formidable/node-formidable/issues/158), [#488](https://github.com/node-formidable/node-formidable/issues/488), [#595](https://github.com/node-formidable/node-formidable/issues/595)) | ||
* fix: make opts.filename from #591 work with opts.keepExtensions ([#597](https://github.com/node-formidable/node-formidable/pull/597)) | ||
### v1.2.1 (2018-03-20) | ||
@@ -29,0 +30,0 @@ |
{ | ||
"name": "formidable", | ||
"version": "2.0.0-canary.20200402.1", | ||
"version": "2.0.0-canary.20200402.2", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "A node.js module for parsing form data, especially file uploads.", |
@@ -512,3 +512,3 @@ <p align="center"> | ||
form.onPart = function(part) { | ||
form.onPart = function (part) { | ||
// let formidable handle only non-file parts | ||
@@ -515,0 +515,0 @@ if (part.filename === '' || !part.mime) { |
@@ -47,7 +47,2 @@ /* eslint-disable class-methods-use-this */ | ||
this.options.filename = | ||
typeof this.options.filename === 'function' | ||
? this.options.filename.bind(this) | ||
: this._uploadPath.bind(this); | ||
// initialize with null | ||
@@ -65,2 +60,14 @@ [ | ||
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; | ||
@@ -295,3 +302,3 @@ this._fieldsSize = 0; | ||
const file = new File({ | ||
path: this.options.filename(part, this), | ||
path: this._rename(part), | ||
name: part.filename, | ||
@@ -439,13 +446,25 @@ type: part.mime, | ||
); | ||
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 (firstDot === lastDot) { | ||
return extname; | ||
} | ||
return basename.slice(firstDot, lastDot) + extname; | ||
} | ||
_uploadPath(part, fp) { | ||
const name = fp || `${this.uploadDir}${path.sep}${toHexoId()}`; | ||
if (part && this.options.keepExtensions) { | ||
let ext = path.extname(typeof part === 'string' ? part : part.filename); | ||
ext = ext.replace(/(\.[a-z0-9]+).*/i, '$1'); | ||
return `${name}${ext}`; | ||
const filename = typeof part === 'string' ? part : part.filename; | ||
return `${name}${this._getExtension(filename)}`; | ||
} | ||
@@ -452,0 +471,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2087301
2169