]>
Commit | Line | Data |
---|---|---|
40e87e9e C |
1 | import * as express from 'express' |
2 | import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares' | |
6e46de09 | 3 | import { addVideoCaptionValidator, deleteVideoCaptionValidator, listVideoCaptionsValidator } from '../../../middlewares/validators' |
40e87e9e | 4 | import { createReqFiles } from '../../../helpers/express-utils' |
74dc3bca | 5 | import { MIMETYPES } from '../../../initializers/constants' |
40e87e9e C |
6 | import { getFormattedObjects } from '../../../helpers/utils' |
7 | import { VideoCaptionModel } from '../../../models/video/video-caption' | |
40e87e9e | 8 | import { logger } from '../../../helpers/logger' |
8dc8a34e | 9 | import { federateVideoIfNeeded } from '../../../lib/activitypub/videos' |
f4001cf4 | 10 | import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils' |
6dd9de95 | 11 | import { CONFIG } from '../../../initializers/config' |
74dc3bca | 12 | import { sequelizeTypescript } from '../../../initializers/database' |
26d6bf65 | 13 | import { MVideoCaptionVideo } from '@server/types/models' |
2d53be02 | 14 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' |
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 C |
59 | |
60 | const videoCaption = new VideoCaptionModel({ | |
61 | videoId: video.id, | |
62 | language: req.params.captionLanguage | |
453e83ea | 63 | }) as MVideoCaptionVideo |
40e87e9e C |
64 | videoCaption.Video = video |
65 | ||
66 | // Move physical file | |
f4001cf4 | 67 | await moveAndProcessCaptionFile(videoCaptionPhysicalFile, videoCaption) |
40e87e9e C |
68 | |
69 | await sequelizeTypescript.transaction(async t => { | |
ca6d3622 | 70 | await VideoCaptionModel.insertOrReplaceLanguage(video.id, req.params.captionLanguage, null, t) |
40e87e9e C |
71 | |
72 | // Update video update | |
73 | await federateVideoIfNeeded(video, false, t) | |
74 | }) | |
75 | ||
2d53be02 | 76 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
40e87e9e C |
77 | } |
78 | ||
79 | async function deleteVideoCaption (req: express.Request, res: express.Response) { | |
453e83ea | 80 | const video = res.locals.videoAll |
dae86118 | 81 | const videoCaption = res.locals.videoCaption |
40e87e9e C |
82 | |
83 | await sequelizeTypescript.transaction(async t => { | |
84 | await videoCaption.destroy({ transaction: t }) | |
85 | ||
86 | // Send video update | |
87 | await federateVideoIfNeeded(video, false, t) | |
88 | }) | |
89 | ||
90 | logger.info('Video caption %s of video %s deleted.', videoCaption.language, video.uuid) | |
91 | ||
2d53be02 | 92 | return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() |
40e87e9e | 93 | } |