]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/validators/videos/video-files.ts
Merge branch 'release/4.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-files.ts
index 282594ab6041b62c72cc8959f29dd0250fb26738..92c5b9483b9d1ef6e5f8d9dec370ea0eb1257e3c 100644 (file)
@@ -1,22 +1,19 @@
 import express from 'express'
-import { MUser, MVideo } from '@server/types/models'
-import { HttpStatusCode, UserRight } from '../../../../shared'
-import { logger } from '../../../helpers/logger'
+import { param } from 'express-validator'
+import { isIdValid } from '@server/helpers/custom-validators/misc'
+import { MVideo } from '@server/types/models'
+import { HttpStatusCode } from '@shared/models'
 import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared'
 
 const videoFilesDeleteWebTorrentValidator = [
   isValidVideoIdParam('id'),
 
   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
-    logger.debug('Checking videoFilesDeleteWebTorrent parameters', { parameters: req.params })
-
     if (areValidationErrors(req, res)) return
     if (!await doesVideoExist(req.params.id, res)) return
 
     const video = res.locals.videoAll
-    const user = res.locals.oauth.token.User
 
-    if (!checkUserCanDeleteFiles(user, res)) return
     if (!checkLocalVideo(video, res)) return
 
     if (!video.hasWebTorrentFiles()) {
@@ -37,19 +34,50 @@ const videoFilesDeleteWebTorrentValidator = [
   }
 ]
 
-const videoFilesDeleteHLSValidator = [
+const videoFilesDeleteWebTorrentFileValidator = [
   isValidVideoIdParam('id'),
 
+  param('videoFileId')
+    .custom(isIdValid),
+
   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
-    logger.debug('Checking videoFilesDeleteHLS parameters', { parameters: req.params })
+    if (areValidationErrors(req, res)) return
+    if (!await doesVideoExist(req.params.id, res)) return
+
+    const video = res.locals.videoAll
+
+    if (!checkLocalVideo(video, res)) return
 
+    const files = video.VideoFiles
+    if (!files.find(f => f.id === +req.params.videoFileId)) {
+      return res.fail({
+        status: HttpStatusCode.NOT_FOUND_404,
+        message: 'This video does not have this WebTorrent file id'
+      })
+    }
+
+    if (files.length === 1 && !video.getHLSPlaylist()) {
+      return res.fail({
+        status: HttpStatusCode.BAD_REQUEST_400,
+        message: 'Cannot delete WebTorrent files since this video does not have HLS playlist'
+      })
+    }
+
+    return next()
+  }
+]
+
+// ---------------------------------------------------------------------------
+
+const videoFilesDeleteHLSValidator = [
+  isValidVideoIdParam('id'),
+
+  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
     if (areValidationErrors(req, res)) return
     if (!await doesVideoExist(req.params.id, res)) return
 
     const video = res.locals.videoAll
-    const user = res.locals.oauth.token.User
 
-    if (!checkUserCanDeleteFiles(user, res)) return
     if (!checkLocalVideo(video, res)) return
 
     if (!video.getHLSPlaylist()) {
@@ -70,9 +98,53 @@ const videoFilesDeleteHLSValidator = [
   }
 ]
 
+const videoFilesDeleteHLSFileValidator = [
+  isValidVideoIdParam('id'),
+
+  param('videoFileId')
+    .custom(isIdValid),
+
+  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
+    if (areValidationErrors(req, res)) return
+    if (!await doesVideoExist(req.params.id, res)) return
+
+    const video = res.locals.videoAll
+
+    if (!checkLocalVideo(video, res)) return
+
+    if (!video.getHLSPlaylist()) {
+      return res.fail({
+        status: HttpStatusCode.BAD_REQUEST_400,
+        message: 'This video does not have HLS files'
+      })
+    }
+
+    const hlsFiles = video.getHLSPlaylist().VideoFiles
+    if (!hlsFiles.find(f => f.id === +req.params.videoFileId)) {
+      return res.fail({
+        status: HttpStatusCode.NOT_FOUND_404,
+        message: 'This HLS playlist does not have this file id'
+      })
+    }
+
+    // Last file to delete
+    if (hlsFiles.length === 1 && !video.hasWebTorrentFiles()) {
+      return res.fail({
+        status: HttpStatusCode.BAD_REQUEST_400,
+        message: 'Cannot delete last HLS playlist file since this video does not have WebTorrent files'
+      })
+    }
+
+    return next()
+  }
+]
+
 export {
   videoFilesDeleteWebTorrentValidator,
-  videoFilesDeleteHLSValidator
+  videoFilesDeleteWebTorrentFileValidator,
+
+  videoFilesDeleteHLSValidator,
+  videoFilesDeleteHLSFileValidator
 }
 
 // ---------------------------------------------------------------------------
@@ -89,16 +161,3 @@ function checkLocalVideo (video: MVideo, res: express.Response) {
 
   return true
 }
-
-function checkUserCanDeleteFiles (user: MUser, res: express.Response) {
-  if (user.hasRight(UserRight.MANAGE_VIDEO_FILES) !== true) {
-    res.fail({
-      status: HttpStatusCode.FORBIDDEN_403,
-      message: 'User cannot update video files'
-    })
-
-    return false
-  }
-
-  return true
-}