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'
13 import { MVideoCaptionVideo } from '@server/typings/models'
15 const reqVideoCaptionAdd = createReqFiles(
17 MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT,
19 captionfile: CONFIG.STORAGE.CAPTIONS_DIR
23 const videoCaptionsRouter = express.Router()
25 videoCaptionsRouter.get('/:videoId/captions',
26 asyncMiddleware(listVideoCaptionsValidator),
27 asyncMiddleware(listVideoCaptions)
29 videoCaptionsRouter.put('/:videoId/captions/:captionLanguage',
32 asyncMiddleware(addVideoCaptionValidator),
33 asyncRetryTransactionMiddleware(addVideoCaption)
35 videoCaptionsRouter.delete('/:videoId/captions/:captionLanguage',
37 asyncMiddleware(deleteVideoCaptionValidator),
38 asyncRetryTransactionMiddleware(deleteVideoCaption)
41 // ---------------------------------------------------------------------------
47 // ---------------------------------------------------------------------------
49 async function listVideoCaptions (req: express.Request, res: express.Response) {
50 const data = await VideoCaptionModel.listVideoCaptions(res.locals.videoId.id)
52 return res.json(getFormattedObjects(data, data.length))
55 async function addVideoCaption (req: express.Request, res: express.Response) {
56 const videoCaptionPhysicalFile = req.files['captionfile'][0]
57 const video = res.locals.videoAll
59 const videoCaption = new VideoCaptionModel({
61 language: req.params.captionLanguage
62 }) as MVideoCaptionVideo
63 videoCaption.Video = video
66 await moveAndProcessCaptionFile(videoCaptionPhysicalFile, videoCaption)
68 await sequelizeTypescript.transaction(async t => {
69 await VideoCaptionModel.insertOrReplaceLanguage(video.id, req.params.captionLanguage, t)
71 // Update video update
72 await federateVideoIfNeeded(video, false, t)
75 return res.status(204).end()
78 async function deleteVideoCaption (req: express.Request, res: express.Response) {
79 const video = res.locals.videoAll
80 const videoCaption = res.locals.videoCaption
82 await sequelizeTypescript.transaction(async t => {
83 await videoCaption.destroy({ transaction: t })
86 await federateVideoIfNeeded(video, false, t)
89 logger.info('Video caption %s of video %s deleted.', videoCaption.language, video.uuid)
91 return res.type('json').status(204).end()