diff options
author | Chocobozzz <me@florianbigard.com> | 2017-12-29 19:10:13 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2017-12-29 19:10:13 +0100 |
commit | c5911fd347c76e8bdc05ea9f3ee9efed4a58c236 (patch) | |
tree | b8d287daca6c45305090cbec9da97d1155f275bd /server/helpers | |
parent | 8b0d42ee372de6589796be26b83e5bffb1b69cdf (diff) | |
download | PeerTube-c5911fd347c76e8bdc05ea9f3ee9efed4a58c236.tar.gz PeerTube-c5911fd347c76e8bdc05ea9f3ee9efed4a58c236.tar.zst PeerTube-c5911fd347c76e8bdc05ea9f3ee9efed4a58c236.zip |
Begin to add avatar to actors
Diffstat (limited to 'server/helpers')
-rw-r--r-- | server/helpers/custom-validators/users.ts | 21 | ||||
-rw-r--r-- | server/helpers/utils.ts | 30 |
2 files changed, 47 insertions, 4 deletions
diff --git a/server/helpers/custom-validators/users.ts b/server/helpers/custom-validators/users.ts index 159c2a700..6ed60c1c4 100644 --- a/server/helpers/custom-validators/users.ts +++ b/server/helpers/custom-validators/users.ts | |||
@@ -1,7 +1,7 @@ | |||
1 | import * as validator from 'validator' | 1 | import * as validator from 'validator' |
2 | import 'express-validator' | 2 | import 'express-validator' |
3 | 3 | ||
4 | import { exists } from './misc' | 4 | import { exists, isArray } from './misc' |
5 | import { CONSTRAINTS_FIELDS } from '../../initializers' | 5 | import { CONSTRAINTS_FIELDS } from '../../initializers' |
6 | import { UserRole } from '../../../shared' | 6 | import { UserRole } from '../../../shared' |
7 | 7 | ||
@@ -37,6 +37,22 @@ function isUserRoleValid (value: any) { | |||
37 | return exists(value) && validator.isInt('' + value) && UserRole[value] !== undefined | 37 | return exists(value) && validator.isInt('' + value) && UserRole[value] !== undefined |
38 | } | 38 | } |
39 | 39 | ||
40 | function isAvatarFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) { | ||
41 | // Should have files | ||
42 | if (!files) return false | ||
43 | if (isArray(files)) return false | ||
44 | |||
45 | // Should have videofile file | ||
46 | const avatarfile = files['avatarfile'] | ||
47 | if (!avatarfile || avatarfile.length === 0) return false | ||
48 | |||
49 | // The file should exist | ||
50 | const file = avatarfile[0] | ||
51 | if (!file || !file.originalname) return false | ||
52 | |||
53 | return new RegExp('^image/(png|jpeg)$', 'i').test(file.mimetype) | ||
54 | } | ||
55 | |||
40 | // --------------------------------------------------------------------------- | 56 | // --------------------------------------------------------------------------- |
41 | 57 | ||
42 | export { | 58 | export { |
@@ -45,5 +61,6 @@ export { | |||
45 | isUserVideoQuotaValid, | 61 | isUserVideoQuotaValid, |
46 | isUserUsernameValid, | 62 | isUserUsernameValid, |
47 | isUserDisplayNSFWValid, | 63 | isUserDisplayNSFWValid, |
48 | isUserAutoPlayVideoValid | 64 | isUserAutoPlayVideoValid, |
65 | isAvatarFile | ||
49 | } | 66 | } |
diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts index 769aa83c6..7a32e286c 100644 --- a/server/helpers/utils.ts +++ b/server/helpers/utils.ts | |||
@@ -1,8 +1,9 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import * as multer from 'multer' | ||
2 | import { Model } from 'sequelize-typescript' | 3 | import { Model } from 'sequelize-typescript' |
3 | import { ResultList } from '../../shared' | 4 | import { ResultList } from '../../shared' |
4 | import { VideoResolution } from '../../shared/models/videos' | 5 | import { VideoResolution } from '../../shared/models/videos' |
5 | import { CONFIG, REMOTE_SCHEME } from '../initializers' | 6 | import { CONFIG, REMOTE_SCHEME, VIDEO_MIMETYPE_EXT } from '../initializers' |
6 | import { UserModel } from '../models/account/user' | 7 | import { UserModel } from '../models/account/user' |
7 | import { ActorModel } from '../models/activitypub/actor' | 8 | import { ActorModel } from '../models/activitypub/actor' |
8 | import { ApplicationModel } from '../models/application/application' | 9 | import { ApplicationModel } from '../models/application/application' |
@@ -26,6 +27,30 @@ function badRequest (req: express.Request, res: express.Response, next: express. | |||
26 | return res.type('json').status(400).end() | 27 | return res.type('json').status(400).end() |
27 | } | 28 | } |
28 | 29 | ||
30 | function createReqFiles (fieldName: string, storageDir: string, mimeTypes: { [ id: string ]: string }) { | ||
31 | const storage = multer.diskStorage({ | ||
32 | destination: (req, file, cb) => { | ||
33 | cb(null, storageDir) | ||
34 | }, | ||
35 | |||
36 | filename: async (req, file, cb) => { | ||
37 | const extension = mimeTypes[file.mimetype] | ||
38 | let randomString = '' | ||
39 | |||
40 | try { | ||
41 | randomString = await generateRandomString(16) | ||
42 | } catch (err) { | ||
43 | logger.error('Cannot generate random string for file name.', err) | ||
44 | randomString = 'fake-random-string' | ||
45 | } | ||
46 | |||
47 | cb(null, randomString + extension) | ||
48 | } | ||
49 | }) | ||
50 | |||
51 | return multer({ storage }).fields([{ name: fieldName, maxCount: 1 }]) | ||
52 | } | ||
53 | |||
29 | async function generateRandomString (size: number) { | 54 | async function generateRandomString (size: number) { |
30 | const raw = await pseudoRandomBytesPromise(size) | 55 | const raw = await pseudoRandomBytesPromise(size) |
31 | 56 | ||
@@ -122,5 +147,6 @@ export { | |||
122 | resetSequelizeInstance, | 147 | resetSequelizeInstance, |
123 | getServerActor, | 148 | getServerActor, |
124 | SortType, | 149 | SortType, |
125 | getHostWithPort | 150 | getHostWithPort, |
151 | createReqFiles | ||
126 | } | 152 | } |