Merge pull request #188 from cfjedimaster/master

look for a nameConflict option and use a uuid if value is makeUnique
This commit is contained in:
Raymond Feng 2017-03-01 15:04:30 -08:00 committed by GitHub
commit 4d4e530955
3 changed files with 13 additions and 1 deletions

View File

@ -10,6 +10,7 @@ var g = require('strong-globalize')();
var IncomingForm = require('formidable'); var IncomingForm = require('formidable');
var StringDecoder = require('string_decoder').StringDecoder; var StringDecoder = require('string_decoder').StringDecoder;
var path = require('path'); var path = require('path');
var uuid = require('uuid');
var defaultOptions = { var defaultOptions = {
maxFileSize: 10 * 1024 * 1024, // 10 MB maxFileSize: 10 * 1024 * 1024, // 10 MB
@ -84,6 +85,9 @@ exports.upload = function(provider, req, res, options, cb) {
if ('function' === typeof options.getFilename) { if ('function' === typeof options.getFilename) {
file.originalFilename = file.name; file.originalFilename = file.name;
file.name = options.getFilename(file, req, res); file.name = options.getFilename(file, req, res);
} else if (options.nameConflict === 'makeUnique') {
file.originalFilename = file.name;
file.name = uuid.v4() + path.extname(file.name);
} }
// Get allowed mime types // Get allowed mime types
@ -135,6 +139,7 @@ exports.upload = function(provider, req, res, options, cb) {
if (file.acl) { if (file.acl) {
uploadParams.acl = file.acl; uploadParams.acl = file.acl;
} }
var writer = provider.upload(uploadParams); var writer = provider.upload(uploadParams);
writer.on('error', function(err) { writer.on('error', function(err) {

View File

@ -46,6 +46,9 @@ function StorageService(options) {
if (options.maxFileSize) { if (options.maxFileSize) {
this.maxFileSize = options.maxFileSize; this.maxFileSize = options.maxFileSize;
} }
if (options.nameConflict) {
this.nameConflict = options.nameConflict;
}
} }
function map(obj) { function map(obj) {
@ -241,6 +244,9 @@ StorageService.prototype.upload = function(req, res, options, cb) {
if (this.maxFileSize && !options.maxFileSize) { if (this.maxFileSize && !options.maxFileSize) {
options.maxFileSize = this.maxFileSize; options.maxFileSize = this.maxFileSize;
} }
if (this.nameConflict && !options.nameConflict) {
options.nameConflict = this.nameConflict;
}
return handler.upload(this.client, req, res, options, cb); return handler.upload(this.client, req, res, options, cb);
}; };

View File

@ -15,7 +15,8 @@
"async": "^0.9.0", "async": "^0.9.0",
"formidable": "^1.0.16", "formidable": "^1.0.16",
"pkgcloud": "^1.1.0", "pkgcloud": "^1.1.0",
"strong-globalize": "^2.6.2" "strong-globalize": "^2.6.2",
"uuid":"^3.0.1"
}, },
"devDependencies": { "devDependencies": {
"eslint": "^2.13.1", "eslint": "^2.13.1",