mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-03-24 06:32:12 -03:00
Refactor example images handling by introducing migration logic, updating metadata structure, and enhancing image loading in the UI
This commit is contained in:
@@ -128,76 +128,6 @@ class ExampleImagesFileManager:
|
||||
'is_video': file_ext in SUPPORTED_MEDIA_EXTENSIONS['videos']
|
||||
})
|
||||
|
||||
# Check if files use 1-based indexing (look for patterns like "image_1.jpg")
|
||||
has_one_based = any(re.match(r'image_1\.\w+$', f['name']) for f in files)
|
||||
has_zero_based = any(re.match(r'image_0\.\w+$', f['name']) for f in files)
|
||||
|
||||
# If there are 1-based indices and no 0-based indices, rename files
|
||||
if has_one_based and not has_zero_based:
|
||||
logger.info(f"Converting 1-based to 0-based indexing in {model_folder}")
|
||||
# Sort files to ensure correct order
|
||||
files.sort(key=lambda x: x['name'])
|
||||
|
||||
# First, create rename mapping to avoid conflicts
|
||||
renames = []
|
||||
for file in files:
|
||||
match = re.match(r'image_(\d+)\.(\w+)$', file['name'])
|
||||
if match:
|
||||
index = int(match.group(1))
|
||||
ext = match.group(2)
|
||||
if index > 0: # Only rename if index is positive
|
||||
new_name = f"image_{index-1}.{ext}"
|
||||
renames.append((file['name'], new_name))
|
||||
|
||||
# Use temporary filenames to avoid conflicts
|
||||
for old_name, new_name in renames:
|
||||
old_path = os.path.join(model_folder, old_name)
|
||||
temp_path = os.path.join(model_folder, f"temp_{old_name}")
|
||||
try:
|
||||
os.rename(old_path, temp_path)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to rename {old_path} to {temp_path}: {e}")
|
||||
|
||||
# Rename from temporary names to final names
|
||||
for old_name, new_name in renames:
|
||||
temp_path = os.path.join(model_folder, f"temp_{old_name}")
|
||||
new_path = os.path.join(model_folder, new_name)
|
||||
try:
|
||||
os.rename(temp_path, new_path)
|
||||
logger.debug(f"Renamed {old_name} to {new_name}")
|
||||
|
||||
# Update file list entry
|
||||
for file in files:
|
||||
if file['name'] == old_name:
|
||||
file['name'] = new_name
|
||||
file['path'] = f'/example_images_static/{model_hash}/{new_name}'
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to rename {temp_path} to {new_path}: {e}")
|
||||
|
||||
# Refresh file list after renaming
|
||||
files = []
|
||||
for file in os.listdir(model_folder):
|
||||
file_path = os.path.join(model_folder, file)
|
||||
if os.path.isfile(file_path):
|
||||
file_ext = os.path.splitext(file)[1].lower()
|
||||
if (file_ext in SUPPORTED_MEDIA_EXTENSIONS['images'] or
|
||||
file_ext in SUPPORTED_MEDIA_EXTENSIONS['videos']):
|
||||
files.append({
|
||||
'name': file,
|
||||
'path': f'/example_images_static/{model_hash}/{file}',
|
||||
'extension': file_ext,
|
||||
'is_video': file_ext in SUPPORTED_MEDIA_EXTENSIONS['videos']
|
||||
})
|
||||
|
||||
# Sort files by index for consistent order
|
||||
def extract_index(filename):
|
||||
match = re.match(r'image_(\d+)\.\w+$', filename)
|
||||
if match:
|
||||
return int(match.group(1))
|
||||
return float('inf') # Place non-matching files at the end
|
||||
|
||||
files.sort(key=lambda x: extract_index(x['name']))
|
||||
|
||||
return web.json_response({
|
||||
'success': True,
|
||||
'files': files
|
||||
|
||||
Reference in New Issue
Block a user