backup
This commit is contained in:
+22
-40
@@ -6,53 +6,33 @@ import rarfile
|
||||
import tempfile
|
||||
import shutil
|
||||
|
||||
|
||||
def decide_target_format(original_path, desired_format="cbz"):
|
||||
"""
|
||||
Decide si el archivo debe convertirse y cuál será su ruta final.
|
||||
No realiza la conversión.
|
||||
"""
|
||||
base, _ = os.path.splitext(original_path)
|
||||
target_path = f"{base}.{desired_format.lower()}"
|
||||
|
||||
needs_conversion = not original_path.lower().endswith(desired_format.lower())
|
||||
|
||||
return {
|
||||
"needs_conversion": needs_conversion,
|
||||
"target_format": desired_format.lower(),
|
||||
"target_path": target_path
|
||||
}
|
||||
from processors.validator import validate_comic
|
||||
|
||||
|
||||
def convert_comic(path, desired_format="cbz"):
|
||||
"""
|
||||
Convierte un archivo CBR/CBZ al formato deseado.
|
||||
NO limpia basura.
|
||||
NO renombra páginas.
|
||||
NO reordena nada.
|
||||
"""
|
||||
info = decide_target_format(path, desired_format)
|
||||
validation = validate_comic(path)
|
||||
|
||||
if not info["needs_conversion"]:
|
||||
return info # Nada que hacer
|
||||
if validation.errors:
|
||||
raise Exception(f"Archivo corrupto: {path}")
|
||||
|
||||
ext = os.path.splitext(path)[1].lower()
|
||||
real = validation.real_format
|
||||
|
||||
# 1) Abrir archivo original
|
||||
if ext == ".cbr":
|
||||
archive = rarfile.RarFile(path, "r")
|
||||
elif ext == ".cbz":
|
||||
archive = zipfile.ZipFile(path, "r")
|
||||
else:
|
||||
raise Exception("Formato no soportado")
|
||||
# Si ya está en el formato deseado → nada que hacer
|
||||
if desired_format == "cbz" and real == "zip":
|
||||
return {"needs_conversion": False, "target_path": path}
|
||||
|
||||
if desired_format == "cbr" and real == "rar":
|
||||
return {"needs_conversion": False, "target_path": path}
|
||||
|
||||
# Abrir según formato real
|
||||
archive = zipfile.ZipFile(path, "r") if real == "zip" else rarfile.RarFile(path, "r")
|
||||
|
||||
# 2) Extraer a carpeta temporal
|
||||
temp_dir = tempfile.mkdtemp()
|
||||
archive.extractall(temp_dir)
|
||||
archive.close()
|
||||
|
||||
# 3) Reempaquetar en el formato deseado
|
||||
target_path = info["target_path"]
|
||||
base, _ = os.path.splitext(path)
|
||||
target_path = f"{base}.{desired_format}"
|
||||
|
||||
if desired_format == "cbz":
|
||||
with zipfile.ZipFile(target_path, "w", zipfile.ZIP_DEFLATED) as new_zip:
|
||||
@@ -63,10 +43,12 @@ def convert_comic(path, desired_format="cbz"):
|
||||
new_zip.write(full, rel)
|
||||
|
||||
elif desired_format == "cbr":
|
||||
# rarfile no puede crear RAR → hay que usar "rar" externo
|
||||
raise NotImplementedError("Crear CBR requiere la herramienta 'rar' instalada")
|
||||
raise NotImplementedError("Crear CBR requiere 'rar' instalado")
|
||||
|
||||
# 4) Limpiar temporal
|
||||
shutil.rmtree(temp_dir)
|
||||
|
||||
return info
|
||||
# Mover original a backup
|
||||
from core.backup import move_to_backup
|
||||
move_to_backup(path)
|
||||
|
||||
return {"needs_conversion": True, "target_path": target_path}
|
||||
|
||||
Reference in New Issue
Block a user