generated from garasingulik/typescript-express-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream.ts
52 lines (40 loc) · 1.56 KB
/
stream.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
import * as express from 'express'
import * as iotsReporters from 'io-ts-reporters'
import * as T from '../lib/types'
import config from '../config'
import { convertStream, stopStream } from '../lib/ffmpeg'
import { authenticationMiddleware } from './authorization'
export const StreamRoutes = {
register: (app: express.Application) => {
app.post('/stream/convert', authenticationMiddleware, async (req, res) => {
const data = T.parseData<T.StreamConvertRequestType>(req.body, T.StreamConvertRequest)
if (T.isParseError(data)) {
return res.status(400).json({
errors: iotsReporters.default.report(data)
})
}
let sourceUrl = data.url
if (data.url.includes('?')) {
sourceUrl = data.url.split('?')[0]
}
const streamId = T.getStreamId(sourceUrl)
const outputPath = await T.wrapPromise(convertStream(data.url, streamId))
return res.json({ url: `${config.url}/static/${outputPath}` })
})
app.post('/stream/stop', authenticationMiddleware, async (req, res) => {
const data = T.parseData<T.StreamConvertRequestType>(req.body, T.StreamConvertRequest)
if (T.isParseError(data)) {
return res.status(400).json({
errors: iotsReporters.default.report(data)
})
}
let sourceUrl = data.url
if (data.url.includes('?')) {
sourceUrl = data.url.split('?')[0]
}
const streamId = T.getStreamId(sourceUrl)
const result = await T.wrapPromise(stopStream(streamId))
return res.json({ success: `${result}` })
})
}
}