Skip to content

Commit

Permalink
@uppy/core: add instance ID to generated IDs (#5080)
Browse files Browse the repository at this point in the history
  • Loading branch information
Murderlon committed Apr 29, 2024
1 parent 9c11f31 commit d2be2d8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/@uppy/core/src/Uppy.ts
Expand Up @@ -915,7 +915,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,
)
}

0 comments on commit d2be2d8

Please sign in to comment.