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