1 import * as express from 'express'
2 import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares'
4 addVideoCaptionValidator,
5 deleteVideoCaptionValidator,
6 listVideoCaptionsValidator
7 } from '../../../middlewares/validators/video-captions'
8 import { createReqFiles } from '../../../helpers/express-utils'
9 import { CONFIG, sequelizeTypescript, VIDEO_CAPTIONS_MIMETYPE_EXT } from '../../../initializers'
10 import { getFormattedObjects } from '../../../helpers/utils'
11 import { VideoCaptionModel } from '../../../models/video/video-caption'
12 import { VideoModel } from '../../../models/video/video'
13 import { logger } from '../../../helpers/logger'
14 import { federateVideoIfNeeded } from '../../../lib/activitypub'
15 import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils'
17 const reqVideoCaptionAdd = createReqFiles(
19 VIDEO_CAPTIONS_MIMETYPE_EXT,
21 captionfile: CONFIG.STORAGE.CAPTIONS_DIR
25 const videoCaptionsRouter = express.Router()
27 videoCaptionsRouter.get('/:videoId/captions',
28 asyncMiddleware(listVideoCaptionsValidator),
29 asyncMiddleware(listVideoCaptions)
31 videoCaptionsRouter.put('/:videoId/captions/:captionLanguage',
34 asyncMiddleware(addVideoCaptionValidator),
35 asyncRetryTransactionMiddleware(addVideoCaption)
37 videoCaptionsRouter.delete('/:videoId/captions/:captionLanguage',
39 asyncMiddleware(deleteVideoCaptionValidator),
40 asyncRetryTransactionMiddleware(deleteVideoCaption)
43 // ---------------------------------------------------------------------------
49 // ---------------------------------------------------------------------------
51 async function listVideoCaptions (req: express.Request, res: express.Response) {
52 const data = await VideoCaptionModel.listVideoCaptions(res.locals.video.id)
54 return res.json(getFormattedObjects(data, data.length))
57 async function addVideoCaption (req: express.Request, res: express.Response) {
58 const videoCaptionPhysicalFile = req.files['captionfile'][0]
59 const video = res.locals.video as VideoModel
61 const videoCaption = new VideoCaptionModel({
63 language: req.params.captionLanguage
65 videoCaption.Video = video
68 await moveAndProcessCaptionFile(videoCaptionPhysicalFile, videoCaption)
70 await sequelizeTypescript.transaction(async t => {
71 await VideoCaptionModel.insertOrReplaceLanguage(video.id, req.params.captionLanguage, t)
73 // Update video update
74 await federateVideoIfNeeded(video, false, t)
77 return res.status(204).end()
80 async function deleteVideoCaption (req: express.Request, res: express.Response) {
81 const video = res.locals.video as VideoModel
82 const videoCaption = res.locals.videoCaption as VideoCaptionModel
84 await sequelizeTypescript.transaction(async t => {
85 await videoCaption.destroy({ transaction: t })
88 await federateVideoIfNeeded(video, false, t)
91 logger.info('Video caption %s of video %s deleted.', videoCaption.language, video.uuid)
93 return res.type('json').status(204).end()