diff options
author | Chocobozzz <me@florianbigard.com> | 2018-06-29 11:29:23 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-06-29 11:29:23 +0200 |
commit | 4bbfc6c606c8d3794bae25c64c516120af41f4eb (patch) | |
tree | 8d6012f3c04e55e7325e3f00eb9061776cc7a953 /server/controllers | |
parent | 3ff5a19b4c988d6c712b7ce63c4cf04f99d047ce (diff) | |
download | PeerTube-4bbfc6c606c8d3794bae25c64c516120af41f4eb.tar.gz PeerTube-4bbfc6c606c8d3794bae25c64c516120af41f4eb.tar.zst PeerTube-4bbfc6c606c8d3794bae25c64c516120af41f4eb.zip |
API: Add ability to update video channel avatar
Diffstat (limited to 'server/controllers')
-rw-r--r-- | server/controllers/api/users.ts | 34 | ||||
-rw-r--r-- | server/controllers/api/video-channel.ts | 30 |
2 files changed, 35 insertions, 29 deletions
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 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import 'multer' | 2 | import 'multer' |
3 | import { extname, join } from 'path' | ||
4 | import * as uuidv4 from 'uuid/v4' | ||
5 | import * as RateLimit from 'express-rate-limit' | 3 | import * as RateLimit from 'express-rate-limit' |
6 | import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared' | 4 | import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared' |
7 | import { processImage } from '../../helpers/image-utils' | ||
8 | import { logger } from '../../helpers/logger' | 5 | import { logger } from '../../helpers/logger' |
9 | import { getFormattedObjects } from '../../helpers/utils' | 6 | import { getFormattedObjects } from '../../helpers/utils' |
10 | import { AVATARS_SIZE, CONFIG, IMAGE_MIMETYPE_EXT, RATES_LIMIT, sequelizeTypescript } from '../../initializers' | 7 | import { CONFIG, IMAGE_MIMETYPE_EXT, RATES_LIMIT, sequelizeTypescript } from '../../initializers' |
11 | import { updateActorAvatarInstance } from '../../lib/activitypub' | ||
12 | import { sendUpdateActor } from '../../lib/activitypub/send' | 8 | import { sendUpdateActor } from '../../lib/activitypub/send' |
13 | import { Emailer } from '../../lib/emailer' | 9 | import { Emailer } from '../../lib/emailer' |
14 | import { Redis } from '../../lib/redis' | 10 | import { Redis } from '../../lib/redis' |
@@ -33,12 +29,7 @@ import { | |||
33 | usersUpdateValidator, | 29 | usersUpdateValidator, |
34 | usersVideoRatingValidator | 30 | usersVideoRatingValidator |
35 | } from '../../middlewares' | 31 | } from '../../middlewares' |
36 | import { | 32 | import { usersAskResetPasswordValidator, usersResetPasswordValidator, videosSortValidator } from '../../middlewares/validators' |
37 | usersAskResetPasswordValidator, | ||
38 | usersResetPasswordValidator, | ||
39 | usersUpdateMyAvatarValidator, | ||
40 | videosSortValidator | ||
41 | } from '../../middlewares/validators' | ||
42 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' | 33 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' |
43 | import { UserModel } from '../../models/account/user' | 34 | import { UserModel } from '../../models/account/user' |
44 | import { OAuthTokenModel } from '../../models/oauth/oauth-token' | 35 | import { OAuthTokenModel } from '../../models/oauth/oauth-token' |
@@ -46,6 +37,8 @@ import { VideoModel } from '../../models/video/video' | |||
46 | import { VideoSortField } from '../../../client/src/app/shared/video/sort-field.type' | 37 | import { VideoSortField } from '../../../client/src/app/shared/video/sort-field.type' |
47 | import { createReqFiles } from '../../helpers/express-utils' | 38 | import { createReqFiles } from '../../helpers/express-utils' |
48 | import { UserVideoQuota } from '../../../shared/models/users/user-video-quota.model' | 39 | import { UserVideoQuota } from '../../../shared/models/users/user-video-quota.model' |
40 | import { updateAvatarValidator } from '../../middlewares/validators/avatar' | ||
41 | import { updateActorAvatarFile } from '../../lib/avatar' | ||
49 | 42 | ||
50 | const reqAvatarFile = createReqFiles([ 'avatarfile' ], IMAGE_MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.AVATARS_DIR }) | 43 | const reqAvatarFile = createReqFiles([ 'avatarfile' ], IMAGE_MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.AVATARS_DIR }) |
51 | const loginRateLimiter = new RateLimit({ | 44 | const loginRateLimiter = new RateLimit({ |
@@ -121,7 +114,7 @@ usersRouter.put('/me', | |||
121 | usersRouter.post('/me/avatar/pick', | 114 | usersRouter.post('/me/avatar/pick', |
122 | authenticate, | 115 | authenticate, |
123 | reqAvatarFile, | 116 | reqAvatarFile, |
124 | usersUpdateMyAvatarValidator, | 117 | updateAvatarValidator, |
125 | asyncMiddleware(updateMyAvatar) | 118 | asyncMiddleware(updateMyAvatar) |
126 | ) | 119 | ) |
127 | 120 | ||
@@ -304,22 +297,9 @@ async function updateMe (req: express.Request, res: express.Response, next: expr | |||
304 | 297 | ||
305 | async function updateMyAvatar (req: express.Request, res: express.Response, next: express.NextFunction) { | 298 | async function updateMyAvatar (req: express.Request, res: express.Response, next: express.NextFunction) { |
306 | const avatarPhysicalFile = req.files[ 'avatarfile' ][ 0 ] | 299 | const avatarPhysicalFile = req.files[ 'avatarfile' ][ 0 ] |
307 | const user = res.locals.oauth.token.user | 300 | const account = res.locals.oauth.token.user.Account |
308 | const actor = user.Account.Actor | ||
309 | |||
310 | const extension = extname(avatarPhysicalFile.filename) | ||
311 | const avatarName = uuidv4() + extension | ||
312 | const destination = join(CONFIG.STORAGE.AVATARS_DIR, avatarName) | ||
313 | await processImage(avatarPhysicalFile, destination, AVATARS_SIZE) | ||
314 | |||
315 | const avatar = await sequelizeTypescript.transaction(async t => { | ||
316 | const updatedActor = await updateActorAvatarInstance(actor, avatarName, t) | ||
317 | await updatedActor.save({ transaction: t }) | ||
318 | 301 | ||
319 | await sendUpdateActor(user.Account, t) | 302 | const avatar = await updateActorAvatarFile(avatarPhysicalFile, account.Actor, account) |
320 | |||
321 | return updatedActor.Avatar | ||
322 | }) | ||
323 | 303 | ||
324 | return res | 304 | return res |
325 | .json({ | 305 | .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' | |||
19 | import { sendUpdateActor } from '../../lib/activitypub/send' | 19 | import { sendUpdateActor } from '../../lib/activitypub/send' |
20 | import { VideoChannelCreate, VideoChannelUpdate } from '../../../shared' | 20 | import { VideoChannelCreate, VideoChannelUpdate } from '../../../shared' |
21 | import { createVideoChannel } from '../../lib/video-channel' | 21 | import { createVideoChannel } from '../../lib/video-channel' |
22 | import { isNSFWHidden } from '../../helpers/express-utils' | 22 | import { createReqFiles, isNSFWHidden } from '../../helpers/express-utils' |
23 | import { setAsyncActorKeys } from '../../lib/activitypub' | 23 | import { setAsyncActorKeys } from '../../lib/activitypub' |
24 | import { AccountModel } from '../../models/account/account' | 24 | import { AccountModel } from '../../models/account/account' |
25 | import { sequelizeTypescript } from '../../initializers' | 25 | import { CONFIG, IMAGE_MIMETYPE_EXT, sequelizeTypescript } from '../../initializers' |
26 | import { logger } from '../../helpers/logger' | 26 | import { logger } from '../../helpers/logger' |
27 | import { VideoModel } from '../../models/video/video' | 27 | import { VideoModel } from '../../models/video/video' |
28 | import { updateAvatarValidator } from '../../middlewares/validators/avatar' | ||
29 | import { updateActorAvatarFile } from '../../lib/avatar' | ||
30 | |||
31 | const reqAvatarFile = createReqFiles([ 'avatarfile' ], IMAGE_MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.AVATARS_DIR }) | ||
28 | 32 | ||
29 | const videoChannelRouter = express.Router() | 33 | const videoChannelRouter = express.Router() |
30 | 34 | ||
@@ -42,6 +46,15 @@ videoChannelRouter.post('/', | |||
42 | asyncRetryTransactionMiddleware(addVideoChannel) | 46 | asyncRetryTransactionMiddleware(addVideoChannel) |
43 | ) | 47 | ) |
44 | 48 | ||
49 | videoChannelRouter.post('/:id/avatar/pick', | ||
50 | authenticate, | ||
51 | reqAvatarFile, | ||
52 | // Check the rights | ||
53 | asyncMiddleware(videoChannelsUpdateValidator), | ||
54 | updateAvatarValidator, | ||
55 | asyncMiddleware(updateVideoChannelAvatar) | ||
56 | ) | ||
57 | |||
45 | videoChannelRouter.put('/:id', | 58 | videoChannelRouter.put('/:id', |
46 | authenticate, | 59 | authenticate, |
47 | asyncMiddleware(videoChannelsUpdateValidator), | 60 | asyncMiddleware(videoChannelsUpdateValidator), |
@@ -83,6 +96,19 @@ async function listVideoChannels (req: express.Request, res: express.Response, n | |||
83 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | 96 | return res.json(getFormattedObjects(resultList.data, resultList.total)) |
84 | } | 97 | } |
85 | 98 | ||
99 | async function updateVideoChannelAvatar (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
100 | const avatarPhysicalFile = req.files[ 'avatarfile' ][ 0 ] | ||
101 | const videoChannel = res.locals.videoChannel | ||
102 | |||
103 | const avatar = await updateActorAvatarFile(avatarPhysicalFile, videoChannel.Actor, videoChannel) | ||
104 | |||
105 | return res | ||
106 | .json({ | ||
107 | avatar: avatar.toFormattedJSON() | ||
108 | }) | ||
109 | .end() | ||
110 | } | ||
111 | |||
86 | async function addVideoChannel (req: express.Request, res: express.Response) { | 112 | async function addVideoChannel (req: express.Request, res: express.Response) { |
87 | const videoChannelInfo: VideoChannelCreate = req.body | 113 | const videoChannelInfo: VideoChannelCreate = req.body |
88 | const account: AccountModel = res.locals.oauth.token.User.Account | 114 | const account: AccountModel = res.locals.oauth.token.User.Account |