aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2017-12-29 19:10:13 +0100
committerChocobozzz <me@florianbigard.com>2017-12-29 19:10:13 +0100
commitc5911fd347c76e8bdc05ea9f3ee9efed4a58c236 (patch)
treeb8d287daca6c45305090cbec9da97d1155f275bd /server/controllers/api
parent8b0d42ee372de6589796be26b83e5bffb1b69cdf (diff)
downloadPeerTube-c5911fd347c76e8bdc05ea9f3ee9efed4a58c236.tar.gz
PeerTube-c5911fd347c76e8bdc05ea9f3ee9efed4a58c236.tar.zst
PeerTube-c5911fd347c76e8bdc05ea9f3ee9efed4a58c236.zip
Begin to add avatar to actors
Diffstat (limited to 'server/controllers/api')
-rw-r--r--server/controllers/api/users.ts53
-rw-r--r--server/controllers/api/videos/index.ts27
2 files changed, 53 insertions, 27 deletions
diff --git a/server/controllers/api/users.ts b/server/controllers/api/users.ts
index 75393ad17..57b98b84a 100644
--- a/server/controllers/api/users.ts
+++ b/server/controllers/api/users.ts
@@ -1,20 +1,26 @@
1import * as express from 'express' 1import * as express from 'express'
2import { extname, join } from 'path'
3import * as uuidv4 from 'uuid/v4'
2import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared' 4import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared'
5import { renamePromise } from '../../helpers/core-utils'
3import { retryTransactionWrapper } from '../../helpers/database-utils' 6import { retryTransactionWrapper } from '../../helpers/database-utils'
4import { logger } from '../../helpers/logger' 7import { logger } from '../../helpers/logger'
5import { getFormattedObjects } from '../../helpers/utils' 8import { createReqFiles, getFormattedObjects } from '../../helpers/utils'
6import { CONFIG } from '../../initializers' 9import { AVATAR_MIMETYPE_EXT, CONFIG, sequelizeTypescript } from '../../initializers'
7import { createUserAccountAndChannel } from '../../lib/user' 10import { createUserAccountAndChannel } from '../../lib/user'
8import { 11import {
9 asyncMiddleware, authenticate, ensureUserHasRight, ensureUserRegistrationAllowed, paginationValidator, setPagination, setUsersSort, 12 asyncMiddleware, authenticate, ensureUserHasRight, ensureUserRegistrationAllowed, paginationValidator, setPagination, setUsersSort,
10 setVideosSort, token, usersAddValidator, usersGetValidator, usersRegisterValidator, usersRemoveValidator, usersSortValidator, 13 setVideosSort, token, usersAddValidator, usersGetValidator, usersRegisterValidator, usersRemoveValidator, usersSortValidator,
11 usersUpdateMeValidator, usersUpdateValidator, usersVideoRatingValidator 14 usersUpdateMeValidator, usersUpdateValidator, usersVideoRatingValidator
12} from '../../middlewares' 15} from '../../middlewares'
13import { videosSortValidator } from '../../middlewares/validators' 16import { usersUpdateMyAvatarValidator, videosSortValidator } from '../../middlewares/validators'
14import { AccountVideoRateModel } from '../../models/account/account-video-rate' 17import { AccountVideoRateModel } from '../../models/account/account-video-rate'
15import { UserModel } from '../../models/account/user' 18import { UserModel } from '../../models/account/user'
19import { AvatarModel } from '../../models/avatar/avatar'
16import { VideoModel } from '../../models/video/video' 20import { VideoModel } from '../../models/video/video'
17 21
22const reqAvatarFile = createReqFiles('avatarfile', CONFIG.STORAGE.AVATARS_DIR, AVATAR_MIMETYPE_EXT)
23
18const usersRouter = express.Router() 24const usersRouter = express.Router()
19 25
20usersRouter.get('/me', 26usersRouter.get('/me',
@@ -71,6 +77,13 @@ usersRouter.put('/me',
71 asyncMiddleware(updateMe) 77 asyncMiddleware(updateMe)
72) 78)
73 79
80usersRouter.post('/me/avatar/pick',
81 authenticate,
82 reqAvatarFile,
83 usersUpdateMyAvatarValidator,
84 asyncMiddleware(updateMyAvatar)
85)
86
74usersRouter.put('/:id', 87usersRouter.put('/:id',
75 authenticate, 88 authenticate,
76 ensureUserHasRight(UserRight.MANAGE_USERS), 89 ensureUserHasRight(UserRight.MANAGE_USERS),
@@ -216,6 +229,40 @@ async function updateMe (req: express.Request, res: express.Response, next: expr
216 return res.sendStatus(204) 229 return res.sendStatus(204)
217} 230}
218 231
232async function updateMyAvatar (req: express.Request, res: express.Response, next: express.NextFunction) {
233 const avatarPhysicalFile = req.files['avatarfile'][0]
234 const actor = res.locals.oauth.token.user.Account.Actor
235
236 const avatarDir = CONFIG.STORAGE.AVATARS_DIR
237 const source = join(avatarDir, avatarPhysicalFile.filename)
238 const extension = extname(avatarPhysicalFile.filename)
239 const avatarName = uuidv4() + extension
240 const destination = join(avatarDir, avatarName)
241
242 await renamePromise(source, destination)
243
244 const { avatar } = await sequelizeTypescript.transaction(async t => {
245 const avatar = await AvatarModel.create({
246 filename: avatarName
247 }, { transaction: t })
248
249 if (actor.Avatar) {
250 await actor.Avatar.destroy({ transaction: t })
251 }
252
253 actor.set('avatarId', avatar.id)
254 await actor.save({ transaction: t })
255
256 return { actor, avatar }
257 })
258
259 return res
260 .json({
261 avatar: avatar.toFormattedJSON()
262 })
263 .end()
264}
265
219async function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) { 266async function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) {
220 const body: UserUpdate = req.body 267 const body: UserUpdate = req.body
221 const user = res.locals.user as UserModel 268 const user = res.locals.user as UserModel
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts
index 11e3da5cc..ff0d967e1 100644
--- a/server/controllers/api/videos/index.ts
+++ b/server/controllers/api/videos/index.ts
@@ -6,7 +6,7 @@ import { renamePromise } from '../../../helpers/core-utils'
6import { retryTransactionWrapper } from '../../../helpers/database-utils' 6import { retryTransactionWrapper } from '../../../helpers/database-utils'
7import { getVideoFileHeight } from '../../../helpers/ffmpeg-utils' 7import { getVideoFileHeight } from '../../../helpers/ffmpeg-utils'
8import { logger } from '../../../helpers/logger' 8import { logger } from '../../../helpers/logger'
9import { generateRandomString, getFormattedObjects, getServerActor, resetSequelizeInstance } from '../../../helpers/utils' 9import { createReqFiles, generateRandomString, getFormattedObjects, getServerActor, resetSequelizeInstance } from '../../../helpers/utils'
10import { 10import {
11 CONFIG, sequelizeTypescript, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_MIMETYPE_EXT, 11 CONFIG, sequelizeTypescript, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_MIMETYPE_EXT,
12 VIDEO_PRIVACIES 12 VIDEO_PRIVACIES
@@ -29,28 +29,7 @@ import { rateVideoRouter } from './rate'
29 29
30const videosRouter = express.Router() 30const videosRouter = express.Router()
31 31
32// multer configuration 32const reqVideoFile = createReqFiles('videofile', CONFIG.STORAGE.VIDEOS_DIR, VIDEO_MIMETYPE_EXT)
33const storage = multer.diskStorage({
34 destination: (req, file, cb) => {
35 cb(null, CONFIG.STORAGE.VIDEOS_DIR)
36 },
37
38 filename: async (req, file, cb) => {
39 const extension = VIDEO_MIMETYPE_EXT[file.mimetype]
40 let randomString = ''
41
42 try {
43 randomString = await generateRandomString(16)
44 } catch (err) {
45 logger.error('Cannot generate random string for file name.', err)
46 randomString = 'fake-random-string'
47 }
48
49 cb(null, randomString + extension)
50 }
51})
52
53const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }])
54 33
55videosRouter.use('/', abuseVideoRouter) 34videosRouter.use('/', abuseVideoRouter)
56videosRouter.use('/', blacklistRouter) 35videosRouter.use('/', blacklistRouter)
@@ -85,7 +64,7 @@ videosRouter.put('/:id',
85) 64)
86videosRouter.post('/upload', 65videosRouter.post('/upload',
87 authenticate, 66 authenticate,
88 reqFiles, 67 reqVideoFile,
89 asyncMiddleware(videosAddValidator), 68 asyncMiddleware(videosAddValidator),
90 asyncMiddleware(addVideoRetryWrapper) 69 asyncMiddleware(addVideoRetryWrapper)
91) 70)