]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/custom-validators/video-captions.ts
emit more specific status codes on video upload (#3423)
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / video-captions.ts
index fd4dc740b31fd4fac0c400a3d38cf859eafb4365..528edf60ce6810a01a19b1491eb42f7438e38b8d 100644 (file)
@@ -1,41 +1,21 @@
-import { CONSTRAINTS_FIELDS, VIDEO_LANGUAGES } from '../../initializers'
+import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_LANGUAGES } from '../../initializers/constants'
 import { exists, isFileValid } from './misc'
-import { Response } from 'express'
-import { VideoModel } from '../../models/video/video'
-import { VideoCaptionModel } from '../../models/video/video-caption'
 
 function isVideoCaptionLanguageValid (value: any) {
-  return exists(value) && VIDEO_LANGUAGES[ value ] !== undefined
+  return exists(value) && VIDEO_LANGUAGES[value] !== undefined
 }
 
-const videoCaptionTypes = CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
-                                          .map(v => v.replace('.', ''))
-                                          .join('|')
-const videoCaptionsTypesRegex = `text/(${videoCaptionTypes})`
-
+const videoCaptionTypesRegex = Object.keys(MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT)
+                                .concat([ 'application/octet-stream' ]) // MacOS sends application/octet-stream
+                                .map(m => `(${m})`)
+                                .join('|')
 function isVideoCaptionFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) {
-  return isFileValid(files, videoCaptionsTypesRegex, field, CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max)
-}
-
-async function isVideoCaptionExist (video: VideoModel, language: string, res: Response) {
-  const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(video.id, language)
-
-  if (!videoCaption) {
-    res.status(404)
-       .json({ error: 'Video caption not found' })
-       .end()
-
-    return false
-  }
-
-  res.locals.videoCaption = videoCaption
-  return true
+  return isFileValid(files, videoCaptionTypesRegex, field, CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max)
 }
 
 // ---------------------------------------------------------------------------
 
 export {
   isVideoCaptionFile,
-  isVideoCaptionLanguageValid,
-  isVideoCaptionExist
+  isVideoCaptionLanguageValid
 }