-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathprofile-videos.ts
More file actions
250 lines (220 loc) · 8.39 KB
/
Copy pathprofile-videos.ts
File metadata and controls
250 lines (220 loc) · 8.39 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import { Type } from '@sinclair/typebox';
import type { Static } from '@sinclair/typebox';
import moment from 'moment';
import type ConfigStateless from '../config.js';
import Schema from '@openaddresses/batch-schema';
import Err from '@openaddresses/batch-error';
import Auth, { AuthUserAccess } from '../../common/auth.js';
import { StandardResponse, ProfileVideoResponse, ProfileVideoPosition } from '../../common/types.js';
import { sql } from 'drizzle-orm';
import { randomUUID } from 'node:crypto';
import ECSVideoControl from '../lib/control/video-service.js';
import * as Default from '../lib/limits.js';
export default async function router(schema: Schema, config: ConfigStateless) {
const videoControl = new ECSVideoControl(config);
await schema.get('/profile/video', {
name: 'Get Videos',
group: 'ProfileVideo',
description: `
Return a list of Profile Videos
`,
query: Type.Object({
limit: Type.Integer({ default: 1000 }),
page: Default.Page,
order: Default.Order,
}),
res: Type.Object({
total: Type.Integer(),
items: Type.Array(ProfileVideoResponse),
}),
}, async (req, res) => {
try {
const user = await Auth.as_user(config, req, { token: true });
const list = await config.models.ProfileVideo.list({
limit: req.query.limit,
page: req.query.page,
order: req.query.order,
where: sql`
username = ${user.email}
`,
});
res.json(list);
} catch (err) {
Err.respond(err, res);
}
});
await schema.post('/profile/video', {
name: 'Create Video',
group: 'ProfileVideo',
description: `
Push a new Profile Video to the Video Wall
Either an existing Video Lease ID or a Stream URL must be provided.
If a URL is provided it is resolved to an existing lease where possible,
otherwise a proxy lease is created on behalf of the user.
`,
body: Type.Object({
lease: Type.Optional(Type.Integer({ description: 'Existing Video Lease ID' })),
url: Type.Optional(Type.String({ description: 'Video Stream URL' })),
name: Type.Optional(Type.String({ description: 'Human readable name - used if a new proxy lease is created' })),
position: Type.Optional(ProfileVideoPosition),
}),
res: ProfileVideoResponse,
}, async (req, res) => {
try {
const user = await Auth.as_user(config, req);
const admin = user.access === AuthUserAccess.ADMIN;
let lease: number | undefined = undefined;
if (req.body.lease !== undefined) {
lease = (await videoControl.from(req.body.lease, {
username: user.email,
admin,
})).id;
} else if (req.body.url !== undefined) {
let requested: URL;
try {
requested = new URL(req.body.url);
} catch (err) {
throw new Err(400, err instanceof Error ? err : new Error(String(err)), 'Invalid Video Stream URL');
}
const media = await videoControl.url();
// The lease path lives in the URL path for HLS/WebRTC/RTSP and in the streamid query for SRT
const uuid = req.body.url.match(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/);
if (media && media.hostname === requested.hostname && uuid && uuid[0]) {
try {
lease = (await videoControl.from(uuid[0], {
username: user.email,
admin,
})).id;
} catch (err) {
if (!(err instanceof Err)) throw err;
// The URL points at a lease the user cannot access directly
// Fall through and create a user owned proxy lease
}
}
if (lease === undefined) {
lease = (await videoControl.generate({
name: req.body.name || 'Video Wall Stream',
ephemeral: true,
expiration: moment().add(24, 'hours').toISOString(),
source_id: null,
path: randomUUID(),
username: user.email,
recording: false,
publish: false,
secure: false,
share: false,
proxy: req.body.url,
})).id;
}
} else {
throw new Err(400, null, 'Either a lease or url must be provided');
}
const existing = await config.models.ProfileVideo.list({
where: sql`
username = ${user.email}
AND lease = ${lease}
`,
});
if (existing.total > 0) {
res.json(existing.items[0]);
return;
}
let position: Static<typeof ProfileVideoPosition> | undefined = req.body.position;
if (!position) {
const videos = await config.models.ProfileVideo.list({
limit: 1000,
where: sql`
username = ${user.email}
`,
});
// Place new videos at the bottom of the wall
let y = 0;
for (const video of videos.items) {
y = Math.max(y, video.position.y + video.position.h);
}
position = { x: 0, y, w: 4, h: 6 };
}
const video = await config.models.ProfileVideo.generate({
lease,
position,
username: user.email,
});
res.json(video);
} catch (err) {
Err.respond(err, res);
}
});
await schema.patch('/profile/video/:id', {
name: 'Update Video',
group: 'ProfileVideo',
description: `
Update a Profile Video - used to persist Video Wall placement
`,
params: Type.Object({
id: Type.String(),
}),
body: Type.Object({
position: Type.Optional(ProfileVideoPosition),
}),
res: ProfileVideoResponse,
}, async (req, res) => {
try {
const user = await Auth.as_user(config, req);
const video = await config.models.ProfileVideo.from(sql`
id = ${req.params.id} AND username = ${user.email}
`);
const updated = await config.models.ProfileVideo.commit(video.id, {
...req.body,
updated: sql`Now()`,
});
res.json(updated);
} catch (err) {
Err.respond(err, res);
}
});
await schema.delete('/profile/video/:id', {
name: 'Delete Video',
group: 'ProfileVideo',
description: `
Delete a Video
`,
params: Type.Object({
id: Type.String(),
}),
res: StandardResponse,
}, async (req, res) => {
try {
const user = await Auth.as_user(config, req);
await config.models.ProfileVideo.delete(sql`
id = ${req.params.id} AND username = ${user.email}
`);
res.json({
status: 200,
message: 'Video Deleted',
});
} catch (err) {
Err.respond(err, res);
}
});
await schema.get('/profile/video/:id', {
name: 'Get Video',
group: 'ProfileVideo',
description: `
Get a video
`,
params: Type.Object({
id: Type.String(),
}),
res: ProfileVideoResponse,
}, async (req, res) => {
try {
const user = await Auth.as_user(config, req);
const video = await config.models.ProfileVideo.from(sql`
id = ${req.params.id} AND username = ${user.email}
`);
res.json(video);
} catch (err) {
Err.respond(err, res);
}
});
}