-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
108 lines (92 loc) · 2.99 KB
/
index.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict'
const fs = require('fs')
const express = require('express')
const Jimp = require('jimp')
const os = require('os')
const https = require('https')
const chalk = require('chalk')
const shortid = require('shortid')
const multer = require('multer')
const upload = multer()
const svg2png = require('svg2png')
const mkdirp = require('mkdirp')
mkdirp(__dirname + '/uploads')
let app = express()
app.set('trust proxy', true)
// root, welcome page
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/index.html')
})
// upload an image form
app.get('/upload', function(req, res) {
res.sendFile(__dirname + '/public/upload.html')
})
// the main attraction
app.post('/upload', upload.single('file'), function(req, res) {
if(req.file.mimetype == 'image/svg+xml') {
// svg uploads
svg2png(req.file.buffer, {
width: 640,
height: 720
}).then(function(buffer) {
req.file.buffer = buffer
fileUpload(req, res)
}).catch(function(e) {
console.log(`[ ${chalk.red.bold('err!')} ] ${e}`)
res.redirect('/upload/fail')
})
} else {
// png, jpeg, bmp uploads
fileUpload(req, res)
}
})
function fileUpload(req, res) {
let id = shortid.generate()
Jimp.read(req.file.buffer).then(function(img) {
img.contain(640, 720) // fit to scratch forums max image size
img.autocrop() // crop same-color borders from image (if any)
img.write(__dirname + '/uploads/' + id + '.png', function(e) {
if(e) {
console.log(`[ ${chalk.red.bold('err!')} ] ${e}`)
res.redirect('/upload/fail')
} else res.redirect('/' + id)
})
}).catch(function(e) {
console.log(`[ ${chalk.red.bold('err!')} ] ${e}`)
res.redirect('/upload/fail')
})
}
// get and display raw image
app.get('/:id', function(req, res) {
let file = __dirname + `/uploads/${req.params.id}.png`
// only allow displaying on Scratch or OpenSprites
if(os.hostname() == 'opensprites.org') {
if(req.get('host') !== 'scratch.mit.edu' && req.get('host') !== 'opensprites.org' && req.get('host') !== 'opensprites.org:3000') {
console.log(`[ ${chalk.yellow('warn')} ] We're on OpenSprites, but somebody wants a file from ${req.get('host')}. Ignoring.`)
res.sendFile(__dirname + '/public/404.html')
return
}
}
fs.stat(file, function(err, stat) {
if(err) res.sendFile(__dirname + '/public/404.html')
else res.sendFile(file)
})
})
app.use('/dist', express.static('dist'))
app.use(express.static('public'))
app.get('*', function(req, res) {
res.sendFile(__dirname + '/public/404.html')
})
// ssl for opensprites.org *only*
if(os.hostname() == 'opensprites.org') {
https.createServer({
key: fs.readFileSync('/home/admin/conf/web/ssl.opensprites.org.key'),
cert: fs.readFileSync('/home/admin/conf/web/ssl.opensprites.org.crt')
}, app).listen(3000, function() {
console.log('[ ---- ] ' + chalk.blue('Listening on ' + chalk.bold('https://opensprites.org:3000') + '...'))
})
} else {
app.listen(3000, function() {
console.log('[ ---- ] ' + chalk.blue('Listening on ' + chalk.bold('localhost:3000') + '...'))
})
}