Skip to content

Commit

Permalink
fix: renamer should calculate sha1/md5/crc32 if messing
Browse files Browse the repository at this point in the history
  • Loading branch information
sjorge committed Dec 21, 2024
1 parent c8613b9 commit 32f7a4c
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/lib/anime/renamer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -168,7 +169,7 @@ export class AnimeRenamer {
);
}

private async readpisodeDataCache(
private async readEpisodeDataCache(
animeEpisodeHash: Ed2kHash,
refresh: boolean = false,
): Promise<AnimeStringFormatData | undefined> {
Expand Down Expand Up @@ -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,
);
Expand All @@ -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,
Expand Down

0 comments on commit 32f7a4c

Please sign in to comment.