Skip to content

Instantly share code, notes, and snippets.

@JedWatson JedWatson/files.js
Created Apr 8, 2016

Embed
What would you like to do?
File Field Scratchpad
keystone.init({
...options
});
var S3Storage = require('keystone-storage-s3');
keystone.addStorage('s3bucket1', S3Storage, {
key: 'my key',
bucket: 'keystone-stuff',
secret: 'abracadabra',
});
keystone.addStorage('local', 'fs', {
path: '/files/go/here',
});
///
var Post = keystone.createList('Post');
Post.add({
picture: { type: Types.Image, storage: 's3bucket1', path: 'images' },
cloudAttachment: { type: Types.File, storage: 's3bucket1' },
localAttachments: { type: Types.Files, storage: 'local', path: 'attachments', filename: (file) => file.originalName + '-uploaded-' + Date.now() },
place: { type: Types.Location, fields: {
street1: 'street',
suburb: 'city',
postcode: 'code',
latlng: 'geo',
}, required: 'street1 suburb postcode' }
});
///
addToSchema () {
if (this.storage.addToSchema) {
this.storage.addToSchema(this);
} else {
var schema = this.list.schema;
var schemaPaths = {
path: String,
size: Number,
type: String, // "png" "jpg" "gif" "svg"
width: Number,
height: Number,
}
if (this.options.fields.mimeType) {
schemaPaths.mimeType = String;
}
schema.add(schemaPaths);
}
}
///
addToSchema () {
if (this.storage.addToSchema) {
this.storage.addToSchema(this);
} else {
var schema = this.list.schema;
var schemaPaths = {
path: String,
size: Number,
extension: String,
}
if (this.options.fields.mimeType) {
schemaPaths.mimeType = String;
}
schema.add(schemaPaths);
}
}
updateItem (item, data, callback) {
var file = this.getValueFromData(data);
if (file && file.path) {
storage.saveFile(file, (err, result) => {
if (err) return callback(err);
item.set(this.path, result);
return callback();
});
}
}
///
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.