From e8e122002d5a6a2bedcf3d66d35657c9b9e1ebaf Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 3 Jan 2018 11:36:03 +0100 Subject: Automatically resize avatars --- server/controllers/api/users.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'server/controllers/api') diff --git a/server/controllers/api/users.ts b/server/controllers/api/users.ts index 57b98b84a..6c24434f2 100644 --- a/server/controllers/api/users.ts +++ b/server/controllers/api/users.ts @@ -1,12 +1,13 @@ 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 } from '../../helpers/core-utils' +import { renamePromise, unlinkPromise } from '../../helpers/core-utils' import { retryTransactionWrapper } from '../../helpers/database-utils' import { logger } from '../../helpers/logger' import { createReqFiles, getFormattedObjects } from '../../helpers/utils' -import { AVATAR_MIMETYPE_EXT, CONFIG, sequelizeTypescript } from '../../initializers' +import { AVATAR_MIMETYPE_EXT, AVATARS_SIZE, CONFIG, sequelizeTypescript } from '../../initializers' import { createUserAccountAndChannel } from '../../lib/user' import { asyncMiddleware, authenticate, ensureUserHasRight, ensureUserRegistrationAllowed, paginationValidator, setPagination, setUsersSort, @@ -239,7 +240,11 @@ async function updateMyAvatar (req: express.Request, res: express.Response, next const avatarName = uuidv4() + extension const destination = join(avatarDir, avatarName) - await renamePromise(source, destination) + 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({ -- cgit v1.2.3