-
Notifications
You must be signed in to change notification settings - Fork 79
/
index.ts
172 lines (147 loc) · 4.18 KB
/
index.ts
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
import {urlencoded} from "body-parser"
import cors from "cors"
import {randomBytes} from "crypto"
import express, {
Application,
ErrorRequestHandler,
RequestHandler,
Router,
} from "express"
import PromiseRouter from "express-promise-router"
import {unlink} from "fs"
import multer, {diskStorage} from "multer"
import ogr2ogr from "ogr2ogr"
import {join} from "path"
export interface OgreOpts {
port?: number
timeout?: number
limit?: number
}
class Ogre {
app: Application
private timeout: number
private port: number
private limit: number
constructor({
port = 3000,
timeout = 150000,
limit = 50000000,
}: OgreOpts = {}) {
this.port = port
this.timeout = timeout
this.limit = limit
this.app = express()
this.app.use(express.static(join(__dirname, "/public")))
this.app.use(this.router())
}
router(): Router {
let router = PromiseRouter()
let upload = multer({
storage: diskStorage({
filename(_req, file, cb) {
randomBytes(16, (err, raw) =>
cb(err, raw.toString("hex") + file.originalname),
)
},
}),
limits: {fileSize: this.limit},
})
let encoded = urlencoded({extended: false, limit: this.limit})
router.options("/", this.heartbeat())
router.use(cors())
router.post("/convert", upload.single("upload"), this.convert())
router.post("/convertJson", encoded, this.convertJson())
router.use(this.notFound())
router.use(this.serverError())
return router
}
start(): void {
let server = this.app.listen(this.port)
let handler = (): void => {
server.close(() => process.exit())
setTimeout(process.exit, 30 * 1000)
}
process.on("SIGINT", handler)
process.on("SIGTERM", handler)
}
private heartbeat = (): RequestHandler => (_req, res) => {
res.sendStatus(200)
}
private notFound = (): RequestHandler => (_req, res) => {
res.status(404).send({error: "Not found"})
}
private serverError = (): ErrorRequestHandler => (er, _req, res, _next) => {
console.error(er.stack)
res.status(500).send({error: true, message: er.message})
}
private convert = (): RequestHandler => async (req, res) => {
if (!req.file) {
res.status(400).json({error: true, msg: "No file provided"})
return
}
let b = req.body
let opts = {
timeout: this.timeout,
options: [] as string[],
maxBuffer: this.limit * 10,
}
if (b.targetSrs) opts.options.push("-t_srs", b.targetSrs)
if (b.sourceSrs) opts.options.push("-s_srs", b.sourceSrs)
if ("rfc7946" in b) opts.options.push("-lco", "RFC7946=YES")
res.header(
"Content-Type",
"forcePlainText" in b
? "text/plain; charset=utf-8"
: "application/json; charset=utf-8",
)
if ("forceDownload" in b) res.attachment()
try {
let {data} = await ogr2ogr(req.file.path, opts)
if (b.callback) res.write(b.callback + "(")
res.write(JSON.stringify(data))
if (b.callback) res.write(")")
res.end()
} finally {
unlink(req.file.path, (er) => {
if (er) console.error("unlink file error", er.message)
})
}
}
private convertJson = (): RequestHandler => async (req, res) => {
let b = req.body
if (!b.jsonUrl && !b.json) {
res.status(400).json({error: true, msg: "No json provided"})
return
}
let data
if (b.json) {
try {
data = JSON.parse(b.json)
} catch (er) {
res.status(400).json({error: true, msg: "Invalid json provided"})
return
}
}
let input = b.jsonUrl || data
let output = b.outputName || "ogre"
let opts = {
format: (b.format || "ESRI Shapefile").toLowerCase(),
timeout: this.timeout,
options: [] as string[],
maxBuffer: this.limit * 10,
}
if (b.outputName) opts.options.push("-nln", b.outputName)
if ("forceUTF8" in b) opts.options.push("-lco", "ENCODING=UTF-8")
let out = await ogr2ogr(input, opts)
res.attachment(output + out.extname)
if (out.stream) {
out.stream.pipe(res)
return
} else if (out.text) {
res.send(out.text)
} else {
res.send(out.data)
}
}
}
export default Ogre