]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/captions.ts
Begin advanced search
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / captions.ts
CommitLineData
40e87e9e
C
1import * as express from 'express'
2import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares'
3import {
4 addVideoCaptionValidator,
5 deleteVideoCaptionValidator,
6 listVideoCaptionsValidator
7} from '../../../middlewares/validators/video-captions'
8import { createReqFiles } from '../../../helpers/express-utils'
9import { CONFIG, sequelizeTypescript, VIDEO_CAPTIONS_MIMETYPE_EXT } from '../../../initializers'
10import { getFormattedObjects } from '../../../helpers/utils'
11import { VideoCaptionModel } from '../../../models/video/video-caption'
40e87e9e
C
12import { VideoModel } from '../../../models/video/video'
13import { logger } from '../../../helpers/logger'
14import { federateVideoIfNeeded } from '../../../lib/activitypub'
f4001cf4 15import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils'
40e87e9e
C
16
17const reqVideoCaptionAdd = createReqFiles(
18 [ 'captionfile' ],
19 VIDEO_CAPTIONS_MIMETYPE_EXT,
20 {
21 captionfile: CONFIG.STORAGE.CAPTIONS_DIR
22 }
23)
24
25const videoCaptionsRouter = express.Router()
26
27videoCaptionsRouter.get('/:videoId/captions',
28 asyncMiddleware(listVideoCaptionsValidator),
29 asyncMiddleware(listVideoCaptions)
30)
31videoCaptionsRouter.put('/:videoId/captions/:captionLanguage',
32 authenticate,
33 reqVideoCaptionAdd,
34 asyncMiddleware(addVideoCaptionValidator),
35 asyncRetryTransactionMiddleware(addVideoCaption)
36)
37videoCaptionsRouter.delete('/:videoId/captions/:captionLanguage',
38 authenticate,
39 asyncMiddleware(deleteVideoCaptionValidator),
40 asyncRetryTransactionMiddleware(deleteVideoCaption)
41)
42
43// ---------------------------------------------------------------------------
44
45export {
46 videoCaptionsRouter
47}
48
49// ---------------------------------------------------------------------------
50
51async function listVideoCaptions (req: express.Request, res: express.Response) {
52 const data = await VideoCaptionModel.listVideoCaptions(res.locals.video.id)
53
54 return res.json(getFormattedObjects(data, data.length))
55}
56
57async function addVideoCaption (req: express.Request, res: express.Response) {
58 const videoCaptionPhysicalFile = req.files['captionfile'][0]
59 const video = res.locals.video as VideoModel
60
61 const videoCaption = new VideoCaptionModel({
62 videoId: video.id,
63 language: req.params.captionLanguage
64 })
65 videoCaption.Video = video
66
67 // Move physical file
f4001cf4 68 await moveAndProcessCaptionFile(videoCaptionPhysicalFile, videoCaption)
40e87e9e
C
69
70 await sequelizeTypescript.transaction(async t => {
71 await VideoCaptionModel.insertOrReplaceLanguage(video.id, req.params.captionLanguage, t)
72
73 // Update video update
74 await federateVideoIfNeeded(video, false, t)
75 })
76
77 return res.status(204).end()
78}
79
80async function deleteVideoCaption (req: express.Request, res: express.Response) {
81 const video = res.locals.video as VideoModel
82 const videoCaption = res.locals.videoCaption as VideoCaptionModel
83
84 await sequelizeTypescript.transaction(async t => {
85 await videoCaption.destroy({ transaction: t })
86
87 // Send video update
88 await federateVideoIfNeeded(video, false, t)
89 })
90
91 logger.info('Video caption %s of video %s deleted.', videoCaption.language, video.uuid)
92
93 return res.type('json').status(204).end()
94}