From 2b8b71ba43439edf6fa818be16682cbd212b6b4e Mon Sep 17 00:00:00 2001 From: wilzamguerrero <110712636+wilzamguerrero@users.noreply.github.com> Date: Tue, 8 Oct 2024 20:47:31 -0700 Subject: [PATCH 1/6] Update collections.py --- routes/collections.py | 61 ++++++++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/routes/collections.py b/routes/collections.py index 67cc6c5..52d1175 100644 --- a/routes/collections.py +++ b/routes/collections.py @@ -1,41 +1,66 @@ from os import path, makedirs from aiohttp import web import shutil +import filecmp import time +import os from ..utils import collections_path, get_parent_path, add_uuid_to_filename, \ config_path, get_config, git_init, run_cmd, git_remote_name -# filename, folder_path, folder_type = 'outputs' | 'sources' + +def copy_if_different(source_file, destination_file): + """ + Copia un archivo solo si no existe en el destino o si es diferente. + """ + # Crear las carpetas necesarias en la carpeta de destino + makedirs(path.dirname(destination_file), exist_ok=True) + + # Solo copiar si el archivo no existe o si es diferente + if not path.exists(destination_file) or not filecmp.cmp(source_file, destination_file, shallow=False): + shutil.copy2(source_file, destination_file) + async def api_add_to_collections(request): json_data = await request.json() filename = json_data.get('filename') - if not filename: - return web.Response(status=404) - folder_path = json_data.get('folder_path', '') - folder_type = json_data.get("folder_type", "outputs") + + if not filename: + return web.Response(status=404, text="Filename not provided") + parent_path = get_parent_path(folder_type) - - makedirs(collections_path(), exist_ok=True) - source_file_path = path.join(parent_path, folder_path, filename) + new_filepath = path.join(collections_path(), filename) + if not path.exists(source_file_path): - return web.Response(status=404) + return web.Response(status=404, text="Source file or directory not found") - new_filepath = path.join( - collections_path(), - add_uuid_to_filename(filename) - ) + try: + if path.isdir(source_file_path): + # Asegurarnos de que la carpeta destino exista + makedirs(new_filepath, exist_ok=True) - if path.isdir(source_file_path): - shutil.copytree(source_file_path, new_filepath) - else: - shutil.copy(source_file_path, new_filepath) + # Recorrer todos los archivos en el directorio de origen + for root, dirs, files in os.walk(source_file_path): + for file in files: + source_file = path.join(root, file) + relative_path = path.relpath(source_file, source_file_path) # Obtener la ruta relativa + destination_file = path.join(new_filepath, relative_path) - return web.Response(status=201) + # Copiar solo si es necesario + copy_if_different(source_file, destination_file) + + else: + # Es un archivo, copiar solo si es diferente + copy_if_different(source_file_path, new_filepath) + + return web.Response(status=201, text="Files copied successfully") + + except Exception as e: + # Manejar cualquier excepción y devolver una respuesta de error + return web.Response(status=500, text=f"Error: {str(e)}") # filename, content async def api_create_new_workflow(request): From fa61c21b834b4411190a9b3491bbe0e9be45778a Mon Sep 17 00:00:00 2001 From: wilzamguerrero <110712636+wilzamguerrero@users.noreply.github.com> Date: Tue, 8 Oct 2024 20:48:10 -0700 Subject: [PATCH 2/6] Update utils.py --- utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils.py b/utils.py index d75366e..04c1dc8 100644 --- a/utils.py +++ b/utils.py @@ -169,8 +169,8 @@ def get_info_filename(filename): return path.splitext(filename)[0] + info_file_suffix def add_uuid_to_filename(filename): - name, ext = path.splitext(filename) - return f'{name}_{int(time.time())}{ext}' + # Simplemente devolver el nombre original del archivo + return filename def output_directory_from_comfyui(): if args.output_directory: From c27cb67a078726ab0135edacf886403afe35e953 Mon Sep 17 00:00:00 2001 From: wilzamguerrero <110712636+wilzamguerrero@users.noreply.github.com> Date: Wed, 9 Oct 2024 01:54:50 -0700 Subject: [PATCH 3/6] Update utils.py --- utils.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/utils.py b/utils.py index 04c1dc8..044c392 100644 --- a/utils.py +++ b/utils.py @@ -179,25 +179,30 @@ def output_directory_from_comfyui(): return folder_paths.get_output_directory() def git_init(): + # Inicializa el repositorio si no existe if not path.exists(path.join(collections_path(), '.git')): run_cmd('git init', collections_path()) - ret = run_cmd('git config user.name', collections_path(), - log_cmd=False, log_code=False, log_message=False) + # Configurar el nombre de usuario si no está configurado + ret = run_cmd('git config user.name', collections_path(), log_cmd=False, log_code=False, log_message=False) if len(ret.stdout) == 0: - ret = run_cmd('whoami', collections_path(), - log_cmd=False, log_code=False, log_message=False) + ret = run_cmd('whoami', collections_path(), log_cmd=False, log_code=False, log_message=False) username = ret.stdout.rstrip("\n") run_cmd(f'git config user.name "{username}"', collections_path()) - ret = run_cmd('git config user.email', collections_path(), - log_cmd=False, log_code=False, log_message=False) + # Configurar el correo electrónico si no está configurado + ret = run_cmd('git config user.email', collections_path(), log_cmd=False, log_code=False, log_message=False) if len(ret.stdout) == 0: - ret = run_cmd('hostname', collections_path(), - log_cmd=False, log_code=False, log_message=False) + ret = run_cmd('hostname', collections_path(), log_cmd=False, log_code=False, log_message=False) hostname = ret.stdout.rstrip("\n") run_cmd(f'git config user.email "{hostname}"', collections_path()) + # Cambiar a la rama main si no estamos en ella + ret = run_cmd('git branch --show-current', collections_path(), log_cmd=False, log_code=False, log_message=False) + current_branch = ret.stdout.strip() + if current_branch != 'main': + run_cmd('git checkout -b main', collections_path()) + for dir in [ collections_path(), sources_path(), From a675107f01bce32a2545b3895866b5d2ffc17083 Mon Sep 17 00:00:00 2001 From: wilzamguerrero <110712636+wilzamguerrero@users.noreply.github.com> Date: Wed, 9 Oct 2024 01:55:38 -0700 Subject: [PATCH 4/6] Update collections.py --- routes/collections.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/routes/collections.py b/routes/collections.py index 52d1175..1b4a2b0 100644 --- a/routes/collections.py +++ b/routes/collections.py @@ -110,9 +110,8 @@ async def api_sync_my_collections(_): status=500, ) - cmd = 'git branch --show-current' - ret = run_cmd(cmd, collections_path()) - branch = ret.stdout.replace('\n', '') + # Asegurarnos de que la rama main es la que se usa + branch = "main" cmd = f'git merge {git_remote_name}/{branch}' ret = run_cmd(cmd, collections_path(), log_code=False) From 559b033b91c72a0c16c9cf7e0c51d9df16d392c6 Mon Sep 17 00:00:00 2001 From: wilzamguerrero <110712636+wilzamguerrero@users.noreply.github.com> Date: Thu, 10 Oct 2024 10:42:41 -0700 Subject: [PATCH 5/6] Update collections.py git init --- routes/collections.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/routes/collections.py b/routes/collections.py index 1b4a2b0..c3ed52d 100644 --- a/routes/collections.py +++ b/routes/collections.py @@ -81,6 +81,9 @@ async def api_create_new_workflow(request): return web.Response(status=201) async def api_sync_my_collections(_): + + git_init() + if not path.exists(config_path): return web.Response(status=404) @@ -89,8 +92,6 @@ async def api_sync_my_collections(_): if not git_repo: return web.Response(status=404) - git_init() - cmd = 'git status -s' ret = run_cmd(cmd, collections_path()) if len(ret.stdout) > 0: From af61d0779eb1d3160fc9d8f2a0aa0d93956e338a Mon Sep 17 00:00:00 2001 From: wilzamguerrero <110712636+wilzamguerrero@users.noreply.github.com> Date: Fri, 11 Oct 2024 00:33:20 -0700 Subject: [PATCH 6/6] Update collections.py --- routes/collections.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routes/collections.py b/routes/collections.py index c3ed52d..f16f294 100644 --- a/routes/collections.py +++ b/routes/collections.py @@ -82,8 +82,6 @@ async def api_create_new_workflow(request): async def api_sync_my_collections(_): - git_init() - if not path.exists(config_path): return web.Response(status=404) @@ -92,6 +90,8 @@ async def api_sync_my_collections(_): if not git_repo: return web.Response(status=404) + git_init() + cmd = 'git status -s' ret = run_cmd(cmd, collections_path()) if len(ret.stdout) > 0: