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