Skip to content

Commit

Permalink
when replacing an existing a file remove the old file before replacin…
Browse files Browse the repository at this point in the history
…g it
  • Loading branch information
njs50 committed Mar 15, 2018
1 parent 43c4fac commit 2a86790
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions fields/types/file/FileType.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,27 @@ file.prototype.upload = function (item, file, callback) {
var field = this;
// TODO; Validate there is actuall a file to upload
debug('[%s.%s] Uploading file for item %s:', this.list.key, this.path, item.id, file);
this.storage.uploadFile(file, function (err, result) {
if (err) return callback(err);
debug('[%s.%s] Uploaded file for item %s with result:', field.list.key, field.path, item.id, result);
item.set(field.path, result);
callback(null, result);
});

var upload = function (item, file, callback) {
field.storage.uploadFile(file, function (err, result) {
if (err) return callback(err);
debug('[%s.%s] Uploaded file for item %s with result:', field.list.key, field.path, item.id, result);
item.set(field.path, result);
callback(null, result);
});
};

if (item[field.path] && item[field.path].filename) {

field.remove(item, function (err, result) {
if (err) return callback(err);
upload(item, file, callback);
});

} else {
upload(item, file, callback);
}

};

/**
Expand All @@ -75,7 +90,8 @@ file.prototype.remove = function (item, callback) {
const file = item.get(this.path);
debug('[%s.%s] Removing file for item %s:', this.list.key, this.path, item.id, file);
this.storage.removeFile(file, function (err, result) {
if (err) return callback(err);
// if this file was somehow deleted elsewhere we can ignore this error
if (err && err.statusCode !== 404) return callback(err);
debug('[%s.%s] Removed file for item %s with result:', field.list.key, field.path, item.id, result);
field.reset(item);
callback(null, result);
Expand Down

0 comments on commit 2a86790

Please sign in to comment.