From bc6035325511d8948dab793b386be24c36e61954 Mon Sep 17 00:00:00 2001 From: Xen0Xys Date: Fri, 5 Jul 2024 00:07:53 +0200 Subject: [PATCH] :ambulance: Fix image download crash when image is used 2 times in an episode --- .../webtoon/webtoon-database.service.ts | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/modules/webtoon/webtoon/webtoon-database.service.ts b/src/modules/webtoon/webtoon/webtoon-database.service.ts index d7840ed..9cee6b5 100644 --- a/src/modules/webtoon/webtoon/webtoon-database.service.ts +++ b/src/modules/webtoon/webtoon/webtoon-database.service.ts @@ -80,7 +80,7 @@ export class WebtoonDatabaseService{ }); const imagesToSave: any[] = []; for(let i: number = 0; i < imagesSum.length; i++){ - if(!dbImages.find(dbImage => dbImage.sum === imagesSum[i])) + if(!dbImages.find(dbImage => dbImage.sum === imagesSum[i]) && !imagesToSave.find(image => image.sum === imagesSum[i])) imagesToSave.push({ sum: imagesSum[i], type_id: imageType.id, @@ -89,6 +89,7 @@ export class WebtoonDatabaseService{ await tx.images.createMany({ data: imagesToSave }); + dbImages = await tx.images.findMany({ where: { sum: { @@ -96,14 +97,17 @@ export class WebtoonDatabaseService{ } } }); - // Re-order dbImages to match imagesSum order - dbImages.sort((a: any, b: any) => { - return imagesSum.indexOf(a.sum) - imagesSum.indexOf(b.sum); - }); + const episodeImages = []; + for(let i: number = 0; i < imagesSum.length; i++){ + const dbImage = dbImages.find(dbImage => dbImage.sum === imagesSum[i]); + episodeImages.push({ + id: dbImage.id + }); + } // Create episodeImages await tx.episodeImages.createMany({ - data: dbImages.map((dbImage: any, index: number) => { + data: episodeImages.map((dbImage: any, index: number) => { return { number: index, episode_id: dbEpisode.id, @@ -112,9 +116,6 @@ export class WebtoonDatabaseService{ }) }); - - - // Change webtoon updated_at await tx.webtoons.update({ where: {