]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos/captions.ts
Use video abuse filters on client side
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / captions.ts
index 05412a17f20b442df76a0cd1c9e8a84decc0c3ad..8c1d12ca86e66dcc73ab7bb7b6cffb55c8f44d9b 100644 (file)
@@ -1,23 +1,20 @@
 import * as express from 'express'
 import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares'
-import {
-  addVideoCaptionValidator,
-  deleteVideoCaptionValidator,
-  listVideoCaptionsValidator
-} from '../../../middlewares/validators/video-captions'
+import { addVideoCaptionValidator, deleteVideoCaptionValidator, listVideoCaptionsValidator } from '../../../middlewares/validators'
 import { createReqFiles } from '../../../helpers/express-utils'
-import { CONFIG, sequelizeTypescript, VIDEO_CAPTIONS_MIMETYPE_EXT } from '../../../initializers'
+import { MIMETYPES } from '../../../initializers/constants'
 import { getFormattedObjects } from '../../../helpers/utils'
 import { VideoCaptionModel } from '../../../models/video/video-caption'
-import { renamePromise } from '../../../helpers/core-utils'
-import { join } from 'path'
-import { VideoModel } from '../../../models/video/video'
 import { logger } from '../../../helpers/logger'
-import { federateVideoIfNeeded } from '../../../lib/activitypub'
+import { federateVideoIfNeeded } from '../../../lib/activitypub/videos'
+import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils'
+import { CONFIG } from '../../../initializers/config'
+import { sequelizeTypescript } from '../../../initializers/database'
+import { MVideoCaptionVideo } from '@server/typings/models'
 
 const reqVideoCaptionAdd = createReqFiles(
   [ 'captionfile' ],
-  VIDEO_CAPTIONS_MIMETYPE_EXT,
+  MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT,
   {
     captionfile: CONFIG.STORAGE.CAPTIONS_DIR
   }
@@ -50,31 +47,26 @@ export {
 // ---------------------------------------------------------------------------
 
 async function listVideoCaptions (req: express.Request, res: express.Response) {
-  const data = await VideoCaptionModel.listVideoCaptions(res.locals.video.id)
+  const data = await VideoCaptionModel.listVideoCaptions(res.locals.videoId.id)
 
   return res.json(getFormattedObjects(data, data.length))
 }
 
 async function addVideoCaption (req: express.Request, res: express.Response) {
   const videoCaptionPhysicalFile = req.files['captionfile'][0]
-  const video = res.locals.video as VideoModel
+  const video = res.locals.videoAll
 
   const videoCaption = new VideoCaptionModel({
     videoId: video.id,
     language: req.params.captionLanguage
-  })
+  }) as MVideoCaptionVideo
   videoCaption.Video = video
 
   // Move physical file
-  const videoCaptionsDir = CONFIG.STORAGE.CAPTIONS_DIR
-  const destination = join(videoCaptionsDir, videoCaption.getCaptionName())
-  await renamePromise(videoCaptionPhysicalFile.path, destination)
-  // This is important in case if there is another attempt in the retry process
-  videoCaptionPhysicalFile.filename = videoCaption.getCaptionName()
-  videoCaptionPhysicalFile.path = destination
+  await moveAndProcessCaptionFile(videoCaptionPhysicalFile, videoCaption)
 
   await sequelizeTypescript.transaction(async t => {
-    await VideoCaptionModel.insertOrReplaceLanguage(video.id, req.params.captionLanguage, t)
+    await VideoCaptionModel.insertOrReplaceLanguage(video.id, req.params.captionLanguage, null, t)
 
     // Update video update
     await federateVideoIfNeeded(video, false, t)
@@ -84,8 +76,8 @@ async function addVideoCaption (req: express.Request, res: express.Response) {
 }
 
 async function deleteVideoCaption (req: express.Request, res: express.Response) {
-  const video = res.locals.video as VideoModel
-  const videoCaption = res.locals.videoCaption as VideoCaptionModel
+  const video = res.locals.videoAll
+  const videoCaption = res.locals.videoCaption
 
   await sequelizeTypescript.transaction(async t => {
     await videoCaption.destroy({ transaction: t })