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: add instance ID to generated IDs #5080

Merged
merged 2 commits into from Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/@uppy/core/src/Uppy.ts
Expand Up @@ -911,7 +911,7 @@ export class Uppy<M extends Meta, B extends Body> {
const fileType = getFileType(file)
const fileName = getFileName(fileType, file)
const fileExtension = getFileNameAndExtension(fileName).extension
const id = getSafeFileId(file)
const id = getSafeFileId(file, this.getID())

const meta = file.meta || {}
meta.name = fileName
Expand Down
Expand Up @@ -465,7 +465,7 @@ export default class ProviderView<M extends Meta, B extends Body> extends View<
for (const newFile of files) {
const tagFile = this.getTagFile(newFile)

const id = getSafeFileId(tagFile)
const id = getSafeFileId(tagFile, this.plugin.uppy.getID())
// If the same folder is added again, we don't want to send
// X amount of duplicate file notifications, we want to say
// the folder was already added. This checks if all files are duplicate,
Expand Down
19 changes: 13 additions & 6 deletions packages/@uppy/utils/src/generateFileID.ts
Expand Up @@ -21,11 +21,12 @@ function encodeFilename(name: string): string {
*/
export default function generateFileID(
file: MinimalRequiredUppyFile<any, any>,
instanceId: string,
): string {
// It's tempting to do `[items].filter(Boolean).join('-')` here, but that
// is slower! simple string concatenation is fast

let id = 'uppy'
let id = instanceId || 'uppy'
if (typeof file.name === 'string') {
id += `-${encodeFilename(file.name.toLowerCase())}`
}
Expand Down Expand Up @@ -63,13 +64,19 @@ function hasFileStableId(file: MinimalRequiredUppyFile<any, any>): boolean {
return stableIdProviders.has(file.remote.provider as any)
}

export function getSafeFileId(file: MinimalRequiredUppyFile<any, any>): string {
export function getSafeFileId(
file: MinimalRequiredUppyFile<any, any>,
instanceId: string,
): string {
if (hasFileStableId(file)) return file.id!

const fileType = getFileType(file)

return generateFileID({
...file,
type: fileType,
})
return generateFileID(
{
...file,
type: fileType,
},
instanceId,
)
}