forked from cartesapp/serveur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaceMap.js
45 lines (37 loc) · 1.39 KB
/
placeMap.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
import util from 'util'
import { exec as rawExec } from 'child_process'
export const exec = util.promisify(rawExec)
import fs from 'fs'
// TODO use local style to avoid a download every time
// const style = franceStyle(true)
const userDir = '/home/ubuntu'
const placeMapDir = userDir + '/.placeMapImages/'
export default function placeMapRoute(app) {
app.get('/placeMap', async (req, res) => {
const { zoom = 15, lat, lon, bearing = 0, pitch = 0 } = req.query
const hash =
placeMapDir + [zoom, lat, lon, bearing, pitch].join('-') + '.png'
try {
const file = fs.readFileSync(hash)
if (file) return res.sendFile(hash)
} catch (e) {
console.log('Caught error generating ' + hash)
}
try {
const { stdout, stderr } = await exec(
`xvfb-run -a ${userDir}/mbgl-render --style http://cartes.app/api/styles --output ${hash} -z ${zoom} -x ${lon} -y ${lat} -b ${bearing} -p ${pitch}` // && xdg-open out.png`
)
// const newFile = fs.readFileSync(hash)
/*
console.log('-------------------------------')
console.log('maplibre place map generation')
console.log('stdout:', stdout)
console.log('stderr:', stderr)
*/
return res.sendFile(hash)
} catch (e) {
// we don't want to pollute logs here with non impacting maplibre errors
// TODO what should we return ? A default image of cartes.app
}
})
}