Skip to content

Commit

Permalink
fix(product, dashboard): manual cleanup of uploaded images (#10254)
Browse files Browse the repository at this point in the history
* manual cleanup

* add changeset

* remove thumbnail on deletion
  • Loading branch information
kasperkristensen authored Nov 25, 2024
1 parent df83690 commit c28d0db
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/shy-gorillas-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@medusajs/dashboard": patch
"@medusajs/product": patch
---

fix(product,dashboard): Avoid duplicating images
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ export const EditProductMediaForm = ({ product }: ProductMediaViewProps) => {

await mutateAsync(
{
images: withUpdatedUrls.map((file) => ({ url: file.url })),
thumbnail: thumbnail,
images: withUpdatedUrls.map((file) => ({ url: file.url, id: file.id })),
thumbnail: thumbnail || null,
},
{
onSuccess: () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export class Migration20241122120331 extends Migration {
where pi.image_id = i.id;
`);

// Delete orphaned images
this.addSql(`
delete from "image"
where product_id is null;
`);

this.addSql('alter table if exists "image" alter column "product_id" set not null;');
this.addSql('alter table if exists "image" add constraint "image_product_id_foreign" foreign key ("product_id") references "product" ("id") on update cascade on delete cascade;');
this.addSql('drop table if exists "product_images" cascade;');
Expand Down
10 changes: 10 additions & 0 deletions packages/modules/product/src/services/product-module-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1690,6 +1690,16 @@ export default class ProductModuleService
sharedContext
)
upsertedProduct.images = productImages

await this.productImageService_.delete(
{
product_id: upsertedProduct.id,
id: {
$nin: productImages.map(({ id }) => id),
},
},
sharedContext
)
} else {
await this.productImageService_.delete(
{ product_id: upsertedProduct.id },
Expand Down

0 comments on commit c28d0db

Please sign in to comment.