1
0
Fork 0
peertube/server/controllers/api/videos/captions.ts

93 lines
3.2 KiB
TypeScript
Raw Normal View History

2018-07-12 13:02:00 -04:00
import * as express from 'express'
import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares'
2018-10-05 05:15:06 -04:00
import { addVideoCaptionValidator, deleteVideoCaptionValidator, listVideoCaptionsValidator } from '../../../middlewares/validators'
2018-07-12 13:02:00 -04:00
import { createReqFiles } from '../../../helpers/express-utils'
import { MIMETYPES } from '../../../initializers/constants'
2018-07-12 13:02:00 -04:00
import { getFormattedObjects } from '../../../helpers/utils'
import { VideoCaptionModel } from '../../../models/video/video-caption'
import { logger } from '../../../helpers/logger'
2020-04-23 03:32:53 -04:00
import { federateVideoIfNeeded } from '../../../lib/activitypub/videos'
2018-07-16 08:22:16 -04:00
import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils'
2019-04-11 05:33:44 -04:00
import { CONFIG } from '../../../initializers/config'
import { sequelizeTypescript } from '../../../initializers/database'
2020-06-18 04:45:25 -04:00
import { MVideoCaptionVideo } from '@server/types/models'
2018-07-12 13:02:00 -04:00
const reqVideoCaptionAdd = createReqFiles(
[ 'captionfile' ],
2018-12-11 08:52:50 -05:00
MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT,
2018-07-12 13:02:00 -04: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 05:53:26 -04:00
const data = await VideoCaptionModel.listVideoCaptions(res.locals.videoId.id)
2018-07-12 13:02:00 -04: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 05:53:26 -04:00
const video = res.locals.videoAll
2018-07-12 13:02:00 -04:00
const videoCaption = new VideoCaptionModel({
videoId: video.id,
language: req.params.captionLanguage
2019-08-15 05:53:26 -04:00
}) as MVideoCaptionVideo
2018-07-12 13:02:00 -04:00
videoCaption.Video = video
// Move physical file
2018-07-16 08:22:16 -04:00
await moveAndProcessCaptionFile(videoCaptionPhysicalFile, videoCaption)
2018-07-12 13:02:00 -04:00
await sequelizeTypescript.transaction(async t => {
await VideoCaptionModel.insertOrReplaceLanguage(video.id, req.params.captionLanguage, null, t)
2018-07-12 13:02:00 -04:00
// Update video update
await federateVideoIfNeeded(video, false, t)
})
return res.status(204).end()
}
async function deleteVideoCaption (req: express.Request, res: express.Response) {
2019-08-15 05:53:26 -04:00
const video = res.locals.videoAll
2019-03-19 05:35:15 -04:00
const videoCaption = res.locals.videoCaption
2018-07-12 13:02:00 -04: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)
return res.type('json').status(204).end()
}