Skip to content

Commit c5a48b1

Browse files
authored
Make default hash lib configurable without code changes via CLI argument (comfyanonymous#3947)
* cli_args: Add --duplicate-check-hash-function. * server.py: compare_image_hash configurable hash function Uses an argument added in cli_args to specify the type of hashing to default to for duplicate hash checking. Uses an `eval()` to identify the specific hashlib class to utilize, but ultimately safely operates because we have specific options and only those options/choices in the arg parser. So we don't have any unsafe input there. * Add hasher() to node_helpers * hashlib selection moved to node_helpers * default-hashing-function instead of dupe checking hasher This makes a default-hashing-function option instead of previous selected option. * Use args.default_hashing_function * Use safer handling for node_helpers.hasher() Uses a safer handling method than `eval` to evaluate default hashing function. * Stray parentheses are evil. * Indentation fix. Somehow when I hit save I didn't notice I missed a space to make indentation work proper. Oops!
1 parent f229879 commit c5a48b1

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

comfy/cli_args.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ class LatentPreviewMethod(enum.Enum):
112112
vram_group.add_argument("--novram", action="store_true", help="When lowvram isn't enough.")
113113
vram_group.add_argument("--cpu", action="store_true", help="To use the CPU for everything (slow).")
114114

115+
parser.add_argument("--default-hashing-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.")
115116

116117
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.")
117118
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.")

node_helpers.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import hashlib
2+
3+
from comfy.cli_args import args
4+
15
from PIL import ImageFile, UnidentifiedImageError
26

37
def conditioning_set_values(conditioning, values={}):
@@ -22,3 +26,12 @@ def pillow(fn, arg):
2226
if prev_value is not None:
2327
ImageFile.LOAD_TRUNCATED_IMAGES = prev_value
2428
return x
29+
30+
def hasher():
31+
hashfuncs = {
32+
"md5": hashlib.md5,
33+
"sha1": hashlib.sha1,
34+
"sha256": hashlib.sha256,
35+
"sha512": hashlib.sha512
36+
}
37+
return hashfuncs[args.default_hashing_function]

server.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from comfy.cli_args import args
2626
import comfy.utils
2727
import comfy.model_management
28+
import node_helpers
2829
from app.frontend_management import FrontendManager
2930
from app.user_manager import UserManager
3031

@@ -161,10 +162,12 @@ def get_dir_by_type(dir_type):
161162
return type_dir, dir_type
162163

163164
def compare_image_hash(filepath, image):
165+
hasher = node_helpers.hasher()
166+
164167
# function to compare hashes of two images to see if it already exists, fix to #3465
165168
if os.path.exists(filepath):
166-
a = hashlib.sha256()
167-
b = hashlib.sha256()
169+
a = hasher()
170+
b = hasher()
168171
with open(filepath, "rb") as f:
169172
a.update(f.read())
170173
b.update(image.file.read())

0 commit comments

Comments
 (0)