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

117 lines
4.1 KiB
TypeScript
Raw Normal View History

2024-06-13 02:23:12 -05:00
import { HttpStatusCode, VideoCaptionGenerate } from '@peertube/peertube-models'
import { Hooks } from '@server/lib/plugins/hooks.js'
2024-06-13 02:23:12 -05:00
import { createLocalCaption, createTranscriptionTaskIfNeeded } from '@server/lib/video-captions.js'
import { VideoJobInfoModel } from '@server/models/video/video-job-info.js'
import express from 'express'
import { createReqFiles } from '../../../helpers/express-utils.js'
2024-06-13 02:23:12 -05:00
import { logger, loggerTagsFactory } 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'
2024-06-13 02:23:12 -05:00
import {
addVideoCaptionValidator,
deleteVideoCaptionValidator,
generateVideoCaptionValidator,
listVideoCaptionsValidator
} from '../../../middlewares/validators/index.js'
import { VideoCaptionModel } from '../../../models/video/video-caption.js'
2024-06-13 02:23:12 -05:00
const lTags = loggerTagsFactory('api', 'video-caption')
2018-07-12 12:02:00 -05:00
const reqVideoCaptionAdd = createReqFiles([ 'captionfile' ], MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT)
2018-07-12 12:02:00 -05:00
const videoCaptionsRouter = express.Router()
2024-06-13 02:23:12 -05:00
videoCaptionsRouter.post('/:videoId/captions/generate',
authenticate,
asyncMiddleware(generateVideoCaptionValidator),
asyncMiddleware(createGenerateVideoCaption)
)
2018-07-12 12:02:00 -05:00
videoCaptionsRouter.get('/:videoId/captions',
asyncMiddleware(listVideoCaptionsValidator),
asyncMiddleware(listVideoCaptions)
)
2024-06-13 02:23:12 -05:00
2018-07-12 12:02:00 -05:00
videoCaptionsRouter.put('/:videoId/captions/:captionLanguage',
authenticate,
reqVideoCaptionAdd,
asyncMiddleware(addVideoCaptionValidator),
2024-02-14 02:21:53 -06:00
asyncRetryTransactionMiddleware(createVideoCaption)
2018-07-12 12:02:00 -05:00
)
2024-06-13 02:23:12 -05:00
2018-07-12 12:02:00 -05:00
videoCaptionsRouter.delete('/:videoId/captions/:captionLanguage',
authenticate,
asyncMiddleware(deleteVideoCaptionValidator),
asyncRetryTransactionMiddleware(deleteVideoCaption)
)
// ---------------------------------------------------------------------------
export {
videoCaptionsRouter
}
// ---------------------------------------------------------------------------
2024-06-13 02:23:12 -05:00
async function createGenerateVideoCaption (req: express.Request, res: express.Response) {
const video = res.locals.videoAll
const body = req.body as VideoCaptionGenerate
if (body.forceTranscription === true) {
await VideoJobInfoModel.abortAllTasks(video.uuid, 'pendingTranscription')
}
await createTranscriptionTaskIfNeeded(video)
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
}
2018-07-12 12:02:00 -05:00
async function listVideoCaptions (req: express.Request, res: express.Response) {
const data = await VideoCaptionModel.listVideoCaptions(res.locals.onlyVideo.id)
2018-07-12 12:02:00 -05:00
return res.json(getFormattedObjects(data, data.length))
}
2024-02-14 02:21:53 -06:00
async function createVideoCaption (req: express.Request, res: express.Response) {
2024-02-14 03:20:02 -06:00
const videoCaptionPhysicalFile: Express.Multer.File = 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
const videoCaption = await createLocalCaption({
video,
language: captionLanguage,
path: videoCaptionPhysicalFile.path,
automaticallyGenerated: false
})
2018-07-12 12:02:00 -05: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 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)
})
2024-06-13 02:23:12 -05:00
logger.info('Video caption %s of video %s deleted.', videoCaption.language, video.uuid, lTags(video.uuid))
2018-07-12 12:02:00 -05:00
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
}