Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) deleteing files from the fs or fs adapters was broken, and now is not #4513

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions fields/types/file/FileType.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,34 @@ 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);
}

};

/**
* 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 +85,17 @@ 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 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 Expand Up @@ -101,7 +124,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 +180,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