Support nameConflict and makeUnique options
This commit is contained in:
parent
080f18a985
commit
907f83ac3a
|
@ -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) {
|
||||||
|
|
|
@ -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);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -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",
|
||||||
|
|
Loading…
Reference in New Issue