-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.coffee
62 lines (50 loc) · 1.48 KB
/
app.coffee
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
51
52
53
54
55
56
57
58
59
60
61
AWS = require "aws-sdk"
busboy = require "express-busboy"
express = require "express"
fs = require "fs"
gm = require "gm"
pngquant = require "node-pngquant-native"
serveStatic = require "serve-static"
accessKeyId = process.env.AWS_S3_ACCESS_KEY
acl = process.env.AWS_S3_ACL || "public-read"
bucket = process.env.AWS_S3_BUCKET || "gyazobank"
endpoint = process.env.AWS_S3_ENDPOINT
secretAccessKey = process.env.AWS_S3_SECRET_KEY
s3 = new AWS.S3
endpoint: new AWS.Endpoint endpoint
accessKeyId: accessKeyId
secretAccessKey: secretAccessKey
app = express()
app.use serveStatic "public"
busboy.extend app, {
upload: true
}
app.post "/upload", (req, res) ->
url = ""
if not req.files.imagedata
return res.send url
filepath = req.files.imagedata.file
name = "#{ req.files.imagedata.uuid }.png"
url = "http://#{ bucket }.#{ endpoint }/#{ name }"
fs.readFile filepath, (err, buffer) ->
if err
return res.send ""
gm(buffer)
.options(imageMagick: true)
.autoOrient()
.toBuffer "PNG", (err, pngBuffer) ->
if err
return res.send ""
s3.putObject {
ACL: acl
Body: pngquant.compress pngBuffer
Bucket: bucket
Key: name
ContentType: "image/png"
}, (err, data) ->
if err
console.log err
res.send ""
else
res.send url
app.listen process.env.PORT || 3000