-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from Open-Webtoon-Reader/feature/better-thumbnail
Merge better thumbnail to Main
- Loading branch information
Showing
12 changed files
with
215 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {Controller, Logger, Post, UseGuards} from "@nestjs/common"; | ||
import {UpdateService} from "./update.service"; | ||
import {ApiBearerAuth, ApiResponse, ApiTags} from "@nestjs/swagger"; | ||
import {AdminGuard} from "../admin/guard/admin.guard"; | ||
import {HttpStatusCode} from "axios"; | ||
|
||
|
||
@Controller("update") | ||
@ApiTags("Update") | ||
@UseGuards(AdminGuard) | ||
export class UpdateController{ | ||
|
||
private readonly logger = new Logger(UpdateController.name); | ||
|
||
constructor( | ||
private readonly updateService: UpdateService, | ||
){} | ||
|
||
@Post("webtoons/thumbnails") | ||
@ApiBearerAuth() | ||
@ApiResponse({status: HttpStatusCode.Created, description: "Thumbnails updated"}) | ||
async updateThumbnails(): Promise<void>{ | ||
this.updateService.updateThumbnails().then(() => this.logger.log("Thumbnails updated")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
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"; | ||
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{ | ||
|
||
private readonly logger = new Logger(UpdateService.name); | ||
|
||
constructor( | ||
private readonly prismaService: PrismaService, | ||
private readonly webtoonParser: WebtoonParserService, | ||
private readonly miscService: MiscService, | ||
private readonly webtoonDatabaseService: WebtoonDatabaseService, | ||
){} | ||
|
||
async updateThumbnails(): Promise<void>{ | ||
this.webtoonParser.clearCache(); | ||
await this.webtoonParser.loadCache(); | ||
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); | ||
|
||
// Save new thumbnails | ||
const dbThumbnailType = await tx.imageTypes.findFirst({ | ||
where: { | ||
name: ImageTypes.WEBTOON_THUMBNAIL | ||
}, | ||
select: { | ||
id: true | ||
} | ||
}); | ||
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.convertWebtoonThumbnail(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); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters