]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
d3d3deaa | 2 | import { Hooks } from '@server/lib/plugins/hooks' |
6302d599 | 3 | import { MVideoCaption } from '@server/types/models' |
c0e8b12e | 4 | import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' |
6302d599 | 5 | import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils' |
40e87e9e | 6 | import { createReqFiles } from '../../../helpers/express-utils' |
40e87e9e | 7 | import { logger } from '../../../helpers/logger' |
6302d599 | 8 | import { getFormattedObjects } from '../../../helpers/utils' |
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' | |
40e87e9e | 15 | |
d3d3deaa | 16 | const reqVideoCaptionAdd = createReqFiles([ 'captionfile' ], MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT) |
40e87e9e C |
17 | |
18 | const videoCaptionsRouter = express.Router() | |
19 | ||
20 | videoCaptionsRouter.get('/:videoId/captions', | |
21 | asyncMiddleware(listVideoCaptionsValidator), | |
22 | asyncMiddleware(listVideoCaptions) | |
23 | ) | |
24 | videoCaptionsRouter.put('/:videoId/captions/:captionLanguage', | |
25 | authenticate, | |
26 | reqVideoCaptionAdd, | |
27 | asyncMiddleware(addVideoCaptionValidator), | |
28 | asyncRetryTransactionMiddleware(addVideoCaption) | |
29 | ) | |
30 | videoCaptionsRouter.delete('/:videoId/captions/:captionLanguage', | |
31 | authenticate, | |
32 | asyncMiddleware(deleteVideoCaptionValidator), | |
33 | asyncRetryTransactionMiddleware(deleteVideoCaption) | |
34 | ) | |
35 | ||
36 | // --------------------------------------------------------------------------- | |
37 | ||
38 | export { | |
39 | videoCaptionsRouter | |
40 | } | |
41 | ||
42 | // --------------------------------------------------------------------------- | |
43 | ||
44 | async function listVideoCaptions (req: express.Request, res: express.Response) { | |
795212f7 | 45 | const data = await VideoCaptionModel.listVideoCaptions(res.locals.onlyVideo.id) |
40e87e9e C |
46 | |
47 | return res.json(getFormattedObjects(data, data.length)) | |
48 | } | |
49 | ||
50 | async function addVideoCaption (req: express.Request, res: express.Response) { | |
51 | const videoCaptionPhysicalFile = req.files['captionfile'][0] | |
453e83ea | 52 | const video = res.locals.videoAll |
40e87e9e | 53 | |
6302d599 C |
54 | const captionLanguage = req.params.captionLanguage |
55 | ||
40e87e9e C |
56 | const videoCaption = new VideoCaptionModel({ |
57 | videoId: video.id, | |
6302d599 C |
58 | filename: VideoCaptionModel.generateCaptionName(captionLanguage), |
59 | language: captionLanguage | |
60 | }) as MVideoCaption | |
40e87e9e C |
61 | |
62 | // Move physical file | |
f4001cf4 | 63 | await moveAndProcessCaptionFile(videoCaptionPhysicalFile, videoCaption) |
40e87e9e C |
64 | |
65 | await sequelizeTypescript.transaction(async t => { | |
6302d599 | 66 | await VideoCaptionModel.insertOrReplaceLanguage(videoCaption, t) |
40e87e9e C |
67 | |
68 | // Update video update | |
69 | await federateVideoIfNeeded(video, false, t) | |
70 | }) | |
71 | ||
5e3d29ab | 72 | Hooks.runAction('action:api.video-caption.created', { caption: videoCaption, req, res }) |
73 | ||
2d53be02 | 74 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
40e87e9e C |
75 | } |
76 | ||
77 | async function deleteVideoCaption (req: express.Request, res: express.Response) { | |
453e83ea | 78 | const video = res.locals.videoAll |
dae86118 | 79 | const videoCaption = res.locals.videoCaption |
40e87e9e C |
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 | ||
5e3d29ab | 90 | Hooks.runAction('action:api.video-caption.deleted', { caption: videoCaption, req, res }) |
91 | ||
2d53be02 | 92 | return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() |
40e87e9e | 93 | } |