video-files.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import express from 'express'
  2. import { MVideo } from '@server/types/models'
  3. import { HttpStatusCode } from '@shared/models'
  4. import { logger } from '../../../helpers/logger'
  5. import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared'
  6. const videoFilesDeleteWebTorrentValidator = [
  7. isValidVideoIdParam('id'),
  8. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  9. logger.debug('Checking videoFilesDeleteWebTorrent parameters', { parameters: req.params })
  10. if (areValidationErrors(req, res)) return
  11. if (!await doesVideoExist(req.params.id, res)) return
  12. const video = res.locals.videoAll
  13. if (!checkLocalVideo(video, res)) return
  14. if (!video.hasWebTorrentFiles()) {
  15. return res.fail({
  16. status: HttpStatusCode.BAD_REQUEST_400,
  17. message: 'This video does not have WebTorrent files'
  18. })
  19. }
  20. if (!video.getHLSPlaylist()) {
  21. return res.fail({
  22. status: HttpStatusCode.BAD_REQUEST_400,
  23. message: 'Cannot delete WebTorrent files since this video does not have HLS playlist'
  24. })
  25. }
  26. return next()
  27. }
  28. ]
  29. const videoFilesDeleteHLSValidator = [
  30. isValidVideoIdParam('id'),
  31. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  32. logger.debug('Checking videoFilesDeleteHLS parameters', { parameters: req.params })
  33. if (areValidationErrors(req, res)) return
  34. if (!await doesVideoExist(req.params.id, res)) return
  35. const video = res.locals.videoAll
  36. if (!checkLocalVideo(video, res)) return
  37. if (!video.getHLSPlaylist()) {
  38. return res.fail({
  39. status: HttpStatusCode.BAD_REQUEST_400,
  40. message: 'This video does not have HLS files'
  41. })
  42. }
  43. if (!video.hasWebTorrentFiles()) {
  44. return res.fail({
  45. status: HttpStatusCode.BAD_REQUEST_400,
  46. message: 'Cannot delete HLS playlist since this video does not have WebTorrent files'
  47. })
  48. }
  49. return next()
  50. }
  51. ]
  52. export {
  53. videoFilesDeleteWebTorrentValidator,
  54. videoFilesDeleteHLSValidator
  55. }
  56. // ---------------------------------------------------------------------------
  57. function checkLocalVideo (video: MVideo, res: express.Response) {
  58. if (video.remote) {
  59. res.fail({
  60. status: HttpStatusCode.BAD_REQUEST_400,
  61. message: 'Cannot delete files of remote video'
  62. })
  63. return false
  64. }
  65. return true
  66. }