validator.py
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
# processors/validator.py
|
||||
|
||||
import os
|
||||
import zipfile
|
||||
import rarfile # Necesita: pip install rarfile
|
||||
|
||||
IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".webp"}
|
||||
TRASH_FILES = {"thumbs.db", ".ds_store"}
|
||||
|
||||
class ValidationResult:
|
||||
def __init__(self, path):
|
||||
self.path = path
|
||||
self.is_valid = True
|
||||
self.errors = []
|
||||
self.warnings = []
|
||||
self.images = []
|
||||
|
||||
def add_error(self, msg):
|
||||
self.is_valid = False
|
||||
self.errors.append(msg)
|
||||
|
||||
def add_warning(self, msg):
|
||||
self.warnings.append(msg)
|
||||
|
||||
def __str__(self):
|
||||
status = "OK" if self.is_valid else "ERROR"
|
||||
return f"[{status}] {self.path}"
|
||||
|
||||
|
||||
def validate_comic(path):
|
||||
"""
|
||||
Valida un archivo CBR/CBZ y devuelve un ValidationResult.
|
||||
"""
|
||||
result = ValidationResult(path)
|
||||
ext = os.path.splitext(path)[1].lower()
|
||||
|
||||
try:
|
||||
if ext == ".cbz":
|
||||
archive = zipfile.ZipFile(path, "r")
|
||||
file_list = archive.namelist()
|
||||
|
||||
elif ext == ".cbr":
|
||||
archive = rarfile.RarFile(path, "r")
|
||||
file_list = archive.namelist()
|
||||
|
||||
else:
|
||||
result.add_error("Extensión no reconocida")
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
result.add_error(f"No se pudo abrir el archivo: {e}")
|
||||
return result
|
||||
|
||||
# Comprobar imágenes
|
||||
for f in file_list:
|
||||
name = f.lower()
|
||||
_, fext = os.path.splitext(name)
|
||||
|
||||
if fext in IMAGE_EXTENSIONS:
|
||||
result.images.append(f)
|
||||
|
||||
if os.path.basename(name) in TRASH_FILES:
|
||||
result.add_warning(f"Archivo basura encontrado: {f}")
|
||||
|
||||
if not result.images:
|
||||
result.add_error("No contiene imágenes válidas")
|
||||
|
||||
return result
|
||||
Reference in New Issue
Block a user