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