Created
January 10, 2015 16:38
-
-
Save erichosick/e772c438058b3b18dfbc to your computer and use it in GitHub Desktop.
S3 Image Upload Stuff
This file contains hidden or 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
// see https://github.com/CollectionFS/Meteor-cfs-s3 | |
// see http://help.modulus.io/customer/portal/articles/1653448-file-storage | |
// - /app-storage/tmp vs /mnt/data/tmp ? app-storage is permanent | |
FS.debug = true; | |
FS.HTTP.setBaseUrl('/media'); | |
FS.HTTP.setHeadersForGet([ | |
['Cache-Control', 'public, max-age=31536000'] | |
]); | |
var settings = {}; | |
if (Meteor.isServer) { | |
Meteor.settings = Meteor.settings || {}; | |
var awsSecretAccessKey = envVarGet("AWS_SECRET_ACCESS_KEY", "AWS S3 upload credentials require this environment variables."); | |
var awsAccessKeyId = envVarGet("AWS_ACCESS_KEY_ID", "AWS S3 upload credentials require this environment variables."); | |
var awsBucket = envVarGet("AWS_BUCKET", "AWS S3 upload requires a bucket to place files in."); | |
var tempDir = envVarGet("AWS_TEMP_DIR", "Modulus, and other services, need to provide a temporary folder."); | |
console.log("AWS Settings Used..."); | |
console.log(" bucket is: " + awsBucket); | |
console.log(" access key is: " + awsAccessKeyId.substr(0,4) + "..."); | |
console.log(" secret key is: " + awsSecretAccessKey.substr(0,4) + "..."); | |
if (awsSecretAccessKey && awsAccessKeyId && awsBucket) { | |
settings = { | |
region: "us-west-2", | |
secretAccessKey: awsSecretAccessKey, | |
accessKeyId: awsAccessKeyId, | |
bucket: awsBucket, | |
ACL: "public-read" | |
} | |
if (tempDir) { | |
FS.TempStore.Storage = new FS.Store.FileSystem("_tempstore", { | |
internal: true, | |
path: tempDir | |
}); | |
} | |
} | |
} | |
settings.transformWrite = function(fileObj, readStream, writeStream) { | |
// Transform the image into a thumbnail | |
if (!fileObj) { | |
console.log("Warning: FileObj was null or undefined so could not transform image."); | |
return undefined; | |
} | |
var fileName = fileObj.name(); | |
var isImage = fileObj.isImage(); | |
if (!isImage) { | |
if (FS.debug) { | |
console.log("Warning: FileObj was not an image so no conversion was done."); | |
} | |
readStream.pipe(writeStream); | |
return undefined; | |
} | |
if (FS.debug) { | |
console.log("Resizing object named " + fileName); | |
} | |
// var resized = gm(readStream, fileObj.name()).resize('40', '40'); | |
// if (!resized) { | |
// console.log("Problem with resizing."); | |
// return undefined; | |
// } | |
// console.log(resized); | |
// resized.stream().pipe(writeStream); | |
readStream.pipe(writeStream); // TODO: This is going to take a bit of work to get working and not MVP. For now, just return the stream. - Eric H | |
return undefined; | |
// gm(readStream, fileObj.name()).resize('40', '40').stream().pipe(writeStream); | |
} | |
var imageStore = new FS.Store.S3("images", settings); | |
Images = new FS.Collection("images", { | |
stores: [imageStore], | |
filter: { | |
allow: { | |
contentTypes: ['image/*'] //allow only images in this FS.Collection | |
} | |
} | |
}); | |
Images.allow({ | |
insert: function() { | |
return true; | |
}, | |
update: function() { | |
return true; | |
}, | |
remove: function() { | |
return true; | |
}, | |
download: function() { | |
return true; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment