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

83 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-08-27 14:32:44 +02:00
import express from 'express'
import { HttpStatusCode } from '@peertube/peertube-models'
import { Hooks } from '@server/lib/plugins/hooks.js'
import { createReqFiles } from '../../../helpers/express-utils.js'
import { logger } from '../../../helpers/logger.js'
import { getFormattedObjects } from '../../../helpers/utils.js'
import { MIMETYPES } from '../../../initializers/constants.js'
import { sequelizeTypescript } from '../../../initializers/database.js'
import { federateVideoIfNeeded } from '../../../lib/activitypub/videos/index.js'
import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares/index.js'
import { addVideoCaptionValidator, deleteVideoCaptionValidator, listVideoCaptionsValidator } from '../../../middlewares/validators/index.js'
import { VideoCaptionModel } from '../../../models/video/video-caption.js'
2024-02-14 09:21:53 +01:00
import { createLocalCaption } from '@server/lib/video-captions.js'
2018-07-12 19:02:00 +02:00
const reqVideoCaptionAdd = createReqFiles([ 'captionfile' ], MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT)
2018-07-12 19:02:00 +02:00
const videoCaptionsRouter = express.Router()
videoCaptionsRouter.get('/:videoId/captions',
asyncMiddleware(listVideoCaptionsValidator),
asyncMiddleware(listVideoCaptions)
)
videoCaptionsRouter.put('/:videoId/captions/:captionLanguage',
authenticate,
reqVideoCaptionAdd,
asyncMiddleware(addVideoCaptionValidator),
2024-02-14 09:21:53 +01:00
asyncRetryTransactionMiddleware(createVideoCaption)
2018-07-12 19:02:00 +02:00
)
videoCaptionsRouter.delete('/:videoId/captions/:captionLanguage',
authenticate,
asyncMiddleware(deleteVideoCaptionValidator),
asyncRetryTransactionMiddleware(deleteVideoCaption)
)
// ---------------------------------------------------------------------------
export {
videoCaptionsRouter
}
// ---------------------------------------------------------------------------
async function listVideoCaptions (req: express.Request, res: express.Response) {
const data = await VideoCaptionModel.listVideoCaptions(res.locals.onlyVideo.id)
2018-07-12 19:02:00 +02:00
return res.json(getFormattedObjects(data, data.length))
}
2024-02-14 09:21:53 +01:00
async function createVideoCaption (req: express.Request, res: express.Response) {
2024-02-14 10:20:02 +01:00
const videoCaptionPhysicalFile: Express.Multer.File = req.files['captionfile'][0]
2019-08-15 11:53:26 +02:00
const video = res.locals.videoAll
2018-07-12 19:02:00 +02:00
2021-02-15 14:08:16 +01:00
const captionLanguage = req.params.captionLanguage
2024-02-14 10:20:02 +01:00
const videoCaption = await createLocalCaption({ video, language: captionLanguage, path: videoCaptionPhysicalFile.path })
2018-07-12 19:02:00 +02:00
await sequelizeTypescript.transaction(async t => {
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 19:02:00 +02:00
}
async function deleteVideoCaption (req: express.Request, res: express.Response) {
2019-08-15 11:53:26 +02:00
const video = res.locals.videoAll
2019-03-19 10:35:15 +01:00
const videoCaption = res.locals.videoCaption
2018-07-12 19:02:00 +02: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 19:02:00 +02:00
}