From 907f83ac3a969444d6b50fe4cfeadb6cc6d96d82 Mon Sep 17 00:00:00 2001 From: Raymond Camden Date: Wed, 1 Feb 2017 13:09:41 -0600 Subject: [PATCH] Support nameConflict and makeUnique options --- lib/storage-handler.js | 5 +++++ lib/storage-service.js | 6 ++++++ package.json | 3 ++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/storage-handler.js b/lib/storage-handler.js index 52733a2..1141681 100644 --- a/lib/storage-handler.js +++ b/lib/storage-handler.js @@ -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) { diff --git a/lib/storage-service.js b/lib/storage-service.js index ef50b80..9a67be8 100644 --- a/lib/storage-service.js +++ b/lib/storage-service.js @@ -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); }; diff --git a/package.json b/package.json index 24cdbf8..1595442 100644 --- a/package.json +++ b/package.json @@ -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",