Skip to content

Commit

Permalink
✨ Add webtoon thumbnail update route from old to new images
Browse files Browse the repository at this point in the history
  • Loading branch information
Xen0Xys committed Jun 7, 2024
1 parent a94af82 commit 181f39a
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {ThrottlerGuard, ThrottlerModule} from "@nestjs/throttler";
import {APP_GUARD} from "@nestjs/core";
import {MigrationModule} from "./modules/webtoon/migration/migration.module";
import {ScheduleModule} from "@nestjs/schedule";
import {UpdateModule} from "./modules/webtoon/update/update.module";

@Module({
imports: [
Expand All @@ -20,6 +21,7 @@ import {ScheduleModule} from "@nestjs/schedule";
WebtoonModule,
AdminModule,
MigrationModule,
UpdateModule,
],
controllers: [],
providers: [
Expand Down
8 changes: 8 additions & 0 deletions src/modules/misc/misc.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ export class MiscService{
return response.data as Buffer;
}

async convertThumbnail(url: string){
const webpImage: Buffer = await this.downloadImage(url);
return await sharp(webpImage).resize(240, 240, {
fit: "cover",
position: "center"
}).toBuffer();
}

parseWebtoonStars(stars: string): WebtoonStarModel{
// TODO: Fix dutch space and comma issue
stars = stars.replace(" ", " ");
Expand Down
20 changes: 20 additions & 0 deletions src/modules/webtoon/update/update.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {Controller, Post, UseGuards} from "@nestjs/common";
import {UpdateService} from "./update.service";
import {ApiBearerAuth, ApiTags} from "@nestjs/swagger";
import {AdminGuard} from "../admin/guard/admin.guard";


@Controller("update")
@ApiTags("Update")
@UseGuards(AdminGuard)
export class UpdateController{
constructor(
private readonly updateService: UpdateService,
){}

@Post("webtoons/thumbnails")
@ApiBearerAuth()
async updateThumbnails(): Promise<void>{
await this.updateService.updateThumbnails();
}
}
13 changes: 13 additions & 0 deletions src/modules/webtoon/update/update.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {Module} from "@nestjs/common";
import {UpdateService} from "./update.service";
import {UpdateController} from "./update.controller";
import {MiscModule} from "../../misc/misc.module";
import {WebtoonModule} from "../webtoon/webtoon.module";


@Module({
providers: [UpdateService],
controllers: [UpdateController],
imports: [MiscModule, WebtoonModule]
})
export class UpdateModule{}
69 changes: 69 additions & 0 deletions src/modules/webtoon/update/update.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {Injectable} 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";
import {MiscService} from "../../misc/misc.service";
import {WebtoonDatabaseService} from "../webtoon/webtoon-database.service";
import ImageTypes from "../webtoon/models/enums/image-types";


@Injectable()
export class UpdateService{

constructor(
private readonly prismaService: PrismaService,
private readonly webtoonParser: WebtoonParserService,
private readonly miscService: MiscService,
private readonly webtoonDatabaseService: WebtoonDatabaseService,
){}

async updateThumbnails(): Promise<void>{
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
}
});
}

// Delete old thumbnails
await this.prismaService.images.deleteMany({
where: {
id: {
in: thumbnailsToDelete
}
}
});
}

}
10 changes: 1 addition & 9 deletions src/modules/webtoon/webtoon/webtoon-downloader.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class WebtoonDownloaderService{

async downloadWebtoon(webtoon: WebtoonModel): Promise<WebtoonDataModel>{
const downloadPromises: Promise<Buffer>[] = [];
downloadPromises.push(this.convertThumbnail(webtoon.thumbnail));
downloadPromises.push(this.miscService.convertThumbnail(webtoon.thumbnail));
downloadPromises.push(this.miscService.downloadImage(webtoon.banner.background));
downloadPromises.push(this.miscService.downloadImage(webtoon.banner.top));
downloadPromises.push(this.miscService.downloadImage(webtoon.banner.mobile));
Expand All @@ -66,12 +66,4 @@ export class WebtoonDownloaderService{
mobileBanner: mobile,
} as WebtoonDataModel;
}

async convertThumbnail(url: string){
const webpImage = await this.miscService.downloadImage(url);
return await sharp(webpImage).resize(240, 240, {
fit: "cover",
position: "center"
}).toBuffer();
}
}
2 changes: 1 addition & 1 deletion src/modules/webtoon/webtoon/webtoon.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ import {MiscModule} from "../../misc/misc.module";
imports: [MiscModule],
controllers: [WebtoonController],
providers: [WebtoonParserService, WebtoonDownloaderService, WebtoonDatabaseService, DownloadManagerService],
exports: [DownloadManagerService, WebtoonDatabaseService],
exports: [DownloadManagerService, WebtoonDatabaseService, WebtoonParserService],
})
export class WebtoonModule{}

0 comments on commit 181f39a

Please sign in to comment.