aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos/captions.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-07-12 19:02:00 +0200
committerChocobozzz <me@florianbigard.com>2018-07-16 11:50:08 +0200
commit40e87e9ecc54e3513fb586928330a7855eb192c6 (patch)
treeaf1111ecba85f9cd8286811ff332a67cf21be2f6 /server/controllers/api/videos/captions.ts
parentd4557fd3ecc8d4ed4fb0e5c868929bc36c959ed2 (diff)
downloadPeerTube-40e87e9ecc54e3513fb586928330a7855eb192c6.tar.gz
PeerTube-40e87e9ecc54e3513fb586928330a7855eb192c6.tar.zst
PeerTube-40e87e9ecc54e3513fb586928330a7855eb192c6.zip
Implement captions/subtitles
Diffstat (limited to 'server/controllers/api/videos/captions.ts')
-rw-r--r--server/controllers/api/videos/captions.ts100
1 files changed, 100 insertions, 0 deletions
diff --git a/server/controllers/api/videos/captions.ts b/server/controllers/api/videos/captions.ts
new file mode 100644
index 000000000..05412a17f
--- /dev/null
+++ b/server/controllers/api/videos/captions.ts
@@ -0,0 +1,100 @@
1import * as express from 'express'
2import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares'
3import {
4 addVideoCaptionValidator,
5 deleteVideoCaptionValidator,
6 listVideoCaptionsValidator
7} from '../../../middlewares/validators/video-captions'
8import { createReqFiles } from '../../../helpers/express-utils'
9import { CONFIG, sequelizeTypescript, VIDEO_CAPTIONS_MIMETYPE_EXT } from '../../../initializers'
10import { getFormattedObjects } from '../../../helpers/utils'
11import { VideoCaptionModel } from '../../../models/video/video-caption'
12import { renamePromise } from '../../../helpers/core-utils'
13import { join } from 'path'
14import { VideoModel } from '../../../models/video/video'
15import { logger } from '../../../helpers/logger'
16import { federateVideoIfNeeded } from '../../../lib/activitypub'
17
18const reqVideoCaptionAdd = createReqFiles(
19 [ 'captionfile' ],
20 VIDEO_CAPTIONS_MIMETYPE_EXT,
21 {
22 captionfile: CONFIG.STORAGE.CAPTIONS_DIR
23 }
24)
25
26const videoCaptionsRouter = express.Router()
27
28videoCaptionsRouter.get('/:videoId/captions',
29 asyncMiddleware(listVideoCaptionsValidator),
30 asyncMiddleware(listVideoCaptions)
31)
32videoCaptionsRouter.put('/:videoId/captions/:captionLanguage',
33 authenticate,
34 reqVideoCaptionAdd,
35 asyncMiddleware(addVideoCaptionValidator),
36 asyncRetryTransactionMiddleware(addVideoCaption)
37)
38videoCaptionsRouter.delete('/:videoId/captions/:captionLanguage',
39 authenticate,
40 asyncMiddleware(deleteVideoCaptionValidator),
41 asyncRetryTransactionMiddleware(deleteVideoCaption)
42)
43
44// ---------------------------------------------------------------------------
45
46export {
47 videoCaptionsRouter
48}
49
50// ---------------------------------------------------------------------------
51
52async function listVideoCaptions (req: express.Request, res: express.Response) {
53 const data = await VideoCaptionModel.listVideoCaptions(res.locals.video.id)
54
55 return res.json(getFormattedObjects(data, data.length))
56}
57
58async function addVideoCaption (req: express.Request, res: express.Response) {
59 const videoCaptionPhysicalFile = req.files['captionfile'][0]
60 const video = res.locals.video as VideoModel
61
62 const videoCaption = new VideoCaptionModel({
63 videoId: video.id,
64 language: req.params.captionLanguage
65 })
66 videoCaption.Video = video
67
68 // Move physical file
69 const videoCaptionsDir = CONFIG.STORAGE.CAPTIONS_DIR
70 const destination = join(videoCaptionsDir, videoCaption.getCaptionName())
71 await renamePromise(videoCaptionPhysicalFile.path, destination)
72 // This is important in case if there is another attempt in the retry process
73 videoCaptionPhysicalFile.filename = videoCaption.getCaptionName()
74 videoCaptionPhysicalFile.path = destination
75
76 await sequelizeTypescript.transaction(async t => {
77 await VideoCaptionModel.insertOrReplaceLanguage(video.id, req.params.captionLanguage, t)
78
79 // Update video update
80 await federateVideoIfNeeded(video, false, t)
81 })
82
83 return res.status(204).end()
84}
85
86async function deleteVideoCaption (req: express.Request, res: express.Response) {
87 const video = res.locals.video as VideoModel
88 const videoCaption = res.locals.videoCaption as VideoCaptionModel
89
90 await sequelizeTypescript.transaction(async t => {
91 await videoCaption.destroy({ transaction: t })
92
93 // Send video update
94 await federateVideoIfNeeded(video, false, t)
95 })
96
97 logger.info('Video caption %s of video %s deleted.', videoCaption.language, video.uuid)
98
99 return res.type('json').status(204).end()
100}