Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Cleanup helpers

Chocobozzz8d468a16fd33
diff --git a/server/helpers/core-utils.ts b/server/helpers/core-utils.ts
index 4ff07848ca..4431153369 100644
--- a/server/helpers/core-utils.ts
+++ b/server/helpers/core-utils.ts
@@ -3,23 +3,14 @@
Useful to avoid circular dependencies.
*/
-import { join } from 'path'
-import { pseudoRandomBytes } from 'crypto'
-import {
- readdir,
- readFile,
- rename,
- unlink,
- writeFile,
- access,
- stat,
- Stats
-} from 'fs'
-import * as mkdirp from 'mkdirp'
import * as bcrypt from 'bcrypt'
import * as createTorrent from 'create-torrent'
-import * as rimraf from 'rimraf'
+import { pseudoRandomBytes } from 'crypto'
+import { readdir, readFile, rename, stat, Stats, unlink, writeFile } from 'fs'
+import * as mkdirp from 'mkdirp'
+import { join } from 'path'
import * as pem from 'pem'
+import * as rimraf from 'rimraf'
function isTestInstance () {
return process.env.NODE_ENV === 'test'
@@ -100,7 +91,6 @@ function promisify2WithVoid<T, U> (func: (arg1: T, arg2: U, cb: (err: any) => vo
}
}
-const readFilePromise = promisify2<string, string, string>(readFile)
const readFileBufferPromise = promisify1<string, Buffer>(readFile)
const unlinkPromise = promisify1WithVoid<string>(unlink)
const renamePromise = promisify2WithVoid<string, string>(rename)
@@ -108,7 +98,6 @@ const writeFilePromise = promisify2WithVoid<string, any>(writeFile)
const readdirPromise = promisify1<string, string[]>(readdir)
const mkdirpPromise = promisify1<string, string>(mkdirp)
const pseudoRandomBytesPromise = promisify1<number, Buffer>(pseudoRandomBytes)
-const accessPromise = promisify1WithVoid<string | Buffer>(access)
const createPrivateKey = promisify1<number, { key: string }>(pem.createPrivateKey)
const getPublicKey = promisify1<string, { publicKey: string }>(pem.getPublicKey)
const bcryptComparePromise = promisify2<any, string, boolean>(bcrypt.compare)
@@ -130,14 +119,12 @@ export {
promisify1,
readdirPromise,
- readFilePromise,
readFileBufferPromise,
unlinkPromise,
renamePromise,
writeFilePromise,
mkdirpPromise,
pseudoRandomBytesPromise,
- accessPromise,
createPrivateKey,
getPublicKey,
bcryptComparePromise,
diff --git a/server/helpers/custom-validators/accounts.ts b/server/helpers/custom-validators/accounts.ts
index 088d5fc8f4..83540e5457 100644
--- a/server/helpers/custom-validators/accounts.ts
+++ b/server/helpers/custom-validators/accounts.ts
@@ -14,12 +14,6 @@ function isAccountNameValid (value: string) {
return isUserUsernameValid(value)
}
-function isAccountNameWithHostValid (value: string) {
- const [ name, host ] = value.split('@')
-
- return isAccountNameValid(name) && isHostValid(host)
-}
-
function checkVideoAccountExists (id: string, res: express.Response, callback: () => void) {
let promise: Promise<AccountInstance>
if (validator.isInt(id)) {
@@ -48,6 +42,5 @@ function checkVideoAccountExists (id: string, res: express.Response, callback: (
export {
checkVideoAccountExists,
- isAccountNameWithHostValid,
isAccountNameValid
}
diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts
index 5787c3850c..5de01f74b4 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) {
return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME)
}
-function isVideoChannelUUIDValid (value: string) {
- return exists(value) && validator.isUUID('' + value, 4)
-}
-
function checkVideoChannelExists (id: string, res: express.Response, callback: () => void) {
let promise: Promise<VideoChannelInstance>
if (validator.isInt(id)) {
@@ -57,7 +53,6 @@ function checkVideoChannelExists (id: string, res: express.Response, callback: (
export {
isVideoChannelDescriptionValid,
isVideoChannelNameValid,
- isVideoChannelUUIDValid,
checkVideoChannelExists,
isVideoChannelUrlValid
}
diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts
index 07aadadb10..c893d2c7cf 100644
--- a/server/helpers/custom-validators/videos.ts
+++ b/server/helpers/custom-validators/videos.ts
@@ -1,37 +1,25 @@
-import * as Promise from 'bluebird'
-import * as express from 'express'
+import * as Bluebird from 'bluebird'
+import { Response } from 'express'
import 'express-validator'
import { values } from 'lodash'
import 'multer'
import * as validator from 'validator'
import { VideoRateType } from '../../../shared'
-import { logger } from '../../helpers'
-import {
- CONSTRAINTS_FIELDS,
- database as db,
- VIDEO_CATEGORIES,
- VIDEO_LANGUAGES,
- VIDEO_LICENCES,
- VIDEO_PRIVACIES,
- VIDEO_RATE_TYPES
-} from '../../initializers'
-import { VideoInstance } from '../../models'
+import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_RATE_TYPES } from '../../initializers'
+import { database as db } from '../../initializers/database'
+import { VideoInstance } from '../../models/video/video-interface'
+import { logger } from '../logger'
import { isActivityPubUrlValid } from './activitypub/misc'
import { exists, isArray } from './misc'
+import { VIDEO_PRIVACIES } from '../../initializers/constants'
const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
-const VIDEO_EVENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_EVENTS
function isVideoCategoryValid (value: number) {
return VIDEO_CATEGORIES[value] !== undefined
}
-// Maybe we don't know the remote category, but that doesn't matter
-function isRemoteVideoCategoryValid (value: string) {
- return validator.isInt('' + value)
-}
-
function isVideoUrlValid (value: string) {
return isActivityPubUrlValid(value)
}
@@ -40,29 +28,10 @@ function isVideoLicenceValid (value: number) {
return VIDEO_LICENCES[value] !== undefined
}
-function isVideoPrivacyValid (value: string) {
- return VIDEO_PRIVACIES[value] !== undefined
-}
-
-// Maybe we don't know the remote privacy setting, but that doesn't matter
-function isRemoteVideoPrivacyValid (value: string) {
- return validator.isInt('' + value)
-}
-
-// Maybe we don't know the remote licence, but that doesn't matter
-function isRemoteVideoLicenceValid (value: string) {
- return validator.isInt('' + value)
-}
-
function isVideoLanguageValid (value: number) {
return value === null || VIDEO_LANGUAGES[value] !== undefined
}
-// Maybe we don't know the remote language, but that doesn't matter
-function isRemoteVideoLanguageValid (value: string) {
- return validator.isInt('' + value)
-}
-
function isVideoNSFWValid (value: any) {
return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
}
@@ -93,14 +62,6 @@ function isVideoTagsValid (tags: string[]) {
tags.every(tag => isVideoTagValid(tag))
}
-function isVideoThumbnailValid (value: string) {
- return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL)
-}
-
-function isVideoThumbnailDataValid (value: string) {
- return exists(value) && validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA)
-}
-
function isVideoAbuseReasonValid (value: string) {
return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
}
@@ -109,18 +70,6 @@ function isVideoViewsValid (value: string) {
return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
}
-function isVideoLikesValid (value: string) {
- return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES)
-}
-
-function isVideoDislikesValid (value: string) {
- return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES)
-}
-
-function isVideoEventCountValid (value: string) {
- return exists(value) && validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT)
-}
-
function isVideoRatingTypeValid (value: string) {
return values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
}
@@ -141,24 +90,16 @@ function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } |
return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype)
}
-function isVideoFileSizeValid (value: string) {
- return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE)
-}
-
-function isVideoFileResolutionValid (value: string) {
- return exists(value) && validator.isInt(value + '')
-}
-
-function isVideoFileExtnameValid (value: string) {
- return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1
-}
-
function isVideoFileInfoHashValid (value: string) {
return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
}
-function checkVideoExists (id: string, res: express.Response, callback: () => void) {
- let promise: Promise<VideoInstance>
+function isVideoPrivacyValid (value: string) {
+ return VIDEO_PRIVACIES[value] !== undefined
+}
+
+function checkVideoExists (id: string, res: Response, callback: () => void) {
+ let promise: Bluebird<VideoInstance>
if (validator.isInt(id)) {
promise = db.Video.loadAndPopulateAccountAndServerAndTags(+id)
} else { // UUID
@@ -168,17 +109,17 @@ function checkVideoExists (id: string, res: express.Response, callback: () => vo
promise.then(video => {
if (!video) {
return res.status(404)
- .json({ error: 'Video not found' })
- .end()
+ .json({ error: 'Video not found' })
+ .end()
}
res.locals.video = video
callback()
})
- .catch(err => {
- logger.error('Error in video request validator.', err)
- return res.sendStatus(500)
- })
+ .catch(err => {
+ logger.error('Error in video request validator.', err)
+ return res.sendStatus(500)
+ })
}
// ---------------------------------------------------------------------------
@@ -193,25 +134,13 @@ export {
isVideoFileInfoHashValid,
isVideoNameValid,
isVideoTagsValid,
- isVideoThumbnailValid,
- isVideoThumbnailDataValid,
- isVideoFileExtnameValid,
isVideoAbuseReasonValid,
isVideoFile,
isVideoViewsValid,
- isVideoLikesValid,
isVideoRatingTypeValid,
- isVideoDislikesValid,
- isVideoEventCountValid,
- isVideoFileSizeValid,
- isVideoPrivacyValid,
- isRemoteVideoPrivacyValid,
isVideoDurationValid,
- isVideoFileResolutionValid,
- checkVideoExists,
isVideoTagValid,
- isRemoteVideoCategoryValid,
- isRemoteVideoLicenceValid,
isVideoUrlValid,
- isRemoteVideoLanguageValid
+ isVideoPrivacyValid,
+ checkVideoExists
}
diff --git a/server/helpers/database-utils.ts b/server/helpers/database-utils.ts
index dacd747c92..fb8ad22b06 100644
--- a/server/helpers/database-utils.ts
+++ b/server/helpers/database-utils.ts
@@ -1,4 +1,3 @@
-// TODO: import from ES6 when retry typing file will include errorFilter function
import * as retry from 'async/retry'
import * as Bluebird from 'bluebird'
import { logger } from './logger'
diff --git a/server/helpers/ffmpeg-utils.ts b/server/helpers/ffmpeg-utils.ts
index c07dddefe9..8ad2059617 100644
--- a/server/helpers/ffmpeg-utils.ts
+++ b/server/helpers/ffmpeg-utils.ts
@@ -1,7 +1,6 @@
import * as ffmpeg from 'fluent-ffmpeg'
-
-import { CONFIG } from '../initializers'
import { VideoResolution } from '../../shared/models/videos/video-resolution.enum'
+import { CONFIG } from '../initializers'
function getVideoFileHeight (path: string) {
return new Promise<number>((res, rej) => {
diff --git a/server/helpers/logger.ts b/server/helpers/logger.ts
index d8f2f687cb..8d809d16d6 100644
--- a/server/helpers/logger.ts
+++ b/server/helpers/logger.ts
@@ -2,8 +2,6 @@
import * as mkdirp from 'mkdirp'
import * as path from 'path'
import * as winston from 'winston'
-
-// Do not use barrel (dependencies issues)
import { CONFIG } from '../initializers/constants'
const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
diff --git a/server/helpers/peertube-crypto.ts b/server/helpers/peertube-crypto.ts
index c61abfa8e7..74e4cc703b 100644
--- a/server/helpers/peertube-crypto.ts
+++ b/server/helpers/peertube-crypto.ts
@@ -1,17 +1,8 @@
-import {
- PRIVATE_RSA_KEY_SIZE,
- BCRYPT_SALT_SIZE
-} from '../initializers'
-import {
- bcryptComparePromise,
- bcryptGenSaltPromise,
- bcryptHashPromise,
- createPrivateKey,
- getPublicKey
-} from './core-utils'
-import { logger } from './logger'
+import { BCRYPT_SALT_SIZE, PRIVATE_RSA_KEY_SIZE } from '../initializers'
import { AccountInstance } from '../models/account/account-interface'
+import { bcryptComparePromise, bcryptGenSaltPromise, bcryptHashPromise, createPrivateKey, getPublicKey } from './core-utils'
import { jsig } from './custom-jsonld-signature'
+import { logger } from './logger'
async function createPrivateAndPublicKeys () {
logger.info('Generating a RSA key...')
diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts
index 3af14a68a5..3464341e66 100644
--- a/server/helpers/utils.ts
+++ b/server/helpers/utils.ts
@@ -1,11 +1,10 @@
import * as express from 'express'
import * as Sequelize from 'sequelize'
-
-import { pseudoRandomBytesPromise } from './core-utils'
-import { CONFIG, database as db } from '../initializers'
import { ResultList } from '../../shared'
import { VideoResolution } from '../../shared/models/videos/video-resolution.enum'
+import { CONFIG, database as db } from '../initializers'
import { AccountInstance } from '../models/account/account-interface'
+import { pseudoRandomBytesPromise } from './core-utils'
import { logger } from './logger'
function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
diff --git a/server/helpers/webfinger.ts b/server/helpers/webfinger.ts
index a5b4785feb..ab28889818 100644
--- a/server/helpers/webfinger.ts
+++ b/server/helpers/webfinger.ts
@@ -1,7 +1,6 @@
import * as WebFinger from 'webfinger.js'
import { WebFingerData } from '../../shared'
import { fetchRemoteAccount } from '../lib/activitypub/account'
-
import { isTestInstance } from './core-utils'
import { isActivityPubUrlValid } from './custom-validators'
diff --git a/server/middlewares/validators/videos.ts b/server/middlewares/validators/videos.ts
index 158b475e3f..df0eb7b96c 100644
--- a/server/middlewares/validators/videos.ts
+++ b/server/middlewares/validators/videos.ts
@@ -1,30 +1,30 @@
-import { body, param, query } from 'express-validator/check'
import * as express from 'express'
-
-import { database as db } from '../../initializers/database'
-import { checkErrors } from './utils'
-import { CONSTRAINTS_FIELDS, SEARCHABLE_COLUMNS } from '../../initializers'
+import { body, param, query } from 'express-validator/check'
+import { UserRight, VideoPrivacy } from '../../../shared'
+import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc'
import {
- logger,
- isVideoDurationValid,
- isVideoFile,
- isVideoNameValid,
+ checkVideoExists,
+ isVideoAbuseReasonValid,
isVideoCategoryValid,
- isVideoLicenceValid,
isVideoDescriptionValid,
+ isVideoDurationValid,
+ isVideoFile,
isVideoLanguageValid,
- isVideoTagsValid,
+ isVideoLicenceValid,
+ isVideoNameValid,
isVideoNSFWValid,
- isIdOrUUIDValid,
- isVideoAbuseReasonValid,
+ isVideoPrivacyValid,
isVideoRatingTypeValid,
- getDurationFromVideoFile,
- checkVideoExists,
- isIdValid,
- isVideoPrivacyValid
-} from '../../helpers'
-import { UserRight, VideoPrivacy } from '../../../shared'
+ isVideoTagsValid
+} from '../../helpers/custom-validators/videos'
+import { getDurationFromVideoFile } from '../../helpers/ffmpeg-utils'
+import { logger } from '../../helpers/logger'
+import { CONSTRAINTS_FIELDS, SEARCHABLE_COLUMNS } from '../../initializers'
+
+import { database as db } from '../../initializers/database'
+import { UserInstance } from '../../models/account/user-interface'
import { authenticate } from '../oauth'
+import { checkErrors } from './utils'
const videosAddValidator = [
body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage(
@@ -185,7 +185,7 @@ const videosRemoveValidator = [
checkErrors(req, res, () => {
checkVideoExists(req.params.id, res, () => {
// Check if the user who did the request is able to delete the video
- checkUserCanDeleteVideo(res.locals.oauth.token.User.id, res, () => {
+ checkUserCanDeleteVideo(res.locals.oauth.token.User, res, () => {
next()
})
})
@@ -246,7 +246,7 @@ export {
// ---------------------------------------------------------------------------
-function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) {
+function checkUserCanDeleteVideo (user: UserInstance, res: express.Response, callback: () => void) {
// Retrieve the user who did the request
if (res.locals.video.isOwned() === false) {
return res.status(403)
@@ -258,7 +258,6 @@ function checkUserCanDeleteVideo (userId: number, res: express.Response, callbac
// The user can delete it if s/he is an admin
// Or if s/he is the video's account
const account = res.locals.video.VideoChannel.Account
- const user = res.locals.oauth.token.User
if (user.hasRight(UserRight.REMOVE_ANY_VIDEO) === false && account.userId !== user.id) {
return res.status(403)
.json({ error: 'Cannot remove video of another user' })
diff --git a/server/middlewares/validators/webfinger.ts b/server/middlewares/validators/webfinger.ts
index 3e61a6cc35..7852c1c2b4 100644
--- a/server/middlewares/validators/webfinger.ts
+++ b/server/middlewares/validators/webfinger.ts
@@ -1,9 +1,9 @@
-import { query } from 'express-validator/check'
import * as express from 'express'
-
-import { checkErrors } from './utils'
-import { logger, isWebfingerResourceValid } from '../../helpers'
+import { query } from 'express-validator/check'
+import { isWebfingerResourceValid } from '../../helpers/custom-validators/webfinger'
import { database as db } from '../../initializers'
+import { checkErrors } from './utils'
+import { logger } from '../../helpers/logger'
const webfingerValidator = [
query('resource').custom(isWebfingerResourceValid).withMessage('Should have a valid webfinger resource'),