Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make default hash lib configurable without code changes via CLI argument #3947

Merged
2 changes: 2 additions & 0 deletions comfy/cli_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class LatentPreviewMethod(enum.Enum):
vram_group.add_argument("--novram", action="store_true", help="When lowvram isn't enough.")
vram_group.add_argument("--cpu", action="store_true", help="To use the CPU for everything (slow).")

parser.add_argument("--duplicate-check-hash-function", type=str, choices=['md5', 'sha1', 'sha256', 'sha512'], default='sha256', help="Allows you to choose the hash function to use for duplicate filename / contents comparison. Default is sha256.")


parser.add_argument("--disable-smart-memory", action="store_true", help="Force ComfyUI to agressively offload to regular ram instead of keeping models in vram when it can.")
parser.add_argument("--deterministic", action="store_true", help="Make pytorch use slower deterministic algorithms when it can. Note that this might not make images deterministic in all cases.")
Expand Down
10 changes: 8 additions & 2 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,16 @@ def get_dir_by_type(dir_type):
return type_dir, dir_type

def compare_image_hash(filepath, image):
# This eval operation is *safe* because we can only accept certain strings passed in via args.
# Because that can only be one of 'md5', 'sha1', 'sha256', or 'sha512' which we already define
# in comfy.cli_args as an argument passable to the args and only from a set of options, we can
# safely use an eval here, as the eval data is "safe" already.
hasher = eval(f"hashlib.{args.duplicate_check_hash_function}")

# function to compare hashes of two images to see if it already exists, fix to #3465
if os.path.exists(filepath):
a = hashlib.sha256()
b = hashlib.sha256()
a = hasher()
b = hasher()
with open(filepath, "rb") as f:
a.update(f.read())
b.update(image.file.read())
Expand Down