diff options
Diffstat (limited to 'server/controllers/api')
-rw-r--r-- | server/controllers/api/users.ts | 53 | ||||
-rw-r--r-- | server/controllers/api/videos/index.ts | 27 |
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 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { extname, join } from 'path' | ||
3 | import * as uuidv4 from 'uuid/v4' | ||
2 | import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared' | 4 | import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared' |
5 | import { renamePromise } from '../../helpers/core-utils' | ||
3 | import { retryTransactionWrapper } from '../../helpers/database-utils' | 6 | import { retryTransactionWrapper } from '../../helpers/database-utils' |
4 | import { logger } from '../../helpers/logger' | 7 | import { logger } from '../../helpers/logger' |
5 | import { getFormattedObjects } from '../../helpers/utils' | 8 | import { createReqFiles, getFormattedObjects } from '../../helpers/utils' |
6 | import { CONFIG } from '../../initializers' | 9 | import { AVATAR_MIMETYPE_EXT, CONFIG, sequelizeTypescript } from '../../initializers' |
7 | import { createUserAccountAndChannel } from '../../lib/user' | 10 | import { createUserAccountAndChannel } from '../../lib/user' |
8 | import { | 11 | import { |
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' |
13 | import { videosSortValidator } from '../../middlewares/validators' | 16 | import { usersUpdateMyAvatarValidator, videosSortValidator } from '../../middlewares/validators' |
14 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' | 17 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' |
15 | import { UserModel } from '../../models/account/user' | 18 | import { UserModel } from '../../models/account/user' |
19 | import { AvatarModel } from '../../models/avatar/avatar' | ||
16 | import { VideoModel } from '../../models/video/video' | 20 | import { VideoModel } from '../../models/video/video' |
17 | 21 | ||
22 | const reqAvatarFile = createReqFiles('avatarfile', CONFIG.STORAGE.AVATARS_DIR, AVATAR_MIMETYPE_EXT) | ||
23 | |||
18 | const usersRouter = express.Router() | 24 | const usersRouter = express.Router() |
19 | 25 | ||
20 | usersRouter.get('/me', | 26 | usersRouter.get('/me', |
@@ -71,6 +77,13 @@ usersRouter.put('/me', | |||
71 | asyncMiddleware(updateMe) | 77 | asyncMiddleware(updateMe) |
72 | ) | 78 | ) |
73 | 79 | ||
80 | usersRouter.post('/me/avatar/pick', | ||
81 | authenticate, | ||
82 | reqAvatarFile, | ||
83 | usersUpdateMyAvatarValidator, | ||
84 | asyncMiddleware(updateMyAvatar) | ||
85 | ) | ||
86 | |||
74 | usersRouter.put('/:id', | 87 | usersRouter.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 | ||
232 | async 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 | |||
219 | async function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) { | 266 | async 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' | |||
6 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | 6 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
7 | import { getVideoFileHeight } from '../../../helpers/ffmpeg-utils' | 7 | import { getVideoFileHeight } from '../../../helpers/ffmpeg-utils' |
8 | import { logger } from '../../../helpers/logger' | 8 | import { logger } from '../../../helpers/logger' |
9 | import { generateRandomString, getFormattedObjects, getServerActor, resetSequelizeInstance } from '../../../helpers/utils' | 9 | import { createReqFiles, generateRandomString, getFormattedObjects, getServerActor, resetSequelizeInstance } from '../../../helpers/utils' |
10 | import { | 10 | import { |
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 | ||
30 | const videosRouter = express.Router() | 30 | const videosRouter = express.Router() |
31 | 31 | ||
32 | // multer configuration | 32 | const reqVideoFile = createReqFiles('videofile', CONFIG.STORAGE.VIDEOS_DIR, VIDEO_MIMETYPE_EXT) |
33 | const 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 | |||
53 | const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }]) | ||
54 | 33 | ||
55 | videosRouter.use('/', abuseVideoRouter) | 34 | videosRouter.use('/', abuseVideoRouter) |
56 | videosRouter.use('/', blacklistRouter) | 35 | videosRouter.use('/', blacklistRouter) |
@@ -85,7 +64,7 @@ videosRouter.put('/:id', | |||
85 | ) | 64 | ) |
86 | videosRouter.post('/upload', | 65 | videosRouter.post('/upload', |
87 | authenticate, | 66 | authenticate, |
88 | reqFiles, | 67 | reqVideoFile, |
89 | asyncMiddleware(videosAddValidator), | 68 | asyncMiddleware(videosAddValidator), |
90 | asyncMiddleware(addVideoRetryWrapper) | 69 | asyncMiddleware(addVideoRetryWrapper) |
91 | ) | 70 | ) |