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