Skip to content

Commit

Permalink
(fix) deleteing files from the fs or fs adapters was broken, and now …
Browse files Browse the repository at this point in the history
…is not.
  • Loading branch information
cadriel committed Nov 17, 2017
1 parent 0ca9eb1 commit b7e36f7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
21 changes: 14 additions & 7 deletions fields/types/file/FileType.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ file.prototype.upload = function (item, file, callback) {
* Resets the field value
*/
file.prototype.reset = function (item) {
debug('[%s.%s] Reset file for item %s:', this.list.key, this.path, item);
var value = {};
Object.keys(this.storage.schema).forEach(function (path) {
value[path] = null;
Expand All @@ -69,10 +70,16 @@ file.prototype.reset = function (item) {
/**
* Deletes the stored file and resets the field value
*/
// TODO: Should we accept a callback here? Seems like a good idea.
file.prototype.remove = function (item) {
this.storage.removeFile(item.get(this.path));
this.reset();
file.prototype.remove = function (item, callback) {
const field = this;
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);
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 Expand Up @@ -101,7 +108,7 @@ function validateInput (value) {
// undefined, null and empty values are always valid
if (value === undefined || value === null || value === '') return true;
// If a string is provided, check it is an upload or delete instruction
if (typeof value === 'string' && /^(upload\:)|(delete$)/.test(value)) return true;
if (typeof value === 'string' && /^(upload\:)|(delete$)|(remove$)/.test(value)) return true;
// If the value is an object with a filename property, it is a stored value
// TODO: Need to actually check a dynamic path based on the adapter
if (typeof value === 'object' && value.filename) return true;
Expand Down Expand Up @@ -157,8 +164,8 @@ file.prototype.updateItem = function (item, data, files, callback) {

// Providing the string "remove" removes the file and resets the field
if (value === 'remove') {
this.remove(item);
utils.defer(callback);
return this.remove(item, callback);
// utils.defer(callback);
}

// Find an uploaded file in the files argument, either referenced in the
Expand Down
2 changes: 1 addition & 1 deletion fields/types/file/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var storage = new keystone.Storage({
}
});
MyList.add({
file: { type: Types.File, storage: storage }
file: { type: Types.File, storage: storage, autoCleanup: false }
});
```

Expand Down

0 comments on commit b7e36f7

Please sign in to comment.