aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-23 17:53:38 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:40:53 +0100
commit8d468a16fd33ec2660c3c59b3f7def53eb0cc4a1 (patch)
tree078708f8316ed6294088b159f79e861664fca953 /server/helpers
parent39445ead45aaaea801ec09991b8dd2464f722e47 (diff)
downloadPeerTube-8d468a16fd33ec2660c3c59b3f7def53eb0cc4a1.tar.gz
PeerTube-8d468a16fd33ec2660c3c59b3f7def53eb0cc4a1.tar.zst
PeerTube-8d468a16fd33ec2660c3c59b3f7def53eb0cc4a1.zip
Cleanup helpers
Diffstat (limited to 'server/helpers')
-rw-r--r--server/helpers/core-utils.ts23
-rw-r--r--server/helpers/custom-validators/accounts.ts7
-rw-r--r--server/helpers/custom-validators/video-channels.ts5
-rw-r--r--server/helpers/custom-validators/videos.ts113
-rw-r--r--server/helpers/database-utils.ts1
-rw-r--r--server/helpers/ffmpeg-utils.ts3
-rw-r--r--server/helpers/logger.ts2
-rw-r--r--server/helpers/peertube-crypto.ts15
-rw-r--r--server/helpers/utils.ts5
-rw-r--r--server/helpers/webfinger.ts1
10 files changed, 32 insertions, 143 deletions
diff --git a/server/helpers/core-utils.ts b/server/helpers/core-utils.ts
index 4ff07848c..443115336 100644
--- a/server/helpers/core-utils.ts
+++ b/server/helpers/core-utils.ts
@@ -3,23 +3,14 @@
3 Useful to avoid circular dependencies. 3 Useful to avoid circular dependencies.
4*/ 4*/
5 5
6import { join } from 'path'
7import { pseudoRandomBytes } from 'crypto'
8import {
9 readdir,
10 readFile,
11 rename,
12 unlink,
13 writeFile,
14 access,
15 stat,
16 Stats
17} from 'fs'
18import * as mkdirp from 'mkdirp'
19import * as bcrypt from 'bcrypt' 6import * as bcrypt from 'bcrypt'
20import * as createTorrent from 'create-torrent' 7import * as createTorrent from 'create-torrent'
21import * as rimraf from 'rimraf' 8import { pseudoRandomBytes } from 'crypto'
9import { readdir, readFile, rename, stat, Stats, unlink, writeFile } from 'fs'
10import * as mkdirp from 'mkdirp'
11import { join } from 'path'
22import * as pem from 'pem' 12import * as pem from 'pem'
13import * as rimraf from 'rimraf'
23 14
24function isTestInstance () { 15function isTestInstance () {
25 return process.env.NODE_ENV === 'test' 16 return process.env.NODE_ENV === 'test'
@@ -100,7 +91,6 @@ function promisify2WithVoid<T, U> (func: (arg1: T, arg2: U, cb: (err: any) => vo
100 } 91 }
101} 92}
102 93
103const readFilePromise = promisify2<string, string, string>(readFile)
104const readFileBufferPromise = promisify1<string, Buffer>(readFile) 94const readFileBufferPromise = promisify1<string, Buffer>(readFile)
105const unlinkPromise = promisify1WithVoid<string>(unlink) 95const unlinkPromise = promisify1WithVoid<string>(unlink)
106const renamePromise = promisify2WithVoid<string, string>(rename) 96const renamePromise = promisify2WithVoid<string, string>(rename)
@@ -108,7 +98,6 @@ const writeFilePromise = promisify2WithVoid<string, any>(writeFile)
108const readdirPromise = promisify1<string, string[]>(readdir) 98const readdirPromise = promisify1<string, string[]>(readdir)
109const mkdirpPromise = promisify1<string, string>(mkdirp) 99const mkdirpPromise = promisify1<string, string>(mkdirp)
110const pseudoRandomBytesPromise = promisify1<number, Buffer>(pseudoRandomBytes) 100const pseudoRandomBytesPromise = promisify1<number, Buffer>(pseudoRandomBytes)
111const accessPromise = promisify1WithVoid<string | Buffer>(access)
112const createPrivateKey = promisify1<number, { key: string }>(pem.createPrivateKey) 101const createPrivateKey = promisify1<number, { key: string }>(pem.createPrivateKey)
113const getPublicKey = promisify1<string, { publicKey: string }>(pem.getPublicKey) 102const getPublicKey = promisify1<string, { publicKey: string }>(pem.getPublicKey)
114const bcryptComparePromise = promisify2<any, string, boolean>(bcrypt.compare) 103const bcryptComparePromise = promisify2<any, string, boolean>(bcrypt.compare)
@@ -130,14 +119,12 @@ export {
130 promisify1, 119 promisify1,
131 120
132 readdirPromise, 121 readdirPromise,
133 readFilePromise,
134 readFileBufferPromise, 122 readFileBufferPromise,
135 unlinkPromise, 123 unlinkPromise,
136 renamePromise, 124 renamePromise,
137 writeFilePromise, 125 writeFilePromise,
138 mkdirpPromise, 126 mkdirpPromise,
139 pseudoRandomBytesPromise, 127 pseudoRandomBytesPromise,
140 accessPromise,
141 createPrivateKey, 128 createPrivateKey,
142 getPublicKey, 129 getPublicKey,
143 bcryptComparePromise, 130 bcryptComparePromise,
diff --git a/server/helpers/custom-validators/accounts.ts b/server/helpers/custom-validators/accounts.ts
index 088d5fc8f..83540e545 100644
--- a/server/helpers/custom-validators/accounts.ts
+++ b/server/helpers/custom-validators/accounts.ts
@@ -14,12 +14,6 @@ function isAccountNameValid (value: string) {
14 return isUserUsernameValid(value) 14 return isUserUsernameValid(value)
15} 15}
16 16
17function isAccountNameWithHostValid (value: string) {
18 const [ name, host ] = value.split('@')
19
20 return isAccountNameValid(name) && isHostValid(host)
21}
22
23function checkVideoAccountExists (id: string, res: express.Response, callback: () => void) { 17function checkVideoAccountExists (id: string, res: express.Response, callback: () => void) {
24 let promise: Promise<AccountInstance> 18 let promise: Promise<AccountInstance>
25 if (validator.isInt(id)) { 19 if (validator.isInt(id)) {
@@ -48,6 +42,5 @@ function checkVideoAccountExists (id: string, res: express.Response, callback: (
48 42
49export { 43export {
50 checkVideoAccountExists, 44 checkVideoAccountExists,
51 isAccountNameWithHostValid,
52 isAccountNameValid 45 isAccountNameValid
53} 46}
diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts
index 5787c3850..5de01f74b 100644
--- a/server/helpers/custom-validators/video-channels.ts
+++ b/server/helpers/custom-validators/video-channels.ts
@@ -24,10 +24,6 @@ function isVideoChannelNameValid (value: string) {
24 return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME) 24 return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME)
25} 25}
26 26
27function isVideoChannelUUIDValid (value: string) {
28 return exists(value) && validator.isUUID('' + value, 4)
29}
30
31function checkVideoChannelExists (id: string, res: express.Response, callback: () => void) { 27function checkVideoChannelExists (id: string, res: express.Response, callback: () => void) {
32 let promise: Promise<VideoChannelInstance> 28 let promise: Promise<VideoChannelInstance>
33 if (validator.isInt(id)) { 29 if (validator.isInt(id)) {
@@ -57,7 +53,6 @@ function checkVideoChannelExists (id: string, res: express.Response, callback: (
57export { 53export {
58 isVideoChannelDescriptionValid, 54 isVideoChannelDescriptionValid,
59 isVideoChannelNameValid, 55 isVideoChannelNameValid,
60 isVideoChannelUUIDValid,
61 checkVideoChannelExists, 56 checkVideoChannelExists,
62 isVideoChannelUrlValid 57 isVideoChannelUrlValid
63} 58}
diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts
index 07aadadb1..c893d2c7c 100644
--- a/server/helpers/custom-validators/videos.ts
+++ b/server/helpers/custom-validators/videos.ts
@@ -1,37 +1,25 @@
1import * as Promise from 'bluebird' 1import * as Bluebird from 'bluebird'
2import * as express from 'express' 2import { Response } from 'express'
3import 'express-validator' 3import 'express-validator'
4import { values } from 'lodash' 4import { values } from 'lodash'
5import 'multer' 5import 'multer'
6import * as validator from 'validator' 6import * as validator from 'validator'
7import { VideoRateType } from '../../../shared' 7import { VideoRateType } from '../../../shared'
8import { logger } from '../../helpers' 8import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_RATE_TYPES } from '../../initializers'
9import { 9import { database as db } from '../../initializers/database'
10 CONSTRAINTS_FIELDS, 10import { VideoInstance } from '../../models/video/video-interface'
11 database as db, 11import { logger } from '../logger'
12 VIDEO_CATEGORIES,
13 VIDEO_LANGUAGES,
14 VIDEO_LICENCES,
15 VIDEO_PRIVACIES,
16 VIDEO_RATE_TYPES
17} from '../../initializers'
18import { VideoInstance } from '../../models'
19import { isActivityPubUrlValid } from './activitypub/misc' 12import { isActivityPubUrlValid } from './activitypub/misc'
20import { exists, isArray } from './misc' 13import { exists, isArray } from './misc'
14import { VIDEO_PRIVACIES } from '../../initializers/constants'
21 15
22const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS 16const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
23const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES 17const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
24const VIDEO_EVENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_EVENTS
25 18
26function isVideoCategoryValid (value: number) { 19function isVideoCategoryValid (value: number) {
27 return VIDEO_CATEGORIES[value] !== undefined 20 return VIDEO_CATEGORIES[value] !== undefined
28} 21}
29 22
30// Maybe we don't know the remote category, but that doesn't matter
31function isRemoteVideoCategoryValid (value: string) {
32 return validator.isInt('' + value)
33}
34
35function isVideoUrlValid (value: string) { 23function isVideoUrlValid (value: string) {
36 return isActivityPubUrlValid(value) 24 return isActivityPubUrlValid(value)
37} 25}
@@ -40,29 +28,10 @@ function isVideoLicenceValid (value: number) {
40 return VIDEO_LICENCES[value] !== undefined 28 return VIDEO_LICENCES[value] !== undefined
41} 29}
42 30
43function isVideoPrivacyValid (value: string) {
44 return VIDEO_PRIVACIES[value] !== undefined
45}
46
47// Maybe we don't know the remote privacy setting, but that doesn't matter
48function isRemoteVideoPrivacyValid (value: string) {
49 return validator.isInt('' + value)
50}
51
52// Maybe we don't know the remote licence, but that doesn't matter
53function isRemoteVideoLicenceValid (value: string) {
54 return validator.isInt('' + value)
55}
56
57function isVideoLanguageValid (value: number) { 31function isVideoLanguageValid (value: number) {
58 return value === null || VIDEO_LANGUAGES[value] !== undefined 32 return value === null || VIDEO_LANGUAGES[value] !== undefined
59} 33}
60 34
61// Maybe we don't know the remote language, but that doesn't matter
62function isRemoteVideoLanguageValid (value: string) {
63 return validator.isInt('' + value)
64}
65
66function isVideoNSFWValid (value: any) { 35function isVideoNSFWValid (value: any) {
67 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value)) 36 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
68} 37}
@@ -93,14 +62,6 @@ function isVideoTagsValid (tags: string[]) {
93 tags.every(tag => isVideoTagValid(tag)) 62 tags.every(tag => isVideoTagValid(tag))
94} 63}
95 64
96function isVideoThumbnailValid (value: string) {
97 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL)
98}
99
100function isVideoThumbnailDataValid (value: string) {
101 return exists(value) && validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA)
102}
103
104function isVideoAbuseReasonValid (value: string) { 65function isVideoAbuseReasonValid (value: string) {
105 return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON) 66 return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
106} 67}
@@ -109,18 +70,6 @@ function isVideoViewsValid (value: string) {
109 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS) 70 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
110} 71}
111 72
112function isVideoLikesValid (value: string) {
113 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES)
114}
115
116function isVideoDislikesValid (value: string) {
117 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES)
118}
119
120function isVideoEventCountValid (value: string) {
121 return exists(value) && validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT)
122}
123
124function isVideoRatingTypeValid (value: string) { 73function isVideoRatingTypeValid (value: string) {
125 return values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1 74 return values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
126} 75}
@@ -141,24 +90,16 @@ function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } |
141 return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype) 90 return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype)
142} 91}
143 92
144function isVideoFileSizeValid (value: string) {
145 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE)
146}
147
148function isVideoFileResolutionValid (value: string) {
149 return exists(value) && validator.isInt(value + '')
150}
151
152function isVideoFileExtnameValid (value: string) {
153 return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1
154}
155
156function isVideoFileInfoHashValid (value: string) { 93function isVideoFileInfoHashValid (value: string) {
157 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH) 94 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
158} 95}
159 96
160function checkVideoExists (id: string, res: express.Response, callback: () => void) { 97function isVideoPrivacyValid (value: string) {
161 let promise: Promise<VideoInstance> 98 return VIDEO_PRIVACIES[value] !== undefined
99}
100
101function checkVideoExists (id: string, res: Response, callback: () => void) {
102 let promise: Bluebird<VideoInstance>
162 if (validator.isInt(id)) { 103 if (validator.isInt(id)) {
163 promise = db.Video.loadAndPopulateAccountAndServerAndTags(+id) 104 promise = db.Video.loadAndPopulateAccountAndServerAndTags(+id)
164 } else { // UUID 105 } else { // UUID
@@ -168,17 +109,17 @@ function checkVideoExists (id: string, res: express.Response, callback: () => vo
168 promise.then(video => { 109 promise.then(video => {
169 if (!video) { 110 if (!video) {
170 return res.status(404) 111 return res.status(404)
171 .json({ error: 'Video not found' }) 112 .json({ error: 'Video not found' })
172 .end() 113 .end()
173 } 114 }
174 115
175 res.locals.video = video 116 res.locals.video = video
176 callback() 117 callback()
177 }) 118 })
178 .catch(err => { 119 .catch(err => {
179 logger.error('Error in video request validator.', err) 120 logger.error('Error in video request validator.', err)
180 return res.sendStatus(500) 121 return res.sendStatus(500)
181 }) 122 })
182} 123}
183 124
184// --------------------------------------------------------------------------- 125// ---------------------------------------------------------------------------
@@ -193,25 +134,13 @@ export {
193 isVideoFileInfoHashValid, 134 isVideoFileInfoHashValid,
194 isVideoNameValid, 135 isVideoNameValid,
195 isVideoTagsValid, 136 isVideoTagsValid,
196 isVideoThumbnailValid,
197 isVideoThumbnailDataValid,
198 isVideoFileExtnameValid,
199 isVideoAbuseReasonValid, 137 isVideoAbuseReasonValid,
200 isVideoFile, 138 isVideoFile,
201 isVideoViewsValid, 139 isVideoViewsValid,
202 isVideoLikesValid,
203 isVideoRatingTypeValid, 140 isVideoRatingTypeValid,
204 isVideoDislikesValid,
205 isVideoEventCountValid,
206 isVideoFileSizeValid,
207 isVideoPrivacyValid,
208 isRemoteVideoPrivacyValid,
209 isVideoDurationValid, 141 isVideoDurationValid,
210 isVideoFileResolutionValid,
211 checkVideoExists,
212 isVideoTagValid, 142 isVideoTagValid,
213 isRemoteVideoCategoryValid,
214 isRemoteVideoLicenceValid,
215 isVideoUrlValid, 143 isVideoUrlValid,
216 isRemoteVideoLanguageValid 144 isVideoPrivacyValid,
145 checkVideoExists
217} 146}
diff --git a/server/helpers/database-utils.ts b/server/helpers/database-utils.ts
index dacd747c9..fb8ad22b0 100644
--- a/server/helpers/database-utils.ts
+++ b/server/helpers/database-utils.ts
@@ -1,4 +1,3 @@
1// TODO: import from ES6 when retry typing file will include errorFilter function
2import * as retry from 'async/retry' 1import * as retry from 'async/retry'
3import * as Bluebird from 'bluebird' 2import * as Bluebird from 'bluebird'
4import { logger } from './logger' 3import { logger } from './logger'
diff --git a/server/helpers/ffmpeg-utils.ts b/server/helpers/ffmpeg-utils.ts
index c07dddefe..8ad205961 100644
--- a/server/helpers/ffmpeg-utils.ts
+++ b/server/helpers/ffmpeg-utils.ts
@@ -1,7 +1,6 @@
1import * as ffmpeg from 'fluent-ffmpeg' 1import * as ffmpeg from 'fluent-ffmpeg'
2
3import { CONFIG } from '../initializers'
4import { VideoResolution } from '../../shared/models/videos/video-resolution.enum' 2import { VideoResolution } from '../../shared/models/videos/video-resolution.enum'
3import { CONFIG } from '../initializers'
5 4
6function getVideoFileHeight (path: string) { 5function getVideoFileHeight (path: string) {
7 return new Promise<number>((res, rej) => { 6 return new Promise<number>((res, rej) => {
diff --git a/server/helpers/logger.ts b/server/helpers/logger.ts
index d8f2f687c..8d809d16d 100644
--- a/server/helpers/logger.ts
+++ b/server/helpers/logger.ts
@@ -2,8 +2,6 @@
2import * as mkdirp from 'mkdirp' 2import * as mkdirp from 'mkdirp'
3import * as path from 'path' 3import * as path from 'path'
4import * as winston from 'winston' 4import * as winston from 'winston'
5
6// Do not use barrel (dependencies issues)
7import { CONFIG } from '../initializers/constants' 5import { CONFIG } from '../initializers/constants'
8 6
9const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT 7const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
diff --git a/server/helpers/peertube-crypto.ts b/server/helpers/peertube-crypto.ts
index c61abfa8e..74e4cc703 100644
--- a/server/helpers/peertube-crypto.ts
+++ b/server/helpers/peertube-crypto.ts
@@ -1,17 +1,8 @@
1import { 1import { BCRYPT_SALT_SIZE, PRIVATE_RSA_KEY_SIZE } from '../initializers'
2 PRIVATE_RSA_KEY_SIZE,
3 BCRYPT_SALT_SIZE
4} from '../initializers'
5import {
6 bcryptComparePromise,
7 bcryptGenSaltPromise,
8 bcryptHashPromise,
9 createPrivateKey,
10 getPublicKey
11} from './core-utils'
12import { logger } from './logger'
13import { AccountInstance } from '../models/account/account-interface' 2import { AccountInstance } from '../models/account/account-interface'
3import { bcryptComparePromise, bcryptGenSaltPromise, bcryptHashPromise, createPrivateKey, getPublicKey } from './core-utils'
14import { jsig } from './custom-jsonld-signature' 4import { jsig } from './custom-jsonld-signature'
5import { logger } from './logger'
15 6
16async function createPrivateAndPublicKeys () { 7async function createPrivateAndPublicKeys () {
17 logger.info('Generating a RSA key...') 8 logger.info('Generating a RSA key...')
diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts
index 3af14a68a..3464341e6 100644
--- a/server/helpers/utils.ts
+++ b/server/helpers/utils.ts
@@ -1,11 +1,10 @@
1import * as express from 'express' 1import * as express from 'express'
2import * as Sequelize from 'sequelize' 2import * as Sequelize from 'sequelize'
3
4import { pseudoRandomBytesPromise } from './core-utils'
5import { CONFIG, database as db } from '../initializers'
6import { ResultList } from '../../shared' 3import { ResultList } from '../../shared'
7import { VideoResolution } from '../../shared/models/videos/video-resolution.enum' 4import { VideoResolution } from '../../shared/models/videos/video-resolution.enum'
5import { CONFIG, database as db } from '../initializers'
8import { AccountInstance } from '../models/account/account-interface' 6import { AccountInstance } from '../models/account/account-interface'
7import { pseudoRandomBytesPromise } from './core-utils'
9import { logger } from './logger' 8import { logger } from './logger'
10 9
11function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) { 10function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
diff --git a/server/helpers/webfinger.ts b/server/helpers/webfinger.ts
index a5b4785fe..ab2888981 100644
--- a/server/helpers/webfinger.ts
+++ b/server/helpers/webfinger.ts
@@ -1,7 +1,6 @@
1import * as WebFinger from 'webfinger.js' 1import * as WebFinger from 'webfinger.js'
2import { WebFingerData } from '../../shared' 2import { WebFingerData } from '../../shared'
3import { fetchRemoteAccount } from '../lib/activitypub/account' 3import { fetchRemoteAccount } from '../lib/activitypub/account'
4
5import { isTestInstance } from './core-utils' 4import { isTestInstance } from './core-utils'
6import { isActivityPubUrlValid } from './custom-validators' 5import { isActivityPubUrlValid } from './custom-validators'
7 6