Skip to content
This repository has been archived by the owner on Mar 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #44 from adieuadieu/fix/43-s3-object-content-type-…
Browse files Browse the repository at this point in the history
…not-set-properly

fix: include output image's ContentType in S3 Object's parameter
  • Loading branch information
adieuadieu authored Jul 23, 2017
2 parents 0daab33 + 1e44d4b commit e21c818
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Changed


## [0.11.0] - 2017-07-23
### Changed
- add output image's ContentType to S3 Object's parameters (previously defaulted to `application/octet-stream`) (#43, #44)


## [0.10.0] - 2017-07-02
### Added
- added this CHANGELOG from @adieuadieu
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-sharp-image",
"version": "0.10.0",
"version": "0.10.1",
"description": "Serverless-based Lambda function to resize images based on S3 events with the awesome Sharp library",
"main": "handler.js",
"engines": {
Expand Down
28 changes: 21 additions & 7 deletions src/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@ import { makeKey, decodeS3EventKey } from './utils'

const { outputs } = config

export default (async function processItem (
{ eventName, s3: { object: { key: undecodedKey } } = { object: { key: false } } },
) {
const imageMimeTypes = {
jpeg: 'image/jpeg',
png: 'image/png',
webp: 'image/webp',
gif: 'image/gif',
svg: 'image/svg+xml',
}

export default (async function processItem ({
eventName,
s3: { object: { key: undecodedKey } } = { object: { key: false } },
}) {
const key = decodeS3EventKey(undecodedKey)
console.log('Processing: ', key)

if (eventName.split(':')[0] !== 'ObjectCreated') {
throw new Error(
`Event does not contain a valid type (e.g. ObjectCreated). Invoked by event name: ${eventName}`,
`Event does not contain a valid type (e.g. ObjectCreated). Invoked by event name: ${eventName}`
)
}

Expand All @@ -26,7 +35,12 @@ export default (async function processItem (
const context = { key, type }

return Promise.all(
streams.map((stream, index) =>
upload(stream, { ...outputs[index].params, Key: makeKey(outputs[index].key, context) })),
streams.map(async (stream, index) =>
upload(stream, {
ContentType: imageMimeTypes[(await stream.metadata()).format],
...outputs[index].params,
Key: makeKey(outputs[index].key, context),
})
)
)
});
})

0 comments on commit e21c818

Please sign in to comment.