In your use-case above, it seems that dest
and prefix
represent generated values that need not be available in the Keystone Admin UI. Mongoose has a feature called virtual attributes which you can use precisely for this use case. Virtual attributes are defined by invoking the .virtual()
method of the Mongoose schema. Since Keystone exposes the mongoose schema of any List in its .schema
property, you could easily convert dest
and prefix
into virtual attributes.
In addition to virtual attributes, Mongoose also provides instance methods, from which you can reference any defined virtual attributes. You convert format
into an instance method, then simply reference the prefix
virtual attribute from within it.
Finally, make sure that you define any virtual attributes and instance methods prior to calling the List's .register()
method. Check out the Keystone documentation on schema plugins for more details.
Here's your code example using virtual attributes and instance methods.
Sorry for the long and winded useless explanation above. I didn't realize that dest
and prefix
were part of the LocalFile
schema.
Having a custom path per upload doesn't make much sense to me. A custom/separate path to store a single file doesn't seem very useful, IMHO. However, having a custom filename that defaults to the auto created slug
does. Here's how I would do it. Hopefully this works for you.
var Thing = new keystone.List('Thing', {
autokey: { path: 'slug', from: 'title', unique: true }
}
});
Thing.add({
image: {
type: Types.LocalFile,
dest: 'public/images/things/',
prefix: '/images/things/',
filename: function(item, file) {
return item.slug;
},
format: function(item, file) {
return '<img src="' + item.prefix + item.filename + '" style="max-width:300px" />';
}
});
Thing.register();
NOTE: Mongoose allows you to define both a getter
and a setter
(using the .get()
and .set()
methods respectively) for each defined virtual attribute. However, since dest
and prefix
appear to be read-only in your use-case, I only defined a getter
for them in my sample code above.