]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/api/videos/captions.ts
Avoid concurrency issue on transcoding
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / captions.ts
... / ...
CommitLineData
1import express from 'express'
2import { Hooks } from '@server/lib/plugins/hooks'
3import { MVideoCaption } from '@server/types/models'
4import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
5import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils'
6import { createReqFiles } from '../../../helpers/express-utils'
7import { logger } from '../../../helpers/logger'
8import { getFormattedObjects } from '../../../helpers/utils'
9import { MIMETYPES } from '../../../initializers/constants'
10import { sequelizeTypescript } from '../../../initializers/database'
11import { federateVideoIfNeeded } from '../../../lib/activitypub/videos'
12import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares'
13import { addVideoCaptionValidator, deleteVideoCaptionValidator, listVideoCaptionsValidator } from '../../../middlewares/validators'
14import { VideoCaptionModel } from '../../../models/video/video-caption'
15
16const reqVideoCaptionAdd = createReqFiles([ 'captionfile' ], MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT)
17
18const videoCaptionsRouter = express.Router()
19
20videoCaptionsRouter.get('/:videoId/captions',
21 asyncMiddleware(listVideoCaptionsValidator),
22 asyncMiddleware(listVideoCaptions)
23)
24videoCaptionsRouter.put('/:videoId/captions/:captionLanguage',
25 authenticate,
26 reqVideoCaptionAdd,
27 asyncMiddleware(addVideoCaptionValidator),
28 asyncRetryTransactionMiddleware(addVideoCaption)
29)
30videoCaptionsRouter.delete('/:videoId/captions/:captionLanguage',
31 authenticate,
32 asyncMiddleware(deleteVideoCaptionValidator),
33 asyncRetryTransactionMiddleware(deleteVideoCaption)
34)
35
36// ---------------------------------------------------------------------------
37
38export {
39 videoCaptionsRouter
40}
41
42// ---------------------------------------------------------------------------
43
44async function listVideoCaptions (req: express.Request, res: express.Response) {
45 const data = await VideoCaptionModel.listVideoCaptions(res.locals.onlyVideo.id)
46
47 return res.json(getFormattedObjects(data, data.length))
48}
49
50async function addVideoCaption (req: express.Request, res: express.Response) {
51 const videoCaptionPhysicalFile = req.files['captionfile'][0]
52 const video = res.locals.videoAll
53
54 const captionLanguage = req.params.captionLanguage
55
56 const videoCaption = new VideoCaptionModel({
57 videoId: video.id,
58 filename: VideoCaptionModel.generateCaptionName(captionLanguage),
59 language: captionLanguage
60 }) as MVideoCaption
61
62 // Move physical file
63 await moveAndProcessCaptionFile(videoCaptionPhysicalFile, videoCaption)
64
65 await sequelizeTypescript.transaction(async t => {
66 await VideoCaptionModel.insertOrReplaceLanguage(videoCaption, t)
67
68 // Update video update
69 await federateVideoIfNeeded(video, false, t)
70 })
71
72 Hooks.runAction('action:api.video-caption.created', { caption: videoCaption, req, res })
73
74 return res.status(HttpStatusCode.NO_CONTENT_204).end()
75}
76
77async function deleteVideoCaption (req: express.Request, res: express.Response) {
78 const video = res.locals.videoAll
79 const videoCaption = res.locals.videoCaption
80
81 await sequelizeTypescript.transaction(async t => {
82 await videoCaption.destroy({ transaction: t })
83
84 // Send video update
85 await federateVideoIfNeeded(video, false, t)
86 })
87
88 logger.info('Video caption %s of video %s deleted.', videoCaption.language, video.uuid)
89
90 Hooks.runAction('action:api.video-caption.deleted', { caption: videoCaption, req, res })
91
92 return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()
93}