Skip to content

Commit

Permalink
resize: Actually upload to S3
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnor committed Apr 5, 2017
1 parent 04e5872 commit 8597cc4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
.env
6 changes: 6 additions & 0 deletions config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ envvar = (key, defaultValue) ->
module.exports =
msgflo:
broker: process.env.CLOUDAMQP_URL or process.env.MSGFLO_BROKER or 'amqp://localhost'
s3:
key: process.env.BUCKETEER_AWS_ACCESS_KEY_ID or process.env.IMAGERESIZE_S3_KEY
secret: process.env.BUCKETEER_AWS_SECRET_ACCESS_KEY or process.env.IMAGERESIZE_S3_SECRET
bucket: process.env.BUCKETEER_BUCKET_NAME or process.env.IMAGERESIZE_S3_BUCKET
prefix: process.env.IMAGERESIZE_S3_PREFIX or 'public/images/'
region: process.env.IMAGERESIZE_S3_REGION or 'us-east-1'
resize:
timeout: 1000*parseInt(envvar('IMAGERESIZE_RESIZE_TIMEOUT', 10)) # time before aborting
concurrent: parseInt(envvar('IMAGERESIZE_RESIZE_CONCURRENT', 5)) # number of concurrent jobs
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"coffee-script": "^1.12.4",
"express": "^4.15.2",
"knex": "^0.12.9",
"knox": "^0.9.2",
"msgflo": "^0.10.8",
"msgflo-nodejs": "^0.10.1",
"pg": "^6.1.5",
Expand Down
29 changes: 20 additions & 9 deletions src/resize.coffee
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
# Actual resizing functionality

sharp = require 'sharp'
knox = require 'knox'
request = require 'request'
fs = require 'fs'
uuid = require 'uuid'
bluebird = require 'bluebird'
path = require 'path'

common = require './common'
config = require '../config'

# Returns a Stream transformer which resizes image input data
# and outputs a new image within the specified @height / @width
# http://sharp.dimens.io/en/stable/api-resize/#resize
exports.createResizer = (width, height, options = {}) ->
throw new Error "Height not specified" if not height?
throw new Error "Width not specified" if not width?
Expand All @@ -31,26 +35,33 @@ exports.createResizer = (width, height, options = {}) ->
transformer.max()
return transformer

uploadS3 = (buffer, path, headers={}) ->
client = knox.createClient config.s3
bluebird.promisify(client.putBuffer, context: client)(buffer, path, headers)

exports.resizeImageAndUpload = (image) ->
bluebird.resolve()
.then () ->
throw new Error "Missing .input URL" if not image?.input
throw new Error "Missing .id for image" if not image?.id

reader = request image.input
options =
format: image.format
format: image.format or 'jpeg'
policy: image.policy
resizer = exports.createResizer image.width, image.height, options
writer = fs.createWriteStream "#{image.id}.jpg" # FIXME: actually upload somewhere
outputPath = path.join config.s3.prefix, "#{image.id}.#{options.format}"
reader.pipe(resizer)
resizer.pipe(writer)

return common.streamEnd writer
resizer.toBuffer()
.then (buffer) ->
return uploadS3 buffer, outputPath
.then () ->
return "https://#{config.s3.bucket}.s3.amazonaws.com/#{outputPath}"

main = () ->
[node, script, input, output, width, height] = process.argv
if not (input and output and height and width)
console.error 'Usage: imageresizer-resize-file http://example.net/URL.jpg OUTPUT.jpg height width'
[node, script, input, width, height] = process.argv
if not (input and height and width)
console.error 'Usage: imageresizer-resize-file http://example.net/URL.jpg height width'
process.exit 1

image =
Expand All @@ -60,7 +71,7 @@ main = () ->
input: input

exports.resizeImageAndUpload image
.asCallback (err, r) ->
.asCallback (err, output) ->
if err
console.error err
process.exit 2
Expand Down

0 comments on commit 8597cc4

Please sign in to comment.