-
Notifications
You must be signed in to change notification settings - Fork 3
/
photos_router.js
202 lines (166 loc) · 5.44 KB
/
photos_router.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
const express = require('express')
const multer = require('multer')
const config = require('./config')
const photos = require('./photos')
const { expressjwt: jwt } = require('express-jwt')
const { expressJwtSecret } = require('jwks-rsa')
const db = require('./db')
let upload = multer({dest: config.photos.uploadPath})
let router = express.Router()
module.exports = router
let jwtCallback = jwt({
secret: expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: config.sso.jwksUri
}),
algorithms: ['RS256']
})
router.post('/summits/:association/:code/upload', jwtCallback, upload.array('photo'), async (req, res) => {
try {
res.cacheControl = {
noCache: true
}
if (!req.auth.callsign) {
res.status(401).send('Missing callsign in SSO token').end()
return
}
let summitCode = req.params.association + '/' + req.params.code
let summit = await db.getDb().collection('summits').findOne({code: summitCode})
if (!summit) {
res.status(404).end()
return
}
if (req.files) {
let dbPhotos = []
for (let file of req.files) {
let photo = await photos.importPhoto(file.path, req.auth.callsign)
dbPhotos.push(photo)
}
// Check for duplicates
if (summit.photos) {
dbPhotos = dbPhotos.filter(photo => !summit.photos.some(summitPhoto => summitPhoto.filename === photo.filename ))
}
if (dbPhotos.length > 0) {
await db.getDb().collection('summits').updateOne({code: summitCode}, { $push: { photos: { $each: dbPhotos } } })
}
res.json(dbPhotos)
} else {
res.status(400).end()
}
} catch (err) {
console.error(err)
res.status(500).end()
}
})
router.delete('/summits/:association/:code/:filename', jwtCallback, async (req, res) => {
res.cacheControl = {
noCache: true
}
if (!req.auth.callsign) {
res.status(401).send('Missing callsign in SSO token').end()
return
}
let summitCode = req.params.association + '/' + req.params.code
let summit = await db.getDb().collection('summits').findOne({code: summitCode})
let photo = summit.photos.find(photo => photo.filename === req.params.filename)
if (!photo) {
res.status(404).end()
return
}
// Check that uploader is currently logged in user
if (photo.author !== req.auth.callsign) {
res.status(401).send('Cannot delete another user\'s photos').end()
return
}
await db.getDb().collection('summits').updateOne({code: summitCode}, { $pull: { photos: { filename: req.params.filename } } })
res.status(204).end()
})
router.post('/summits/:association/:code/reorder', jwtCallback, async (req, res) => {
res.cacheControl = {
noCache: true
}
if (!req.auth.callsign) {
res.status(401).send('Missing callsign in SSO token').end()
return
}
let summitCode = req.params.association + '/' + req.params.code
// Assign new sortOrder index to photos of this user, in the order given by req.body.filenames
let updates = req.body.filenames.map((filename, index) => {
return db.getDb().collection('summits').updateOne(
{ code: summitCode, 'photos.author': req.auth.callsign, 'photos.filename': filename },
{ $set: { 'photos.$.sortOrder': index + 1 } }
)
})
await Promise.all(updates)
res.status(204).end()
})
router.post('/summits/:association/:code/:filename', jwtCallback, async (req, res) => {
res.cacheControl = {
noCache: true
}
if (!req.auth.callsign) {
res.status(401).send('Missing callsign in SSO token').end()
return
}
let summitCode = req.params.association + '/' + req.params.code
let summit = await db.getDb().collection('summits').findOne({code: summitCode})
let photo = summit.photos.find(photo => photo.filename === req.params.filename)
if (!photo) {
res.status(404).end()
return
}
// Check that editor is the currently logged in user
if (photo.author !== req.auth.callsign) {
res.status(401).send('Cannot delete another user\'s photos').end()
return
}
let update = {
$set: {},
$unset: {}
}
if (req.body.title) {
update.$set['photos.$.title'] = req.body.title
} else {
update.$unset['photos.$.title'] = ''
}
if (req.body.date) {
update.$set['photos.$.date'] = new Date(req.body.date)
} else {
update.$unset['photos.$.date'] = ''
}
if (req.body.coordinates) {
update.$set['photos.$.coordinates'] = req.body.coordinates
update.$set['photos.$.positioningError'] = req.body.positioningError
} else {
update.$unset['photos.$.coordinates'] = ''
update.$unset['photos.$.positioningError'] = ''
}
if (req.body.direction !== null && req.body.direction !== undefined && req.body.direction !== '') {
update.$set['photos.$.direction'] = req.body.direction
} else {
update.$unset['photos.$.direction'] = ''
}
if (req.body.isCover) {
update.$set['photos.$.isCover'] = true
// Only one photo can be the cover photo, so unmark all others first
await db.getDb().collection('summits').updateOne(
{ code: summitCode },
{ $unset: { 'photos.$[].isCover': '' } }
)
} else {
update.$unset['photos.$.isCover'] = ''
}
if (Object.keys(update.$set).length === 0) {
delete update.$set
}
if (Object.keys(update.$unset).length === 0) {
delete update.$unset
}
await db.getDb().collection('summits').updateOne(
{ code: summitCode, 'photos.filename': req.params.filename },
update
)
res.status(204).end()
})