2
1

captions.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import * as express from 'express'
  2. import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares'
  3. import { addVideoCaptionValidator, deleteVideoCaptionValidator, listVideoCaptionsValidator } from '../../../middlewares/validators'
  4. import { createReqFiles } from '../../../helpers/express-utils'
  5. import { MIMETYPES } from '../../../initializers/constants'
  6. import { getFormattedObjects } from '../../../helpers/utils'
  7. import { VideoCaptionModel } from '../../../models/video/video-caption'
  8. import { logger } from '../../../helpers/logger'
  9. import { federateVideoIfNeeded } from '../../../lib/activitypub'
  10. import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils'
  11. import { CONFIG } from '../../../initializers/config'
  12. import { sequelizeTypescript } from '../../../initializers/database'
  13. const reqVideoCaptionAdd = createReqFiles(
  14. [ 'captionfile' ],
  15. MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT,
  16. {
  17. captionfile: CONFIG.STORAGE.CAPTIONS_DIR
  18. }
  19. )
  20. const videoCaptionsRouter = express.Router()
  21. videoCaptionsRouter.get('/:videoId/captions',
  22. asyncMiddleware(listVideoCaptionsValidator),
  23. asyncMiddleware(listVideoCaptions)
  24. )
  25. videoCaptionsRouter.put('/:videoId/captions/:captionLanguage',
  26. authenticate,
  27. reqVideoCaptionAdd,
  28. asyncMiddleware(addVideoCaptionValidator),
  29. asyncRetryTransactionMiddleware(addVideoCaption)
  30. )
  31. videoCaptionsRouter.delete('/:videoId/captions/:captionLanguage',
  32. authenticate,
  33. asyncMiddleware(deleteVideoCaptionValidator),
  34. asyncRetryTransactionMiddleware(deleteVideoCaption)
  35. )
  36. // ---------------------------------------------------------------------------
  37. export {
  38. videoCaptionsRouter
  39. }
  40. // ---------------------------------------------------------------------------
  41. async function listVideoCaptions (req: express.Request, res: express.Response) {
  42. const data = await VideoCaptionModel.listVideoCaptions(res.locals.video.id)
  43. return res.json(getFormattedObjects(data, data.length))
  44. }
  45. async function addVideoCaption (req: express.Request, res: express.Response) {
  46. const videoCaptionPhysicalFile = req.files['captionfile'][0]
  47. const video = res.locals.video
  48. const videoCaption = new VideoCaptionModel({
  49. videoId: video.id,
  50. language: req.params.captionLanguage
  51. })
  52. videoCaption.Video = video
  53. // Move physical file
  54. await moveAndProcessCaptionFile(videoCaptionPhysicalFile, videoCaption)
  55. await sequelizeTypescript.transaction(async t => {
  56. await VideoCaptionModel.insertOrReplaceLanguage(video.id, req.params.captionLanguage, t)
  57. // Update video update
  58. await federateVideoIfNeeded(video, false, t)
  59. })
  60. return res.status(204).end()
  61. }
  62. async function deleteVideoCaption (req: express.Request, res: express.Response) {
  63. const video = res.locals.video
  64. const videoCaption = res.locals.videoCaption
  65. await sequelizeTypescript.transaction(async t => {
  66. await videoCaption.destroy({ transaction: t })
  67. // Send video update
  68. await federateVideoIfNeeded(video, false, t)
  69. })
  70. logger.info('Video caption %s of video %s deleted.', videoCaption.language, video.uuid)
  71. return res.type('json').status(204).end()
  72. }