diff options
Diffstat (limited to 'server/controllers/api/users.ts')
-rw-r--r-- | server/controllers/api/users.ts | 53 |
1 files changed, 50 insertions, 3 deletions
diff --git a/server/controllers/api/users.ts b/server/controllers/api/users.ts index 75393ad17..57b98b84a 100644 --- a/server/controllers/api/users.ts +++ b/server/controllers/api/users.ts | |||
@@ -1,20 +1,26 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { extname, join } from 'path' | ||
3 | import * as uuidv4 from 'uuid/v4' | ||
2 | import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared' | 4 | import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared' |
5 | import { renamePromise } from '../../helpers/core-utils' | ||
3 | import { retryTransactionWrapper } from '../../helpers/database-utils' | 6 | import { retryTransactionWrapper } from '../../helpers/database-utils' |
4 | import { logger } from '../../helpers/logger' | 7 | import { logger } from '../../helpers/logger' |
5 | import { getFormattedObjects } from '../../helpers/utils' | 8 | import { createReqFiles, getFormattedObjects } from '../../helpers/utils' |
6 | import { CONFIG } from '../../initializers' | 9 | import { AVATAR_MIMETYPE_EXT, CONFIG, sequelizeTypescript } from '../../initializers' |
7 | import { createUserAccountAndChannel } from '../../lib/user' | 10 | import { createUserAccountAndChannel } from '../../lib/user' |
8 | import { | 11 | import { |
9 | asyncMiddleware, authenticate, ensureUserHasRight, ensureUserRegistrationAllowed, paginationValidator, setPagination, setUsersSort, | 12 | asyncMiddleware, authenticate, ensureUserHasRight, ensureUserRegistrationAllowed, paginationValidator, setPagination, setUsersSort, |
10 | setVideosSort, token, usersAddValidator, usersGetValidator, usersRegisterValidator, usersRemoveValidator, usersSortValidator, | 13 | setVideosSort, token, usersAddValidator, usersGetValidator, usersRegisterValidator, usersRemoveValidator, usersSortValidator, |
11 | usersUpdateMeValidator, usersUpdateValidator, usersVideoRatingValidator | 14 | usersUpdateMeValidator, usersUpdateValidator, usersVideoRatingValidator |
12 | } from '../../middlewares' | 15 | } from '../../middlewares' |
13 | import { videosSortValidator } from '../../middlewares/validators' | 16 | import { usersUpdateMyAvatarValidator, videosSortValidator } from '../../middlewares/validators' |
14 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' | 17 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' |
15 | import { UserModel } from '../../models/account/user' | 18 | import { UserModel } from '../../models/account/user' |
19 | import { AvatarModel } from '../../models/avatar/avatar' | ||
16 | import { VideoModel } from '../../models/video/video' | 20 | import { VideoModel } from '../../models/video/video' |
17 | 21 | ||
22 | const reqAvatarFile = createReqFiles('avatarfile', CONFIG.STORAGE.AVATARS_DIR, AVATAR_MIMETYPE_EXT) | ||
23 | |||
18 | const usersRouter = express.Router() | 24 | const usersRouter = express.Router() |
19 | 25 | ||
20 | usersRouter.get('/me', | 26 | usersRouter.get('/me', |
@@ -71,6 +77,13 @@ usersRouter.put('/me', | |||
71 | asyncMiddleware(updateMe) | 77 | asyncMiddleware(updateMe) |
72 | ) | 78 | ) |
73 | 79 | ||
80 | usersRouter.post('/me/avatar/pick', | ||
81 | authenticate, | ||
82 | reqAvatarFile, | ||
83 | usersUpdateMyAvatarValidator, | ||
84 | asyncMiddleware(updateMyAvatar) | ||
85 | ) | ||
86 | |||
74 | usersRouter.put('/:id', | 87 | usersRouter.put('/:id', |
75 | authenticate, | 88 | authenticate, |
76 | ensureUserHasRight(UserRight.MANAGE_USERS), | 89 | ensureUserHasRight(UserRight.MANAGE_USERS), |
@@ -216,6 +229,40 @@ async function updateMe (req: express.Request, res: express.Response, next: expr | |||
216 | return res.sendStatus(204) | 229 | return res.sendStatus(204) |
217 | } | 230 | } |
218 | 231 | ||
232 | async function updateMyAvatar (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
233 | const avatarPhysicalFile = req.files['avatarfile'][0] | ||
234 | const actor = res.locals.oauth.token.user.Account.Actor | ||
235 | |||
236 | const avatarDir = CONFIG.STORAGE.AVATARS_DIR | ||
237 | const source = join(avatarDir, avatarPhysicalFile.filename) | ||
238 | const extension = extname(avatarPhysicalFile.filename) | ||
239 | const avatarName = uuidv4() + extension | ||
240 | const destination = join(avatarDir, avatarName) | ||
241 | |||
242 | await renamePromise(source, destination) | ||
243 | |||
244 | const { avatar } = await sequelizeTypescript.transaction(async t => { | ||
245 | const avatar = await AvatarModel.create({ | ||
246 | filename: avatarName | ||
247 | }, { transaction: t }) | ||
248 | |||
249 | if (actor.Avatar) { | ||
250 | await actor.Avatar.destroy({ transaction: t }) | ||
251 | } | ||
252 | |||
253 | actor.set('avatarId', avatar.id) | ||
254 | await actor.save({ transaction: t }) | ||
255 | |||
256 | return { actor, avatar } | ||
257 | }) | ||
258 | |||
259 | return res | ||
260 | .json({ | ||
261 | avatar: avatar.toFormattedJSON() | ||
262 | }) | ||
263 | .end() | ||
264 | } | ||
265 | |||
219 | async function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) { | 266 | async function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
220 | const body: UserUpdate = req.body | 267 | const body: UserUpdate = req.body |
221 | const user = res.locals.user as UserModel | 268 | const user = res.locals.user as UserModel |