From 03b3416552e3594437d6b36552fb195051939f18 Mon Sep 17 00:00:00 2001 From: Chris Montoro Date: Fri, 13 Feb 2015 04:22:38 -0500 Subject: [PATCH] [s3FileType] delete file on remove, change 'Remove File' and 'Change File' actions should delete previous file from S3. --- fields/types/s3file/S3FileType.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/fields/types/s3file/S3FileType.js b/fields/types/s3file/S3FileType.js index 12c9b71cb4..fabe63c2d5 100644 --- a/fields/types/s3file/S3FileType.js +++ b/fields/types/s3file/S3FileType.js @@ -136,6 +136,14 @@ s3file.prototype.addToSchema = function() { url: '' }); }; + + var del = function(item){ + try { + var client = knox.createClient(field.s3config); + client.deleteFile(item.get(paths.path) + item.get(paths.filename), function(err, res){ return res ? res.resume() : false; }); + } catch(e) {} + reset(item); + }; var schemaMethods = { exists: function() { @@ -147,6 +155,9 @@ s3file.prototype.addToSchema = function() { * @api public */ reset: function() { + if (exists(this)){ + return del(this); + } reset(this); }, /** @@ -155,11 +166,7 @@ s3file.prototype.addToSchema = function() { * @api public */ delete: function() { - try { - var client = knox.createClient(field.s3config); - client.deleteFile(this.get(paths.path) + this.get(paths.filename), function(err, res){ return res ? res.resume() : false; }); - } catch(e) {} - reset(this); + del(this); } }; @@ -284,6 +291,7 @@ s3file.prototype.uploadFile = function(item, file, update, callback) { }; if (update) { + field.apply(item, 'reset'); item.set(field.path, fileData); }