-
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.
✨ Add webtoon thumbnail update route from old to new images
- Loading branch information
Showing
7 changed files
with
114 additions
and
10 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
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(); | ||
} | ||
} |
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,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 | ||
} | ||
} | ||
}); | ||
} | ||
|
||
} |
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