Automatically resize avatars
authorChocobozzz <me@florianbigard.com>
Wed, 3 Jan 2018 10:36:03 +0000 (11:36 +0100)
committerChocobozzz <me@florianbigard.com>
Wed, 3 Jan 2018 10:36:03 +0000 (11:36 +0100)
12 files changed:
package.json
server.ts
server/controllers/activitypub/inbox.ts
server/controllers/activitypub/outbox.ts
server/controllers/api/users.ts
server/initializers/constants.ts
server/middlewares/validators/users.ts
server/models/server/server.ts
server/models/video/video-comment.ts
server/tests/api/fixtures/avatar-resized.png [new file with mode: 0644]
server/tests/api/users/users.ts
yarn.lock

index adaccaf37f769bc414bad731c68ad09965c6fe7d..e082eeb6ebf579be64dab2f9be2a8477d6c24b5f 100644 (file)
     "@types/request": "^2.0.3",
     "@types/sanitize-html": "^1.14.0",
     "@types/sequelize": "^4.0.55",
+    "@types/sharp": "^0.17.6",
     "@types/supertest": "^2.0.3",
     "@types/validator": "^6.2.0",
     "@types/webtorrent": "^0.98.4",
index e46ff85c7c214200a3b03b8d2de884151284ee49..05fc39acbb5a808cfeccf5e4384ce440ee912884 100644 (file)
--- a/server.ts
+++ b/server.ts
@@ -164,7 +164,7 @@ function onDatabaseInitDone () {
     .then(() => {
       // ----------- Make the server listening -----------
       server.listen(port, () => {
-        VideosPreviewCache.Instance.init(CONFIG.CACHE.PREVIEWS.FILE_SIZE)
+        VideosPreviewCache.Instance.init(CONFIG.CACHE.PREVIEWS.SIZE)
         activitypubHttpJobScheduler.activate()
         transcodingJobScheduler.activate()
 
index bfcb7b3695200b994b15c354c6a0ab97a1eac4cb..8d65639f8909a10eb0a5c6849f1343524956a899 100644 (file)
@@ -16,7 +16,7 @@ inboxRouter.post('/inbox',
   asyncMiddleware(inboxController)
 )
 
-inboxRouter.post('/account/:name/inbox',
+inboxRouter.post('/accounts/:name/inbox',
   signatureValidator,
   asyncMiddleware(checkSignature),
   localAccountValidator,
index 01ba253c68972ec298c9b49dfa06988f9690813e..620f9ee83c973fd71e8e90f89533e3b9a0e78b14 100644 (file)
@@ -11,7 +11,7 @@ import { VideoModel } from '../../models/video/video'
 
 const outboxRouter = express.Router()
 
-outboxRouter.get('/account/:name/outbox',
+outboxRouter.get('/accounts/:name/outbox',
   localAccountValidator,
   asyncMiddleware(outboxController)
 )
index 57b98b84ad0b7022e45f2a55746bae71135cc646..6c24434f2f588a15949c4b328fbbb1ef97c704dd 100644 (file)
@@ -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({
index aefb91537765b26c9024f91e813fce5bcb771bc5..d9b21b3895ebc937b958d2b372e8a06cf9eb59b6 100644 (file)
@@ -316,6 +316,10 @@ const PREVIEWS_SIZE = {
   width: 560,
   height: 315
 }
+const AVATARS_SIZE = {
+  width: 120,
+  height: 120
+}
 
 const EMBED_SIZE = {
   width: 560,
@@ -355,6 +359,7 @@ CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WE
 
 export {
   API_VERSION,
+  AVATARS_SIZE,
   ACCEPT_HEADERS,
   BCRYPT_SALT_SIZE,
   CACHE,
index 7de3e442ccec31cbabe8ccc295adfc45982eabc7..d22a745b42a34cce17d151b5c6012651e7c79589 100644 (file)
@@ -12,7 +12,6 @@ import { isSignupAllowed } from '../../helpers/utils'
 import { CONSTRAINTS_FIELDS } from '../../initializers'
 import { UserModel } from '../../models/account/user'
 import { areValidationErrors } from './utils'
-import Multer = require('multer')
 
 const usersAddValidator = [
   body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'),
@@ -105,7 +104,7 @@ const usersUpdateMyAvatarValidator = [
   ),
 
   (req: express.Request, res: express.Response, next: express.NextFunction) => {
-    logger.debug('Checking usersUpdateMyAvatarValidator parameters', { parameters: req.body })
+    logger.debug('Checking usersUpdateMyAvatarValidator parameters', { files: req.files })
 
     if (areValidationErrors(req, res)) return
 
index 122e5f74fcd5190bb38494bf387901e4ff37a533..d35aa0ca402222f3ec6a2c54bdf371739867846f 100644 (file)
@@ -27,7 +27,7 @@ export class ServerModel extends Model<ServerModel> {
   @AllowNull(false)
   @Default(SERVERS_SCORE.BASE)
   @IsInt
-  @Max(SERVERS_SCORE.max)
+  @Max(SERVERS_SCORE.MAX)
   @Column
   score: number
 
index 829022a51c0ec9b49039d912cf4deee0ea0c30a4..63675c20bf688abde380b46fc6820a30c6c782c2 100644 (file)
@@ -214,7 +214,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
 
   static listThreadCommentsForApi (videoId: number, threadId: number) {
     const query = {
-      order: [ [ 'createdAt', 'DESC' ] ],
+      order: [ [ 'createdAt', 'ASC' ] ],
       where: {
         videoId,
         [ Sequelize.Op.or ]: [
diff --git a/server/tests/api/fixtures/avatar-resized.png b/server/tests/api/fixtures/avatar-resized.png
new file mode 100644 (file)
index 0000000..e05fc07
Binary files /dev/null and b/server/tests/api/fixtures/avatar-resized.png differ
index 3390b2d56e3b79ecbb845e5b1d25d48f100ee8ec..f7e5972d3ecb7d6386560f262530f187855043c8 100644 (file)
@@ -352,7 +352,7 @@ describe('Test users', function () {
     const res = await getMyUserInformation(server.url, accessTokenUser)
     const user = res.body
 
-    const test = await testVideoImage(server.url, 'avatar', user.account.avatar.path, '.png')
+    const test = await testVideoImage(server.url, 'avatar-resized', user.account.avatar.path, '.png')
     expect(test).to.equal(true)
   })
 
index 67337c08bbc6f53c5018649f5b4286d1a166d8af..7929d6ae0021f827e9bb3d9008d054f9d7b31e6f 100644 (file)
--- a/yarn.lock
+++ b/yarn.lock
     "@types/express-serve-static-core" "*"
     "@types/mime" "*"
 
+"@types/sharp@^0.17.6":
+  version "0.17.6"
+  resolved "https://registry.yarnpkg.com/@types/sharp/-/sharp-0.17.6.tgz#3138602163b30b4969f75a2755a3f90caaaa9be3"
+  dependencies:
+    "@types/node" "*"
+
 "@types/simple-peer@*":
   version "6.1.4"
   resolved "https://registry.yarnpkg.com/@types/simple-peer/-/simple-peer-6.1.4.tgz#1d1384e1d8dc17b9e7d1673d704febe91ca48191"