1 import express from 'express'
2 import { MVideoCaption } from '@server/types/models'
3 import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
4 import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils'
5 import { createReqFiles } from '../../../helpers/express-utils'
6 import { logger } from '../../../helpers/logger'
7 import { getFormattedObjects } from '../../../helpers/utils'
8 import { CONFIG } from '../../../initializers/config'
9 import { MIMETYPES } from '../../../initializers/constants'
10 import { sequelizeTypescript } from '../../../initializers/database'
11 import { federateVideoIfNeeded } from '../../../lib/activitypub/videos'
12 import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares'
13 import { addVideoCaptionValidator, deleteVideoCaptionValidator, listVideoCaptionsValidator } from '../../../middlewares/validators'
14 import { VideoCaptionModel } from '../../../models/video/video-caption'
16 const reqVideoCaptionAdd = createReqFiles(
18 MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT,
20 captionfile: CONFIG.STORAGE.CAPTIONS_DIR
24 const videoCaptionsRouter = express.Router()
26 videoCaptionsRouter.get('/:videoId/captions',
27 asyncMiddleware(listVideoCaptionsValidator),
28 asyncMiddleware(listVideoCaptions)
30 videoCaptionsRouter.put('/:videoId/captions/:captionLanguage',
33 asyncMiddleware(addVideoCaptionValidator),
34 asyncRetryTransactionMiddleware(addVideoCaption)
36 videoCaptionsRouter.delete('/:videoId/captions/:captionLanguage',
38 asyncMiddleware(deleteVideoCaptionValidator),
39 asyncRetryTransactionMiddleware(deleteVideoCaption)
42 // ---------------------------------------------------------------------------
48 // ---------------------------------------------------------------------------
50 async function listVideoCaptions (req: express.Request, res: express.Response) {
51 const data = await VideoCaptionModel.listVideoCaptions(res.locals.videoId.id)
53 return res.json(getFormattedObjects(data, data.length))
56 async function addVideoCaption (req: express.Request, res: express.Response) {
57 const videoCaptionPhysicalFile = req.files['captionfile'][0]
58 const video = res.locals.videoAll
60 const captionLanguage = req.params.captionLanguage
62 const videoCaption = new VideoCaptionModel({
64 filename: VideoCaptionModel.generateCaptionName(captionLanguage),
65 language: captionLanguage
69 await moveAndProcessCaptionFile(videoCaptionPhysicalFile, videoCaption)
71 await sequelizeTypescript.transaction(async t => {
72 await VideoCaptionModel.insertOrReplaceLanguage(videoCaption, t)
74 // Update video update
75 await federateVideoIfNeeded(video, false, t)
78 return res.status(HttpStatusCode.NO_CONTENT_204).end()
81 async function deleteVideoCaption (req: express.Request, res: express.Response) {
82 const video = res.locals.videoAll
83 const videoCaption = res.locals.videoCaption
85 await sequelizeTypescript.transaction(async t => {
86 await videoCaption.destroy({ transaction: t })
89 await federateVideoIfNeeded(video, false, t)
92 logger.info('Video caption %s of video %s deleted.', videoCaption.language, video.uuid)
94 return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()