mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-03-25 07:05:43 -03:00
refactor: Enhance preview file handling and add update_preview_in_cache method for ModelScanner
This commit is contained in:
@@ -17,7 +17,8 @@ from ..services.settings_manager import settings
|
|||||||
import asyncio
|
import asyncio
|
||||||
from .update_routes import UpdateRoutes
|
from .update_routes import UpdateRoutes
|
||||||
from ..services.recipe_scanner import RecipeScanner
|
from ..services.recipe_scanner import RecipeScanner
|
||||||
from ..utils.constants import PREVIEW_EXTENSIONS
|
from ..utils.constants import PREVIEW_EXTENSIONS, CARD_PREVIEW_WIDTH
|
||||||
|
from ..utils.exif_utils import ExifUtils
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -302,18 +303,29 @@ class ApiRoutes:
|
|||||||
|
|
||||||
async def _save_preview_file(self, model_path: str, preview_data: bytes, content_type: str) -> str:
|
async def _save_preview_file(self, model_path: str, preview_data: bytes, content_type: str) -> str:
|
||||||
"""Save preview file and return its path"""
|
"""Save preview file and return its path"""
|
||||||
# Determine file extension based on content type
|
|
||||||
if content_type.startswith('video/'):
|
|
||||||
extension = '.preview.mp4'
|
|
||||||
else:
|
|
||||||
extension = '.preview.png'
|
|
||||||
|
|
||||||
base_name = os.path.splitext(os.path.basename(model_path))[0]
|
base_name = os.path.splitext(os.path.basename(model_path))[0]
|
||||||
folder = os.path.dirname(model_path)
|
folder = os.path.dirname(model_path)
|
||||||
|
|
||||||
|
# Determine if content is video or image
|
||||||
|
if content_type.startswith('video/'):
|
||||||
|
# For videos, keep original format and use .mp4 extension
|
||||||
|
extension = '.mp4'
|
||||||
|
optimized_data = preview_data
|
||||||
|
else:
|
||||||
|
# For images, optimize and convert to WebP
|
||||||
|
optimized_data, _ = ExifUtils.optimize_image(
|
||||||
|
image_data=preview_data,
|
||||||
|
target_width=CARD_PREVIEW_WIDTH,
|
||||||
|
format='webp',
|
||||||
|
quality=85,
|
||||||
|
preserve_metadata=True
|
||||||
|
)
|
||||||
|
extension = '.webp' # Use .webp without .preview part
|
||||||
|
|
||||||
preview_path = os.path.join(folder, base_name + extension).replace(os.sep, '/')
|
preview_path = os.path.join(folder, base_name + extension).replace(os.sep, '/')
|
||||||
|
|
||||||
with open(preview_path, 'wb') as f:
|
with open(preview_path, 'wb') as f:
|
||||||
f.write(preview_data)
|
f.write(optimized_data)
|
||||||
|
|
||||||
return preview_path
|
return preview_path
|
||||||
|
|
||||||
|
|||||||
@@ -538,3 +538,18 @@ class ModelScanner:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error getting model info by name: {e}", exc_info=True)
|
logger.error(f"Error getting model info by name: {e}", exc_info=True)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
async def update_preview_in_cache(self, file_path: str, preview_url: str) -> bool:
|
||||||
|
"""Update preview URL in cache for a specific lora
|
||||||
|
|
||||||
|
Args:
|
||||||
|
file_path: The file path of the lora to update
|
||||||
|
preview_url: The new preview URL
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if the update was successful, False if cache doesn't exist or lora wasn't found
|
||||||
|
"""
|
||||||
|
if self._cache is None:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return await self._cache.update_preview_url(file_path, preview_url)
|
||||||
@@ -3,6 +3,7 @@ import { state } from '../state/index.js';
|
|||||||
import { showLoraModal } from './loraModal/index.js';
|
import { showLoraModal } from './loraModal/index.js';
|
||||||
import { bulkManager } from '../managers/BulkManager.js';
|
import { bulkManager } from '../managers/BulkManager.js';
|
||||||
import { NSFW_LEVELS } from '../utils/constants.js';
|
import { NSFW_LEVELS } from '../utils/constants.js';
|
||||||
|
import { replacePreview } from '../api/loraApi.js'
|
||||||
|
|
||||||
export function createLoraCard(lora) {
|
export function createLoraCard(lora) {
|
||||||
const card = document.createElement('div');
|
const card = document.createElement('div');
|
||||||
|
|||||||
Reference in New Issue
Block a user