2022-06-21 08:31:25 -05:00
|
|
|
import express from 'express'
|
2023-07-19 09:02:49 -05:00
|
|
|
import { body, header } from 'express-validator'
|
|
|
|
import { getResumableUploadPath } from '@server/helpers/upload'
|
2022-06-21 08:31:25 -05:00
|
|
|
import { getVideoWithAttributes } from '@server/helpers/video'
|
2023-07-19 09:02:49 -05:00
|
|
|
import { CONFIG } from '@server/initializers/config'
|
|
|
|
import { uploadx } from '@server/lib/uploadx'
|
2022-06-21 08:31:25 -05:00
|
|
|
import { VideoSourceModel } from '@server/models/video/video-source'
|
|
|
|
import { MVideoFullLight } from '@server/types/models'
|
|
|
|
import { HttpStatusCode, UserRight } from '@shared/models'
|
2023-07-19 09:02:49 -05:00
|
|
|
import { Metadata as UploadXMetadata } from '@uploadx/core'
|
|
|
|
import { logger } from '../../../helpers/logger'
|
2022-06-21 08:31:25 -05:00
|
|
|
import { areValidationErrors, checkUserCanManageVideo, doesVideoExist, isValidVideoIdParam } from '../shared'
|
2023-07-19 09:02:49 -05:00
|
|
|
import { addDurationToVideoFileIfNeeded, checkVideoFileCanBeEdited, commonVideoFileChecks, isVideoFileAccepted } from './shared'
|
2022-06-21 08:31:25 -05:00
|
|
|
|
2023-07-19 09:02:49 -05:00
|
|
|
export const videoSourceGetLatestValidator = [
|
2022-06-21 08:31:25 -05:00
|
|
|
isValidVideoIdParam('id'),
|
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
2023-07-19 09:02:49 -05:00
|
|
|
if (!await doesVideoExist(req.params.id, res, 'all')) return
|
2022-06-21 08:31:25 -05:00
|
|
|
|
|
|
|
const video = getVideoWithAttributes(res) as MVideoFullLight
|
|
|
|
|
2023-07-19 09:02:49 -05:00
|
|
|
const user = res.locals.oauth.token.User
|
|
|
|
if (!checkUserCanManageVideo(user, video, UserRight.UPDATE_ANY_VIDEO, res)) return
|
|
|
|
|
|
|
|
res.locals.videoSource = await VideoSourceModel.loadLatest(video.id)
|
|
|
|
|
2022-06-21 08:31:25 -05:00
|
|
|
if (!res.locals.videoSource) {
|
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.NOT_FOUND_404,
|
|
|
|
message: 'Video source not found'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-19 09:02:49 -05:00
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
export const replaceVideoSourceResumableValidator = [
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
const body: express.CustomUploadXFile<UploadXMetadata> = req.body
|
|
|
|
const file = { ...body, duration: undefined, path: getResumableUploadPath(body.name), filename: body.metadata.filename }
|
|
|
|
const cleanup = () => uploadx.storage.delete(file).catch(err => logger.error('Cannot delete the file %s', file.name, { err }))
|
|
|
|
|
|
|
|
if (!await checkCanUpdateVideoFile({ req, res })) {
|
|
|
|
return cleanup()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!await addDurationToVideoFileIfNeeded({ videoFile: file, res, middlewareName: 'updateVideoFileResumableValidator' })) {
|
|
|
|
return cleanup()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!await isVideoFileAccepted({ req, res, videoFile: file, hook: 'filter:api.video.update-file.accept.result' })) {
|
|
|
|
return cleanup()
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.updateVideoFileResumable = { ...file, originalname: file.filename }
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
export const replaceVideoSourceResumableInitValidator = [
|
|
|
|
body('filename')
|
|
|
|
.exists(),
|
|
|
|
|
|
|
|
header('x-upload-content-length')
|
|
|
|
.isNumeric()
|
|
|
|
.exists()
|
|
|
|
.withMessage('Should specify the file length'),
|
|
|
|
header('x-upload-content-type')
|
|
|
|
.isString()
|
|
|
|
.exists()
|
|
|
|
.withMessage('Should specify the file mimetype'),
|
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2022-06-21 08:31:25 -05:00
|
|
|
const user = res.locals.oauth.token.User
|
2023-07-19 09:02:49 -05:00
|
|
|
|
|
|
|
logger.debug('Checking updateVideoFileResumableInitValidator parameters and headers', {
|
|
|
|
parameters: req.body,
|
|
|
|
headers: req.headers
|
|
|
|
})
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res, { omitLog: true })) return
|
|
|
|
|
|
|
|
if (!await checkCanUpdateVideoFile({ req, res })) return
|
|
|
|
|
|
|
|
const videoFileMetadata = {
|
|
|
|
mimetype: req.headers['x-upload-content-type'] as string,
|
|
|
|
size: +req.headers['x-upload-content-length'],
|
|
|
|
originalname: req.body.filename
|
|
|
|
}
|
|
|
|
|
|
|
|
const files = { videofile: [ videoFileMetadata ] }
|
|
|
|
if (await commonVideoFileChecks({ res, user, videoFileSize: videoFileMetadata.size, files }) === false) return
|
2022-06-21 08:31:25 -05:00
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2023-07-19 09:02:49 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Private
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function checkCanUpdateVideoFile (options: {
|
|
|
|
req: express.Request
|
|
|
|
res: express.Response
|
|
|
|
}) {
|
|
|
|
const { req, res } = options
|
|
|
|
|
|
|
|
if (!CONFIG.VIDEO_FILE.UPDATE.ENABLED) {
|
|
|
|
res.fail({
|
|
|
|
status: HttpStatusCode.FORBIDDEN_403,
|
|
|
|
message: 'Updating the file of an existing video is not allowed on this instance'
|
|
|
|
})
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!await doesVideoExist(req.params.id, res)) return false
|
|
|
|
|
|
|
|
const user = res.locals.oauth.token.User
|
|
|
|
const video = res.locals.videoAll
|
|
|
|
|
|
|
|
if (!checkUserCanManageVideo(user, video, UserRight.UPDATE_ANY_VIDEO, res)) return false
|
|
|
|
|
|
|
|
if (!checkVideoFileCanBeEdited(video, res)) return false
|
|
|
|
|
|
|
|
return true
|
2022-06-21 08:31:25 -05:00
|
|
|
}
|