fix: correct hash schema and stream hashing

This commit is contained in:
William Valentin
2026-02-04 19:39:17 -08:00
parent a133afad06
commit 1952fbaf30
2 changed files with 8 additions and 10 deletions

View File

@@ -1,11 +1,12 @@
import { createHash } from "node:crypto"; import { createHash } from "node:crypto";
import { promises as fs } from "fs"; import { createReadStream } from "node:fs";
export async function computeFileSha256(filePath: string): Promise<string> { export async function computeFileSha256(filePath: string): Promise<string> {
const hash = createHash("sha256"); const hash = createHash("sha256");
const content = await fs.readFile(filePath); const stream = createReadStream(filePath);
return await new Promise((resolve, reject) => {
hash.update(content); stream.on("data", (chunk) => hash.update(chunk));
stream.on("error", reject);
return hash.digest("hex"); stream.on("end", () => resolve(hash.digest("hex")));
});
} }

View File

@@ -1,12 +1,9 @@
CREATE TABLE IF NOT EXISTS asset_hashes ( CREATE TABLE IF NOT EXISTS asset_hashes (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(), asset_id uuid PRIMARY KEY REFERENCES assets(id) ON DELETE CASCADE,
asset_id uuid NOT NULL REFERENCES assets(id) ON DELETE CASCADE,
bucket text NOT NULL, bucket text NOT NULL,
sha256 text NOT NULL, sha256 text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now() created_at timestamptz NOT NULL DEFAULT now()
); );
CREATE INDEX IF NOT EXISTS asset_hashes_asset_id_idx ON asset_hashes(asset_id);
CREATE UNIQUE INDEX IF NOT EXISTS asset_hashes_bucket_sha256_idx CREATE UNIQUE INDEX IF NOT EXISTS asset_hashes_bucket_sha256_idx
ON asset_hashes(bucket, sha256) WHERE sha256 IS NOT NULL; ON asset_hashes(bucket, sha256) WHERE sha256 IS NOT NULL;