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