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 { renamePromise } from '../../../helpers/core-utils'
13 import { join } from 'path'
14 import { VideoModel } from '../../../models/video/video'
15 import { logger } from '../../../helpers/logger'
16 import { federateVideoIfNeeded } from '../../../lib/activitypub'
18 const reqVideoCaptionAdd = createReqFiles(
20 VIDEO_CAPTIONS_MIMETYPE_EXT,
22 captionfile: CONFIG.STORAGE.CAPTIONS_DIR
26 const videoCaptionsRouter = express.Router()
28 videoCaptionsRouter.get('/:videoId/captions',
29 asyncMiddleware(listVideoCaptionsValidator),
30 asyncMiddleware(listVideoCaptions)
32 videoCaptionsRouter.put('/:videoId/captions/:captionLanguage',
35 asyncMiddleware(addVideoCaptionValidator),
36 asyncRetryTransactionMiddleware(addVideoCaption)
38 videoCaptionsRouter.delete('/:videoId/captions/:captionLanguage',
40 asyncMiddleware(deleteVideoCaptionValidator),
41 asyncRetryTransactionMiddleware(deleteVideoCaption)
44 // ---------------------------------------------------------------------------
50 // ---------------------------------------------------------------------------
52 async function listVideoCaptions (req: express.Request, res: express.Response) {
53 const data = await VideoCaptionModel.listVideoCaptions(res.locals.video.id)
55 return res.json(getFormattedObjects(data, data.length))
58 async function addVideoCaption (req: express.Request, res: express.Response) {
59 const videoCaptionPhysicalFile = req.files['captionfile'][0]
60 const video = res.locals.video as VideoModel
62 const videoCaption = new VideoCaptionModel({
64 language: req.params.captionLanguage
66 videoCaption.Video = video
69 const videoCaptionsDir = CONFIG.STORAGE.CAPTIONS_DIR
70 const destination = join(videoCaptionsDir, videoCaption.getCaptionName())
71 await renamePromise(videoCaptionPhysicalFile.path, destination)
72 // This is important in case if there is another attempt in the retry process
73 videoCaptionPhysicalFile.filename = videoCaption.getCaptionName()
74 videoCaptionPhysicalFile.path = destination
76 await sequelizeTypescript.transaction(async t => {
77 await VideoCaptionModel.insertOrReplaceLanguage(video.id, req.params.captionLanguage, t)
79 // Update video update
80 await federateVideoIfNeeded(video, false, t)
83 return res.status(204).end()
86 async function deleteVideoCaption (req: express.Request, res: express.Response) {
87 const video = res.locals.video as VideoModel
88 const videoCaption = res.locals.videoCaption as VideoCaptionModel
90 await sequelizeTypescript.transaction(async t => {
91 await videoCaption.destroy({ transaction: t })
94 await federateVideoIfNeeded(video, false, t)
97 logger.info('Video caption %s of video %s deleted.', videoCaption.language, video.uuid)
99 return res.type('json').status(204).end()