X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fusers.ts;h=6c24434f2f588a15949c4b328fbbb1ef97c704dd;hb=e8e122002d5a6a2bedcf3d66d35657c9b9e1ebaf;hp=75393ad17d9956603bbe80935bcd95c5c45c103e;hpb=da854ddd502cd70685ef779c673b9e63757b8aa0;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/users.ts b/server/controllers/api/users.ts index 75393ad17..6c24434f2 100644 --- a/server/controllers/api/users.ts +++ b/server/controllers/api/users.ts @@ -1,20 +1,27 @@ import * as express from 'express' +import { extname, join } from 'path' +import * as sharp from 'sharp' +import * as uuidv4 from 'uuid/v4' import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared' +import { renamePromise, unlinkPromise } from '../../helpers/core-utils' import { retryTransactionWrapper } from '../../helpers/database-utils' import { logger } from '../../helpers/logger' -import { getFormattedObjects } from '../../helpers/utils' -import { CONFIG } from '../../initializers' +import { createReqFiles, getFormattedObjects } from '../../helpers/utils' +import { AVATAR_MIMETYPE_EXT, AVATARS_SIZE, CONFIG, sequelizeTypescript } from '../../initializers' import { createUserAccountAndChannel } from '../../lib/user' import { asyncMiddleware, authenticate, ensureUserHasRight, ensureUserRegistrationAllowed, paginationValidator, setPagination, setUsersSort, setVideosSort, token, usersAddValidator, usersGetValidator, usersRegisterValidator, usersRemoveValidator, usersSortValidator, usersUpdateMeValidator, usersUpdateValidator, usersVideoRatingValidator } from '../../middlewares' -import { videosSortValidator } from '../../middlewares/validators' +import { usersUpdateMyAvatarValidator, videosSortValidator } from '../../middlewares/validators' import { AccountVideoRateModel } from '../../models/account/account-video-rate' import { UserModel } from '../../models/account/user' +import { AvatarModel } from '../../models/avatar/avatar' import { VideoModel } from '../../models/video/video' +const reqAvatarFile = createReqFiles('avatarfile', CONFIG.STORAGE.AVATARS_DIR, AVATAR_MIMETYPE_EXT) + const usersRouter = express.Router() usersRouter.get('/me', @@ -71,6 +78,13 @@ usersRouter.put('/me', asyncMiddleware(updateMe) ) +usersRouter.post('/me/avatar/pick', + authenticate, + reqAvatarFile, + usersUpdateMyAvatarValidator, + asyncMiddleware(updateMyAvatar) +) + usersRouter.put('/:id', authenticate, ensureUserHasRight(UserRight.MANAGE_USERS), @@ -216,6 +230,44 @@ async function updateMe (req: express.Request, res: express.Response, next: expr return res.sendStatus(204) } +async function updateMyAvatar (req: express.Request, res: express.Response, next: express.NextFunction) { + const avatarPhysicalFile = req.files['avatarfile'][0] + const actor = res.locals.oauth.token.user.Account.Actor + + const avatarDir = CONFIG.STORAGE.AVATARS_DIR + const source = join(avatarDir, avatarPhysicalFile.filename) + const extension = extname(avatarPhysicalFile.filename) + const avatarName = uuidv4() + extension + const destination = join(avatarDir, avatarName) + + await sharp(source) + .resize(AVATARS_SIZE.width, AVATARS_SIZE.height) + .toFile(destination) + + await unlinkPromise(source) + + const { avatar } = await sequelizeTypescript.transaction(async t => { + const avatar = await AvatarModel.create({ + filename: avatarName + }, { transaction: t }) + + if (actor.Avatar) { + await actor.Avatar.destroy({ transaction: t }) + } + + actor.set('avatarId', avatar.id) + await actor.save({ transaction: t }) + + return { actor, avatar } + }) + + return res + .json({ + avatar: avatar.toFormattedJSON() + }) + .end() +} + async function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) { const body: UserUpdate = req.body const user = res.locals.user as UserModel