]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/captions.ts
Put private videos under a specific subdirectory
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / captions.ts
1 import express from 'express'
2 import { Hooks } from '@server/lib/plugins/hooks'
3 import { MVideoCaption } from '@server/types/models'
4 import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
5 import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils'
6 import { createReqFiles } from '../../../helpers/express-utils'
7 import { logger } from '../../../helpers/logger'
8 import { getFormattedObjects } from '../../../helpers/utils'
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'
15
16 const reqVideoCaptionAdd = createReqFiles([ 'captionfile' ], MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT)
17
18 const videoCaptionsRouter = express.Router()
19
20 videoCaptionsRouter.get('/:videoId/captions',
21 asyncMiddleware(listVideoCaptionsValidator),
22 asyncMiddleware(listVideoCaptions)
23 )
24 videoCaptionsRouter.put('/:videoId/captions/:captionLanguage',
25 authenticate,
26 reqVideoCaptionAdd,
27 asyncMiddleware(addVideoCaptionValidator),
28 asyncRetryTransactionMiddleware(addVideoCaption)
29 )
30 videoCaptionsRouter.delete('/:videoId/captions/:captionLanguage',
31 authenticate,
32 asyncMiddleware(deleteVideoCaptionValidator),
33 asyncRetryTransactionMiddleware(deleteVideoCaption)
34 )
35
36 // ---------------------------------------------------------------------------
37
38 export {
39 videoCaptionsRouter
40 }
41
42 // ---------------------------------------------------------------------------
43
44 async 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
50 async 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
77 async 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 }