]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
6302d599 | 2 | import { MVideoCaption } from '@server/types/models' |
c0e8b12e | 3 | import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' |
6302d599 | 4 | import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils' |
40e87e9e | 5 | import { createReqFiles } from '../../../helpers/express-utils' |
40e87e9e | 6 | import { logger } from '../../../helpers/logger' |
6302d599 | 7 | import { getFormattedObjects } from '../../../helpers/utils' |
6dd9de95 | 8 | import { CONFIG } from '../../../initializers/config' |
6302d599 | 9 | import { MIMETYPES } from '../../../initializers/constants' |
74dc3bca | 10 | import { sequelizeTypescript } from '../../../initializers/database' |
6302d599 C |
11 | import { federateVideoIfNeeded } from '../../../lib/activitypub/videos' |
12 | import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares' | |
13 | import { addVideoCaptionValidator, deleteVideoCaptionValidator, listVideoCaptionsValidator } from '../../../middlewares/validators' | |
14 | import { VideoCaptionModel } from '../../../models/video/video-caption' | |
5e3d29ab | 15 | import { Hooks } from '@server/lib/plugins/hooks' |
40e87e9e C |
16 | |
17 | const reqVideoCaptionAdd = createReqFiles( | |
18 | [ 'captionfile' ], | |
14e2014a | 19 | MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT, |
40e87e9e C |
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) { | |
453e83ea | 52 | const data = await VideoCaptionModel.listVideoCaptions(res.locals.videoId.id) |
40e87e9e C |
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] | |
453e83ea | 59 | const video = res.locals.videoAll |
40e87e9e | 60 | |
6302d599 C |
61 | const captionLanguage = req.params.captionLanguage |
62 | ||
40e87e9e C |
63 | const videoCaption = new VideoCaptionModel({ |
64 | videoId: video.id, | |
6302d599 C |
65 | filename: VideoCaptionModel.generateCaptionName(captionLanguage), |
66 | language: captionLanguage | |
67 | }) as MVideoCaption | |
40e87e9e C |
68 | |
69 | // Move physical file | |
f4001cf4 | 70 | await moveAndProcessCaptionFile(videoCaptionPhysicalFile, videoCaption) |
40e87e9e C |
71 | |
72 | await sequelizeTypescript.transaction(async t => { | |
6302d599 | 73 | await VideoCaptionModel.insertOrReplaceLanguage(videoCaption, t) |
40e87e9e C |
74 | |
75 | // Update video update | |
76 | await federateVideoIfNeeded(video, false, t) | |
77 | }) | |
78 | ||
5e3d29ab | 79 | Hooks.runAction('action:api.video-caption.created', { caption: videoCaption, req, res }) |
80 | ||
2d53be02 | 81 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
40e87e9e C |
82 | } |
83 | ||
84 | async function deleteVideoCaption (req: express.Request, res: express.Response) { | |
453e83ea | 85 | const video = res.locals.videoAll |
dae86118 | 86 | const videoCaption = res.locals.videoCaption |
40e87e9e C |
87 | |
88 | await sequelizeTypescript.transaction(async t => { | |
89 | await videoCaption.destroy({ transaction: t }) | |
90 | ||
91 | // Send video update | |
92 | await federateVideoIfNeeded(video, false, t) | |
93 | }) | |
94 | ||
95 | logger.info('Video caption %s of video %s deleted.', videoCaption.language, video.uuid) | |
96 | ||
5e3d29ab | 97 | Hooks.runAction('action:api.video-caption.deleted', { caption: videoCaption, req, res }) |
98 | ||
2d53be02 | 99 | return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() |
40e87e9e | 100 | } |