Skip to content

Commit 96df2c9

Browse files
committed
Calculate model hash only if there is no calculated one on disk already. Store on disk after calculation
1 parent af4eec9 commit 96df2c9

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

nodes.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,29 @@ def parse_name(ckpt_name):
2121
return filename
2222

2323

24-
def calculate_sha256(file_path):
25-
sha256_hash = hashlib.sha256()
24+
def get_sha256(file_path):
25+
26+
file_no_ext = os.path.splitext(file_path)[0]
27+
hash_file = file_no_ext + ".sha256"
28+
29+
if os.path.exists(hash_file):
30+
try:
31+
with open(hash_file, "r") as f:
32+
return f.read().strip()
33+
except OSError as e:
34+
print(f"comfy-image-saver: Error reading existing hash file: {e}")
2635

36+
sha256_hash = hashlib.sha256()
2737
with open(file_path, "rb") as f:
28-
# Read the file in chunks to avoid loading the entire file into memory
2938
for byte_block in iter(lambda: f.read(4096), b""):
3039
sha256_hash.update(byte_block)
3140

41+
try:
42+
with open(hash_file, "w") as f:
43+
f.write(sha256_hash.hexdigest())
44+
except OSError as e:
45+
print(f"comfy-image-saver: Error writing hash to {hash_file}: {e}")
46+
3247
return sha256_hash.hexdigest()
3348

3449

@@ -355,7 +370,7 @@ def save_files(
355370
)
356371

357372
ckpt_path = folder_paths.get_full_path("checkpoints", modelname)
358-
modelhash = calculate_sha256(ckpt_path)[:10]
373+
modelhash = get_sha256(ckpt_path)[:10]
359374
civitai_sampler_name = self.get_civitai_sampler_name(sampler_name, scheduler)
360375
comment = f"{handle_whitespace(positive)}\nNegative prompt: {handle_whitespace(negative)}\nSteps: {steps}, Sampler: {civitai_sampler_name}, CFG scale: {cfg}, Seed: {seed_value}, Size: {width}x{height}, Model hash: {modelhash}, Model: {basemodelname}, Version: ComfyUI"
361376
output_path = os.path.join(self.output_dir, path)

0 commit comments

Comments
 (0)