]>
Commit | Line | Data |
---|---|---|
40e87e9e C |
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 { renamePromise } from '../../../helpers/core-utils' | |
13 | import { join } from 'path' | |
14 | import { VideoModel } from '../../../models/video/video' | |
15 | import { logger } from '../../../helpers/logger' | |
16 | import { federateVideoIfNeeded } from '../../../lib/activitypub' | |
17 | ||
18 | const reqVideoCaptionAdd = createReqFiles( | |
19 | [ 'captionfile' ], | |
20 | VIDEO_CAPTIONS_MIMETYPE_EXT, | |
21 | { | |
22 | captionfile: CONFIG.STORAGE.CAPTIONS_DIR | |
23 | } | |
24 | ) | |
25 | ||
26 | const videoCaptionsRouter = express.Router() | |
27 | ||
28 | videoCaptionsRouter.get('/:videoId/captions', | |
29 | asyncMiddleware(listVideoCaptionsValidator), | |
30 | asyncMiddleware(listVideoCaptions) | |
31 | ) | |
32 | videoCaptionsRouter.put('/:videoId/captions/:captionLanguage', | |
33 | authenticate, | |
34 | reqVideoCaptionAdd, | |
35 | asyncMiddleware(addVideoCaptionValidator), | |
36 | asyncRetryTransactionMiddleware(addVideoCaption) | |
37 | ) | |
38 | videoCaptionsRouter.delete('/:videoId/captions/:captionLanguage', | |
39 | authenticate, | |
40 | asyncMiddleware(deleteVideoCaptionValidator), | |
41 | asyncRetryTransactionMiddleware(deleteVideoCaption) | |
42 | ) | |
43 | ||
44 | // --------------------------------------------------------------------------- | |
45 | ||
46 | export { | |
47 | videoCaptionsRouter | |
48 | } | |
49 | ||
50 | // --------------------------------------------------------------------------- | |
51 | ||
52 | async function listVideoCaptions (req: express.Request, res: express.Response) { | |
53 | const data = await VideoCaptionModel.listVideoCaptions(res.locals.video.id) | |
54 | ||
55 | return res.json(getFormattedObjects(data, data.length)) | |
56 | } | |
57 | ||
58 | async function addVideoCaption (req: express.Request, res: express.Response) { | |
59 | const videoCaptionPhysicalFile = req.files['captionfile'][0] | |
60 | const video = res.locals.video as VideoModel | |
61 | ||
62 | const videoCaption = new VideoCaptionModel({ | |
63 | videoId: video.id, | |
64 | language: req.params.captionLanguage | |
65 | }) | |
66 | videoCaption.Video = video | |
67 | ||
68 | // Move physical file | |
69 | const videoCaptionsDir = CONFIG.STORAGE.CAPTIONS_DIR | |
70 | const destination = join(videoCaptionsDir, videoCaption.getCaptionName()) | |
71 | await renamePromise(videoCaptionPhysicalFile.path, destination) | |
72 | // This is important in case if there is another attempt in the retry process | |
73 | videoCaptionPhysicalFile.filename = videoCaption.getCaptionName() | |
74 | videoCaptionPhysicalFile.path = destination | |
75 | ||
76 | await sequelizeTypescript.transaction(async t => { | |
77 | await VideoCaptionModel.insertOrReplaceLanguage(video.id, req.params.captionLanguage, t) | |
78 | ||
79 | // Update video update | |
80 | await federateVideoIfNeeded(video, false, t) | |
81 | }) | |
82 | ||
83 | return res.status(204).end() | |
84 | } | |
85 | ||
86 | async function deleteVideoCaption (req: express.Request, res: express.Response) { | |
87 | const video = res.locals.video as VideoModel | |
88 | const videoCaption = res.locals.videoCaption as VideoCaptionModel | |
89 | ||
90 | await sequelizeTypescript.transaction(async t => { | |
91 | await videoCaption.destroy({ transaction: t }) | |
92 | ||
93 | // Send video update | |
94 | await federateVideoIfNeeded(video, false, t) | |
95 | }) | |
96 | ||
97 | logger.info('Video caption %s of video %s deleted.', videoCaption.language, video.uuid) | |
98 | ||
99 | return res.type('json').status(204).end() | |
100 | } |