From 09b2fdfc5985d5cb7047215b0120d863f2bb7466 Mon Sep 17 00:00:00 2001 From: Will Miao <13051207myq@gmail.com> Date: Sat, 15 Mar 2025 09:45:07 +0800 Subject: [PATCH] Refactor API and DownloadManager to utilize version-level properties for model file existence and size, improving data handling and UI responsiveness. --- py/routes/api_routes.py | 21 ++++++++++++++++----- static/js/managers/DownloadManager.js | 19 ++++++++++++------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/py/routes/api_routes.py b/py/routes/api_routes.py index 22b7ed1d..3bf20778 100644 --- a/py/routes/api_routes.py +++ b/py/routes/api_routes.py @@ -539,13 +539,24 @@ class ApiRoutes: # Check local availability for each version for version in versions: - for file in version.get('files', []): - sha256 = file.get('hashes', {}).get('SHA256') + # Find the model file (type="Model") in the files list + model_file = next((file for file in version.get('files', []) + if file.get('type') == 'Model'), None) + + if model_file: + sha256 = model_file.get('hashes', {}).get('SHA256') if sha256: - file['existsLocally'] = self.scanner.has_lora_hash(sha256) - if file['existsLocally']: - file['localPath'] = self.scanner.get_lora_path_by_hash(sha256) + # Set existsLocally and localPath at the version level + version['existsLocally'] = self.scanner.has_lora_hash(sha256) + if version['existsLocally']: + version['localPath'] = self.scanner.get_lora_path_by_hash(sha256) + # Also set the model file size at the version level for easier access + version['modelSizeKB'] = model_file.get('sizeKB') + else: + # No model file found in this version + version['existsLocally'] = False + return web.json_response(versions) except Exception as e: logger.error(f"Error fetching model versions: {e}") diff --git a/static/js/managers/DownloadManager.js b/static/js/managers/DownloadManager.js index 75631bb1..e4b211ac 100644 --- a/static/js/managers/DownloadManager.js +++ b/static/js/managers/DownloadManager.js @@ -120,16 +120,21 @@ export class DownloadManager { versionList.innerHTML = this.versions.map(version => { const firstImage = version.images?.find(img => !img.url.endsWith('.mp4')); const thumbnailUrl = firstImage ? firstImage.url : '/loras_static/images/no-preview.png'; - const fileSize = (version.files[0]?.sizeKB / 1024).toFixed(2); - const existsLocally = version.files[0]?.existsLocally; - const localPath = version.files[0]?.localPath; + // Use version-level size or fallback to first file + const fileSize = version.modelSizeKB ? + (version.modelSizeKB / 1024).toFixed(2) : + (version.files[0]?.sizeKB / 1024).toFixed(2); + + // Use version-level existsLocally flag + const existsLocally = version.existsLocally; + const localPath = version.localPath; // 更新本地状态指示器为badge样式 const localStatus = existsLocally ? `
In Library -
${localPath}
+
${localPath || ''}
` : ''; return ` @@ -177,12 +182,12 @@ export class DownloadManager { this.updateNextButtonState(); } - // Add new method to update Next button state + // Update this method to use version-level existsLocally updateNextButtonState() { const nextButton = document.querySelector('#versionStep .primary-btn'); if (!nextButton) return; - const existsLocally = this.currentVersion?.files[0]?.existsLocally; + const existsLocally = this.currentVersion?.existsLocally; if (existsLocally) { nextButton.disabled = true; @@ -202,7 +207,7 @@ export class DownloadManager { } // Double-check if the version exists locally - const existsLocally = this.currentVersion.files[0]?.existsLocally; + const existsLocally = this.currentVersion.existsLocally; if (existsLocally) { showToast('This version already exists in your library', 'info'); return;