]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos/captions.ts
Refactor requests
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / captions.ts
index fd7b165fb9ca45f735214f3c8336c2ccb75f89d9..4008de60fc59c2a12ddf175383e63cdd218a26ab 100644 (file)
@@ -1,16 +1,17 @@
 import * as express from 'express'
-import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares'
-import { addVideoCaptionValidator, deleteVideoCaptionValidator, listVideoCaptionsValidator } from '../../../middlewares/validators'
+import { MVideoCaption } from '@server/types/models'
+import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
+import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils'
 import { createReqFiles } from '../../../helpers/express-utils'
-import { MIMETYPES } from '../../../initializers/constants'
-import { getFormattedObjects } from '../../../helpers/utils'
-import { VideoCaptionModel } from '../../../models/video/video-caption'
 import { logger } from '../../../helpers/logger'
-import { federateVideoIfNeeded } from '../../../lib/activitypub'
-import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils'
+import { getFormattedObjects } from '../../../helpers/utils'
 import { CONFIG } from '../../../initializers/config'
+import { MIMETYPES } from '../../../initializers/constants'
 import { sequelizeTypescript } from '../../../initializers/database'
-import { MVideoCaptionVideo } from '@server/typings/models'
+import { federateVideoIfNeeded } from '../../../lib/activitypub/videos'
+import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares'
+import { addVideoCaptionValidator, deleteVideoCaptionValidator, listVideoCaptionsValidator } from '../../../middlewares/validators'
+import { VideoCaptionModel } from '../../../models/video/video-caption'
 
 const reqVideoCaptionAdd = createReqFiles(
   [ 'captionfile' ],
@@ -56,23 +57,25 @@ async function addVideoCaption (req: express.Request, res: express.Response) {
   const videoCaptionPhysicalFile = req.files['captionfile'][0]
   const video = res.locals.videoAll
 
+  const captionLanguage = req.params.captionLanguage
+
   const videoCaption = new VideoCaptionModel({
     videoId: video.id,
-    language: req.params.captionLanguage
-  }) as MVideoCaptionVideo
-  videoCaption.Video = video
+    filename: VideoCaptionModel.generateCaptionName(captionLanguage),
+    language: captionLanguage
+  }) as MVideoCaption
 
   // Move physical file
   await moveAndProcessCaptionFile(videoCaptionPhysicalFile, videoCaption)
 
   await sequelizeTypescript.transaction(async t => {
-    await VideoCaptionModel.insertOrReplaceLanguage(video.id, req.params.captionLanguage, null, t)
+    await VideoCaptionModel.insertOrReplaceLanguage(videoCaption, t)
 
     // Update video update
     await federateVideoIfNeeded(video, false, t)
   })
 
-  return res.status(204).end()
+  return res.status(HttpStatusCode.NO_CONTENT_204).end()
 }
 
 async function deleteVideoCaption (req: express.Request, res: express.Response) {
@@ -88,5 +91,5 @@ async function deleteVideoCaption (req: express.Request, res: express.Response)
 
   logger.info('Video caption %s of video %s deleted.', videoCaption.language, video.uuid)
 
-  return res.type('json').status(204).end()
+  return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()
 }