From 8063cee3cd1140fa5b7521cfe54be81f50d41115 Mon Sep 17 00:00:00 2001 From: Will Miao <13051207myq@gmail.com> Date: Fri, 30 May 2025 16:38:18 +0800 Subject: [PATCH] Add rename functionality for checkpoint and LoRA files with loading indicators --- static/js/api/checkpointApi.js | 36 +++++++++++++++++++ static/js/api/loraApi.js | 36 +++++++++++++++++++ .../checkpointModal/ModelMetadata.js | 17 ++------- .../js/components/loraModal/ModelMetadata.js | 17 ++------- 4 files changed, 78 insertions(+), 28 deletions(-) diff --git a/static/js/api/checkpointApi.js b/static/js/api/checkpointApi.js index c7bcab6c..fdbc6c6f 100644 --- a/static/js/api/checkpointApi.js +++ b/static/js/api/checkpointApi.js @@ -153,4 +153,40 @@ export async function saveModelMetadata(filePath, data) { */ export function excludeCheckpoint(filePath) { return baseExcludeModel(filePath, 'checkpoint'); +} + +/** + * Rename a checkpoint file + * @param {string} filePath - Current file path + * @param {string} newFileName - New file name (without path) + * @returns {Promise} - Promise that resolves with the server response + */ +export async function renameCheckpointFile(filePath, newFileName) { + try { + // Show loading indicator + state.loadingManager.showSimpleLoading('Renaming checkpoint file...'); + + const response = await fetch('/api/rename_checkpoint', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + file_path: filePath, + new_file_name: newFileName + }) + }); + + if (!response.ok) { + throw new Error(`Server returned ${response.status}: ${response.statusText}`); + } + + return await response.json(); + } catch (error) { + console.error('Error renaming checkpoint file:', error); + throw error; + } finally { + // Hide loading indicator + state.loadingManager.hide(); + } } \ No newline at end of file diff --git a/static/js/api/loraApi.js b/static/js/api/loraApi.js index e9b13df0..42502d3f 100644 --- a/static/js/api/loraApi.js +++ b/static/js/api/loraApi.js @@ -181,4 +181,40 @@ export async function fetchModelDescription(modelId, filePath) { console.error('Error fetching model description:', error); throw error; } +} + +/** + * Rename a LoRA file + * @param {string} filePath - Current file path + * @param {string} newFileName - New file name (without path) + * @returns {Promise} - Promise that resolves with the server response + */ +export async function renameLoraFile(filePath, newFileName) { + try { + // Show loading indicator + state.loadingManager.showSimpleLoading('Renaming LoRA file...'); + + const response = await fetch('/api/rename_lora', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + file_path: filePath, + new_file_name: newFileName + }) + }); + + if (!response.ok) { + throw new Error(`Server returned ${response.status}: ${response.statusText}`); + } + + return await response.json(); + } catch (error) { + console.error('Error renaming LoRA file:', error); + throw error; + } finally { + // Hide loading indicator + state.loadingManager.hide(); + } } \ No newline at end of file diff --git a/static/js/components/checkpointModal/ModelMetadata.js b/static/js/components/checkpointModal/ModelMetadata.js index f3964208..68d3c85f 100644 --- a/static/js/components/checkpointModal/ModelMetadata.js +++ b/static/js/components/checkpointModal/ModelMetadata.js @@ -5,7 +5,7 @@ import { showToast } from '../../utils/uiHelpers.js'; import { BASE_MODELS } from '../../utils/constants.js'; import { updateCheckpointCard } from '../../utils/cardUpdater.js'; -import { saveModelMetadata } from '../../api/checkpointApi.js'; +import { saveModelMetadata, renameCheckpointFile } from '../../api/checkpointApi.js'; /** * Set up model name editing functionality @@ -419,19 +419,8 @@ export function setupFileNameEditing(filePath) { try { // Use the passed filePath (which includes the original filename) - // Call API to rename the file - const response = await fetch('/api/rename_checkpoint', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - file_path: filePath, // Use the full original path - new_file_name: newFileName - }) - }); - - const result = await response.json(); + // Call API to rename the file using the new function from checkpointApi.js + const result = await renameCheckpointFile(filePath, newFileName); if (result.success) { showToast('File name updated successfully', 'success'); diff --git a/static/js/components/loraModal/ModelMetadata.js b/static/js/components/loraModal/ModelMetadata.js index 79df94ea..7b376cd1 100644 --- a/static/js/components/loraModal/ModelMetadata.js +++ b/static/js/components/loraModal/ModelMetadata.js @@ -5,7 +5,7 @@ import { showToast } from '../../utils/uiHelpers.js'; import { BASE_MODELS } from '../../utils/constants.js'; import { updateLoraCard } from '../../utils/cardUpdater.js'; -import { saveModelMetadata } from '../../api/loraApi.js'; +import { saveModelMetadata, renameLoraFile } from '../../api/loraApi.js'; /** * 设置模型名称编辑功能 @@ -459,19 +459,8 @@ export function setupFileNameEditing(filePath) { // Get the file path from the dataset const filePath = this.dataset.filePath; - // Call API to rename the file - const response = await fetch('/api/rename_lora', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - file_path: filePath, - new_file_name: newFileName - }) - }); - - const result = await response.json(); + // Call API to rename the file using the new function from loraApi.js + const result = await renameLoraFile(filePath, newFileName); if (result.success) { showToast('File name updated successfully', 'success');