pingdotgg / uploadthing

File uploads for modern web devs
https://uploadthing.com
MIT License
4.23k stars 314 forks source link

[bug]: Unable to upload image with same custom ID even after removing image with that custom ID from storage #948

Open jaqb8 opened 2 months ago

jaqb8 commented 2 months ago

Provide environment information

System:
    OS: macOS 14.6.1
    CPU: (8) arm64 Apple M1 Pro
    Memory: 655.58 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.20.3 - ~/.nvm/versions/node/v18.20.3/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.7.0 - ~/.nvm/versions/node/v18.20.3/bin/npm
    pnpm: 8.6.11 - /usr/local/bin/pnpm
    bun: 1.0.7 - ~/.bun/bin/bun
  Browsers:
    Brave Browser: 119.1.60.110
    Chrome: 128.0.6613.138
    Edge: 128.0.2739.67
    Safari: 17.6
  npmPackages:
    @uploadthing/react: ^7.0.1 => 7.0.1
    typescript: ^5.5.3 => 5.5.4
    uploadthing: ^7.0.1 => 7.0.1

Describe the bug

I set customId of a file to one of my entities id (cuid) in middleware.

  imageUploader: f({ image: { maxFileSize: "1MB" } })
    .input(
      z.object({
        landlordId: z.string(),
      }),
    )
    .middleware(async ({ req, input, files }) => {
      const fileOverrides = files.map((file) => ({
        ...file,
        customId: input.landlordId,
      }));
      return { landlordId: input.landlordId, [UTFiles]: fileOverrides };
    })

This only works once - even if I remove the image with the customId from storage and then try to upload a new file with the same customId, the following error appears.

image

Link to reproduction

n/a

To reproduce

  1. Create a file with customId in storage.
  2. Remove the file from storage.
  3. Try uploading a new file with the previously used customId.

Additional information

No response

๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ฆ Contributing

Code of Conduct

markflorkowski commented 2 months ago

Can confirm this is an issue. We currently have a unique constraint on appId + customId, and deleted file records (just db row, not files) are retained for a period after deletion, which can cause conflict.

Perhaps we can add status into the unique constraint to avoid this cc @juliusmarminge

t3dotgg commented 1 month ago

@markflorkowski adding status to the unique constraint sounds like a race condition disaster waiting to happen. Ideally we'd either ignore deleted assets or change their ids once deleted to prevent the clash