Support nameConflict and makeUnique options

This commit is contained in:
Raymond Camden 2017-02-01 13:09:41 -06:00
parent 080f18a985
commit 907f83ac3a
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 StringDecoder = require('string_decoder').StringDecoder;
var path = require('path');
var uuid = require('uuid');
var defaultOptions = {
maxFileSize: 10 * 1024 * 1024, // 10 MB
@ -84,6 +85,9 @@ exports.upload = function(provider, req, res, options, cb) {
if ('function' === typeof options.getFilename) {
file.originalFilename = file.name;
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
@ -135,6 +139,7 @@ exports.upload = function(provider, req, res, options, cb) {
if (file.acl) {
uploadParams.acl = file.acl;
}
var writer = provider.upload(uploadParams);
writer.on('error', function(err) {

View File

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

View File

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