From 32f7a4c402d55bc48efd7b8881d495eaf06f0d53 Mon Sep 17 00:00:00 2001 From: sjorge Date: Sat, 21 Dec 2024 22:37:07 +0100 Subject: [PATCH] fix: renamer should calculate sha1/md5/crc32 if messing --- src/lib/anime/renamer.ts | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/lib/anime/renamer.ts b/src/lib/anime/renamer.ts index c29138d..7e25c6d 100644 --- a/src/lib/anime/renamer.ts +++ b/src/lib/anime/renamer.ts @@ -4,6 +4,7 @@ import fs from "node:fs"; import path from "node:path"; import toml from "@iarna/toml"; +import Bun from "bun"; import { machineIdSync } from "node-machine-id"; import { AnidbUDPClient, AnidbError } from "anidb-udp-client"; @@ -168,7 +169,7 @@ export class AnimeRenamer { ); } - private async readpisodeDataCache( + private async readEpisodeDataCache( animeEpisodeHash: Ed2kHash, refresh: boolean = false, ): Promise { @@ -410,7 +411,7 @@ export class AnimeRenamer { } as AnimeRenamerEpisode; // read episodeData from cache - let episodeData = await this.readpisodeDataCache( + let episodeData = await this.readEpisodeDataCache( this.hashCache[animeEpisodeFile], refresh, ); @@ -421,6 +422,26 @@ export class AnimeRenamer { this.hashCache[animeEpisodeFile], ); + if ( + episodeData?.crc32 == "" || + episodeData?.sha1 == "" || + episodeData?.md5 == "" + ) { + const data = fs.readFileSync(animeEpisodeFile, { encoding: "binary" }); + + episodeData.crc32 = Bun.hash + .crc32(Buffer.from(data, "binary")) + .toString(16); + + episodeData.md5 = new Bun.CryptoHasher("md5") + .update(data, "binary") + .digest("hex"); + + episodeData.sha1 = new Bun.CryptoHasher("sha1") + .update(data, "binary") + .digest("hex"); + } + await this.writeEpisodeDataCache( this.hashCache[animeEpisodeFile], episodeData,