aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-03-04 10:57:36 +0100
committerChocobozzz <me@florianbigard.com>2022-03-04 10:57:36 +0100
commitd3d3deaa7aae49d6d949aebcba015af75d292ccc (patch)
tree42e487810f5b7362c28f744371d160a4effc7566 /server/controllers/api
parent90370f7cc85ee4b45eb0d1bafed2d9d68610df9f (diff)
downloadPeerTube-d3d3deaa7aae49d6d949aebcba015af75d292ccc.tar.gz
PeerTube-d3d3deaa7aae49d6d949aebcba015af75d292ccc.tar.zst
PeerTube-d3d3deaa7aae49d6d949aebcba015af75d292ccc.zip
Refactor user build and express file middlewares
Diffstat (limited to 'server/controllers/api')
-rw-r--r--server/controllers/api/users/index.ts37
-rw-r--r--server/controllers/api/users/me.ts2
-rw-r--r--server/controllers/api/video-channel.ts5
-rw-r--r--server/controllers/api/video-playlist.ts2
-rw-r--r--server/controllers/api/videos/captions.ts11
-rw-r--r--server/controllers/api/videos/editor.ts2
-rw-r--r--server/controllers/api/videos/import.ts7
-rw-r--r--server/controllers/api/videos/live.ts10
-rw-r--r--server/controllers/api/videos/update.ts10
-rw-r--r--server/controllers/api/videos/upload.ts15
10 files changed, 24 insertions, 77 deletions
diff --git a/server/controllers/api/users/index.ts b/server/controllers/api/users/index.ts
index 7efc3a137..8a06bfe93 100644
--- a/server/controllers/api/users/index.ts
+++ b/server/controllers/api/users/index.ts
@@ -3,8 +3,9 @@ import RateLimit from 'express-rate-limit'
3import { tokensRouter } from '@server/controllers/api/users/token' 3import { tokensRouter } from '@server/controllers/api/users/token'
4import { Hooks } from '@server/lib/plugins/hooks' 4import { Hooks } from '@server/lib/plugins/hooks'
5import { OAuthTokenModel } from '@server/models/oauth/oauth-token' 5import { OAuthTokenModel } from '@server/models/oauth/oauth-token'
6import { MUser, MUserAccountDefault } from '@server/types/models' 6import { MUserAccountDefault } from '@server/types/models'
7import { HttpStatusCode, UserAdminFlag, UserCreate, UserCreateResult, UserRegister, UserRight, UserRole, UserUpdate } from '@shared/models' 7import { pick } from '@shared/core-utils'
8import { HttpStatusCode, UserCreate, UserCreateResult, UserRegister, UserRight, UserUpdate } from '@shared/models'
8import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '../../../helpers/audit-logger' 9import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '../../../helpers/audit-logger'
9import { logger } from '../../../helpers/logger' 10import { logger } from '../../../helpers/logger'
10import { generateRandomString, getFormattedObjects } from '../../../helpers/utils' 11import { generateRandomString, getFormattedObjects } from '../../../helpers/utils'
@@ -14,7 +15,7 @@ import { sequelizeTypescript } from '../../../initializers/database'
14import { Emailer } from '../../../lib/emailer' 15import { Emailer } from '../../../lib/emailer'
15import { Notifier } from '../../../lib/notifier' 16import { Notifier } from '../../../lib/notifier'
16import { Redis } from '../../../lib/redis' 17import { Redis } from '../../../lib/redis'
17import { createUserAccountAndChannelAndPlaylist, sendVerifyUserEmail } from '../../../lib/user' 18import { buildUser, createUserAccountAndChannelAndPlaylist, sendVerifyUserEmail } from '../../../lib/user'
18import { 19import {
19 asyncMiddleware, 20 asyncMiddleware,
20 asyncRetryTransactionMiddleware, 21 asyncRetryTransactionMiddleware,
@@ -175,18 +176,11 @@ export {
175async function createUser (req: express.Request, res: express.Response) { 176async function createUser (req: express.Request, res: express.Response) {
176 const body: UserCreate = req.body 177 const body: UserCreate = req.body
177 178
178 const userToCreate = new UserModel({ 179 const userToCreate = buildUser({
179 username: body.username, 180 ...pick(body, [ 'username', 'password', 'email', 'role', 'videoQuota', 'videoQuotaDaily', 'adminFlags' ]),
180 password: body.password, 181
181 email: body.email, 182 emailVerified: null
182 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, 183 })
183 p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED,
184 autoPlayVideo: true,
185 role: body.role,
186 videoQuota: body.videoQuota,
187 videoQuotaDaily: body.videoQuotaDaily,
188 adminFlags: body.adminFlags || UserAdminFlag.NONE
189 }) as MUser
190 184
191 // NB: due to the validator usersAddValidator, password==='' can only be true if we can send the mail. 185 // NB: due to the validator usersAddValidator, password==='' can only be true if we can send the mail.
192 const createPassword = userToCreate.password === '' 186 const createPassword = userToCreate.password === ''
@@ -225,16 +219,9 @@ async function createUser (req: express.Request, res: express.Response) {
225async function registerUser (req: express.Request, res: express.Response) { 219async function registerUser (req: express.Request, res: express.Response) {
226 const body: UserRegister = req.body 220 const body: UserRegister = req.body
227 221
228 const userToCreate = new UserModel({ 222 const userToCreate = buildUser({
229 username: body.username, 223 ...pick(body, [ 'username', 'password', 'email' ]),
230 password: body.password, 224
231 email: body.email,
232 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
233 p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED,
234 autoPlayVideo: true,
235 role: UserRole.USER,
236 videoQuota: CONFIG.USER.VIDEO_QUOTA,
237 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY,
238 emailVerified: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION ? false : null 225 emailVerified: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION ? false : null
239 }) 226 })
240 227
diff --git a/server/controllers/api/users/me.ts b/server/controllers/api/users/me.ts
index a1d621152..595abcf95 100644
--- a/server/controllers/api/users/me.ts
+++ b/server/controllers/api/users/me.ts
@@ -35,7 +35,7 @@ import { VideoImportModel } from '../../../models/video/video-import'
35 35
36const auditLogger = auditLoggerFactory('users') 36const auditLogger = auditLoggerFactory('users')
37 37
38const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.TMP_DIR }) 38const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT)
39 39
40const meRouter = express.Router() 40const meRouter = express.Router()
41 41
diff --git a/server/controllers/api/video-channel.ts b/server/controllers/api/video-channel.ts
index 2f869d9b3..2454b1ec9 100644
--- a/server/controllers/api/video-channel.ts
+++ b/server/controllers/api/video-channel.ts
@@ -12,7 +12,6 @@ import { resetSequelizeInstance } from '../../helpers/database-utils'
12import { buildNSFWFilter, createReqFiles, getCountVideos, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils' 12import { buildNSFWFilter, createReqFiles, getCountVideos, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
13import { logger } from '../../helpers/logger' 13import { logger } from '../../helpers/logger'
14import { getFormattedObjects } from '../../helpers/utils' 14import { getFormattedObjects } from '../../helpers/utils'
15import { CONFIG } from '../../initializers/config'
16import { MIMETYPES } from '../../initializers/constants' 15import { MIMETYPES } from '../../initializers/constants'
17import { sequelizeTypescript } from '../../initializers/database' 16import { sequelizeTypescript } from '../../initializers/database'
18import { sendUpdateActor } from '../../lib/activitypub/send' 17import { sendUpdateActor } from '../../lib/activitypub/send'
@@ -51,8 +50,8 @@ import { VideoChannelModel } from '../../models/video/video-channel'
51import { VideoPlaylistModel } from '../../models/video/video-playlist' 50import { VideoPlaylistModel } from '../../models/video/video-playlist'
52 51
53const auditLogger = auditLoggerFactory('channels') 52const auditLogger = auditLoggerFactory('channels')
54const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.TMP_DIR }) 53const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT)
55const reqBannerFile = createReqFiles([ 'bannerfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { bannerfile: CONFIG.STORAGE.TMP_DIR }) 54const reqBannerFile = createReqFiles([ 'bannerfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT)
56 55
57const videoChannelRouter = express.Router() 56const videoChannelRouter = express.Router()
58 57
diff --git a/server/controllers/api/video-playlist.ts b/server/controllers/api/video-playlist.ts
index ee6c73855..1255d14c6 100644
--- a/server/controllers/api/video-playlist.ts
+++ b/server/controllers/api/video-playlist.ts
@@ -47,7 +47,7 @@ import { AccountModel } from '../../models/account/account'
47import { VideoPlaylistModel } from '../../models/video/video-playlist' 47import { VideoPlaylistModel } from '../../models/video/video-playlist'
48import { VideoPlaylistElementModel } from '../../models/video/video-playlist-element' 48import { VideoPlaylistElementModel } from '../../models/video/video-playlist-element'
49 49
50const reqThumbnailFile = createReqFiles([ 'thumbnailfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { thumbnailfile: CONFIG.STORAGE.TMP_DIR }) 50const reqThumbnailFile = createReqFiles([ 'thumbnailfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT)
51 51
52const videoPlaylistRouter = express.Router() 52const videoPlaylistRouter = express.Router()
53 53
diff --git a/server/controllers/api/videos/captions.ts b/server/controllers/api/videos/captions.ts
index 2a9a9d233..2b511a398 100644
--- a/server/controllers/api/videos/captions.ts
+++ b/server/controllers/api/videos/captions.ts
@@ -1,26 +1,19 @@
1import express from 'express' 1import express from 'express'
2import { Hooks } from '@server/lib/plugins/hooks'
2import { MVideoCaption } from '@server/types/models' 3import { MVideoCaption } from '@server/types/models'
3import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' 4import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
4import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils' 5import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils'
5import { createReqFiles } from '../../../helpers/express-utils' 6import { createReqFiles } from '../../../helpers/express-utils'
6import { logger } from '../../../helpers/logger' 7import { logger } from '../../../helpers/logger'
7import { getFormattedObjects } from '../../../helpers/utils' 8import { getFormattedObjects } from '../../../helpers/utils'
8import { CONFIG } from '../../../initializers/config'
9import { MIMETYPES } from '../../../initializers/constants' 9import { MIMETYPES } from '../../../initializers/constants'
10import { sequelizeTypescript } from '../../../initializers/database' 10import { sequelizeTypescript } from '../../../initializers/database'
11import { federateVideoIfNeeded } from '../../../lib/activitypub/videos' 11import { federateVideoIfNeeded } from '../../../lib/activitypub/videos'
12import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares' 12import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares'
13import { addVideoCaptionValidator, deleteVideoCaptionValidator, listVideoCaptionsValidator } from '../../../middlewares/validators' 13import { addVideoCaptionValidator, deleteVideoCaptionValidator, listVideoCaptionsValidator } from '../../../middlewares/validators'
14import { VideoCaptionModel } from '../../../models/video/video-caption' 14import { VideoCaptionModel } from '../../../models/video/video-caption'
15import { Hooks } from '@server/lib/plugins/hooks'
16 15
17const reqVideoCaptionAdd = createReqFiles( 16const reqVideoCaptionAdd = createReqFiles([ 'captionfile' ], MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT)
18 [ 'captionfile' ],
19 MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT,
20 {
21 captionfile: CONFIG.STORAGE.CAPTIONS_DIR
22 }
23)
24 17
25const videoCaptionsRouter = express.Router() 18const videoCaptionsRouter = express.Router()
26 19
diff --git a/server/controllers/api/videos/editor.ts b/server/controllers/api/videos/editor.ts
index 61e2eb5da..588cc1a8c 100644
--- a/server/controllers/api/videos/editor.ts
+++ b/server/controllers/api/videos/editor.ts
@@ -1,6 +1,5 @@
1import express from 'express' 1import express from 'express'
2import { createAnyReqFiles } from '@server/helpers/express-utils' 2import { createAnyReqFiles } from '@server/helpers/express-utils'
3import { CONFIG } from '@server/initializers/config'
4import { MIMETYPES } from '@server/initializers/constants' 3import { MIMETYPES } from '@server/initializers/constants'
5import { JobQueue } from '@server/lib/job-queue' 4import { JobQueue } from '@server/lib/job-queue'
6import { buildTaskFileFieldname, getTaskFile } from '@server/lib/video-editor' 5import { buildTaskFileFieldname, getTaskFile } from '@server/lib/video-editor'
@@ -21,7 +20,6 @@ const editorRouter = express.Router()
21 20
22const tasksFiles = createAnyReqFiles( 21const tasksFiles = createAnyReqFiles(
23 MIMETYPES.VIDEO.MIMETYPE_EXT, 22 MIMETYPES.VIDEO.MIMETYPE_EXT,
24 CONFIG.STORAGE.TMP_DIR,
25 (req: express.Request, file: Express.Multer.File, cb: (err: Error, result?: boolean) => void) => { 23 (req: express.Request, file: Express.Multer.File, cb: (err: Error, result?: boolean) => void) => {
26 const body = req.body as VideoEditorCreateEdition 24 const body = req.body as VideoEditorCreateEdition
27 25
diff --git a/server/controllers/api/videos/import.ts b/server/controllers/api/videos/import.ts
index b54fa822c..44283e266 100644
--- a/server/controllers/api/videos/import.ts
+++ b/server/controllers/api/videos/import.ts
@@ -61,12 +61,7 @@ const videoImportsRouter = express.Router()
61 61
62const reqVideoFileImport = createReqFiles( 62const reqVideoFileImport = createReqFiles(
63 [ 'thumbnailfile', 'previewfile', 'torrentfile' ], 63 [ 'thumbnailfile', 'previewfile', 'torrentfile' ],
64 Object.assign({}, MIMETYPES.TORRENT.MIMETYPE_EXT, MIMETYPES.IMAGE.MIMETYPE_EXT), 64 { ...MIMETYPES.TORRENT.MIMETYPE_EXT, ...MIMETYPES.IMAGE.MIMETYPE_EXT }
65 {
66 thumbnailfile: CONFIG.STORAGE.TMP_DIR,
67 previewfile: CONFIG.STORAGE.TMP_DIR,
68 torrentfile: CONFIG.STORAGE.TMP_DIR
69 }
70) 65)
71 66
72videoImportsRouter.post('/imports', 67videoImportsRouter.post('/imports',
diff --git a/server/controllers/api/videos/live.ts b/server/controllers/api/videos/live.ts
index 8b8cacff9..49cabb6f3 100644
--- a/server/controllers/api/videos/live.ts
+++ b/server/controllers/api/videos/live.ts
@@ -1,6 +1,5 @@
1import express from 'express' 1import express from 'express'
2import { createReqFiles } from '@server/helpers/express-utils' 2import { createReqFiles } from '@server/helpers/express-utils'
3import { CONFIG } from '@server/initializers/config'
4import { ASSETS_PATH, MIMETYPES } from '@server/initializers/constants' 3import { ASSETS_PATH, MIMETYPES } from '@server/initializers/constants'
5import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url' 4import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url'
6import { federateVideoIfNeeded } from '@server/lib/activitypub/videos' 5import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
@@ -19,14 +18,7 @@ import { VideoModel } from '../../../models/video/video'
19 18
20const liveRouter = express.Router() 19const liveRouter = express.Router()
21 20
22const reqVideoFileLive = createReqFiles( 21const reqVideoFileLive = createReqFiles([ 'thumbnailfile', 'previewfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT)
23 [ 'thumbnailfile', 'previewfile' ],
24 MIMETYPES.IMAGE.MIMETYPE_EXT,
25 {
26 thumbnailfile: CONFIG.STORAGE.TMP_DIR,
27 previewfile: CONFIG.STORAGE.TMP_DIR
28 }
29)
30 22
31liveRouter.post('/live', 23liveRouter.post('/live',
32 authenticate, 24 authenticate,
diff --git a/server/controllers/api/videos/update.ts b/server/controllers/api/videos/update.ts
index f600847d4..8906003fc 100644
--- a/server/controllers/api/videos/update.ts
+++ b/server/controllers/api/videos/update.ts
@@ -11,7 +11,6 @@ import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../
11import { resetSequelizeInstance } from '../../../helpers/database-utils' 11import { resetSequelizeInstance } from '../../../helpers/database-utils'
12import { createReqFiles } from '../../../helpers/express-utils' 12import { createReqFiles } from '../../../helpers/express-utils'
13import { logger, loggerTagsFactory } from '../../../helpers/logger' 13import { logger, loggerTagsFactory } from '../../../helpers/logger'
14import { CONFIG } from '../../../initializers/config'
15import { MIMETYPES } from '../../../initializers/constants' 14import { MIMETYPES } from '../../../initializers/constants'
16import { sequelizeTypescript } from '../../../initializers/database' 15import { sequelizeTypescript } from '../../../initializers/database'
17import { federateVideoIfNeeded } from '../../../lib/activitypub/videos' 16import { federateVideoIfNeeded } from '../../../lib/activitypub/videos'
@@ -26,14 +25,7 @@ const lTags = loggerTagsFactory('api', 'video')
26const auditLogger = auditLoggerFactory('videos') 25const auditLogger = auditLoggerFactory('videos')
27const updateRouter = express.Router() 26const updateRouter = express.Router()
28 27
29const reqVideoFileUpdate = createReqFiles( 28const reqVideoFileUpdate = createReqFiles([ 'thumbnailfile', 'previewfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT)
30 [ 'thumbnailfile', 'previewfile' ],
31 MIMETYPES.IMAGE.MIMETYPE_EXT,
32 {
33 thumbnailfile: CONFIG.STORAGE.TMP_DIR,
34 previewfile: CONFIG.STORAGE.TMP_DIR
35 }
36)
37 29
38updateRouter.put('/:id', 30updateRouter.put('/:id',
39 openapiOperationDoc({ operationId: 'putVideo' }), 31 openapiOperationDoc({ operationId: 'putVideo' }),
diff --git a/server/controllers/api/videos/upload.ts b/server/controllers/api/videos/upload.ts
index 3c026ad1f..dd69cf238 100644
--- a/server/controllers/api/videos/upload.ts
+++ b/server/controllers/api/videos/upload.ts
@@ -24,9 +24,8 @@ import { HttpStatusCode, VideoCreate, VideoResolution, VideoState } from '@share
24import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' 24import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger'
25import { retryTransactionWrapper } from '../../../helpers/database-utils' 25import { retryTransactionWrapper } from '../../../helpers/database-utils'
26import { createReqFiles } from '../../../helpers/express-utils' 26import { createReqFiles } from '../../../helpers/express-utils'
27import { ffprobePromise, buildFileMetadata, getVideoStreamFPS, getVideoStreamDimensionsInfo } from '../../../helpers/ffmpeg' 27import { buildFileMetadata, ffprobePromise, getVideoStreamDimensionsInfo, getVideoStreamFPS } from '../../../helpers/ffmpeg'
28import { logger, loggerTagsFactory } from '../../../helpers/logger' 28import { logger, loggerTagsFactory } from '../../../helpers/logger'
29import { CONFIG } from '../../../initializers/config'
30import { MIMETYPES } from '../../../initializers/constants' 29import { MIMETYPES } from '../../../initializers/constants'
31import { sequelizeTypescript } from '../../../initializers/database' 30import { sequelizeTypescript } from '../../../initializers/database'
32import { federateVideoIfNeeded } from '../../../lib/activitypub/videos' 31import { federateVideoIfNeeded } from '../../../lib/activitypub/videos'
@@ -52,21 +51,13 @@ const uploadRouter = express.Router()
52 51
53const reqVideoFileAdd = createReqFiles( 52const reqVideoFileAdd = createReqFiles(
54 [ 'videofile', 'thumbnailfile', 'previewfile' ], 53 [ 'videofile', 'thumbnailfile', 'previewfile' ],
55 Object.assign({}, MIMETYPES.VIDEO.MIMETYPE_EXT, MIMETYPES.IMAGE.MIMETYPE_EXT), 54 { ...MIMETYPES.VIDEO.MIMETYPE_EXT, ...MIMETYPES.IMAGE.MIMETYPE_EXT }
56 {
57 videofile: CONFIG.STORAGE.TMP_DIR,
58 thumbnailfile: CONFIG.STORAGE.TMP_DIR,
59 previewfile: CONFIG.STORAGE.TMP_DIR
60 }
61) 55)
62 56
63const reqVideoFileAddResumable = createReqFiles( 57const reqVideoFileAddResumable = createReqFiles(
64 [ 'thumbnailfile', 'previewfile' ], 58 [ 'thumbnailfile', 'previewfile' ],
65 MIMETYPES.IMAGE.MIMETYPE_EXT, 59 MIMETYPES.IMAGE.MIMETYPE_EXT,
66 { 60 getResumableUploadPath()
67 thumbnailfile: getResumableUploadPath(),
68 previewfile: getResumableUploadPath()
69 }
70) 61)
71 62
72uploadRouter.post('/upload', 63uploadRouter.post('/upload',