Skip to content

Commit

Permalink
[s3FileType] delete file on remove, change
Browse files Browse the repository at this point in the history
'Remove File' and 'Change File' actions should delete previous file from S3.
  • Loading branch information
montmanu committed Feb 13, 2015
1 parent 6966718 commit 03b3416
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions fields/types/s3file/S3FileType.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -147,6 +155,9 @@ s3file.prototype.addToSchema = function() {
* @api public
*/
reset: function() {
if (exists(this)){
return del(this);
}
reset(this);
},
/**
Expand All @@ -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);
}
};

Expand Down Expand Up @@ -284,6 +291,7 @@ s3file.prototype.uploadFile = function(item, file, update, callback) {
};

if (update) {
field.apply(item, 'reset');
item.set(field.path, fileData);
}

Expand Down

0 comments on commit 03b3416

Please sign in to comment.