Refactor middleware helpers
This commit is contained in:
parent
a8b666e9f1
commit
3e753302d8
34 changed files with 311 additions and 241 deletions
|
@ -1,7 +1,4 @@
|
|||
import * as Bluebird from 'bluebird'
|
||||
import { Response } from 'express'
|
||||
import 'express-validator'
|
||||
import { AccountModel } from '../../models/account/account'
|
||||
import { isUserDescriptionValid, isUserUsernameValid } from './users'
|
||||
import { exists } from './misc'
|
||||
|
||||
|
@ -17,47 +14,10 @@ function isAccountDescriptionValid (value: string) {
|
|||
return isUserDescriptionValid(value)
|
||||
}
|
||||
|
||||
function doesAccountIdExist (id: number, res: Response, sendNotFound = true) {
|
||||
const promise = AccountModel.load(id)
|
||||
|
||||
return doesAccountExist(promise, res, sendNotFound)
|
||||
}
|
||||
|
||||
function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
|
||||
const promise = AccountModel.loadLocalByName(name)
|
||||
|
||||
return doesAccountExist(promise, res, sendNotFound)
|
||||
}
|
||||
|
||||
function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
|
||||
return doesAccountExist(AccountModel.loadByNameWithHost(nameWithDomain), res, sendNotFound)
|
||||
}
|
||||
|
||||
async function doesAccountExist (p: Bluebird<AccountModel>, res: Response, sendNotFound: boolean) {
|
||||
const account = await p
|
||||
|
||||
if (!account) {
|
||||
if (sendNotFound === true) {
|
||||
res.status(404)
|
||||
.send({ error: 'Account not found' })
|
||||
.end()
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
res.locals.account = account
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
isAccountIdValid,
|
||||
doesAccountIdExist,
|
||||
doesLocalAccountNameExist,
|
||||
isAccountDescriptionValid,
|
||||
doesAccountNameWithHostExist,
|
||||
isAccountNameValid
|
||||
}
|
||||
|
|
|
@ -18,25 +18,9 @@ function isVideoAbuseStateValid (value: string) {
|
|||
return exists(value) && VIDEO_ABUSE_STATES[ value ] !== undefined
|
||||
}
|
||||
|
||||
async function doesVideoAbuseExist (abuseId: number, videoId: number, res: Response) {
|
||||
const videoAbuse = await VideoAbuseModel.loadByIdAndVideoId(abuseId, videoId)
|
||||
|
||||
if (videoAbuse === null) {
|
||||
res.status(404)
|
||||
.json({ error: 'Video abuse not found' })
|
||||
.end()
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
res.locals.videoAbuse = videoAbuse
|
||||
return true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
doesVideoAbuseExist,
|
||||
isVideoAbuseStateValid,
|
||||
isVideoAbuseReasonValid,
|
||||
isVideoAbuseModerationCommentValid
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
import { Response } from 'express'
|
||||
import * as validator from 'validator'
|
||||
import { exists } from './misc'
|
||||
import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
|
||||
import { VideoBlacklistModel } from '../../models/video/video-blacklist'
|
||||
import { VideoBlacklistType } from '../../../shared/models/videos'
|
||||
|
||||
const VIDEO_BLACKLIST_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_BLACKLIST
|
||||
|
@ -11,21 +9,6 @@ function isVideoBlacklistReasonValid (value: string) {
|
|||
return value === null || validator.isLength(value, VIDEO_BLACKLIST_CONSTRAINTS_FIELDS.REASON)
|
||||
}
|
||||
|
||||
async function doesVideoBlacklistExist (videoId: number, res: Response) {
|
||||
const videoBlacklist = await VideoBlacklistModel.loadByVideoId(videoId)
|
||||
|
||||
if (videoBlacklist === null) {
|
||||
res.status(404)
|
||||
.json({ error: 'Blacklisted video not found' })
|
||||
.end()
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
res.locals.videoBlacklist = videoBlacklist
|
||||
return true
|
||||
}
|
||||
|
||||
function isVideoBlacklistTypeValid (value: any) {
|
||||
return exists(value) && validator.isInt('' + value) && VideoBlacklistType[value] !== undefined
|
||||
}
|
||||
|
@ -34,6 +17,5 @@ function isVideoBlacklistTypeValid (value: any) {
|
|||
|
||||
export {
|
||||
isVideoBlacklistReasonValid,
|
||||
isVideoBlacklistTypeValid,
|
||||
doesVideoBlacklistExist
|
||||
isVideoBlacklistTypeValid
|
||||
}
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_LANGUAGES } from '../../initializers/constants'
|
||||
import { exists, isFileValid } from './misc'
|
||||
import { Response } from 'express'
|
||||
import { VideoModel } from '../../models/video/video'
|
||||
import { VideoCaptionModel } from '../../models/video/video-caption'
|
||||
|
||||
function isVideoCaptionLanguageValid (value: any) {
|
||||
return exists(value) && VIDEO_LANGUAGES[ value ] !== undefined
|
||||
|
@ -16,25 +13,9 @@ function isVideoCaptionFile (files: { [ fieldname: string ]: Express.Multer.File
|
|||
return isFileValid(files, videoCaptionTypesRegex, field, CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max)
|
||||
}
|
||||
|
||||
async function doesVideoCaptionExist (video: VideoModel, language: string, res: Response) {
|
||||
const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(video.id, language)
|
||||
|
||||
if (!videoCaption) {
|
||||
res.status(404)
|
||||
.json({ error: 'Video caption not found' })
|
||||
.end()
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
res.locals.videoCaption = videoCaption
|
||||
return true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
isVideoCaptionFile,
|
||||
isVideoCaptionLanguageValid,
|
||||
doesVideoCaptionExist
|
||||
isVideoCaptionLanguageValid
|
||||
}
|
||||
|
|
|
@ -20,33 +20,12 @@ function isVideoChannelSupportValid (value: string) {
|
|||
return value === null || (exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.SUPPORT))
|
||||
}
|
||||
|
||||
async function doesLocalVideoChannelNameExist (name: string, res: express.Response) {
|
||||
const videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
|
||||
|
||||
return processVideoChannelExist(videoChannel, res)
|
||||
}
|
||||
|
||||
async function doesVideoChannelIdExist (id: number, res: express.Response) {
|
||||
const videoChannel = await VideoChannelModel.loadAndPopulateAccount(+id)
|
||||
|
||||
return processVideoChannelExist(videoChannel, res)
|
||||
}
|
||||
|
||||
async function doesVideoChannelNameWithHostExist (nameWithDomain: string, res: express.Response) {
|
||||
const videoChannel = await VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithDomain)
|
||||
|
||||
return processVideoChannelExist(videoChannel, res)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
doesVideoChannelNameWithHostExist,
|
||||
doesLocalVideoChannelNameExist,
|
||||
isVideoChannelDescriptionValid,
|
||||
isVideoChannelNameValid,
|
||||
isVideoChannelSupportValid,
|
||||
doesVideoChannelIdExist
|
||||
}
|
||||
|
||||
function processVideoChannelExist (videoChannel: VideoChannelModel, res: express.Response) {
|
||||
|
|
|
@ -26,27 +26,9 @@ function isVideoPlaylistTypeValid (value: any) {
|
|||
return exists(value) && VIDEO_PLAYLIST_TYPES[ value ] !== undefined
|
||||
}
|
||||
|
||||
async function doesVideoPlaylistExist (id: number | string, res: express.Response, fetchType: 'summary' | 'all' = 'summary') {
|
||||
const videoPlaylist = fetchType === 'summary'
|
||||
? await VideoPlaylistModel.loadWithAccountAndChannelSummary(id, undefined)
|
||||
: await VideoPlaylistModel.loadWithAccountAndChannel(id, undefined)
|
||||
|
||||
if (!videoPlaylist) {
|
||||
res.status(404)
|
||||
.json({ error: 'Video playlist not found' })
|
||||
.end()
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
res.locals.videoPlaylist = videoPlaylist
|
||||
return true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
doesVideoPlaylistExist,
|
||||
isVideoPlaylistNameValid,
|
||||
isVideoPlaylistDescriptionValid,
|
||||
isVideoPlaylistPrivacyValid,
|
||||
|
|
|
@ -15,10 +15,8 @@ import {
|
|||
} from '../../initializers/constants'
|
||||
import { VideoModel } from '../../models/video/video'
|
||||
import { exists, isArray, isDateValid, isFileValid } from './misc'
|
||||
import { VideoChannelModel } from '../../models/video/video-channel'
|
||||
import { UserModel } from '../../models/account/user'
|
||||
import * as magnetUtil from 'magnet-uri'
|
||||
import { fetchVideo, VideoFetchType } from '../video'
|
||||
|
||||
const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
|
||||
|
||||
|
@ -143,79 +141,10 @@ function isVideoMagnetUriValid (value: string) {
|
|||
return parsed && isVideoFileInfoHashValid(parsed.infoHash)
|
||||
}
|
||||
|
||||
function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: UserRight, res: Response) {
|
||||
// Retrieve the user who did the request
|
||||
if (video.isOwned() === false) {
|
||||
res.status(403)
|
||||
.json({ error: 'Cannot manage a video of another server.' })
|
||||
.end()
|
||||
return false
|
||||
}
|
||||
|
||||
// Check if the user can delete the video
|
||||
// The user can delete it if he has the right
|
||||
// Or if s/he is the video's account
|
||||
const account = video.VideoChannel.Account
|
||||
if (user.hasRight(right) === false && account.userId !== user.id) {
|
||||
res.status(403)
|
||||
.json({ error: 'Cannot manage a video of another user.' })
|
||||
.end()
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') {
|
||||
const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined
|
||||
|
||||
const video = await fetchVideo(id, fetchType, userId)
|
||||
|
||||
if (video === null) {
|
||||
res.status(404)
|
||||
.json({ error: 'Video not found' })
|
||||
.end()
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
if (fetchType !== 'none') res.locals.video = video
|
||||
return true
|
||||
}
|
||||
|
||||
async function doesVideoChannelOfAccountExist (channelId: number, user: UserModel, res: Response) {
|
||||
if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
|
||||
const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
|
||||
if (videoChannel === null) {
|
||||
res.status(400)
|
||||
.json({ error: 'Unknown video `video channel` on this instance.' })
|
||||
.end()
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
res.locals.videoChannel = videoChannel
|
||||
return true
|
||||
}
|
||||
|
||||
const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id)
|
||||
if (videoChannel === null) {
|
||||
res.status(400)
|
||||
.json({ error: 'Unknown video `video channel` for this account.' })
|
||||
.end()
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
res.locals.videoChannel = videoChannel
|
||||
return true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
isVideoCategoryValid,
|
||||
checkUserCanManageVideo,
|
||||
isVideoLicenceValid,
|
||||
isVideoLanguageValid,
|
||||
isVideoTruncatedDescriptionValid,
|
||||
|
@ -237,9 +166,7 @@ export {
|
|||
isVideoPrivacyValid,
|
||||
isVideoFileResolutionValid,
|
||||
isVideoFileSizeValid,
|
||||
doesVideoExist,
|
||||
isVideoImage,
|
||||
doesVideoChannelOfAccountExist,
|
||||
isVideoSupportValid,
|
||||
isVideoFilterValid
|
||||
}
|
||||
|
|
46
server/helpers/middlewares/accounts.ts
Normal file
46
server/helpers/middlewares/accounts.ts
Normal file
|
@ -0,0 +1,46 @@
|
|||
import { Response } from 'express'
|
||||
import { AccountModel } from '../../models/account/account'
|
||||
import * as Bluebird from 'bluebird'
|
||||
|
||||
function doesAccountIdExist (id: number, res: Response, sendNotFound = true) {
|
||||
const promise = AccountModel.load(id)
|
||||
|
||||
return doesAccountExist(promise, res, sendNotFound)
|
||||
}
|
||||
|
||||
function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
|
||||
const promise = AccountModel.loadLocalByName(name)
|
||||
|
||||
return doesAccountExist(promise, res, sendNotFound)
|
||||
}
|
||||
|
||||
function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
|
||||
return doesAccountExist(AccountModel.loadByNameWithHost(nameWithDomain), res, sendNotFound)
|
||||
}
|
||||
|
||||
async function doesAccountExist (p: Bluebird<AccountModel>, res: Response, sendNotFound: boolean) {
|
||||
const account = await p
|
||||
|
||||
if (!account) {
|
||||
if (sendNotFound === true) {
|
||||
res.status(404)
|
||||
.send({ error: 'Account not found' })
|
||||
.end()
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
res.locals.account = account
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
doesAccountIdExist,
|
||||
doesLocalAccountNameExist,
|
||||
doesAccountNameWithHostExist,
|
||||
doesAccountExist
|
||||
}
|
7
server/helpers/middlewares/index.ts
Normal file
7
server/helpers/middlewares/index.ts
Normal file
|
@ -0,0 +1,7 @@
|
|||
export * from './accounts'
|
||||
export * from './video-abuses'
|
||||
export * from './video-blacklists'
|
||||
export * from './video-captions'
|
||||
export * from './video-channels'
|
||||
export * from './video-playlists'
|
||||
export * from './videos'
|
41
server/helpers/middlewares/video-abuses.ts
Normal file
41
server/helpers/middlewares/video-abuses.ts
Normal file
|
@ -0,0 +1,41 @@
|
|||
import * as express from 'express'
|
||||
import { VideoChannelModel } from '../../models/video/video-channel'
|
||||
|
||||
async function doesLocalVideoChannelNameExist (name: string, res: express.Response) {
|
||||
const videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
|
||||
|
||||
return processVideoChannelExist(videoChannel, res)
|
||||
}
|
||||
|
||||
async function doesVideoChannelIdExist (id: number, res: express.Response) {
|
||||
const videoChannel = await VideoChannelModel.loadAndPopulateAccount(+id)
|
||||
|
||||
return processVideoChannelExist(videoChannel, res)
|
||||
}
|
||||
|
||||
async function doesVideoChannelNameWithHostExist (nameWithDomain: string, res: express.Response) {
|
||||
const videoChannel = await VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithDomain)
|
||||
|
||||
return processVideoChannelExist(videoChannel, res)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
doesLocalVideoChannelNameExist,
|
||||
doesVideoChannelIdExist,
|
||||
doesVideoChannelNameWithHostExist
|
||||
}
|
||||
|
||||
function processVideoChannelExist (videoChannel: VideoChannelModel, res: express.Response) {
|
||||
if (!videoChannel) {
|
||||
res.status(404)
|
||||
.json({ error: 'Video channel not found' })
|
||||
.end()
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
res.locals.videoChannel = videoChannel
|
||||
return true
|
||||
}
|
23
server/helpers/middlewares/video-blacklists.ts
Normal file
23
server/helpers/middlewares/video-blacklists.ts
Normal file
|
@ -0,0 +1,23 @@
|
|||
import { Response } from 'express'
|
||||
import { VideoBlacklistModel } from '../../models/video/video-blacklist'
|
||||
|
||||
async function doesVideoBlacklistExist (videoId: number, res: Response) {
|
||||
const videoBlacklist = await VideoBlacklistModel.loadByVideoId(videoId)
|
||||
|
||||
if (videoBlacklist === null) {
|
||||
res.status(404)
|
||||
.json({ error: 'Blacklisted video not found' })
|
||||
.end()
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
res.locals.videoBlacklist = videoBlacklist
|
||||
return true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
doesVideoBlacklistExist
|
||||
}
|
24
server/helpers/middlewares/video-captions.ts
Normal file
24
server/helpers/middlewares/video-captions.ts
Normal file
|
@ -0,0 +1,24 @@
|
|||
import { VideoModel } from '../../models/video/video'
|
||||
import { Response } from 'express'
|
||||
import { VideoCaptionModel } from '../../models/video/video-caption'
|
||||
|
||||
async function doesVideoCaptionExist (video: VideoModel, language: string, res: Response) {
|
||||
const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(video.id, language)
|
||||
|
||||
if (!videoCaption) {
|
||||
res.status(404)
|
||||
.json({ error: 'Video caption not found' })
|
||||
.end()
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
res.locals.videoCaption = videoCaption
|
||||
return true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
doesVideoCaptionExist
|
||||
}
|
23
server/helpers/middlewares/video-channels.ts
Normal file
23
server/helpers/middlewares/video-channels.ts
Normal file
|
@ -0,0 +1,23 @@
|
|||
import { Response } from 'express'
|
||||
import { VideoAbuseModel } from '../../models/video/video-abuse'
|
||||
|
||||
async function doesVideoAbuseExist (abuseId: number, videoId: number, res: Response) {
|
||||
const videoAbuse = await VideoAbuseModel.loadByIdAndVideoId(abuseId, videoId)
|
||||
|
||||
if (videoAbuse === null) {
|
||||
res.status(404)
|
||||
.json({ error: 'Video abuse not found' })
|
||||
.end()
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
res.locals.videoAbuse = videoAbuse
|
||||
return true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
doesVideoAbuseExist
|
||||
}
|
25
server/helpers/middlewares/video-playlists.ts
Normal file
25
server/helpers/middlewares/video-playlists.ts
Normal file
|
@ -0,0 +1,25 @@
|
|||
import * as express from 'express'
|
||||
import { VideoPlaylistModel } from '../../models/video/video-playlist'
|
||||
|
||||
async function doesVideoPlaylistExist (id: number | string, res: express.Response, fetchType: 'summary' | 'all' = 'summary') {
|
||||
const videoPlaylist = fetchType === 'summary'
|
||||
? await VideoPlaylistModel.loadWithAccountAndChannelSummary(id, undefined)
|
||||
: await VideoPlaylistModel.loadWithAccountAndChannel(id, undefined)
|
||||
|
||||
if (!videoPlaylist) {
|
||||
res.status(404)
|
||||
.json({ error: 'Video playlist not found' })
|
||||
.end()
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
res.locals.videoPlaylist = videoPlaylist
|
||||
return true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
doesVideoPlaylistExist
|
||||
}
|
82
server/helpers/middlewares/videos.ts
Normal file
82
server/helpers/middlewares/videos.ts
Normal file
|
@ -0,0 +1,82 @@
|
|||
import { Response } from 'express'
|
||||
import { fetchVideo, VideoFetchType } from '../video'
|
||||
import { UserModel } from '../../models/account/user'
|
||||
import { UserRight } from '../../../shared/models/users'
|
||||
import { VideoChannelModel } from '../../models/video/video-channel'
|
||||
import { VideoModel } from '../../models/video/video'
|
||||
|
||||
async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') {
|
||||
const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined
|
||||
|
||||
const video = await fetchVideo(id, fetchType, userId)
|
||||
|
||||
if (video === null) {
|
||||
res.status(404)
|
||||
.json({ error: 'Video not found' })
|
||||
.end()
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
if (fetchType !== 'none') res.locals.video = video
|
||||
return true
|
||||
}
|
||||
|
||||
async function doesVideoChannelOfAccountExist (channelId: number, user: UserModel, res: Response) {
|
||||
if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
|
||||
const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
|
||||
if (videoChannel === null) {
|
||||
res.status(400)
|
||||
.json({ error: 'Unknown video `video channel` on this instance.' })
|
||||
.end()
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
res.locals.videoChannel = videoChannel
|
||||
return true
|
||||
}
|
||||
|
||||
const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id)
|
||||
if (videoChannel === null) {
|
||||
res.status(400)
|
||||
.json({ error: 'Unknown video `video channel` for this account.' })
|
||||
.end()
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
res.locals.videoChannel = videoChannel
|
||||
return true
|
||||
}
|
||||
|
||||
function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: UserRight, res: Response) {
|
||||
// Retrieve the user who did the request
|
||||
if (video.isOwned() === false) {
|
||||
res.status(403)
|
||||
.json({ error: 'Cannot manage a video of another server.' })
|
||||
.end()
|
||||
return false
|
||||
}
|
||||
|
||||
// Check if the user can delete the video
|
||||
// The user can delete it if he has the right
|
||||
// Or if s/he is the video's account
|
||||
const account = video.VideoChannel.Account
|
||||
if (user.hasRight(right) === false && account.userId !== user.id) {
|
||||
res.status(403)
|
||||
.json({ error: 'Cannot manage a video of another user.' })
|
||||
.end()
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
doesVideoChannelOfAccountExist,
|
||||
doesVideoExist,
|
||||
checkUserCanManageVideo
|
||||
}
|
|
@ -12,12 +12,15 @@ import { AccountModel } from '../models/account/account'
|
|||
import { VideoChannelModel } from '../models/video/video-channel'
|
||||
import * as Bluebird from 'bluebird'
|
||||
import { CONFIG } from '../initializers/config'
|
||||
import { logger } from '../helpers/logger'
|
||||
|
||||
export class ClientHtml {
|
||||
|
||||
private static htmlCache: { [ path: string ]: string } = {}
|
||||
|
||||
static invalidCache () {
|
||||
logger.info('Cleaning HTML cache.')
|
||||
|
||||
ClientHtml.htmlCache = {}
|
||||
}
|
||||
|
||||
|
@ -146,7 +149,7 @@ export class ClientHtml {
|
|||
|
||||
private static async addAsyncPluginCSS (htmlStringPage: string) {
|
||||
const globalCSSContent = await readFile(PLUGIN_GLOBAL_CSS_PATH)
|
||||
if (!globalCSSContent) return htmlStringPage
|
||||
if (globalCSSContent.byteLength === 0) return htmlStringPage
|
||||
|
||||
const fileHash = sha256(globalCSSContent)
|
||||
const linkTag = `<link rel="stylesheet" href="/plugins/global.css?hash=${fileHash}" />`
|
||||
|
|
|
@ -317,6 +317,8 @@ export class PluginManager implements ServerHook {
|
|||
// ###################### CSS ######################
|
||||
|
||||
private resetCSSGlobalFile () {
|
||||
ClientHtml.invalidCache()
|
||||
|
||||
return outputFile(PLUGIN_GLOBAL_CSS_PATH, '')
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
import * as express from 'express'
|
||||
import { param } from 'express-validator/check'
|
||||
import { isAccountNameValid, doesAccountNameWithHostExist, doesLocalAccountNameExist } from '../../helpers/custom-validators/accounts'
|
||||
import { isAccountNameValid } from '../../helpers/custom-validators/accounts'
|
||||
import { logger } from '../../helpers/logger'
|
||||
import { areValidationErrors } from './utils'
|
||||
import { doesAccountNameWithHostExist, doesLocalAccountNameExist } from '../../helpers/middlewares'
|
||||
|
||||
const localAccountValidator = [
|
||||
param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'),
|
||||
|
|
|
@ -2,13 +2,13 @@ import { body, param } from 'express-validator/check'
|
|||
import * as express from 'express'
|
||||
import { logger } from '../../helpers/logger'
|
||||
import { areValidationErrors } from './utils'
|
||||
import { doesAccountNameWithHostExist } from '../../helpers/custom-validators/accounts'
|
||||
import { AccountBlocklistModel } from '../../models/account/account-blocklist'
|
||||
import { isHostValid } from '../../helpers/custom-validators/servers'
|
||||
import { ServerBlocklistModel } from '../../models/server/server-blocklist'
|
||||
import { ServerModel } from '../../models/server/server'
|
||||
import { getServerActor } from '../../helpers/utils'
|
||||
import { WEBSERVER } from '../../initializers/constants'
|
||||
import { doesAccountNameWithHostExist } from '../../helpers/middlewares'
|
||||
|
||||
const blockAccountValidator = [
|
||||
body('accountName').exists().withMessage('Should have an account name with host'),
|
||||
|
|
|
@ -1,12 +1,16 @@
|
|||
import * as express from 'express'
|
||||
import { param, query } from 'express-validator/check'
|
||||
import { doesAccountIdExist, doesAccountNameWithHostExist } from '../../helpers/custom-validators/accounts'
|
||||
import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc'
|
||||
import { logger } from '../../helpers/logger'
|
||||
import { areValidationErrors } from './utils'
|
||||
import { isValidRSSFeed } from '../../helpers/custom-validators/feeds'
|
||||
import { doesVideoChannelIdExist, doesVideoChannelNameWithHostExist } from '../../helpers/custom-validators/video-channels'
|
||||
import { doesVideoExist } from '../../helpers/custom-validators/videos'
|
||||
import { doesVideoExist } from '../../helpers/middlewares/videos'
|
||||
import {
|
||||
doesAccountIdExist,
|
||||
doesAccountNameWithHostExist,
|
||||
doesVideoChannelIdExist,
|
||||
doesVideoChannelNameWithHostExist
|
||||
} from '../../helpers/middlewares'
|
||||
|
||||
const videoFeedsValidator = [
|
||||
param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
|
||||
|
|
|
@ -3,10 +3,10 @@ import { query } from 'express-validator/check'
|
|||
import { join } from 'path'
|
||||
import { isTestInstance } from '../../helpers/core-utils'
|
||||
import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
|
||||
import { doesVideoExist } from '../../helpers/custom-validators/videos'
|
||||
import { logger } from '../../helpers/logger'
|
||||
import { areValidationErrors } from './utils'
|
||||
import { WEBSERVER } from '../../initializers/constants'
|
||||
import { doesVideoExist } from '../../helpers/middlewares'
|
||||
|
||||
const urlShouldStartWith = WEBSERVER.SCHEME + '://' + join(WEBSERVER.HOST, 'videos', 'watch') + '/'
|
||||
const videoWatchRegex = new RegExp('([^/]+)$')
|
||||
|
|
|
@ -2,12 +2,12 @@ import * as express from 'express'
|
|||
import 'express-validator'
|
||||
import { body, param } from 'express-validator/check'
|
||||
import { exists, isBooleanValid, isIdOrUUIDValid, toIntOrNull } from '../../helpers/custom-validators/misc'
|
||||
import { doesVideoExist } from '../../helpers/custom-validators/videos'
|
||||
import { logger } from '../../helpers/logger'
|
||||
import { areValidationErrors } from './utils'
|
||||
import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
|
||||
import { isHostValid } from '../../helpers/custom-validators/servers'
|
||||
import { ServerModel } from '../../models/server/server'
|
||||
import { doesVideoExist } from '../../helpers/middlewares'
|
||||
|
||||
const videoFileRedundancyGetValidator = [
|
||||
param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
|
||||
|
|
|
@ -13,12 +13,12 @@ import {
|
|||
isUserNSFWPolicyValid,
|
||||
isUserPasswordValid,
|
||||
isUserRoleValid,
|
||||
isUserUsernameValid, isUserVideoLanguages,
|
||||
isUserUsernameValid,
|
||||
isUserVideoLanguages,
|
||||
isUserVideoQuotaDailyValid,
|
||||
isUserVideoQuotaValid,
|
||||
isUserVideosHistoryEnabledValid
|
||||
} from '../../helpers/custom-validators/users'
|
||||
import { doesVideoExist } from '../../helpers/custom-validators/videos'
|
||||
import { logger } from '../../helpers/logger'
|
||||
import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/signup'
|
||||
import { Redis } from '../../lib/redis'
|
||||
|
@ -30,6 +30,7 @@ import { isVideoChannelNameValid } from '../../helpers/custom-validators/video-c
|
|||
import { UserRegister } from '../../../shared/models/users/user-register.model'
|
||||
import { isThemeNameValid } from '../../helpers/custom-validators/plugins'
|
||||
import { isThemeRegistered } from '../../lib/plugins/theme-utils'
|
||||
import { doesVideoExist } from '../../helpers/middlewares'
|
||||
|
||||
const usersAddValidator = [
|
||||
body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'),
|
||||
|
|
|
@ -2,15 +2,14 @@ import * as express from 'express'
|
|||
import 'express-validator'
|
||||
import { body, param } from 'express-validator/check'
|
||||
import { isIdOrUUIDValid, isIdValid } from '../../../helpers/custom-validators/misc'
|
||||
import { doesVideoExist } from '../../../helpers/custom-validators/videos'
|
||||
import { logger } from '../../../helpers/logger'
|
||||
import { areValidationErrors } from '../utils'
|
||||
import {
|
||||
doesVideoAbuseExist,
|
||||
isVideoAbuseModerationCommentValid,
|
||||
isVideoAbuseReasonValid,
|
||||
isVideoAbuseStateValid
|
||||
} from '../../../helpers/custom-validators/video-abuses'
|
||||
import { doesVideoAbuseExist, doesVideoExist } from '../../../helpers/middlewares'
|
||||
|
||||
const videoAbuseReportValidator = [
|
||||
param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
|
||||
|
|
|
@ -1,14 +1,10 @@
|
|||
import * as express from 'express'
|
||||
import { body, param, query } from 'express-validator/check'
|
||||
import { isBooleanValid, isIdOrUUIDValid } from '../../../helpers/custom-validators/misc'
|
||||
import { doesVideoExist } from '../../../helpers/custom-validators/videos'
|
||||
import { logger } from '../../../helpers/logger'
|
||||
import { areValidationErrors } from '../utils'
|
||||
import {
|
||||
doesVideoBlacklistExist,
|
||||
isVideoBlacklistReasonValid,
|
||||
isVideoBlacklistTypeValid
|
||||
} from '../../../helpers/custom-validators/video-blacklist'
|
||||
import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../../helpers/custom-validators/video-blacklist'
|
||||
import { doesVideoBlacklistExist, doesVideoExist } from '../../../helpers/middlewares'
|
||||
|
||||
const videosBlacklistRemoveValidator = [
|
||||
param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
import * as express from 'express'
|
||||
import { areValidationErrors } from '../utils'
|
||||
import { checkUserCanManageVideo, doesVideoExist } from '../../../helpers/custom-validators/videos'
|
||||
import { isIdOrUUIDValid } from '../../../helpers/custom-validators/misc'
|
||||
import { body, param } from 'express-validator/check'
|
||||
import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
|
||||
import { UserRight } from '../../../../shared'
|
||||
import { logger } from '../../../helpers/logger'
|
||||
import { doesVideoCaptionExist, isVideoCaptionFile, isVideoCaptionLanguageValid } from '../../../helpers/custom-validators/video-captions'
|
||||
import { isVideoCaptionFile, isVideoCaptionLanguageValid } from '../../../helpers/custom-validators/video-captions'
|
||||
import { cleanUpReqFiles } from '../../../helpers/express-utils'
|
||||
import { checkUserCanManageVideo, doesVideoCaptionExist, doesVideoExist } from '../../../helpers/middlewares'
|
||||
|
||||
const addVideoCaptionValidator = [
|
||||
param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
|
||||
|
|
|
@ -2,8 +2,6 @@ import * as express from 'express'
|
|||
import { body, param } from 'express-validator/check'
|
||||
import { UserRight } from '../../../../shared'
|
||||
import {
|
||||
doesLocalVideoChannelNameExist,
|
||||
doesVideoChannelNameWithHostExist,
|
||||
isVideoChannelDescriptionValid,
|
||||
isVideoChannelNameValid,
|
||||
isVideoChannelSupportValid
|
||||
|
@ -15,6 +13,7 @@ import { areValidationErrors } from '../utils'
|
|||
import { isActorPreferredUsernameValid } from '../../../helpers/custom-validators/activitypub/actor'
|
||||
import { ActorModel } from '../../../models/activitypub/actor'
|
||||
import { isBooleanValid } from '../../../helpers/custom-validators/misc'
|
||||
import { doesLocalVideoChannelNameExist, doesVideoChannelNameWithHostExist } from '../../../helpers/middlewares'
|
||||
|
||||
const videoChannelsAddValidator = [
|
||||
body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
|
||||
|
|
|
@ -3,7 +3,6 @@ import { body, param } from 'express-validator/check'
|
|||
import { UserRight } from '../../../../shared'
|
||||
import { isIdOrUUIDValid, isIdValid } from '../../../helpers/custom-validators/misc'
|
||||
import { isValidVideoCommentText } from '../../../helpers/custom-validators/video-comments'
|
||||
import { doesVideoExist } from '../../../helpers/custom-validators/videos'
|
||||
import { logger } from '../../../helpers/logger'
|
||||
import { UserModel } from '../../../models/account/user'
|
||||
import { VideoModel } from '../../../models/video/video'
|
||||
|
@ -11,6 +10,7 @@ import { VideoCommentModel } from '../../../models/video/video-comment'
|
|||
import { areValidationErrors } from '../utils'
|
||||
import { Hooks } from '../../../lib/plugins/hooks'
|
||||
import { isLocalVideoThreadAccepted, isLocalVideoCommentReplyAccepted, AcceptResult } from '../../../lib/moderation'
|
||||
import { doesVideoExist } from '../../../helpers/middlewares'
|
||||
|
||||
const listVideoCommentThreadsValidator = [
|
||||
param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
|
||||
|
|
|
@ -6,9 +6,10 @@ import { areValidationErrors } from '../utils'
|
|||
import { getCommonVideoEditAttributes } from './videos'
|
||||
import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../../helpers/custom-validators/video-imports'
|
||||
import { cleanUpReqFiles } from '../../../helpers/express-utils'
|
||||
import { doesVideoChannelOfAccountExist, isVideoMagnetUriValid, isVideoNameValid } from '../../../helpers/custom-validators/videos'
|
||||
import { isVideoMagnetUriValid, isVideoNameValid } from '../../../helpers/custom-validators/videos'
|
||||
import { CONFIG } from '../../../initializers/config'
|
||||
import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
|
||||
import { doesVideoChannelOfAccountExist } from '../../../helpers/middlewares'
|
||||
|
||||
const videoImportAddValidator = getCommonVideoEditAttributes().concat([
|
||||
body('channelId')
|
||||
|
|
|
@ -4,11 +4,10 @@ import { UserRight, VideoPlaylistCreate, VideoPlaylistUpdate } from '../../../..
|
|||
import { logger } from '../../../helpers/logger'
|
||||
import { UserModel } from '../../../models/account/user'
|
||||
import { areValidationErrors } from '../utils'
|
||||
import { doesVideoExist, isVideoImage } from '../../../helpers/custom-validators/videos'
|
||||
import { isVideoImage } from '../../../helpers/custom-validators/videos'
|
||||
import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
|
||||
import { isArrayOf, isIdOrUUIDValid, isIdValid, isUUIDValid, toIntArray, toValueOrNull } from '../../../helpers/custom-validators/misc'
|
||||
import {
|
||||
doesVideoPlaylistExist,
|
||||
isVideoPlaylistDescriptionValid,
|
||||
isVideoPlaylistNameValid,
|
||||
isVideoPlaylistPrivacyValid,
|
||||
|
@ -17,11 +16,11 @@ import {
|
|||
} from '../../../helpers/custom-validators/video-playlists'
|
||||
import { VideoPlaylistModel } from '../../../models/video/video-playlist'
|
||||
import { cleanUpReqFiles } from '../../../helpers/express-utils'
|
||||
import { doesVideoChannelIdExist } from '../../../helpers/custom-validators/video-channels'
|
||||
import { VideoPlaylistElementModel } from '../../../models/video/video-playlist-element'
|
||||
import { authenticatePromiseIfNeeded } from '../../oauth'
|
||||
import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
|
||||
import { VideoPlaylistType } from '../../../../shared/models/videos/playlist/video-playlist-type.model'
|
||||
import { doesVideoChannelIdExist, doesVideoExist, doesVideoPlaylistExist } from '../../../helpers/middlewares'
|
||||
|
||||
const videoPlaylistsAddValidator = getCommonPlaylistEditAttributes().concat([
|
||||
body('displayName')
|
||||
|
|
|
@ -3,12 +3,13 @@ import 'express-validator'
|
|||
import { body, param, query } from 'express-validator/check'
|
||||
import { isIdOrUUIDValid } from '../../../helpers/custom-validators/misc'
|
||||
import { isRatingValid } from '../../../helpers/custom-validators/video-rates'
|
||||
import { doesVideoExist, isVideoRatingTypeValid } from '../../../helpers/custom-validators/videos'
|
||||
import { isVideoRatingTypeValid } from '../../../helpers/custom-validators/videos'
|
||||
import { logger } from '../../../helpers/logger'
|
||||
import { areValidationErrors } from '../utils'
|
||||
import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
|
||||
import { VideoRateType } from '../../../../shared/models/videos'
|
||||
import { isAccountNameValid } from '../../../helpers/custom-validators/accounts'
|
||||
import { doesVideoExist } from '../../../helpers/middlewares'
|
||||
|
||||
const videoUpdateRateValidator = [
|
||||
param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
|
||||
|
|
|
@ -2,10 +2,10 @@ import * as express from 'express'
|
|||
import 'express-validator'
|
||||
import { param } from 'express-validator/check'
|
||||
import { isIdOrUUIDValid, isIdValid } from '../../../helpers/custom-validators/misc'
|
||||
import { doesVideoExist } from '../../../helpers/custom-validators/videos'
|
||||
import { logger } from '../../../helpers/logger'
|
||||
import { VideoShareModel } from '../../../models/video/video-share'
|
||||
import { areValidationErrors } from '../utils'
|
||||
import { doesVideoExist } from '../../../helpers/middlewares'
|
||||
|
||||
const videosShareValidator = [
|
||||
param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import { body, param } from 'express-validator/check'
|
||||
import * as express from 'express'
|
||||
import { isIdOrUUIDValid } from '../../../helpers/custom-validators/misc'
|
||||
import { doesVideoExist } from '../../../helpers/custom-validators/videos'
|
||||
import { areValidationErrors } from '../utils'
|
||||
import { logger } from '../../../helpers/logger'
|
||||
import { doesVideoExist } from '../../../helpers/middlewares'
|
||||
|
||||
const videoWatchingValidator = [
|
||||
param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
|
||||
|
|
|
@ -13,9 +13,6 @@ import {
|
|||
toValueOrNull
|
||||
} from '../../../helpers/custom-validators/misc'
|
||||
import {
|
||||
checkUserCanManageVideo,
|
||||
doesVideoChannelOfAccountExist,
|
||||
doesVideoExist,
|
||||
isScheduleVideoUpdatePrivacyValid,
|
||||
isVideoCategoryValid,
|
||||
isVideoDescriptionValid,
|
||||
|
@ -33,7 +30,7 @@ import {
|
|||
import { getDurationFromVideoFile } from '../../../helpers/ffmpeg-utils'
|
||||
import { logger } from '../../../helpers/logger'
|
||||
import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
|
||||
import { authenticate, authenticatePromiseIfNeeded } from '../../oauth'
|
||||
import { authenticatePromiseIfNeeded } from '../../oauth'
|
||||
import { areValidationErrors } from '../utils'
|
||||
import { cleanUpReqFiles } from '../../../helpers/express-utils'
|
||||
import { VideoModel } from '../../../models/video/video'
|
||||
|
@ -46,6 +43,7 @@ import { getServerActor } from '../../../helpers/utils'
|
|||
import { CONFIG } from '../../../initializers/config'
|
||||
import { isLocalVideoAccepted } from '../../../lib/moderation'
|
||||
import { Hooks } from '../../../lib/plugins/hooks'
|
||||
import { checkUserCanManageVideo, doesVideoChannelOfAccountExist, doesVideoExist } from '../../../helpers/middlewares'
|
||||
|
||||
const videosAddValidator = getCommonVideoEditAttributes().concat([
|
||||
body('videofile')
|
||||
|
|
Loading…
Reference in a new issue