Skip to content

Instantly share code, notes, and snippets.

@DanielDornhardt
Created April 8, 2015 22:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielDornhardt/0f3628c898b946a7cc4d to your computer and use it in GitHub Desktop.
Save DanielDornhardt/0f3628c898b946a7cc4d to your computer and use it in GitHub Desktop.
Saving additional info to images using collectionFS on the server
if Meteor.isServer
crypto = Npm.require 'crypto'
# find our "code" dir / the directory outside of meteor
pathArr = process.env.PWD.split '/'
pathArr.pop()
filesBasePath = pathArr.join '/'
filesBasePath = filesBasePath + '/uploads/example/'
fsOptionsOriginalSizeJpg =
path: filesBasePath + 'original/'
beforeWrite: (fileObj) ->
# We return an object which will change the filename extension and type for this store only
return {
extension: 'jpg'
type: 'image/jpg'
}
transformWrite: (fileObj, readStream, writeStream) ->
# we also want to keep a hash of the original image
hash = crypto.createHash 'sha1'
hash.setEncoding 'hex'
readStream.pipe hash
readStream.once 'end', Meteor.bindEnvironment ->
sha1 = hash.read()
fileObj.update {$set: {originalImageSha1: sha1}}
writeStream.once 'stored', Meteor.bindEnvironment ->
updateImageSizeInStore(fileObj, 'example_originalImages')
# if we already have a jpeg, don't recompress it
if fileObj.original?.type? == 'image/jpeg'
readStream.pipe writeStream
return
# convert to .jpg if it's not already
gm(readStream, fileObj.name()).quality('90').stream('JPEG').pipe(writeStream)
fsOptionsThumbnail =
path: filesBasePath + 'thumbs/',
beforeWrite: (fileObj) ->
# We return an object which will change the filename extension and type for this store only
return {
extension: 'jpg'
type: 'image/jpg'
}
transformWrite: (fileObj, readStream, writeStream) ->
# Transform the images into the 552x411 thumbnail format
gm(readStream, fileObj.name()).resize('552', '411').quality('90').stream('JPEG').pipe(writeStream)
writeStream.once 'stored', Meteor.bindEnvironment ->
updateImageSizeInStore(fileObj, 'example_thumbnailImages')
# initialize collection
@Images = new FS.Collection "example_images",
stores: [
# putting header images on top because these are the first ones we want to show after uploading
new FS.Store.FileSystem "example_thumbnailImages", fsOptionsThumbnail
new FS.Store.FileSystem "example_originalImages", fsOptionsOriginalSizeJpg
]
filter:
maxSize: 20 * 1024 * 1024 # 20 megabytes, in bytes
allow:
contentTypes: ['image/*'] # allow only images in this FS.Collection
# Utility function to save the dimensions of images after converting them using gm
updateImageSizeInStore = (fileObj, storename) ->
readStream = fileObj.createReadStream storename
gm(readStream, fileObj.name({store: storename})).size Meteor.bindEnvironment (err, size) ->
#if err
# throw new Meteor.Error "Error reading filesize when trying to save filesize to db", err
updateOptions = {$set: {}}
updateOptions.$set['imageSizes.' + storename] = size
fileObj.update updateOptions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment