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

feat: enable UppyFile.addFile to specify one or more plugins to specifically run on a per-file basis #5401

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
32 changes: 26 additions & 6 deletions packages/@uppy/core/src/Uppy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import type {
} from '@uppy/utils/lib/Translator'
import supportsUploadProgress from './supportsUploadProgress.ts'
import getFileName from './getFileName.ts'
import getFilePlugins from './getFilePlugins.ts'
import { justErrorsLogger, debugLogger } from './loggers.ts'
import {
Restricter,
Expand Down Expand Up @@ -387,10 +388,12 @@ export class Uppy<

#preProcessors: Set<Processor> = new Set()

#uploaders: Set<Processor> = new Set()
#uploaders: Map<Processor, string> = new Map()

#postProcessors: Set<Processor> = new Set()

#installingPlugin?: string

defaultLocale: Locale

locale!: Locale
Expand Down Expand Up @@ -676,7 +679,7 @@ export class Uppy<
}

addUploader(fn: Processor): void {
this.#uploaders.add(fn)
this.#uploaders.set(fn, this.#installingPlugin ?? '')
}

removeUploader(fn: Processor): boolean {
Expand Down Expand Up @@ -956,6 +959,7 @@ export class Uppy<

const fileType = getFileType(file)
const fileName = getFileName(fileType, file)
const filePlugins = getFilePlugins(file)
const fileExtension = getFileNameAndExtension(fileName).extension
const id = getSafeFileId(file, this.getID())

Expand All @@ -971,6 +975,7 @@ export class Uppy<
source: file.source || '',
id,
name: fileName,
plugins: filePlugins,
extension: fileExtension || '',
meta: {
...this.getState().meta,
Expand Down Expand Up @@ -1814,8 +1819,13 @@ export class Uppy<
} else {
this.#plugins[plugin.type] = [plugin]
}

this.#installingPlugin = pluginId

plugin.install()

this.#installingPlugin = undefined

this.emit('plugin-added', plugin)

return this
Expand Down Expand Up @@ -2099,7 +2109,7 @@ export class Uppy<

const steps = [
...this.#preProcessors,
...this.#uploaders,
...this.#uploaders.keys(),
...this.#postProcessors,
]
try {
Expand All @@ -2120,12 +2130,22 @@ export class Uppy<
})

const { fileIDs } = currentUpload

let uploaderFileIds = fileIDs
const uploaderPlugin = this.#uploaders.get(fn)
if (uploaderPlugin) {
const files = this.getFilesByIds(uploaderFileIds)
uploaderFileIds = files
.filter(
(file) =>
file.plugins?.includes(uploaderPlugin) ||
file.plugins?.length === 0,
)
.map((file) => file.id)
}
// TODO give this the `updatedUpload` object as its only parameter maybe?
// Otherwise when more metadata may be added to the upload this would keep getting more parameters
await fn(fileIDs, uploadID)
await fn(uploaderFileIds, uploadID)

// Update currentUpload value in case it was modified asynchronously.
currentUpload = getCurrentUpload()
}
} catch (err) {
Expand Down
5 changes: 5 additions & 0 deletions packages/@uppy/core/src/getFilePlugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default function getFilePlugins(fileDescriptor: {
plugins?: string[]
}): string[] {
return fileDescriptor.plugins || []
}
1 change: 1 addition & 0 deletions packages/@uppy/utils/src/UppyFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface UppyFile<M extends Meta, B extends Body> {
isGhost: boolean
meta: InternalMetadata & M
name?: string
plugins?: string[]
preview?: string
progress: FileProgress
missingRequiredMetaFields?: string[]
Expand Down
Loading