]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/captions.ts
Process video torrents in order
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / captions.ts
CommitLineData
41fb13c3 1import express from 'express'
d3d3deaa 2import { Hooks } from '@server/lib/plugins/hooks'
6302d599 3import { MVideoCaption } from '@server/types/models'
c0e8b12e 4import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
6302d599 5import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils'
40e87e9e 6import { createReqFiles } from '../../../helpers/express-utils'
40e87e9e 7import { logger } from '../../../helpers/logger'
6302d599 8import { getFormattedObjects } from '../../../helpers/utils'
6302d599 9import { MIMETYPES } from '../../../initializers/constants'
74dc3bca 10import { sequelizeTypescript } from '../../../initializers/database'
6302d599
C
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'
40e87e9e 15
d3d3deaa 16const reqVideoCaptionAdd = createReqFiles([ 'captionfile' ], MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT)
40e87e9e
C
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) {
795212f7 45 const data = await VideoCaptionModel.listVideoCaptions(res.locals.onlyVideo.id)
40e87e9e
C
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]
453e83ea 52 const video = res.locals.videoAll
40e87e9e 53
6302d599
C
54 const captionLanguage = req.params.captionLanguage
55
40e87e9e
C
56 const videoCaption = new VideoCaptionModel({
57 videoId: video.id,
6302d599
C
58 filename: VideoCaptionModel.generateCaptionName(captionLanguage),
59 language: captionLanguage
60 }) as MVideoCaption
40e87e9e
C
61
62 // Move physical file
f4001cf4 63 await moveAndProcessCaptionFile(videoCaptionPhysicalFile, videoCaption)
40e87e9e
C
64
65 await sequelizeTypescript.transaction(async t => {
6302d599 66 await VideoCaptionModel.insertOrReplaceLanguage(videoCaption, t)
40e87e9e
C
67
68 // Update video update
69 await federateVideoIfNeeded(video, false, t)
70 })
71
5e3d29ab 72 Hooks.runAction('action:api.video-caption.created', { caption: videoCaption, req, res })
73
2d53be02 74 return res.status(HttpStatusCode.NO_CONTENT_204).end()
40e87e9e
C
75}
76
77async function deleteVideoCaption (req: express.Request, res: express.Response) {
453e83ea 78 const video = res.locals.videoAll
dae86118 79 const videoCaption = res.locals.videoCaption
40e87e9e
C
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
5e3d29ab 90 Hooks.runAction('action:api.video-caption.deleted', { caption: videoCaption, req, res })
91
2d53be02 92 return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()
40e87e9e 93}