Skip to content

Commit

Permalink
🚑 Fix image download crash when image is used 2 times in an episode
Browse files Browse the repository at this point in the history
  • Loading branch information
Xen0Xys committed Jul 4, 2024
1 parent 51fe1e1 commit bc60353
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/modules/webtoon/webtoon/webtoon-database.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -89,21 +89,25 @@ export class WebtoonDatabaseService{
await tx.images.createMany({
data: imagesToSave
});

dbImages = await tx.images.findMany({
where: {
sum: {
in: imagesSum
}
}
});
// 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,
Expand All @@ -112,9 +116,6 @@ export class WebtoonDatabaseService{
})
});




// Change webtoon updated_at
await tx.webtoons.update({
where: {
Expand Down

0 comments on commit bc60353

Please sign in to comment.