From 4bbfc6c606c8d3794bae25c64c516120af41f4eb Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 29 Jun 2018 11:29:23 +0200 Subject: API: Add ability to update video channel avatar --- server/controllers/api/users.ts | 34 +++++++-------------------------- server/controllers/api/video-channel.ts | 30 +++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 29 deletions(-) (limited to 'server/controllers/api') 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 @@ import * as express from 'express' import 'multer' -import { extname, join } from 'path' -import * as uuidv4 from 'uuid/v4' import * as RateLimit from 'express-rate-limit' import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared' -import { processImage } from '../../helpers/image-utils' import { logger } from '../../helpers/logger' import { getFormattedObjects } from '../../helpers/utils' -import { AVATARS_SIZE, CONFIG, IMAGE_MIMETYPE_EXT, RATES_LIMIT, sequelizeTypescript } from '../../initializers' -import { updateActorAvatarInstance } from '../../lib/activitypub' +import { CONFIG, IMAGE_MIMETYPE_EXT, RATES_LIMIT, sequelizeTypescript } from '../../initializers' import { sendUpdateActor } from '../../lib/activitypub/send' import { Emailer } from '../../lib/emailer' import { Redis } from '../../lib/redis' @@ -33,12 +29,7 @@ import { usersUpdateValidator, usersVideoRatingValidator } from '../../middlewares' -import { - usersAskResetPasswordValidator, - usersResetPasswordValidator, - usersUpdateMyAvatarValidator, - videosSortValidator -} from '../../middlewares/validators' +import { usersAskResetPasswordValidator, usersResetPasswordValidator, videosSortValidator } from '../../middlewares/validators' import { AccountVideoRateModel } from '../../models/account/account-video-rate' import { UserModel } from '../../models/account/user' import { OAuthTokenModel } from '../../models/oauth/oauth-token' @@ -46,6 +37,8 @@ import { VideoModel } from '../../models/video/video' import { VideoSortField } from '../../../client/src/app/shared/video/sort-field.type' import { createReqFiles } from '../../helpers/express-utils' import { UserVideoQuota } from '../../../shared/models/users/user-video-quota.model' +import { updateAvatarValidator } from '../../middlewares/validators/avatar' +import { updateActorAvatarFile } from '../../lib/avatar' const reqAvatarFile = createReqFiles([ 'avatarfile' ], IMAGE_MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.AVATARS_DIR }) const loginRateLimiter = new RateLimit({ @@ -121,7 +114,7 @@ usersRouter.put('/me', usersRouter.post('/me/avatar/pick', authenticate, reqAvatarFile, - usersUpdateMyAvatarValidator, + updateAvatarValidator, asyncMiddleware(updateMyAvatar) ) @@ -304,22 +297,9 @@ async function updateMe (req: express.Request, res: express.Response, next: expr async function updateMyAvatar (req: express.Request, res: express.Response, next: express.NextFunction) { const avatarPhysicalFile = req.files[ 'avatarfile' ][ 0 ] - const user = res.locals.oauth.token.user - const actor = user.Account.Actor - - const extension = extname(avatarPhysicalFile.filename) - const avatarName = uuidv4() + extension - const destination = join(CONFIG.STORAGE.AVATARS_DIR, avatarName) - await processImage(avatarPhysicalFile, destination, AVATARS_SIZE) - - const avatar = await sequelizeTypescript.transaction(async t => { - const updatedActor = await updateActorAvatarInstance(actor, avatarName, t) - await updatedActor.save({ transaction: t }) + const account = res.locals.oauth.token.user.Account - await sendUpdateActor(user.Account, t) - - return updatedActor.Avatar - }) + const avatar = await updateActorAvatarFile(avatarPhysicalFile, account.Actor, account) return res .json({ diff --git a/server/controllers/api/video-channel.ts b/server/controllers/api/video-channel.ts index 61e72125f..1707732ee 100644 --- a/server/controllers/api/video-channel.ts +++ b/server/controllers/api/video-channel.ts @@ -19,12 +19,16 @@ import { videosSortValidator } from '../../middlewares/validators' import { sendUpdateActor } from '../../lib/activitypub/send' import { VideoChannelCreate, VideoChannelUpdate } from '../../../shared' import { createVideoChannel } from '../../lib/video-channel' -import { isNSFWHidden } from '../../helpers/express-utils' +import { createReqFiles, isNSFWHidden } from '../../helpers/express-utils' import { setAsyncActorKeys } from '../../lib/activitypub' import { AccountModel } from '../../models/account/account' -import { sequelizeTypescript } from '../../initializers' +import { CONFIG, IMAGE_MIMETYPE_EXT, sequelizeTypescript } from '../../initializers' import { logger } from '../../helpers/logger' import { VideoModel } from '../../models/video/video' +import { updateAvatarValidator } from '../../middlewares/validators/avatar' +import { updateActorAvatarFile } from '../../lib/avatar' + +const reqAvatarFile = createReqFiles([ 'avatarfile' ], IMAGE_MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.AVATARS_DIR }) const videoChannelRouter = express.Router() @@ -42,6 +46,15 @@ videoChannelRouter.post('/', asyncRetryTransactionMiddleware(addVideoChannel) ) +videoChannelRouter.post('/:id/avatar/pick', + authenticate, + reqAvatarFile, + // Check the rights + asyncMiddleware(videoChannelsUpdateValidator), + updateAvatarValidator, + asyncMiddleware(updateVideoChannelAvatar) +) + videoChannelRouter.put('/:id', authenticate, asyncMiddleware(videoChannelsUpdateValidator), @@ -83,6 +96,19 @@ async function listVideoChannels (req: express.Request, res: express.Response, n return res.json(getFormattedObjects(resultList.data, resultList.total)) } +async function updateVideoChannelAvatar (req: express.Request, res: express.Response, next: express.NextFunction) { + const avatarPhysicalFile = req.files[ 'avatarfile' ][ 0 ] + const videoChannel = res.locals.videoChannel + + const avatar = await updateActorAvatarFile(avatarPhysicalFile, videoChannel.Actor, videoChannel) + + return res + .json({ + avatar: avatar.toFormattedJSON() + }) + .end() +} + async function addVideoChannel (req: express.Request, res: express.Response) { const videoChannelInfo: VideoChannelCreate = req.body const account: AccountModel = res.locals.oauth.token.User.Account -- cgit v1.2.3