Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@uppy/core: improve performance of validating & uploading files #4402

Merged
merged 21 commits into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 51 additions & 22 deletions packages/@uppy/core/src/Restricter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,21 @@ const defaultOptions = {
}

class RestrictionError extends Error {
constructor (message, { isUserFacing = true } = {}) {
super(message)
this.isUserFacing = isUserFacing
}

isRestriction = true
}

class FileRestrictionError extends RestrictionError {
constructor (message, { file, ...opts }) {
super(message, opts)
this.file = file
}
}

class Restricter {
constructor (getOpts, i18n) {
this.i18n = i18n
Expand All @@ -30,16 +42,38 @@ class Restricter {
}
}

validate (file, files) {
const { maxFileSize, minFileSize, maxTotalFileSize, maxNumberOfFiles, allowedFileTypes } = this.getOpts().restrictions
// Because these operations are slow, we cannot run them for every file (if we are adding multiple files)
validateAggregateRestrictions (existingFiles, addingFiles) {
const { maxTotalFileSize, maxNumberOfFiles } = this.getOpts().restrictions

if (maxNumberOfFiles) {
const nonGhostFiles = files.filter(f => !f.isGhost)
if (nonGhostFiles.length + 1 > maxNumberOfFiles) {
const nonGhostFiles = existingFiles.filter(f => !f.isGhost)
if (nonGhostFiles.length + addingFiles.length > maxNumberOfFiles) {
throw new RestrictionError(`${this.i18n('youCanOnlyUploadX', { smart_count: maxNumberOfFiles })}`)
}
}

if (maxTotalFileSize) {
let totalFilesSize = existingFiles.reduce((total, f) => (total + f.size), 0)

for (const addingFile of addingFiles) {
if (addingFile.size != null) { // We can't check maxTotalFileSize if the size is unknown.
totalFilesSize += addingFile.size

if (totalFilesSize > maxTotalFileSize) {
throw new RestrictionError(this.i18n('exceedsSize', {
size: prettierBytes(maxTotalFileSize),
file: addingFile.name,
}))
}
}
}
}
}

validateSingleFile (file) {
const { maxFileSize, minFileSize, allowedFileTypes } = this.getOpts().restrictions

if (allowedFileTypes) {
const isCorrectFileType = allowedFileTypes.some((type) => {
// check if this is a mime-type
Expand All @@ -57,38 +91,33 @@ class Restricter {

if (!isCorrectFileType) {
const allowedFileTypesString = allowedFileTypes.join(', ')
throw new RestrictionError(this.i18n('youCanOnlyUploadFileTypes', { types: allowedFileTypesString }))
}
}

// We can't check maxTotalFileSize if the size is unknown.
if (maxTotalFileSize && file.size != null) {
const totalFilesSize = files.reduce((total, f) => (total + f.size), file.size)

if (totalFilesSize > maxTotalFileSize) {
throw new RestrictionError(this.i18n('exceedsSize', {
size: prettierBytes(maxTotalFileSize),
file: file.name,
}))
throw new FileRestrictionError(this.i18n('youCanOnlyUploadFileTypes', { types: allowedFileTypesString }), { file })
}
}

// We can't check maxFileSize if the size is unknown.
if (maxFileSize && file.size != null && file.size > maxFileSize) {
throw new RestrictionError(this.i18n('exceedsSize', {
throw new FileRestrictionError(this.i18n('exceedsSize', {
size: prettierBytes(maxFileSize),
file: file.name,
}))
}), { file })
}

// We can't check minFileSize if the size is unknown.
if (minFileSize && file.size != null && file.size < minFileSize) {
throw new RestrictionError(this.i18n('inferiorSize', {
throw new FileRestrictionError(this.i18n('inferiorSize', {
size: prettierBytes(minFileSize),
}))
}), { file })
}
}

validate (existingFiles, addingFiles) {
addingFiles.forEach((addingFile) => {
this.validateSingleFile(addingFile)
})
this.validateAggregateRestrictions(existingFiles, addingFiles)
}

validateMinNumberOfFiles (files) {
const { minNumberOfFiles } = this.getOpts().restrictions
if (Object.keys(files).length < minNumberOfFiles) {
Expand All @@ -111,4 +140,4 @@ class Restricter {
}
}

export { Restricter, defaultOptions, RestrictionError }
export { Restricter, defaultOptions, RestrictionError, FileRestrictionError }
Loading