From c5911fd347c76e8bdc05ea9f3ee9efed4a58c236 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 29 Dec 2017 19:10:13 +0100 Subject: Begin to add avatar to actors --- server/helpers/custom-validators/users.ts | 21 +++++++++++++++++++-- server/helpers/utils.ts | 30 ++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 4 deletions(-) (limited to 'server/helpers') 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 @@ import * as validator from 'validator' import 'express-validator' -import { exists } from './misc' +import { exists, isArray } from './misc' import { CONSTRAINTS_FIELDS } from '../../initializers' import { UserRole } from '../../../shared' @@ -37,6 +37,22 @@ function isUserRoleValid (value: any) { return exists(value) && validator.isInt('' + value) && UserRole[value] !== undefined } +function isAvatarFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) { + // Should have files + if (!files) return false + if (isArray(files)) return false + + // Should have videofile file + const avatarfile = files['avatarfile'] + if (!avatarfile || avatarfile.length === 0) return false + + // The file should exist + const file = avatarfile[0] + if (!file || !file.originalname) return false + + return new RegExp('^image/(png|jpeg)$', 'i').test(file.mimetype) +} + // --------------------------------------------------------------------------- export { @@ -45,5 +61,6 @@ export { isUserVideoQuotaValid, isUserUsernameValid, isUserDisplayNSFWValid, - isUserAutoPlayVideoValid + isUserAutoPlayVideoValid, + isAvatarFile } 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 @@ import * as express from 'express' +import * as multer from 'multer' import { Model } from 'sequelize-typescript' import { ResultList } from '../../shared' import { VideoResolution } from '../../shared/models/videos' -import { CONFIG, REMOTE_SCHEME } from '../initializers' +import { CONFIG, REMOTE_SCHEME, VIDEO_MIMETYPE_EXT } from '../initializers' import { UserModel } from '../models/account/user' import { ActorModel } from '../models/activitypub/actor' import { ApplicationModel } from '../models/application/application' @@ -26,6 +27,30 @@ function badRequest (req: express.Request, res: express.Response, next: express. return res.type('json').status(400).end() } +function createReqFiles (fieldName: string, storageDir: string, mimeTypes: { [ id: string ]: string }) { + const storage = multer.diskStorage({ + destination: (req, file, cb) => { + cb(null, storageDir) + }, + + filename: async (req, file, cb) => { + const extension = mimeTypes[file.mimetype] + let randomString = '' + + try { + randomString = await generateRandomString(16) + } catch (err) { + logger.error('Cannot generate random string for file name.', err) + randomString = 'fake-random-string' + } + + cb(null, randomString + extension) + } + }) + + return multer({ storage }).fields([{ name: fieldName, maxCount: 1 }]) +} + async function generateRandomString (size: number) { const raw = await pseudoRandomBytesPromise(size) @@ -122,5 +147,6 @@ export { resetSequelizeInstance, getServerActor, SortType, - getHostWithPort + getHostWithPort, + createReqFiles } -- cgit v1.2.3