PeerTube/server/controllers/api/videos/captions.ts

101 lines
3.6 KiB
TypeScript
Raw Normal View History

2021-08-27 07:32:44 -05:00
import express from 'express'
2021-02-15 07:08:16 -06:00
import { MVideoCaption } from '@server/types/models'
2021-07-16 03:42:24 -05:00
import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
2021-02-15 07:08:16 -06:00
import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils'
2018-07-12 12:02:00 -05:00
import { createReqFiles } from '../../../helpers/express-utils'
import { logger } from '../../../helpers/logger'
2021-02-15 07:08:16 -06:00
import { getFormattedObjects } from '../../../helpers/utils'
2019-04-11 04:33:44 -05:00
import { CONFIG } from '../../../initializers/config'
2021-02-15 07:08:16 -06:00
import { MIMETYPES } from '../../../initializers/constants'
import { sequelizeTypescript } from '../../../initializers/database'
2021-02-15 07:08:16 -06:00
import { federateVideoIfNeeded } from '../../../lib/activitypub/videos'
import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares'
import { addVideoCaptionValidator, deleteVideoCaptionValidator, listVideoCaptionsValidator } from '../../../middlewares/validators'
import { VideoCaptionModel } from '../../../models/video/video-caption'
import { Hooks } from '@server/lib/plugins/hooks'
2018-07-12 12:02:00 -05:00
const reqVideoCaptionAdd = createReqFiles(
[ 'captionfile' ],
2018-12-11 07:52:50 -06:00
MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT,
2018-07-12 12:02:00 -05:00
{
captionfile: CONFIG.STORAGE.CAPTIONS_DIR
}
)
const videoCaptionsRouter = express.Router()
videoCaptionsRouter.get('/:videoId/captions',
asyncMiddleware(listVideoCaptionsValidator),
asyncMiddleware(listVideoCaptions)
)
videoCaptionsRouter.put('/:videoId/captions/:captionLanguage',
authenticate,
reqVideoCaptionAdd,
asyncMiddleware(addVideoCaptionValidator),
asyncRetryTransactionMiddleware(addVideoCaption)
)
videoCaptionsRouter.delete('/:videoId/captions/:captionLanguage',
authenticate,
asyncMiddleware(deleteVideoCaptionValidator),
asyncRetryTransactionMiddleware(deleteVideoCaption)
)
// ---------------------------------------------------------------------------
export {
videoCaptionsRouter
}
// ---------------------------------------------------------------------------
async function listVideoCaptions (req: express.Request, res: express.Response) {
2019-08-15 04:53:26 -05:00
const data = await VideoCaptionModel.listVideoCaptions(res.locals.videoId.id)
2018-07-12 12:02:00 -05:00
return res.json(getFormattedObjects(data, data.length))
}
async function addVideoCaption (req: express.Request, res: express.Response) {
const videoCaptionPhysicalFile = req.files['captionfile'][0]
2019-08-15 04:53:26 -05:00
const video = res.locals.videoAll
2018-07-12 12:02:00 -05:00
2021-02-15 07:08:16 -06:00
const captionLanguage = req.params.captionLanguage
2018-07-12 12:02:00 -05:00
const videoCaption = new VideoCaptionModel({
videoId: video.id,
2021-02-15 07:08:16 -06:00
filename: VideoCaptionModel.generateCaptionName(captionLanguage),
language: captionLanguage
}) as MVideoCaption
2018-07-12 12:02:00 -05:00
// Move physical file
2018-07-16 07:22:16 -05:00
await moveAndProcessCaptionFile(videoCaptionPhysicalFile, videoCaption)
2018-07-12 12:02:00 -05:00
await sequelizeTypescript.transaction(async t => {
2021-02-15 07:08:16 -06:00
await VideoCaptionModel.insertOrReplaceLanguage(videoCaption, t)
2018-07-12 12:02:00 -05:00
// Update video update
await federateVideoIfNeeded(video, false, t)
})
Hooks.runAction('action:api.video-caption.created', { caption: videoCaption, req, res })
return res.status(HttpStatusCode.NO_CONTENT_204).end()
2018-07-12 12:02:00 -05:00
}
async function deleteVideoCaption (req: express.Request, res: express.Response) {
2019-08-15 04:53:26 -05:00
const video = res.locals.videoAll
2019-03-19 04:35:15 -05:00
const videoCaption = res.locals.videoCaption
2018-07-12 12:02:00 -05:00
await sequelizeTypescript.transaction(async t => {
await videoCaption.destroy({ transaction: t })
// Send video update
await federateVideoIfNeeded(video, false, t)
})
logger.info('Video caption %s of video %s deleted.', videoCaption.language, video.uuid)
Hooks.runAction('action:api.video-caption.deleted', { caption: videoCaption, req, res })
return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()
2018-07-12 12:02:00 -05:00
}