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