-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
360 lines (338 loc) Β· 9.68 KB
/
server.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
require('dotenv').config()
const debug = require('debug')('hub:server')
const morgan = require('morgan')
const path = require('path')
const express = require('express')
const multer = require('multer')
const sharp = require('sharp')
const rimraf = require('rimraf')
const story = require('./lib/treatments/story.js')
const isGmColor = require('./lib/isGmColor')
const choo = require('choo')
const grid = require('./client/grid.js')
const detail = require('./client/detail.js')
const {
deleteFromImage,
get36Images,
get36ImagesSince
} = require('./lib/db.js')
const DISABLE_SERVER_RENDER = process.env.DISABLE_SERVER_RENDER === 'true'
const INITIAL_STATIC_SERVER = process.env.INITIAL_STATIC_SERVER === 'true'
debug('DISABLE_SERVER_RENDER', DISABLE_SERVER_RENDER)
debug('INITIAL_STATIC_SERVER', INITIAL_STATIC_SERVER)
const MEDIA_PATH = process.env.MEDIA_PATH
if (!MEDIA_PATH) {
console.error(new Error('No MEDIA_PATH'))
process.exit(1)
}
const SMALL = 'small'
const THUMB = 'thumb'
const STORAGE = 'storage'
const STORIES = 'stories'
const SMALL_PATH = path.join(MEDIA_PATH, SMALL)
const THUMB_PATH = path.join(MEDIA_PATH, THUMB)
const STORAGE_PATH = path.join(MEDIA_PATH, STORAGE)
const STORIES_PATH = path.join(MEDIA_PATH, STORIES)
/**
* Resize small images to disk
*/
const SIZE = 240
debug('DEFAULT RESIZE PX', SIZE)
function resize (from, to, size = SIZE) {
return sharp(from)
.resize(size, size)
.max()
.rotate()
.withoutEnlargement()
.toFile(to)
}
/**
* Returns Promise for deleted directory
*/
function rimRafJpgDir (dir) {
return new Promise((resolve, reject) => {
const glob = path.join(dir, '*.JPG')
rimraf(glob, (err, result) => {
debug(err, result)
if (err) {
debug(err)
return reject(err)
}
debug('rimraf result', result)
resolve(result || `deleted ${glob}`)
})
})
}
/**
* Returns Promise for deleted directories
*/
function rimRafMedia () {
return Promise.all([
rimRafJpgDir(SMALL_PATH),
rimRafJpgDir(THUMB_PATH),
rimRafJpgDir(STORIES_PATH),
rimRafJpgDir(STORAGE_PATH)
]).then(arr => arr[0])
}
/**
* Returns Promise for deleted directories
*/
function clearDB () {
debug('running db command')
return new Promise((resolve, reject) => {
try {
deleteFromImage.run()
} catch (err) {
debug(err)
return reject(err)
}
resolve('deleted')
})
}
const app = express()
app.set('env', process.env.NODE_ENV)
const morganLogPreset = app.get('env') === 'development' ? 'dev' : 'combined'
app.use(morgan(morganLogPreset))
app.set('views', path.join(__dirname, 'views')) // general config
app.set('view engine', 'pug')
const expressStaticOptions = {
index: false,
maxAge: process.env.NODE_ENV === 'production' ? '1d' : '2m'
}
// Simulate Nginx reverse-proxy, eliminate re-resizing static assets
if (INITIAL_STATIC_SERVER) {
app.use('/', express.static(MEDIA_PATH, {
...expressStaticOptions,
fallthrough: true
}))
}
app.get('/stories/:image', function (req, res, next) {
debug('image', req.params.image)
const sourceImagePath = path.join(STORAGE_PATH, req.params.image)
const imageMattPath = path.join(STORIES_PATH, req.params.image)
debug('matt path', imageMattPath)
return story(sourceImagePath, imageMattPath, app.locals.storyColor)
.then(result => {
debug('story created', result)
next()
})
})
app.get('/thumb/:image', function (req, res, next) {
debug('image', req.params.image)
const name = req.params.image
const sourceImagePath = path.join(STORAGE_PATH, name)
const thumbImagePath = path.join(THUMB_PATH, name)
debug('generate thumb', thumbImagePath)
resize(sourceImagePath, thumbImagePath, 240)
.then(result => {
debug('thumb resize', result)
next()
})
.catch(err => {
if (err.message === 'Input file is missing or of an unsupported image format') {
res.status(404).end()
}
})
})
app.get('/small/:image', function (req, res, next) {
debug('image', req.params.image)
const name = req.params.image
const sourceImagePath = path.join(STORAGE_PATH, name)
const destinationImagePath = path.join(SMALL_PATH, name)
debug('generate preview', sourceImagePath)
resize(sourceImagePath, destinationImagePath, 1280)
.then(result => {
debug('small resize', result)
next()
})
.catch(err => {
if (err.message === 'Input file is missing or of an unsupported image format') {
res.status(404).end()
}
})
})
app.use(express.static(path.join(__dirname, 'dist'), expressStaticOptions))
app.use(express.static(path.join(__dirname, 'public'), expressStaticOptions))
app.use(express.static(MEDIA_PATH))
/**
* CHOO ROUTE
*/
const chooApp = choo()
chooApp.route('/', grid)
chooApp.route('/view/:image', detail)
app.get('/', function (req, res, next) {
const since = req.query.since
let rows
try {
if (since) {
debug('req.query.since', since)
rows = get36ImagesSince.all({ since })
} else {
rows = get36Images.all()
}
debug('found %s photos', rows.length)
const images = rows.map(i => {
return {
name: i.file_name,
date: new Date(i.date_time_created.replace(/-/g, '/')).valueOf()
}
})
return res.format({
'text/html': function () {
debug('rendering text/html')
const state = {
images,
params: req.params,
href: req.originalUrl
}
const chooRenderedString = chooApp.toString('/', state)
res.render('index', {
state,
chooRenderedString: DISABLE_SERVER_RENDER ? '<body>π</body>' : chooRenderedString
})
},
'application/json': function () {
debug('rendering application/json')
res.json(images)
},
'default': function () {
// log the request and respond with 406
res.status(406).send('Not Acceptable')
}
})
} catch (err) {
debug(err)
next(err)
}
})
app.get('/view/:image', function (req, res, next) {
debug('hit view route for %s', req.params.image)
try {
const rows = get36Images.all()
debug('found %s photos', rows.length)
const images = rows.map(i => {
return {
name: i.file_name,
date: new Date(i.date_time_created.replace(/-/g, '/')).valueOf()
}
})
return res.format({
'text/html': function () {
debug('rendering text/html')
const state = {
images,
params: req.params,
href: req.originalUrl
}
const chooRenderedString = chooApp.toString(`/view/${req.params.image}`, state)
res.render('index', {
state,
chooRenderedString: DISABLE_SERVER_RENDER ? '<body>π</body>' : chooRenderedString
})
},
'application/json': function () {
debug('rendering application/json')
res.json(images)
},
'default': function () {
// log the request and respond with 406
res.status(406).send('Not Acceptable')
}
})
} catch (err) {
debug(err)
next(err)
}
})
const getFormData = multer().single()
app.get('/admin', function (req, res, next) {
res.render('admin')
})
app.post('/admin', getFormData, function handleFormData (req, res, next) {
debug('command', req.body.command)
const commands = {
all: () => Promise.all([clearDB(), rimRafMedia()]).then(arr => 'deleted all'),
db: clearDB,
small: () => rimRafJpgDir(SMALL_PATH),
thumb: () => rimRafJpgDir(THUMB_PATH),
stories: () => rimRafJpgDir(STORIES_PATH),
storage: () => rimRafJpgDir(STORAGE_PATH),
media: rimRafMedia
}
const command = req.body.command.toLowerCase()
let commandResult
if (isGmColor(command)) {
app.locals.storyColor = command
commandResult = Promise.resolve(`ig story color set to ${command}`)
} else if (commands[command]) {
debug('doing command', req.body.command)
// Do async commmand, then set info after
commandResult = commands[command]()
} else {
debug('failed command', req.body.command)
res.status(406)
res.locals.statusText = 'Not acceptable'
return next()
}
commandResult.then(result => {
debug('result', result)
res.locals.statusText = result
res.status(200)
next()
}).catch(reason => {
res.status(406)
res.locals.statusText = 'Not acceptable' + reason
next()
})
}, function renderFormResponse (req, res, next) {
debug('running send middleware')
return res.format({
'text/html': function () {
// Use error template as the template for the admin page
// even though it's not an error per se
res.render('error', {
status: res.statusCode,
statusText: res.locals.statusText
})
},
'application/json': function () {
res.json({ status: res.statusCode, statusText: res.locals.statusText })
},
'default': function () {
res.send(res.locals.statusText)
}
})
})
// catch 404 and forward to error handler
app.use(function (req, res, next) {
return res.format({
'text/html': function () {
debug('rendering text/html')
res.status(404)
res.render('error', {
status: res.statusCode,
statusText: 'Not found'
})
},
'application/json': function () {
debug('rendering application/json error')
res.status(404).json({ Error: { status: 404, statusText: 'Not found' } })
debug('command', res.body.command)
},
'default': function () {
res.status(404).end()
}
})
})
// Error handlers
// Development error handler, will print stacktrace
app.use(function (err, req, res, next) {
debug('dev error handler', err)
res.status(err.status || 500)
res.render('error', {
status: err.statusCode,
statusText: err.message,
err
})
})
module.exports = app