diff options
author | Chocobozzz <me@florianbigard.com> | 2018-07-12 19:02:00 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-07-16 11:50:08 +0200 |
commit | 40e87e9ecc54e3513fb586928330a7855eb192c6 (patch) | |
tree | af1111ecba85f9cd8286811ff332a67cf21be2f6 /server/controllers/api/videos | |
parent | d4557fd3ecc8d4ed4fb0e5c868929bc36c959ed2 (diff) | |
download | PeerTube-40e87e9ecc54e3513fb586928330a7855eb192c6.tar.gz PeerTube-40e87e9ecc54e3513fb586928330a7855eb192c6.tar.zst PeerTube-40e87e9ecc54e3513fb586928330a7855eb192c6.zip |
Implement captions/subtitles
Diffstat (limited to 'server/controllers/api/videos')
-rw-r--r-- | server/controllers/api/videos/captions.ts | 100 | ||||
-rw-r--r-- | server/controllers/api/videos/index.ts | 2 |
2 files changed, 102 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 @@ | |||
1 | import * as express from 'express' | ||
2 | import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares' | ||
3 | import { | ||
4 | addVideoCaptionValidator, | ||
5 | deleteVideoCaptionValidator, | ||
6 | listVideoCaptionsValidator | ||
7 | } from '../../../middlewares/validators/video-captions' | ||
8 | import { createReqFiles } from '../../../helpers/express-utils' | ||
9 | import { CONFIG, sequelizeTypescript, VIDEO_CAPTIONS_MIMETYPE_EXT } from '../../../initializers' | ||
10 | import { getFormattedObjects } from '../../../helpers/utils' | ||
11 | import { VideoCaptionModel } from '../../../models/video/video-caption' | ||
12 | import { renamePromise } from '../../../helpers/core-utils' | ||
13 | import { join } from 'path' | ||
14 | import { VideoModel } from '../../../models/video/video' | ||
15 | import { logger } from '../../../helpers/logger' | ||
16 | import { federateVideoIfNeeded } from '../../../lib/activitypub' | ||
17 | |||
18 | const reqVideoCaptionAdd = createReqFiles( | ||
19 | [ 'captionfile' ], | ||
20 | VIDEO_CAPTIONS_MIMETYPE_EXT, | ||
21 | { | ||
22 | captionfile: CONFIG.STORAGE.CAPTIONS_DIR | ||
23 | } | ||
24 | ) | ||
25 | |||
26 | const videoCaptionsRouter = express.Router() | ||
27 | |||
28 | videoCaptionsRouter.get('/:videoId/captions', | ||
29 | asyncMiddleware(listVideoCaptionsValidator), | ||
30 | asyncMiddleware(listVideoCaptions) | ||
31 | ) | ||
32 | videoCaptionsRouter.put('/:videoId/captions/:captionLanguage', | ||
33 | authenticate, | ||
34 | reqVideoCaptionAdd, | ||
35 | asyncMiddleware(addVideoCaptionValidator), | ||
36 | asyncRetryTransactionMiddleware(addVideoCaption) | ||
37 | ) | ||
38 | videoCaptionsRouter.delete('/:videoId/captions/:captionLanguage', | ||
39 | authenticate, | ||
40 | asyncMiddleware(deleteVideoCaptionValidator), | ||
41 | asyncRetryTransactionMiddleware(deleteVideoCaption) | ||
42 | ) | ||
43 | |||
44 | // --------------------------------------------------------------------------- | ||
45 | |||
46 | export { | ||
47 | videoCaptionsRouter | ||
48 | } | ||
49 | |||
50 | // --------------------------------------------------------------------------- | ||
51 | |||
52 | async 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 | |||
58 | async 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 | |||
86 | async 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 | } | ||
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index 8c93ae89c..bbb5b8b4c 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts | |||
@@ -53,6 +53,7 @@ import { VideoFilter } from '../../../../shared/models/videos/video-query.type' | |||
53 | import { VideoSortField } from '../../../../client/src/app/shared/video/sort-field.type' | 53 | import { VideoSortField } from '../../../../client/src/app/shared/video/sort-field.type' |
54 | import { createReqFiles, isNSFWHidden } from '../../../helpers/express-utils' | 54 | import { createReqFiles, isNSFWHidden } from '../../../helpers/express-utils' |
55 | import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update' | 55 | import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update' |
56 | import { videoCaptionsRouter } from './captions' | ||
56 | 57 | ||
57 | const videosRouter = express.Router() | 58 | const videosRouter = express.Router() |
58 | 59 | ||
@@ -78,6 +79,7 @@ videosRouter.use('/', abuseVideoRouter) | |||
78 | videosRouter.use('/', blacklistRouter) | 79 | videosRouter.use('/', blacklistRouter) |
79 | videosRouter.use('/', rateVideoRouter) | 80 | videosRouter.use('/', rateVideoRouter) |
80 | videosRouter.use('/', videoCommentRouter) | 81 | videosRouter.use('/', videoCommentRouter) |
82 | videosRouter.use('/', videoCaptionsRouter) | ||
81 | 83 | ||
82 | videosRouter.get('/categories', listVideoCategories) | 84 | videosRouter.get('/categories', listVideoCategories) |
83 | videosRouter.get('/licences', listVideoLicences) | 85 | videosRouter.get('/licences', listVideoLicences) |