]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares' | |
3 | import { | |
4 | addVideoCaptionValidator, | |
5 | deleteVideoCaptionValidator, | |
6 | listVideoCaptionsValidator | |
7 | } from '../../../middlewares/validators/video-captions' | |
8 | import { createReqFiles } from '../../../helpers/express-utils' | |
9 | import { CONFIG, sequelizeTypescript, VIDEO_CAPTIONS_MIMETYPE_EXT } from '../../../initializers' | |
10 | import { getFormattedObjects } from '../../../helpers/utils' | |
11 | import { VideoCaptionModel } from '../../../models/video/video-caption' | |
12 | import { VideoModel } from '../../../models/video/video' | |
13 | import { logger } from '../../../helpers/logger' | |
14 | import { federateVideoIfNeeded } from '../../../lib/activitypub' | |
15 | import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils' | |
16 | ||
17 | const reqVideoCaptionAdd = createReqFiles( | |
18 | [ 'captionfile' ], | |
19 | VIDEO_CAPTIONS_MIMETYPE_EXT, | |
20 | { | |
21 | captionfile: CONFIG.STORAGE.CAPTIONS_DIR | |
22 | } | |
23 | ) | |
24 | ||
25 | const videoCaptionsRouter = express.Router() | |
26 | ||
27 | videoCaptionsRouter.get('/:videoId/captions', | |
28 | asyncMiddleware(listVideoCaptionsValidator), | |
29 | asyncMiddleware(listVideoCaptions) | |
30 | ) | |
31 | videoCaptionsRouter.put('/:videoId/captions/:captionLanguage', | |
32 | authenticate, | |
33 | reqVideoCaptionAdd, | |
34 | asyncMiddleware(addVideoCaptionValidator), | |
35 | asyncRetryTransactionMiddleware(addVideoCaption) | |
36 | ) | |
37 | videoCaptionsRouter.delete('/:videoId/captions/:captionLanguage', | |
38 | authenticate, | |
39 | asyncMiddleware(deleteVideoCaptionValidator), | |
40 | asyncRetryTransactionMiddleware(deleteVideoCaption) | |
41 | ) | |
42 | ||
43 | // --------------------------------------------------------------------------- | |
44 | ||
45 | export { | |
46 | videoCaptionsRouter | |
47 | } | |
48 | ||
49 | // --------------------------------------------------------------------------- | |
50 | ||
51 | async function listVideoCaptions (req: express.Request, res: express.Response) { | |
52 | const data = await VideoCaptionModel.listVideoCaptions(res.locals.video.id) | |
53 | ||
54 | return res.json(getFormattedObjects(data, data.length)) | |
55 | } | |
56 | ||
57 | async function addVideoCaption (req: express.Request, res: express.Response) { | |
58 | const videoCaptionPhysicalFile = req.files['captionfile'][0] | |
59 | const video = res.locals.video as VideoModel | |
60 | ||
61 | const videoCaption = new VideoCaptionModel({ | |
62 | videoId: video.id, | |
63 | language: req.params.captionLanguage | |
64 | }) | |
65 | videoCaption.Video = video | |
66 | ||
67 | // Move physical file | |
68 | await moveAndProcessCaptionFile(videoCaptionPhysicalFile, videoCaption) | |
69 | ||
70 | await sequelizeTypescript.transaction(async t => { | |
71 | await VideoCaptionModel.insertOrReplaceLanguage(video.id, req.params.captionLanguage, t) | |
72 | ||
73 | // Update video update | |
74 | await federateVideoIfNeeded(video, false, t) | |
75 | }) | |
76 | ||
77 | return res.status(204).end() | |
78 | } | |
79 | ||
80 | async function deleteVideoCaption (req: express.Request, res: express.Response) { | |
81 | const video = res.locals.video as VideoModel | |
82 | const videoCaption = res.locals.videoCaption as VideoCaptionModel | |
83 | ||
84 | await sequelizeTypescript.transaction(async t => { | |
85 | await videoCaption.destroy({ transaction: t }) | |
86 | ||
87 | // Send video update | |
88 | await federateVideoIfNeeded(video, false, t) | |
89 | }) | |
90 | ||
91 | logger.info('Video caption %s of video %s deleted.', videoCaption.language, video.uuid) | |
92 | ||
93 | return res.type('json').status(204).end() | |
94 | } |