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: Ability to remove images that I know are being replaced #736

Open
1 task
wondergod007 opened this issue Mar 26, 2024 · 1 comment
Open
1 task

feat: Ability to remove images that I know are being replaced #736

wondergod007 opened this issue Mar 26, 2024 · 1 comment
Labels
✨ enhancement suggestion or feature request to improve uploadthing workaround available solution exists for the issue using existing functionality, although it may not be optimized for DX

Comments

@wondergod007
Copy link

Describe the feature you'd like to request

If I am uploading an image to replace a previously uploaded image, I'd like to pass in the previous image URL so that this can be deleted since I know it will be replaced by the new image being uploaded. This would ensure my file store usage remains low.

Describe the solution you'd like to see

On uploading a new image, I'd like to set a property of the image URL to delete when this image is successfully uploaded.

Additional information

No response

👨‍👧‍👦 Contributing

  • 🙋‍♂️ Yes, I'd be down to file a PR implementing this feature!
@wondergod007 wondergod007 added the ✨ enhancement suggestion or feature request to improve uploadthing label Mar 26, 2024
@markflorkowski
Copy link
Collaborator

markflorkowski commented Mar 26, 2024

We do have some plans to do things like this, for now though, you can pass that information to onUploadComplete() and call deleteFiles() there.

Something like this could work:

export const uploadRouter = {
  imageUploader: f({
    image: {
      maxFileSize: "4MB",
    },
  })
    .input(z.object({
      prevFile: z.string().optional(),
    }))
    .middleware(({ req, input }) => {
      // check for auth
      const user = auth(req);
      if (!user)  throw new Error("Unauthorized");

      // Return some metadata to be stored with the file
      return { userId: user.id, input };
    })
    .onUploadComplete(async ({ file, metadata: { prevFile }}) => {
      if(prevFile) await utapi.deleteFiles(prevFile);
    }),
} satisfies FileRouter;

@markflorkowski markflorkowski added the workaround available solution exists for the issue using existing functionality, although it may not be optimized for DX label Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ enhancement suggestion or feature request to improve uploadthing workaround available solution exists for the issue using existing functionality, although it may not be optimized for DX
Projects
None yet
Development

No branches or pull requests

2 participants