Created
April 8, 2016 11:39
-
-
Save JedWatson/1623a3e7e0b8003ce324db9c3bb0e7b1 to your computer and use it in GitHub Desktop.
File Field Scratchpad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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