diff options
author | Chocobozzz <me@florianbigard.com> | 2022-03-04 10:57:36 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2022-03-04 10:57:36 +0100 |
commit | d3d3deaa7aae49d6d949aebcba015af75d292ccc (patch) | |
tree | 42e487810f5b7362c28f744371d160a4effc7566 /server | |
parent | 90370f7cc85ee4b45eb0d1bafed2d9d68610df9f (diff) | |
download | PeerTube-d3d3deaa7aae49d6d949aebcba015af75d292ccc.tar.gz PeerTube-d3d3deaa7aae49d6d949aebcba015af75d292ccc.tar.zst PeerTube-d3d3deaa7aae49d6d949aebcba015af75d292ccc.zip |
Refactor user build and express file middlewares
Diffstat (limited to 'server')
-rw-r--r-- | server/controllers/api/users/index.ts | 37 | ||||
-rw-r--r-- | server/controllers/api/users/me.ts | 2 | ||||
-rw-r--r-- | server/controllers/api/video-channel.ts | 5 | ||||
-rw-r--r-- | server/controllers/api/video-playlist.ts | 2 | ||||
-rw-r--r-- | server/controllers/api/videos/captions.ts | 11 | ||||
-rw-r--r-- | server/controllers/api/videos/editor.ts | 2 | ||||
-rw-r--r-- | server/controllers/api/videos/import.ts | 7 | ||||
-rw-r--r-- | server/controllers/api/videos/live.ts | 10 | ||||
-rw-r--r-- | server/controllers/api/videos/update.ts | 10 | ||||
-rw-r--r-- | server/controllers/api/videos/upload.ts | 15 | ||||
-rw-r--r-- | server/helpers/express-utils.ts | 7 | ||||
-rw-r--r-- | server/initializers/installer.ts | 12 | ||||
-rw-r--r-- | server/lib/auth/oauth-model.ts | 20 | ||||
-rw-r--r-- | server/lib/user.ts | 57 |
14 files changed, 91 insertions, 106 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' | |||
3 | import { tokensRouter } from '@server/controllers/api/users/token' | 3 | import { tokensRouter } from '@server/controllers/api/users/token' |
4 | import { Hooks } from '@server/lib/plugins/hooks' | 4 | import { Hooks } from '@server/lib/plugins/hooks' |
5 | import { OAuthTokenModel } from '@server/models/oauth/oauth-token' | 5 | import { OAuthTokenModel } from '@server/models/oauth/oauth-token' |
6 | import { MUser, MUserAccountDefault } from '@server/types/models' | 6 | import { MUserAccountDefault } from '@server/types/models' |
7 | import { HttpStatusCode, UserAdminFlag, UserCreate, UserCreateResult, UserRegister, UserRight, UserRole, UserUpdate } from '@shared/models' | 7 | import { pick } from '@shared/core-utils' |
8 | import { HttpStatusCode, UserCreate, UserCreateResult, UserRegister, UserRight, UserUpdate } from '@shared/models' | ||
8 | import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '../../../helpers/audit-logger' | 9 | import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '../../../helpers/audit-logger' |
9 | import { logger } from '../../../helpers/logger' | 10 | import { logger } from '../../../helpers/logger' |
10 | import { generateRandomString, getFormattedObjects } from '../../../helpers/utils' | 11 | import { generateRandomString, getFormattedObjects } from '../../../helpers/utils' |
@@ -14,7 +15,7 @@ import { sequelizeTypescript } from '../../../initializers/database' | |||
14 | import { Emailer } from '../../../lib/emailer' | 15 | import { Emailer } from '../../../lib/emailer' |
15 | import { Notifier } from '../../../lib/notifier' | 16 | import { Notifier } from '../../../lib/notifier' |
16 | import { Redis } from '../../../lib/redis' | 17 | import { Redis } from '../../../lib/redis' |
17 | import { createUserAccountAndChannelAndPlaylist, sendVerifyUserEmail } from '../../../lib/user' | 18 | import { buildUser, createUserAccountAndChannelAndPlaylist, sendVerifyUserEmail } from '../../../lib/user' |
18 | import { | 19 | import { |
19 | asyncMiddleware, | 20 | asyncMiddleware, |
20 | asyncRetryTransactionMiddleware, | 21 | asyncRetryTransactionMiddleware, |
@@ -175,18 +176,11 @@ export { | |||
175 | async function createUser (req: express.Request, res: express.Response) { | 176 | async 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) { | |||
225 | async function registerUser (req: express.Request, res: express.Response) { | 219 | async 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 | ||
36 | const auditLogger = auditLoggerFactory('users') | 36 | const auditLogger = auditLoggerFactory('users') |
37 | 37 | ||
38 | const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.TMP_DIR }) | 38 | const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT) |
39 | 39 | ||
40 | const meRouter = express.Router() | 40 | const 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' | |||
12 | import { buildNSFWFilter, createReqFiles, getCountVideos, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils' | 12 | import { buildNSFWFilter, createReqFiles, getCountVideos, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils' |
13 | import { logger } from '../../helpers/logger' | 13 | import { logger } from '../../helpers/logger' |
14 | import { getFormattedObjects } from '../../helpers/utils' | 14 | import { getFormattedObjects } from '../../helpers/utils' |
15 | import { CONFIG } from '../../initializers/config' | ||
16 | import { MIMETYPES } from '../../initializers/constants' | 15 | import { MIMETYPES } from '../../initializers/constants' |
17 | import { sequelizeTypescript } from '../../initializers/database' | 16 | import { sequelizeTypescript } from '../../initializers/database' |
18 | import { sendUpdateActor } from '../../lib/activitypub/send' | 17 | import { sendUpdateActor } from '../../lib/activitypub/send' |
@@ -51,8 +50,8 @@ import { VideoChannelModel } from '../../models/video/video-channel' | |||
51 | import { VideoPlaylistModel } from '../../models/video/video-playlist' | 50 | import { VideoPlaylistModel } from '../../models/video/video-playlist' |
52 | 51 | ||
53 | const auditLogger = auditLoggerFactory('channels') | 52 | const auditLogger = auditLoggerFactory('channels') |
54 | const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.TMP_DIR }) | 53 | const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT) |
55 | const reqBannerFile = createReqFiles([ 'bannerfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { bannerfile: CONFIG.STORAGE.TMP_DIR }) | 54 | const reqBannerFile = createReqFiles([ 'bannerfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT) |
56 | 55 | ||
57 | const videoChannelRouter = express.Router() | 56 | const 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' | |||
47 | import { VideoPlaylistModel } from '../../models/video/video-playlist' | 47 | import { VideoPlaylistModel } from '../../models/video/video-playlist' |
48 | import { VideoPlaylistElementModel } from '../../models/video/video-playlist-element' | 48 | import { VideoPlaylistElementModel } from '../../models/video/video-playlist-element' |
49 | 49 | ||
50 | const reqThumbnailFile = createReqFiles([ 'thumbnailfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { thumbnailfile: CONFIG.STORAGE.TMP_DIR }) | 50 | const reqThumbnailFile = createReqFiles([ 'thumbnailfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT) |
51 | 51 | ||
52 | const videoPlaylistRouter = express.Router() | 52 | const 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 @@ | |||
1 | import express from 'express' | 1 | import express from 'express' |
2 | import { Hooks } from '@server/lib/plugins/hooks' | ||
2 | import { MVideoCaption } from '@server/types/models' | 3 | import { MVideoCaption } from '@server/types/models' |
3 | import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' | 4 | import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' |
4 | import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils' | 5 | import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils' |
5 | import { createReqFiles } from '../../../helpers/express-utils' | 6 | import { createReqFiles } from '../../../helpers/express-utils' |
6 | import { logger } from '../../../helpers/logger' | 7 | import { logger } from '../../../helpers/logger' |
7 | import { getFormattedObjects } from '../../../helpers/utils' | 8 | import { getFormattedObjects } from '../../../helpers/utils' |
8 | import { CONFIG } from '../../../initializers/config' | ||
9 | import { MIMETYPES } from '../../../initializers/constants' | 9 | import { MIMETYPES } from '../../../initializers/constants' |
10 | import { sequelizeTypescript } from '../../../initializers/database' | 10 | import { sequelizeTypescript } from '../../../initializers/database' |
11 | import { federateVideoIfNeeded } from '../../../lib/activitypub/videos' | 11 | import { federateVideoIfNeeded } from '../../../lib/activitypub/videos' |
12 | import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares' | 12 | import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares' |
13 | import { addVideoCaptionValidator, deleteVideoCaptionValidator, listVideoCaptionsValidator } from '../../../middlewares/validators' | 13 | import { addVideoCaptionValidator, deleteVideoCaptionValidator, listVideoCaptionsValidator } from '../../../middlewares/validators' |
14 | import { VideoCaptionModel } from '../../../models/video/video-caption' | 14 | import { VideoCaptionModel } from '../../../models/video/video-caption' |
15 | import { Hooks } from '@server/lib/plugins/hooks' | ||
16 | 15 | ||
17 | const reqVideoCaptionAdd = createReqFiles( | 16 | const 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 | ||
25 | const videoCaptionsRouter = express.Router() | 18 | const 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 @@ | |||
1 | import express from 'express' | 1 | import express from 'express' |
2 | import { createAnyReqFiles } from '@server/helpers/express-utils' | 2 | import { createAnyReqFiles } from '@server/helpers/express-utils' |
3 | import { CONFIG } from '@server/initializers/config' | ||
4 | import { MIMETYPES } from '@server/initializers/constants' | 3 | import { MIMETYPES } from '@server/initializers/constants' |
5 | import { JobQueue } from '@server/lib/job-queue' | 4 | import { JobQueue } from '@server/lib/job-queue' |
6 | import { buildTaskFileFieldname, getTaskFile } from '@server/lib/video-editor' | 5 | import { buildTaskFileFieldname, getTaskFile } from '@server/lib/video-editor' |
@@ -21,7 +20,6 @@ const editorRouter = express.Router() | |||
21 | 20 | ||
22 | const tasksFiles = createAnyReqFiles( | 21 | const 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 | ||
62 | const reqVideoFileImport = createReqFiles( | 62 | const 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 | ||
72 | videoImportsRouter.post('/imports', | 67 | videoImportsRouter.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 @@ | |||
1 | import express from 'express' | 1 | import express from 'express' |
2 | import { createReqFiles } from '@server/helpers/express-utils' | 2 | import { createReqFiles } from '@server/helpers/express-utils' |
3 | import { CONFIG } from '@server/initializers/config' | ||
4 | import { ASSETS_PATH, MIMETYPES } from '@server/initializers/constants' | 3 | import { ASSETS_PATH, MIMETYPES } from '@server/initializers/constants' |
5 | import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url' | 4 | import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url' |
6 | import { federateVideoIfNeeded } from '@server/lib/activitypub/videos' | 5 | import { federateVideoIfNeeded } from '@server/lib/activitypub/videos' |
@@ -19,14 +18,7 @@ import { VideoModel } from '../../../models/video/video' | |||
19 | 18 | ||
20 | const liveRouter = express.Router() | 19 | const liveRouter = express.Router() |
21 | 20 | ||
22 | const reqVideoFileLive = createReqFiles( | 21 | const 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 | ||
31 | liveRouter.post('/live', | 23 | liveRouter.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 '../../../ | |||
11 | import { resetSequelizeInstance } from '../../../helpers/database-utils' | 11 | import { resetSequelizeInstance } from '../../../helpers/database-utils' |
12 | import { createReqFiles } from '../../../helpers/express-utils' | 12 | import { createReqFiles } from '../../../helpers/express-utils' |
13 | import { logger, loggerTagsFactory } from '../../../helpers/logger' | 13 | import { logger, loggerTagsFactory } from '../../../helpers/logger' |
14 | import { CONFIG } from '../../../initializers/config' | ||
15 | import { MIMETYPES } from '../../../initializers/constants' | 14 | import { MIMETYPES } from '../../../initializers/constants' |
16 | import { sequelizeTypescript } from '../../../initializers/database' | 15 | import { sequelizeTypescript } from '../../../initializers/database' |
17 | import { federateVideoIfNeeded } from '../../../lib/activitypub/videos' | 16 | import { federateVideoIfNeeded } from '../../../lib/activitypub/videos' |
@@ -26,14 +25,7 @@ const lTags = loggerTagsFactory('api', 'video') | |||
26 | const auditLogger = auditLoggerFactory('videos') | 25 | const auditLogger = auditLoggerFactory('videos') |
27 | const updateRouter = express.Router() | 26 | const updateRouter = express.Router() |
28 | 27 | ||
29 | const reqVideoFileUpdate = createReqFiles( | 28 | const 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 | ||
38 | updateRouter.put('/:id', | 30 | updateRouter.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 | |||
24 | import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' | 24 | import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' |
25 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | 25 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
26 | import { createReqFiles } from '../../../helpers/express-utils' | 26 | import { createReqFiles } from '../../../helpers/express-utils' |
27 | import { ffprobePromise, buildFileMetadata, getVideoStreamFPS, getVideoStreamDimensionsInfo } from '../../../helpers/ffmpeg' | 27 | import { buildFileMetadata, ffprobePromise, getVideoStreamDimensionsInfo, getVideoStreamFPS } from '../../../helpers/ffmpeg' |
28 | import { logger, loggerTagsFactory } from '../../../helpers/logger' | 28 | import { logger, loggerTagsFactory } from '../../../helpers/logger' |
29 | import { CONFIG } from '../../../initializers/config' | ||
30 | import { MIMETYPES } from '../../../initializers/constants' | 29 | import { MIMETYPES } from '../../../initializers/constants' |
31 | import { sequelizeTypescript } from '../../../initializers/database' | 30 | import { sequelizeTypescript } from '../../../initializers/database' |
32 | import { federateVideoIfNeeded } from '../../../lib/activitypub/videos' | 31 | import { federateVideoIfNeeded } from '../../../lib/activitypub/videos' |
@@ -52,21 +51,13 @@ const uploadRouter = express.Router() | |||
52 | 51 | ||
53 | const reqVideoFileAdd = createReqFiles( | 52 | const 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 | ||
63 | const reqVideoFileAddResumable = createReqFiles( | 57 | const 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 | ||
72 | uploadRouter.post('/upload', | 63 | uploadRouter.post('/upload', |
diff --git a/server/helpers/express-utils.ts b/server/helpers/express-utils.ts index 08f77966f..82dd4c178 100644 --- a/server/helpers/express-utils.ts +++ b/server/helpers/express-utils.ts | |||
@@ -68,11 +68,11 @@ function badRequest (_req: express.Request, res: express.Response) { | |||
68 | function createReqFiles ( | 68 | function createReqFiles ( |
69 | fieldNames: string[], | 69 | fieldNames: string[], |
70 | mimeTypes: { [id: string]: string | string[] }, | 70 | mimeTypes: { [id: string]: string | string[] }, |
71 | destinations: { [fieldName: string]: string } | 71 | destination = CONFIG.STORAGE.TMP_DIR |
72 | ): RequestHandler { | 72 | ): RequestHandler { |
73 | const storage = diskStorage({ | 73 | const storage = diskStorage({ |
74 | destination: (req, file, cb) => { | 74 | destination: (req, file, cb) => { |
75 | cb(null, destinations[file.fieldname]) | 75 | cb(null, destination) |
76 | }, | 76 | }, |
77 | 77 | ||
78 | filename: (req, file, cb) => { | 78 | filename: (req, file, cb) => { |
@@ -93,12 +93,11 @@ function createReqFiles ( | |||
93 | 93 | ||
94 | function createAnyReqFiles ( | 94 | function createAnyReqFiles ( |
95 | mimeTypes: { [id: string]: string | string[] }, | 95 | mimeTypes: { [id: string]: string | string[] }, |
96 | destinationDirectory: string, | ||
97 | fileFilter: (req: express.Request, file: Express.Multer.File, cb: (err: Error, result: boolean) => void) => void | 96 | fileFilter: (req: express.Request, file: Express.Multer.File, cb: (err: Error, result: boolean) => void) => void |
98 | ): RequestHandler { | 97 | ): RequestHandler { |
99 | const storage = diskStorage({ | 98 | const storage = diskStorage({ |
100 | destination: (req, file, cb) => { | 99 | destination: (req, file, cb) => { |
101 | cb(null, destinationDirectory) | 100 | cb(null, CONFIG.STORAGE.TMP_DIR) |
102 | }, | 101 | }, |
103 | 102 | ||
104 | filename: (req, file, cb) => { | 103 | filename: (req, file, cb) => { |
diff --git a/server/initializers/installer.ts b/server/initializers/installer.ts index 7e321fb76..0517e0084 100644 --- a/server/initializers/installer.ts +++ b/server/initializers/installer.ts | |||
@@ -2,10 +2,9 @@ import { ensureDir, remove } from 'fs-extra' | |||
2 | import passwordGenerator from 'password-generator' | 2 | import passwordGenerator from 'password-generator' |
3 | import { UserRole } from '@shared/models' | 3 | import { UserRole } from '@shared/models' |
4 | import { logger } from '../helpers/logger' | 4 | import { logger } from '../helpers/logger' |
5 | import { createApplicationActor, createUserAccountAndChannelAndPlaylist } from '../lib/user' | 5 | import { buildUser, createApplicationActor, createUserAccountAndChannelAndPlaylist } from '../lib/user' |
6 | import { ApplicationModel } from '../models/application/application' | 6 | import { ApplicationModel } from '../models/application/application' |
7 | import { OAuthClientModel } from '../models/oauth/oauth-client' | 7 | import { OAuthClientModel } from '../models/oauth/oauth-client' |
8 | import { UserModel } from '../models/user/user' | ||
9 | import { applicationExist, clientsExist, usersExist } from './checker-after-init' | 8 | import { applicationExist, clientsExist, usersExist } from './checker-after-init' |
10 | import { CONFIG } from './config' | 9 | import { CONFIG } from './config' |
11 | import { FILES_CACHE, HLS_STREAMING_PLAYLIST_DIRECTORY, LAST_MIGRATION_VERSION, RESUMABLE_UPLOAD_DIRECTORY } from './constants' | 10 | import { FILES_CACHE, HLS_STREAMING_PLAYLIST_DIRECTORY, LAST_MIGRATION_VERSION, RESUMABLE_UPLOAD_DIRECTORY } from './constants' |
@@ -137,18 +136,15 @@ async function createOAuthAdminIfNotExist () { | |||
137 | password = passwordGenerator(16, true) | 136 | password = passwordGenerator(16, true) |
138 | } | 137 | } |
139 | 138 | ||
140 | const userData = { | 139 | const user = buildUser({ |
141 | username, | 140 | username, |
142 | email, | 141 | email, |
143 | password, | 142 | password, |
144 | role, | 143 | role, |
145 | verified: true, | 144 | emailVerified: true, |
146 | nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, | ||
147 | p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED, | ||
148 | videoQuota: -1, | 145 | videoQuota: -1, |
149 | videoQuotaDaily: -1 | 146 | videoQuotaDaily: -1 |
150 | } | 147 | }) |
151 | const user = new UserModel(userData) | ||
152 | 148 | ||
153 | await createUserAccountAndChannelAndPlaylist({ userToCreate: user, channelNames: undefined, validateUser: validatePassword }) | 149 | await createUserAccountAndChannelAndPlaylist({ userToCreate: user, channelNames: undefined, validateUser: validatePassword }) |
154 | logger.info('Username: ' + username) | 150 | logger.info('Username: ' + username) |
diff --git a/server/lib/auth/oauth-model.ts b/server/lib/auth/oauth-model.ts index 5d68f44e9..910fdeec1 100644 --- a/server/lib/auth/oauth-model.ts +++ b/server/lib/auth/oauth-model.ts | |||
@@ -5,14 +5,14 @@ import { ActorModel } from '@server/models/actor/actor' | |||
5 | import { MOAuthClient } from '@server/types/models' | 5 | import { MOAuthClient } from '@server/types/models' |
6 | import { MOAuthTokenUser } from '@server/types/models/oauth/oauth-token' | 6 | import { MOAuthTokenUser } from '@server/types/models/oauth/oauth-token' |
7 | import { MUser } from '@server/types/models/user/user' | 7 | import { MUser } from '@server/types/models/user/user' |
8 | import { UserAdminFlag } from '@shared/models/users/user-flag.model' | 8 | import { pick } from '@shared/core-utils' |
9 | import { UserRole } from '@shared/models/users/user-role' | 9 | import { UserRole } from '@shared/models/users/user-role' |
10 | import { logger } from '../../helpers/logger' | 10 | import { logger } from '../../helpers/logger' |
11 | import { CONFIG } from '../../initializers/config' | 11 | import { CONFIG } from '../../initializers/config' |
12 | import { OAuthClientModel } from '../../models/oauth/oauth-client' | 12 | import { OAuthClientModel } from '../../models/oauth/oauth-client' |
13 | import { OAuthTokenModel } from '../../models/oauth/oauth-token' | 13 | import { OAuthTokenModel } from '../../models/oauth/oauth-token' |
14 | import { UserModel } from '../../models/user/user' | 14 | import { UserModel } from '../../models/user/user' |
15 | import { createUserAccountAndChannelAndPlaylist } from '../user' | 15 | import { buildUser, createUserAccountAndChannelAndPlaylist } from '../user' |
16 | import { TokensCache } from './tokens-cache' | 16 | import { TokensCache } from './tokens-cache' |
17 | 17 | ||
18 | type TokenInfo = { | 18 | type TokenInfo = { |
@@ -229,19 +229,13 @@ async function createUserFromExternal (pluginAuth: string, options: { | |||
229 | const actor = await ActorModel.loadLocalByName(options.username) | 229 | const actor = await ActorModel.loadLocalByName(options.username) |
230 | if (actor) return null | 230 | if (actor) return null |
231 | 231 | ||
232 | const userToCreate = new UserModel({ | 232 | const userToCreate = buildUser({ |
233 | username: options.username, | 233 | ...pick(options, [ 'username', 'email', 'role' ]), |
234 | |||
235 | emailVerified: null, | ||
234 | password: null, | 236 | password: null, |
235 | email: options.email, | ||
236 | nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, | ||
237 | p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED, | ||
238 | autoPlayVideo: true, | ||
239 | role: options.role, | ||
240 | videoQuota: CONFIG.USER.VIDEO_QUOTA, | ||
241 | videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY, | ||
242 | adminFlags: UserAdminFlag.NONE, | ||
243 | pluginAuth | 237 | pluginAuth |
244 | }) as MUser | 238 | }) |
245 | 239 | ||
246 | const { user } = await createUserAccountAndChannelAndPlaylist({ | 240 | const { user } = await createUserAccountAndChannelAndPlaylist({ |
247 | userToCreate, | 241 | userToCreate, |
diff --git a/server/lib/user.ts b/server/lib/user.ts index 3f7499296..ea755f4be 100644 --- a/server/lib/user.ts +++ b/server/lib/user.ts | |||
@@ -1,9 +1,11 @@ | |||
1 | import { Transaction } from 'sequelize/types' | 1 | import { Transaction } from 'sequelize/types' |
2 | import { logger } from '@server/helpers/logger' | ||
3 | import { CONFIG } from '@server/initializers/config' | ||
2 | import { UserModel } from '@server/models/user/user' | 4 | import { UserModel } from '@server/models/user/user' |
3 | import { MActorDefault } from '@server/types/models/actor' | 5 | import { MActorDefault } from '@server/types/models/actor' |
4 | import { buildUUID } from '@shared/extra-utils' | 6 | import { buildUUID } from '@shared/extra-utils' |
5 | import { ActivityPubActorType } from '../../shared/models/activitypub' | 7 | import { ActivityPubActorType } from '../../shared/models/activitypub' |
6 | import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users' | 8 | import { UserAdminFlag, UserNotificationSetting, UserNotificationSettingValue, UserRole } from '../../shared/models/users' |
7 | import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants' | 9 | import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants' |
8 | import { sequelizeTypescript } from '../initializers/database' | 10 | import { sequelizeTypescript } from '../initializers/database' |
9 | import { AccountModel } from '../models/account/account' | 11 | import { AccountModel } from '../models/account/account' |
@@ -19,10 +21,56 @@ import { buildActorInstance } from './local-actor' | |||
19 | import { Redis } from './redis' | 21 | import { Redis } from './redis' |
20 | import { createLocalVideoChannel } from './video-channel' | 22 | import { createLocalVideoChannel } from './video-channel' |
21 | import { createWatchLaterPlaylist } from './video-playlist' | 23 | import { createWatchLaterPlaylist } from './video-playlist' |
22 | import { logger } from '@server/helpers/logger' | ||
23 | 24 | ||
24 | type ChannelNames = { name: string, displayName: string } | 25 | type ChannelNames = { name: string, displayName: string } |
25 | 26 | ||
27 | function buildUser (options: { | ||
28 | username: string | ||
29 | password: string | ||
30 | email: string | ||
31 | |||
32 | role?: UserRole // Default to UserRole.User | ||
33 | adminFlags?: UserAdminFlag // Default to UserAdminFlag.NONE | ||
34 | |||
35 | emailVerified: boolean | null | ||
36 | |||
37 | videoQuota?: number // Default to CONFIG.USER.VIDEO_QUOTA | ||
38 | videoQuotaDaily?: number // Default to CONFIG.USER.VIDEO_QUOTA_DAILY | ||
39 | |||
40 | pluginAuth?: string | ||
41 | }): MUser { | ||
42 | const { | ||
43 | username, | ||
44 | password, | ||
45 | email, | ||
46 | role = UserRole.USER, | ||
47 | emailVerified, | ||
48 | videoQuota = CONFIG.USER.VIDEO_QUOTA, | ||
49 | videoQuotaDaily = CONFIG.USER.VIDEO_QUOTA_DAILY, | ||
50 | adminFlags = UserAdminFlag.NONE, | ||
51 | pluginAuth | ||
52 | } = options | ||
53 | |||
54 | return new UserModel({ | ||
55 | username, | ||
56 | password, | ||
57 | email, | ||
58 | |||
59 | nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, | ||
60 | p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED, | ||
61 | autoPlayVideo: true, | ||
62 | |||
63 | role, | ||
64 | emailVerified, | ||
65 | adminFlags, | ||
66 | |||
67 | videoQuota: videoQuota, | ||
68 | videoQuotaDaily: videoQuotaDaily, | ||
69 | |||
70 | pluginAuth | ||
71 | }) | ||
72 | } | ||
73 | |||
26 | async function createUserAccountAndChannelAndPlaylist (parameters: { | 74 | async function createUserAccountAndChannelAndPlaylist (parameters: { |
27 | userToCreate: MUser | 75 | userToCreate: MUser |
28 | userDisplayName?: string | 76 | userDisplayName?: string |
@@ -118,7 +166,7 @@ async function sendVerifyUserEmail (user: MUser, isPendingEmail = false) { | |||
118 | const email = isPendingEmail ? user.pendingEmail : user.email | 166 | const email = isPendingEmail ? user.pendingEmail : user.email |
119 | const username = user.username | 167 | const username = user.username |
120 | 168 | ||
121 | await Emailer.Instance.addVerifyEmailJob(username, email, url) | 169 | Emailer.Instance.addVerifyEmailJob(username, email, url) |
122 | } | 170 | } |
123 | 171 | ||
124 | async function getOriginalVideoFileTotalFromUser (user: MUserId) { | 172 | async function getOriginalVideoFileTotalFromUser (user: MUserId) { |
@@ -180,7 +228,8 @@ export { | |||
180 | createUserAccountAndChannelAndPlaylist, | 228 | createUserAccountAndChannelAndPlaylist, |
181 | createLocalAccountWithoutKeys, | 229 | createLocalAccountWithoutKeys, |
182 | sendVerifyUserEmail, | 230 | sendVerifyUserEmail, |
183 | isAbleToUploadVideo | 231 | isAbleToUploadVideo, |
232 | buildUser | ||
184 | } | 233 | } |
185 | 234 | ||
186 | // --------------------------------------------------------------------------- | 235 | // --------------------------------------------------------------------------- |