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'
}
}
-const readFilePromise = promisify2<string, string, string>(readFile)
const readFileBufferPromise = promisify1<string, Buffer>(readFile)
const unlinkPromise = promisify1WithVoid<string>(unlink)
const renamePromise = promisify2WithVoid<string, string>(rename)
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)
promisify1,
readdirPromise,
- readFilePromise,
readFileBufferPromise,
unlinkPromise,
renamePromise,
writeFilePromise,
mkdirpPromise,
pseudoRandomBytesPromise,
- accessPromise,
createPrivateKey,
getPublicKey,
bcryptComparePromise,
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)) {
export {
checkVideoAccountExists,
- isAccountNameWithHostValid,
isAccountNameValid
}
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)) {
export {
isVideoChannelDescriptionValid,
isVideoChannelNameValid,
- isVideoChannelUUIDValid,
checkVideoChannelExists,
isVideoChannelUrlValid
}
-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)
}
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))
}
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)
}
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
}
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
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)
+ })
}
// ---------------------------------------------------------------------------
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
}
-// 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'
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) => {
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
-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...')
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) {
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'
-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(
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()
})
})
// ---------------------------------------------------------------------------
-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)
// 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' })
-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'),