-
Notifications
You must be signed in to change notification settings - Fork 0
/
resize.js
50 lines (46 loc) · 1.34 KB
/
resize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const sharp = require('sharp')
const { dirname, basename, extname } = require('path')
const thumbnailKey = ({ Key, prefix }) =>
`.thumbnails/${Key}/${prefix}${extname(Key)}`
module.exports = ({ s3, sourceObject, options, prefix, Key, Bucket }) =>
new Promise((resolve, reject) => {
console.log({ options })
sharp(sourceObject.Body)
.resize(options)
.toBuffer()
.then(resizedBuffer => {
console.log(resizedBuffer.length)
s3.putObject(
{
Key: thumbnailKey({ Key, prefix }),
Body: resizedBuffer,
Bucket,
ContentType: sourceObject.ContentType,
ACL: 'public-read',
},
(err, resizedObject) => {
if (err) {
reject(err)
} else {
resolve(resizedObject)
}
}
)
// s3.putObject(
// {
// Key: `${dirname(Key)}/thumbnail/${prefix}/${basename(Key)}`,
// Body: resizedBuffer,
// Bucket,
// ContentType: sourceObject.ContentType,
// ACL: 'public-read',
// },
// (err, resizedObject) => {
// if (err) {
// reject(err)
// } else {
// resolve(resizedObject)
// }
// }
// )
})
})