]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-imports.ts
Merge branch 'release/4.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-imports.ts
CommitLineData
41fb13c3 1import express from 'express'
a3b472a1 2import { body, param, query } from 'express-validator'
f33e5159 3import { isResolvingToUnicastOnly } from '@server/helpers/dns'
2158ac90
RK
4import { isPreImportVideoAccepted } from '@server/lib/moderation'
5import { Hooks } from '@server/lib/plugins/hooks'
419b520c
C
6import { MUserAccountId, MVideoImport } from '@server/types/models'
7import { HttpStatusCode, UserRight, VideoImportState } from '@shared/models'
2158ac90 8import { VideoImportCreate } from '@shared/models/videos/import/video-import-create.model'
c8861d5d 9import { isIdValid, toIntOrNull } from '../../../helpers/custom-validators/misc'
6e46de09 10import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../../helpers/custom-validators/video-imports'
3e753302 11import { isVideoMagnetUriValid, isVideoNameValid } from '../../../helpers/custom-validators/videos'
2158ac90
RK
12import { cleanUpReqFiles } from '../../../helpers/express-utils'
13import { logger } from '../../../helpers/logger'
6dd9de95 14import { CONFIG } from '../../../initializers/config'
74dc3bca 15import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
419b520c 16import { areValidationErrors, doesVideoChannelOfAccountExist, doesVideoImportExist } from '../shared'
2158ac90 17import { getCommonVideoEditAttributes } from './videos'
fbad87b0 18
418d092a 19const videoImportAddValidator = getCommonVideoEditAttributes().concat([
fbad87b0 20 body('channelId')
c8861d5d 21 .customSanitizer(toIntOrNull)
396f6f01 22 .custom(isIdValid),
ce33919c
C
23 body('targetUrl')
24 .optional()
396f6f01 25 .custom(isVideoImportTargetUrlValid),
ce33919c
C
26 body('magnetUri')
27 .optional()
396f6f01 28 .custom(isVideoMagnetUriValid),
990b6a0b 29 body('torrentfile')
a1587156
C
30 .custom((value, { req }) => isVideoImportTorrentFile(req.files))
31 .withMessage(
32 'This torrent file is not supported or too large. Please, make sure it is of the following type: ' +
33 CONSTRAINTS_FIELDS.VIDEO_IMPORTS.TORRENT_FILE.EXTNAME.join(', ')
34 ),
fbad87b0
C
35 body('name')
36 .optional()
7dab0bd6
RK
37 .custom(isVideoNameValid).withMessage(
38 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
39 ),
fbad87b0
C
40
41 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
fbad87b0 42 const user = res.locals.oauth.token.User
faa9d434 43 const torrentFile = req.files?.['torrentfile'] ? req.files['torrentfile'][0] : undefined
fbad87b0
C
44
45 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
5d08a6a7 46
454c20fa 47 if (CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true && req.body.targetUrl) {
5d08a6a7 48 cleanUpReqFiles(req)
76148b27
RK
49
50 return res.fail({
51 status: HttpStatusCode.CONFLICT_409,
52 message: 'HTTP import is not enabled on this instance.'
53 })
5d08a6a7
C
54 }
55
a84b8fa5
C
56 if (CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED !== true && (req.body.magnetUri || torrentFile)) {
57 cleanUpReqFiles(req)
76148b27
RK
58
59 return res.fail({
60 status: HttpStatusCode.CONFLICT_409,
61 message: 'Torrent/magnet URI import is not enabled on this instance.'
62 })
a84b8fa5
C
63 }
64
0f6acda1 65 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
fbad87b0 66
ce33919c 67 // Check we have at least 1 required param
a84b8fa5 68 if (!req.body.targetUrl && !req.body.magnetUri && !torrentFile) {
ce33919c
C
69 cleanUpReqFiles(req)
70
76148b27 71 return res.fail({ message: 'Should have a magnetUri or a targetUrl or a torrent file.' })
ce33919c
C
72 }
73
7b54a81c
C
74 if (req.body.targetUrl) {
75 const hostname = new URL(req.body.targetUrl).hostname
76
f33e5159
C
77 if (await isResolvingToUnicastOnly(hostname) !== true) {
78 cleanUpReqFiles(req)
7b54a81c 79
f33e5159
C
80 return res.fail({
81 status: HttpStatusCode.FORBIDDEN_403,
82 message: 'Cannot use non unicast IP as targetUrl.'
83 })
7b54a81c
C
84 }
85 }
86
2158ac90
RK
87 if (!await isImportAccepted(req, res)) return cleanUpReqFiles(req)
88
fbad87b0
C
89 return next()
90 }
91])
92
a3b472a1
C
93const getMyVideoImportsValidator = [
94 query('videoChannelSyncId')
95 .optional()
396f6f01 96 .custom(isIdValid),
a3b472a1
C
97
98 (req: express.Request, res: express.Response, next: express.NextFunction) => {
a3b472a1
C
99 if (areValidationErrors(req, res)) return
100
101 return next()
102 }
103]
104
419b520c
C
105const videoImportDeleteValidator = [
106 param('id')
396f6f01 107 .custom(isIdValid),
419b520c
C
108
109 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
419b520c
C
110 if (areValidationErrors(req, res)) return
111
112 if (!await doesVideoImportExist(parseInt(req.params.id), res)) return
113 if (!checkUserCanManageImport(res.locals.oauth.token.user, res.locals.videoImport, res)) return
114
115 if (res.locals.videoImport.state === VideoImportState.PENDING) {
116 return res.fail({
117 status: HttpStatusCode.CONFLICT_409,
118 message: 'Cannot delete a pending video import. Cancel it or wait for the end of the import first.'
119 })
120 }
121
122 return next()
123 }
124]
125
126const videoImportCancelValidator = [
127 param('id')
396f6f01 128 .custom(isIdValid),
419b520c
C
129
130 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
419b520c
C
131 if (areValidationErrors(req, res)) return
132
133 if (!await doesVideoImportExist(parseInt(req.params.id), res)) return
134 if (!checkUserCanManageImport(res.locals.oauth.token.user, res.locals.videoImport, res)) return
135
136 if (res.locals.videoImport.state !== VideoImportState.PENDING) {
137 return res.fail({
138 status: HttpStatusCode.CONFLICT_409,
139 message: 'Cannot cancel a non pending video import.'
140 })
141 }
142
143 return next()
144 }
145]
146
fbad87b0
C
147// ---------------------------------------------------------------------------
148
149export {
419b520c
C
150 videoImportAddValidator,
151 videoImportCancelValidator,
a3b472a1
C
152 videoImportDeleteValidator,
153 getMyVideoImportsValidator
fbad87b0
C
154}
155
156// ---------------------------------------------------------------------------
2158ac90
RK
157
158async function isImportAccepted (req: express.Request, res: express.Response) {
159 const body: VideoImportCreate = req.body
160 const hookName = body.targetUrl
161 ? 'filter:api.video.pre-import-url.accept.result'
162 : 'filter:api.video.pre-import-torrent.accept.result'
163
164 // Check we accept this video
165 const acceptParameters = {
166 videoImportBody: body,
167 user: res.locals.oauth.token.User
168 }
169 const acceptedResult = await Hooks.wrapFun(
170 isPreImportVideoAccepted,
171 acceptParameters,
172 hookName
173 )
174
175 if (!acceptedResult || acceptedResult.accepted !== true) {
176 logger.info('Refused to import video.', { acceptedResult, acceptParameters })
2158ac90 177
76148b27
RK
178 res.fail({
179 status: HttpStatusCode.FORBIDDEN_403,
180 message: acceptedResult.errorMessage || 'Refused to import video'
181 })
2158ac90
RK
182 return false
183 }
184
185 return true
186}
419b520c
C
187
188function checkUserCanManageImport (user: MUserAccountId, videoImport: MVideoImport, res: express.Response) {
189 if (user.hasRight(UserRight.MANAGE_VIDEO_IMPORTS) === false && videoImport.userId !== user.id) {
190 res.fail({
191 status: HttpStatusCode.FORBIDDEN_403,
192 message: 'Cannot manage video import of another user'
193 })
194 return false
195 }
196
197 return true
198}