mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2024-12-14 08:26:22 +00:00
fix: type-is uploads filtering
also improved its parsing logic
This commit is contained in:
parent
0c0b2aa3d1
commit
ff58054158
@ -1208,13 +1208,14 @@ self.list = async (req, res) => {
|
|||||||
queries: {
|
queries: {
|
||||||
exclude: {}
|
exclude: {}
|
||||||
},
|
},
|
||||||
typeIs: [
|
typeIs: {
|
||||||
'image',
|
image: Constants.IMAGE_EXTS,
|
||||||
'video',
|
video: Constants.VIDEO_EXTS,
|
||||||
'audio'
|
audio: Constants.AUDIO_EXTS
|
||||||
],
|
},
|
||||||
flags: {}
|
flags: {}
|
||||||
}
|
}
|
||||||
|
const typeIsKeys = Object.keys(filterObj.typeIs)
|
||||||
|
|
||||||
const sortObj = {
|
const sortObj = {
|
||||||
// Cast columns to specific type if they are stored differently
|
// Cast columns to specific type if they are stored differently
|
||||||
@ -1536,34 +1537,51 @@ self.list = async (req, res) => {
|
|||||||
delete filterObj.queries.sort
|
delete filterObj.queries.sort
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse is keys
|
// Parse type-is keys
|
||||||
let isKeys = 0
|
|
||||||
let isLast
|
|
||||||
if (filterObj.queries.is || filterObj.queries.exclude.is) {
|
if (filterObj.queries.is || filterObj.queries.exclude.is) {
|
||||||
for (const type of filterObj.typeIs) {
|
const types = []
|
||||||
const inQuery = filterObj.queries.is && filterObj.queries.is.includes(type)
|
|
||||||
const inExclude = filterObj.queries.exclude.is && filterObj.queries.exclude.is.includes(type)
|
|
||||||
|
|
||||||
// Prioritize exclude keys when both types found
|
if (filterObj.queries.is) {
|
||||||
if (inQuery || inExclude) {
|
filterObj.queries.is = filterObj.queries.is.map(type => type.toLowerCase())
|
||||||
filterObj.flags[`is${type}`] = inExclude ? false : inQuery
|
types.push(...filterObj.queries.is)
|
||||||
if (isLast !== undefined && isLast !== filterObj.flags[`is${type}`]) {
|
}
|
||||||
throw new ClientError('Cannot mix inclusion and exclusion type-is keys.')
|
if (filterObj.queries.exclude.is) {
|
||||||
}
|
filterObj.queries.exclude.is = filterObj.queries.exclude.is.map(type => type.toLowerCase())
|
||||||
isKeys++
|
types.push(...filterObj.queries.exclude.is)
|
||||||
isLast = filterObj.flags[`is${type}`]
|
}
|
||||||
|
|
||||||
|
let isKeys = 0
|
||||||
|
let isLast
|
||||||
|
|
||||||
|
for (const type of types) {
|
||||||
|
if (!typeIsKeys.includes(type)) {
|
||||||
|
throw new ClientError(`Found invalid type-is key: ${type}.`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (filterObj.queries.is && filterObj.queries.is.includes(type)) {
|
||||||
|
filterObj.flags[`is${type}`] = true
|
||||||
|
} else {
|
||||||
|
filterObj.flags[`is${type}`] = false
|
||||||
|
}
|
||||||
|
|
||||||
|
isKeys++
|
||||||
|
|
||||||
|
if (isLast === undefined) {
|
||||||
|
isLast = filterObj.flags[`is${type}`]
|
||||||
|
} else if (filterObj.flags[`is${type}`] !== isLast) {
|
||||||
|
throw new ClientError('Cannot mix inclusion and exclusion type-is keys.')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Regular user threshold check
|
||||||
|
if (!ismoderator && isKeys > MAX_IS_KEYS) {
|
||||||
|
throw new ClientError(`Users are only allowed to use ${MAX_IS_KEYS} type-is key${MAX_IS_KEYS === 1 ? '' : 's'} at a time.`)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete keys to avoid unexpected behavior
|
// Delete keys to avoid unexpected behavior
|
||||||
delete filterObj.queries.is
|
delete filterObj.queries.is
|
||||||
delete filterObj.queries.exclude.is
|
delete filterObj.queries.exclude.is
|
||||||
}
|
}
|
||||||
|
|
||||||
// Regular user threshold check
|
|
||||||
if (!ismoderator && isKeys > MAX_IS_KEYS) {
|
|
||||||
throw new ClientError(`Users are only allowed to use ${MAX_IS_KEYS} type-is key${MAX_IS_KEYS === 1 ? '' : 's'} at a time.`)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function filter () {
|
function filter () {
|
||||||
@ -1670,7 +1688,7 @@ self.list = async (req, res) => {
|
|||||||
|
|
||||||
// Then, refine using type-is flags
|
// Then, refine using type-is flags
|
||||||
this.andWhere(function () {
|
this.andWhere(function () {
|
||||||
for (const type of filterObj.typeIs) {
|
for (const type of typeIsKeys) {
|
||||||
let func
|
let func
|
||||||
let operator
|
let operator
|
||||||
if (filterObj.flags[`is${type}`] === true) {
|
if (filterObj.flags[`is${type}`] === true) {
|
||||||
@ -1682,7 +1700,7 @@ self.list = async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (func) {
|
if (func) {
|
||||||
for (const pattern of utils[`${type}Exts`].map(ext => `%${ext}`)) {
|
for (const pattern of filterObj.typeIs[type].map(ext => `%${ext}`)) {
|
||||||
this[func]('name', operator, pattern)
|
this[func]('name', operator, pattern)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,8 +10,8 @@ const path = require('path')
|
|||||||
const sharp = require('sharp')
|
const sharp = require('sharp')
|
||||||
const paths = require('./pathsController')
|
const paths = require('./pathsController')
|
||||||
const perms = require('./permissionController')
|
const perms = require('./permissionController')
|
||||||
const Constants = require('./utils/Constants')
|
|
||||||
const ClientError = require('./utils/ClientError')
|
const ClientError = require('./utils/ClientError')
|
||||||
|
const Constants = require('./utils/Constants')
|
||||||
const ServerError = require('./utils/ServerError')
|
const ServerError = require('./utils/ServerError')
|
||||||
const SimpleDataStore = require('./utils/SimpleDataStore')
|
const SimpleDataStore = require('./utils/SimpleDataStore')
|
||||||
const StatsManager = require('./utils/StatsManager')
|
const StatsManager = require('./utils/StatsManager')
|
||||||
|
Loading…
Reference in New Issue
Block a user