Skip to content

Commit

Permalink
🎨 Improve logging and fix potential bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Xen0Xys committed Jun 7, 2024
1 parent acc6400 commit f7dd881
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 45 deletions.
7 changes: 5 additions & 2 deletions src/modules/webtoon/update/update.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Controller, Post, UseGuards} from "@nestjs/common";
import {Controller, Logger, Post, UseGuards} from "@nestjs/common";
import {UpdateService} from "./update.service";
import {ApiBearerAuth, ApiTags} from "@nestjs/swagger";
import {AdminGuard} from "../admin/guard/admin.guard";
Expand All @@ -8,13 +8,16 @@ import {AdminGuard} from "../admin/guard/admin.guard";
@ApiTags("Update")
@UseGuards(AdminGuard)
export class UpdateController{

private readonly logger = new Logger(UpdateController.name);

constructor(
private readonly updateService: UpdateService,
){}

@Post("webtoons/thumbnails")
@ApiBearerAuth()
async updateThumbnails(): Promise<void>{
await this.updateService.updateThumbnails();
this.updateService.updateThumbnails().then(() => this.logger.log("Thumbnails updated"));
}
}
100 changes: 57 additions & 43 deletions src/modules/webtoon/update/update.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Injectable} from "@nestjs/common";
import {Injectable, Logger} from "@nestjs/common";
import {PrismaService} from "../../misc/prisma.service";
import {WebtoonParserService} from "../webtoon/webtoon-parser.service";
import CachedWebtoonModel from "../webtoon/models/models/cached-webtoon.model";
Expand All @@ -10,6 +10,8 @@ import ImageTypes from "../webtoon/models/enums/image-types";
@Injectable()
export class UpdateService{

private readonly logger = new Logger(UpdateService.name);

constructor(
private readonly prismaService: PrismaService,
private readonly webtoonParser: WebtoonParserService,
Expand All @@ -20,53 +22,65 @@ export class UpdateService{
async updateThumbnails(): Promise<void>{
this.webtoonParser.clearCache();
await this.webtoonParser.loadCache();
const dbWebtoons: any[] = await this.prismaService.webtoons.findMany({
select: {
id: true,
title: true,
language: true,
thumbnail_id: true
}
});
const thumbnailsToDelete: number[] = dbWebtoons.map(webtoon => webtoon.thumbnail_id);
// Save new thumbnails
const dbThumbnailType = await this.prismaService.imageTypes.findFirst({
where: {
name: ImageTypes.WEBTOON_THUMBNAIL
},
select: {
id: true
}
});
for(const webtoon of dbWebtoons){
const cachedWebtoon: CachedWebtoonModel = this.webtoonParser.findWebtoon(webtoon.title, webtoon.language);
const thumbnail: Buffer = await this.miscService.convertThumbnail(cachedWebtoon.thumbnail);
const sum: string = this.webtoonDatabaseService.saveImage(thumbnail);
const dbThumbnail = await this.prismaService.images.create({
data: {
sum: sum,
type_id: dbThumbnailType.id
}
});
await this.prismaService.webtoons.update({
where: {
id: webtoon.id
},
data: {
thumbnail_id: dbThumbnail.id
await this.prismaService.$transaction(async(tx) => {
const dbWebtoons: any[] = await tx.webtoons.findMany({
select: {
id: true,
title: true,
language: true,
thumbnail_id: true
}
});
}
const thumbnailsToDelete: number[] = dbWebtoons.map(webtoon => webtoon.thumbnail_id);

// Delete old thumbnails
for(const thumbnailId of thumbnailsToDelete){
const deletedImage = await this.prismaService.images.delete({
// Save new thumbnails
const dbThumbnailType = await tx.imageTypes.findFirst({
where: {
id: thumbnailId
name: ImageTypes.WEBTOON_THUMBNAIL
},
select: {
id: true
}
});
this.webtoonDatabaseService.removeImage(deletedImage.sum);
}
for(const webtoon of dbWebtoons){
this.logger.debug(`Updating thumbnail for webtoon ${webtoon.title} (${webtoon.language})`);
const cachedWebtoon: CachedWebtoonModel = this.webtoonParser.findWebtoon(webtoon.title, webtoon.language);
const thumbnail: Buffer = await this.miscService.convertThumbnail(cachedWebtoon.thumbnail);
const sum: string = this.webtoonDatabaseService.saveImage(thumbnail);
// Check if thumbnail already exists
let dbThumbnail = await tx.images.findFirst({
where: {
sum: sum
}
});
if(dbThumbnail)
thumbnailsToDelete.splice(thumbnailsToDelete.indexOf(dbThumbnail.id), 1);
else
dbThumbnail = await tx.images.create({
data: {
sum: sum,
type_id: dbThumbnailType.id
}
});
// Update webtoon thumbnail
await tx.webtoons.update({
where: {
id: webtoon.id
},
data: {
thumbnail_id: dbThumbnail.id
}
});
}
// Delete old thumbnails
for(const thumbnailId of thumbnailsToDelete){
const deletedImage = await tx.images.delete({
where: {
id: thumbnailId
}
});
this.webtoonDatabaseService.removeImage(deletedImage.sum);
}
});
}

}

0 comments on commit f7dd881

Please sign in to comment.