aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/users.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/users.ts')
-rw-r--r--server/controllers/api/users.ts34
1 files changed, 7 insertions, 27 deletions
diff --git a/server/controllers/api/users.ts b/server/controllers/api/users.ts
index 891056912..c80f27a23 100644
--- a/server/controllers/api/users.ts
+++ b/server/controllers/api/users.ts
@@ -1,14 +1,10 @@
1import * as express from 'express' 1import * as express from 'express'
2import 'multer' 2import 'multer'
3import { extname, join } from 'path'
4import * as uuidv4 from 'uuid/v4'
5import * as RateLimit from 'express-rate-limit' 3import * as RateLimit from 'express-rate-limit'
6import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared' 4import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared'
7import { processImage } from '../../helpers/image-utils'
8import { logger } from '../../helpers/logger' 5import { logger } from '../../helpers/logger'
9import { getFormattedObjects } from '../../helpers/utils' 6import { getFormattedObjects } from '../../helpers/utils'
10import { AVATARS_SIZE, CONFIG, IMAGE_MIMETYPE_EXT, RATES_LIMIT, sequelizeTypescript } from '../../initializers' 7import { CONFIG, IMAGE_MIMETYPE_EXT, RATES_LIMIT, sequelizeTypescript } from '../../initializers'
11import { updateActorAvatarInstance } from '../../lib/activitypub'
12import { sendUpdateActor } from '../../lib/activitypub/send' 8import { sendUpdateActor } from '../../lib/activitypub/send'
13import { Emailer } from '../../lib/emailer' 9import { Emailer } from '../../lib/emailer'
14import { Redis } from '../../lib/redis' 10import { Redis } from '../../lib/redis'
@@ -33,12 +29,7 @@ import {
33 usersUpdateValidator, 29 usersUpdateValidator,
34 usersVideoRatingValidator 30 usersVideoRatingValidator
35} from '../../middlewares' 31} from '../../middlewares'
36import { 32import { usersAskResetPasswordValidator, usersResetPasswordValidator, videosSortValidator } from '../../middlewares/validators'
37 usersAskResetPasswordValidator,
38 usersResetPasswordValidator,
39 usersUpdateMyAvatarValidator,
40 videosSortValidator
41} from '../../middlewares/validators'
42import { AccountVideoRateModel } from '../../models/account/account-video-rate' 33import { AccountVideoRateModel } from '../../models/account/account-video-rate'
43import { UserModel } from '../../models/account/user' 34import { UserModel } from '../../models/account/user'
44import { OAuthTokenModel } from '../../models/oauth/oauth-token' 35import { OAuthTokenModel } from '../../models/oauth/oauth-token'
@@ -46,6 +37,8 @@ import { VideoModel } from '../../models/video/video'
46import { VideoSortField } from '../../../client/src/app/shared/video/sort-field.type' 37import { VideoSortField } from '../../../client/src/app/shared/video/sort-field.type'
47import { createReqFiles } from '../../helpers/express-utils' 38import { createReqFiles } from '../../helpers/express-utils'
48import { UserVideoQuota } from '../../../shared/models/users/user-video-quota.model' 39import { UserVideoQuota } from '../../../shared/models/users/user-video-quota.model'
40import { updateAvatarValidator } from '../../middlewares/validators/avatar'
41import { updateActorAvatarFile } from '../../lib/avatar'
49 42
50const reqAvatarFile = createReqFiles([ 'avatarfile' ], IMAGE_MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.AVATARS_DIR }) 43const reqAvatarFile = createReqFiles([ 'avatarfile' ], IMAGE_MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.AVATARS_DIR })
51const loginRateLimiter = new RateLimit({ 44const loginRateLimiter = new RateLimit({
@@ -121,7 +114,7 @@ usersRouter.put('/me',
121usersRouter.post('/me/avatar/pick', 114usersRouter.post('/me/avatar/pick',
122 authenticate, 115 authenticate,
123 reqAvatarFile, 116 reqAvatarFile,
124 usersUpdateMyAvatarValidator, 117 updateAvatarValidator,
125 asyncMiddleware(updateMyAvatar) 118 asyncMiddleware(updateMyAvatar)
126) 119)
127 120
@@ -304,22 +297,9 @@ async function updateMe (req: express.Request, res: express.Response, next: expr
304 297
305async function updateMyAvatar (req: express.Request, res: express.Response, next: express.NextFunction) { 298async function updateMyAvatar (req: express.Request, res: express.Response, next: express.NextFunction) {
306 const avatarPhysicalFile = req.files[ 'avatarfile' ][ 0 ] 299 const avatarPhysicalFile = req.files[ 'avatarfile' ][ 0 ]
307 const user = res.locals.oauth.token.user 300 const account = res.locals.oauth.token.user.Account
308 const actor = user.Account.Actor
309
310 const extension = extname(avatarPhysicalFile.filename)
311 const avatarName = uuidv4() + extension
312 const destination = join(CONFIG.STORAGE.AVATARS_DIR, avatarName)
313 await processImage(avatarPhysicalFile, destination, AVATARS_SIZE)
314
315 const avatar = await sequelizeTypescript.transaction(async t => {
316 const updatedActor = await updateActorAvatarInstance(actor, avatarName, t)
317 await updatedActor.save({ transaction: t })
318 301
319 await sendUpdateActor(user.Account, t) 302 const avatar = await updateActorAvatarFile(avatarPhysicalFile, account.Actor, account)
320
321 return updatedActor.Avatar
322 })
323 303
324 return res 304 return res
325 .json({ 305 .json({