-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
74 lines (57 loc) · 2.11 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
#!/usr/bin/env node
require('dotenv').config()
const express = require('express')
const cors = require('cors')
const morgan = require('morgan')
const {Client} = require('./lib/api-client')
const {transformIntoDcat} = require('./lib/dcat')
const {transformIntoDebugLog, transformIntoDebugPage} = require('./lib/debug')
const w = require('./lib/w')
const apiClient = new Client()
const app = express()
if (process.env.NODE_ENV !== 'production') {
app.use(morgan('dev'))
}
app.use(cors({origin: true}))
app.use('/:shareId/:shareToken', w(async (req, res, next) => {
const {shareId, shareToken} = req.params
if (shareId && shareToken) {
const share = await apiClient.getShare(shareId)
if (!share || share.urlToken !== shareToken) {
return res.sendStatus(404)
}
req.share = share
}
next()
}))
app.get('/:shareId/:shareToken', w(async (req, res) => {
const {shareId, shareToken} = req.params
const resourcesStream = await apiClient.getResourcesStream(shareId)
res.type('application/json')
resourcesStream.pipe(transformIntoDcat({shareId, shareToken})).pipe(res)
}))
app.get('/:shareId/:shareToken/debug-log', w(async (req, res) => {
const {shareId, shareToken} = req.params
const resourcesStream = await apiClient.getResourcesStream(shareId)
res.type('text/plain')
resourcesStream.pipe(transformIntoDebugLog({shareId, shareToken})).pipe(res)
}))
app.get('/:shareId/:shareToken/debug-page', w(async (req, res) => {
const {shareId, shareToken} = req.params
const resourcesStream = await apiClient.getResourcesStream(shareId)
res.type('text/html')
resourcesStream.pipe(transformIntoDebugPage({shareId, shareToken})).pipe(res)
}))
app.get('/:shareId/:shareToken/download/:resourceId/:linkId', w(async (req, res) => {
const {resourceId, linkId} = req.params
const downloadStream = await apiClient.getDownloadStream(resourceId, linkId)
downloadStream.on('error', function(e){
res.status(404)
res.json({"error" : "File doesn't exist"
})})
downloadStream.pipe(res)
}))
const port = process.env.PORT || 5000
app.listen(port, () => {
console.log(`Start listening on port ${port}`)
})