fix(recipes): normalize relocated preview paths

This commit is contained in:
pixelpaws
2025-10-27 09:50:25 +08:00
parent 22eda58074
commit 139e915711
4 changed files with 73 additions and 9 deletions

View File

@@ -384,16 +384,32 @@ class RecipeScanner:
# Ensure the image file exists
image_path = recipe_data.get('file_path')
if not os.path.exists(image_path):
normalized_image_path = os.path.normpath(image_path) if image_path else image_path
path_updated = False
if image_path and normalized_image_path != image_path:
recipe_data['file_path'] = normalized_image_path
image_path = normalized_image_path
path_updated = True
if image_path and not os.path.exists(image_path):
logger.warning(f"Recipe image not found: {image_path}")
# Try to find the image in the same directory as the recipe
recipe_dir = os.path.dirname(recipe_path)
image_filename = os.path.basename(image_path)
alternative_path = os.path.join(recipe_dir, image_filename)
if os.path.exists(alternative_path):
recipe_data['file_path'] = alternative_path
normalized_alternative = os.path.normpath(alternative_path)
recipe_data['file_path'] = normalized_alternative
image_path = normalized_alternative
path_updated = True
logger.info(
"Updated recipe image path to %s after relocating asset", normalized_alternative
)
else:
logger.warning(f"Could not find alternative image path for {image_path}")
if path_updated:
self._write_recipe_file(recipe_path, recipe_data)
# Ensure loras array exists
if 'loras' not in recipe_data:
@@ -413,18 +429,24 @@ class RecipeScanner:
# Write updated recipe data back to file
try:
with open(recipe_path, 'w', encoding='utf-8') as f:
json.dump(recipe_data, f, indent=4, ensure_ascii=False)
self._write_recipe_file(recipe_path, recipe_data)
logger.info(f"Added fingerprint to recipe: {recipe_path}")
except Exception as e:
logger.error(f"Error writing updated recipe with fingerprint: {e}")
return recipe_data
except Exception as e:
logger.error(f"Error loading recipe file {recipe_path}: {e}")
import traceback
traceback.print_exc(file=sys.stderr)
return None
@staticmethod
def _write_recipe_file(recipe_path: str, recipe_data: Dict[str, Any]) -> None:
"""Persist ``recipe_data`` back to ``recipe_path`` with standard formatting."""
with open(recipe_path, 'w', encoding='utf-8') as file_obj:
json.dump(recipe_data, file_obj, indent=4, ensure_ascii=False)
async def _update_lora_information(self, recipe_data: Dict) -> bool:
"""Update LoRA information with hash and file_name

View File

@@ -73,7 +73,8 @@ class RecipePersistenceService:
)
image_filename = f"{recipe_id}{extension}"
image_path = os.path.join(recipes_dir, image_filename)
with open(image_path, "wb") as file_obj:
normalized_image_path = os.path.normpath(image_path)
with open(normalized_image_path, "wb") as file_obj:
file_obj.write(optimized_image)
current_time = time.time()
@@ -97,7 +98,7 @@ class RecipePersistenceService:
fingerprint = calculate_recipe_fingerprint(loras_data)
recipe_data: Dict[str, Any] = {
"id": recipe_id,
"file_path": image_path,
"file_path": normalized_image_path,
"title": name,
"modified": current_time,
"created_date": current_time,
@@ -116,10 +117,11 @@ class RecipePersistenceService:
json_filename = f"{recipe_id}.recipe.json"
json_path = os.path.join(recipes_dir, json_filename)
json_path = os.path.normpath(json_path)
with open(json_path, "w", encoding="utf-8") as file_obj:
json.dump(recipe_data, file_obj, indent=4, ensure_ascii=False)
self._exif_utils.append_recipe_metadata(image_path, recipe_data)
self._exif_utils.append_recipe_metadata(normalized_image_path, recipe_data)
matching_recipes = await self._find_matching_recipes(recipe_scanner, fingerprint, exclude_id=recipe_id)
await recipe_scanner.add_recipe(recipe_data)
@@ -128,7 +130,7 @@ class RecipePersistenceService:
{
"success": True,
"recipe_id": recipe_id,
"image_path": image_path,
"image_path": normalized_image_path,
"json_path": json_path,
"matching_recipes": matching_recipes,
}