1 import * as express from 'express'
2 import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares'
3 import { addVideoCaptionValidator, deleteVideoCaptionValidator, listVideoCaptionsValidator } from '../../../middlewares/validators'
4 import { createReqFiles } from '../../../helpers/express-utils'
5 import { MIMETYPES } from '../../../initializers/constants'
6 import { getFormattedObjects } from '../../../helpers/utils'
7 import { VideoCaptionModel } from '../../../models/video/video-caption'
8 import { logger } from '../../../helpers/logger'
9 import { federateVideoIfNeeded } from '../../../lib/activitypub'
10 import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils'
11 import { CONFIG } from '../../../initializers/config'
12 import { sequelizeTypescript } from '../../../initializers/database'
14 const reqVideoCaptionAdd = createReqFiles(
16 MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT,
18 captionfile: CONFIG.STORAGE.CAPTIONS_DIR
22 const videoCaptionsRouter = express.Router()
24 videoCaptionsRouter.get('/:videoId/captions',
25 asyncMiddleware(listVideoCaptionsValidator),
26 asyncMiddleware(listVideoCaptions)
28 videoCaptionsRouter.put('/:videoId/captions/:captionLanguage',
31 asyncMiddleware(addVideoCaptionValidator),
32 asyncRetryTransactionMiddleware(addVideoCaption)
34 videoCaptionsRouter.delete('/:videoId/captions/:captionLanguage',
36 asyncMiddleware(deleteVideoCaptionValidator),
37 asyncRetryTransactionMiddleware(deleteVideoCaption)
40 // ---------------------------------------------------------------------------
46 // ---------------------------------------------------------------------------
48 async function listVideoCaptions (req: express.Request, res: express.Response) {
49 const data = await VideoCaptionModel.listVideoCaptions(res.locals.video.id)
51 return res.json(getFormattedObjects(data, data.length))
54 async function addVideoCaption (req: express.Request, res: express.Response) {
55 const videoCaptionPhysicalFile = req.files['captionfile'][0]
56 const video = res.locals.video
58 const videoCaption = new VideoCaptionModel({
60 language: req.params.captionLanguage
62 videoCaption.Video = video
65 await moveAndProcessCaptionFile(videoCaptionPhysicalFile, videoCaption)
67 await sequelizeTypescript.transaction(async t => {
68 await VideoCaptionModel.insertOrReplaceLanguage(video.id, req.params.captionLanguage, t)
70 // Update video update
71 await federateVideoIfNeeded(video, false, t)
74 return res.status(204).end()
77 async function deleteVideoCaption (req: express.Request, res: express.Response) {
78 const video = res.locals.video
79 const videoCaption = res.locals.videoCaption
81 await sequelizeTypescript.transaction(async t => {
82 await videoCaption.destroy({ transaction: t })
85 await federateVideoIfNeeded(video, false, t)
88 logger.info('Video caption %s of video %s deleted.', videoCaption.language, video.uuid)
90 return res.type('json').status(204).end()