-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
159 lines (121 loc) · 4.25 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
#!/usr/bin/env node
import 'dotenv/config.js'
import process from 'node:process'
import express from 'express'
import cors from 'cors'
import morgan from 'morgan'
import createError from 'http-errors'
import {omit} from 'lodash-es'
import mongo from './lib/util/mongo.js'
import w from './lib/util/w.js'
import errorHandler from './lib/util/error-handler.js'
import {sanitizePath} from './lib/util/path.js'
import {readToken, ensureAdmin, computeDownloadToken, checkDownloadToken} from './lib/util/security.js'
import {createStorage, getStorage, askForScan} from './lib/models/storage.js'
import {findDataItemsByScan, itemExists} from './lib/models/tree-item.js'
import {generateGeoJson} from './lib/geojson.js'
import {downloadFile} from './lib/file.js'
await mongo.connect()
const PORT = process.env.PORT || 5000
const {ROOT_URL, NODE_ENV} = process.env
const app = express()
app.set('view engine', 'ejs')
if (NODE_ENV !== 'production') {
app.use(morgan('dev'))
}
app.use(cors({origin: true}))
app.use(express.json())
app.use(readToken)
app.post('/storages', ensureAdmin, w(async (req, res) => {
const storage = await createStorage(req.body)
res.send(storage)
}))
app.param('storageId', w(async (req, res, next) => {
let storageId
try {
storageId = mongo.asObjectId(req.params.storageId)
} catch {
return next(createError(404, 'Invalid storage identifier'))
}
req.storage = await getStorage(storageId)
if (!req.storage) {
return next(createError(404, 'Storage not found'))
}
next()
}))
app.get('/storages/:storageId', w(async (req, res) => {
const storage = req.isAdmin
? req.storage
: omit(req.storage, ['params'])
res.send(storage)
}))
app.post('/storages/:storageId/scan', ensureAdmin, w(async (req, res) => {
const storage = await askForScan(req.storage._id)
res.send(storage)
}))
app.post('/storages/:storageId/generate-download-token', ensureAdmin, w(async (req, res) => {
const token = computeDownloadToken({storage: req.storage._id})
res.send({token})
}))
app.get('/storages/:storageId/data', w(async (req, res) => {
if (!req.storage.result?.lastSuccessfulScan) {
throw createError(404, 'No successful scan found')
}
const {lastSuccessfulScan} = req.storage.result
const dataItems = await findDataItemsByScan({_scan: lastSuccessfulScan, _storage: req.storage._id})
res.send(dataItems)
}))
app.get('/storages/:storageId/geojson', w(async (req, res) => {
if (!req.storage.result?.lastSuccessfulScan) {
throw createError(404, 'No successful scan found')
}
const {lastSuccessfulScan} = req.storage.result
const dataItems = await findDataItemsByScan({_scan: lastSuccessfulScan, _storage: req.storage._id})
const fc = generateGeoJson(dataItems)
res.send(fc)
}))
app.get('/storages/:storageId/preview', w(async (req, res) => {
if (!ROOT_URL) {
throw createError(500, 'Not configured')
}
if (!req.storage.result?.lastSuccessfulScan) {
throw createError(404, 'No successful scan found')
}
const {lastSuccessfulScan} = req.storage.result
const data = await findDataItemsByScan({_scan: lastSuccessfulScan, _storage: req.storage._id})
res.render('preview', {
data: data.filter(i => i.computedMetadata),
storageId: req.storage._id,
rootUrl: ROOT_URL
})
}))
app.get('/storages/:storageId/preview-map', w(async (req, res) => {
if (!ROOT_URL) {
throw createError(500, 'Not configured')
}
if (!req.storage.result?.lastSuccessfulScan) {
throw createError(404, 'No successful scan found')
}
const geojsonUrl = `${ROOT_URL}/storages/${req.storage._id}/geojson`
res.redirect(`https://geojson.io/#data=data:text/x-url,${encodeURIComponent(geojsonUrl)}`)
}))
app.get('/storages/:storageId/files/*', w(async (req, res) => {
const canDownload = checkDownloadToken(
req.query.token,
{storage: req.storage._id}
)
if (!canDownload) {
throw createError(403, 'Token not valid')
}
const parts = req.path.split('/').slice(4)
const fullPath = sanitizePath('/' + parts.join('/'))
const _storage = req.storage._id
if (!(await itemExists({_storage, fullPath}))) {
throw createError(404, 'File not found')
}
await downloadFile(req.storage, fullPath, req, res)
}))
app.use(errorHandler)
app.listen(PORT, () => {
console.log(`Start listening on port ${PORT}`)
})