aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/runners/job-files.ts
blob: 57c27fcfec384206dca60f091aeced632239f112 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import express from 'express'
import { param } from 'express-validator'
import { basename } from 'path'
import { isSafeFilename } from '@server/helpers/custom-validators/misc'
import { hasVideoStudioTaskFile, HttpStatusCode, RunnerJobStudioTranscodingPayload } from '@shared/models'
import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared'

const tags = [ 'runner' ]

export const runnerJobGetVideoTranscodingFileValidator = [
  isValidVideoIdParam('videoId'),

  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
    if (areValidationErrors(req, res)) return

    if (!await doesVideoExist(req.params.videoId, res, 'all')) return

    const runnerJob = res.locals.runnerJob

    if (runnerJob.privatePayload.videoUUID !== res.locals.videoAll.uuid) {
      return res.fail({
        status: HttpStatusCode.FORBIDDEN_403,
        message: 'Job is not associated to this video',
        tags: [ ...tags, res.locals.videoAll.uuid ]
      })
    }

    return next()
  }
]

export const runnerJobGetVideoStudioTaskFileValidator = [
  param('filename').custom(v => isSafeFilename(v)),

  (req: express.Request, res: express.Response, next: express.NextFunction) => {
    if (areValidationErrors(req, res)) return

    const filename = req.params.filename

    const payload = res.locals.runnerJob.payload as RunnerJobStudioTranscodingPayload

    const found = Array.isArray(payload?.tasks) && payload.tasks.some(t => {
      if (hasVideoStudioTaskFile(t)) {
        return basename(t.options.file) === filename
      }

      return false
    })

    if (!found) {
      return res.fail({
        status: HttpStatusCode.BAD_REQUEST_400,
        message: 'File is not associated to this edition task',
        tags: [ ...tags, res.locals.videoAll.uuid ]
      })
    }

    return next()
  }
]