This commit is contained in:
2025-10-23 19:00:49 +02:00
parent 3d5c19939c
commit c81a493fb1
9 changed files with 1225 additions and 131 deletions

View File

@@ -11,6 +11,7 @@ import logging
import os
import re
import shutil
from pathlib import Path
from typing import Any, Callable, Dict, Optional
from urllib.parse import quote
@@ -21,6 +22,7 @@ from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
from yt_dlp import YoutubeDL
from ...infrastructure.security.file_integrity import get_integrity_manager
from ..error_handler import (
DownloadError,
NetworkError,
@@ -387,7 +389,23 @@ class EnhancedAniWorldLoader(Loader):
# Check if file already exists and is valid
if os.path.exists(output_path):
if file_corruption_detector.is_valid_video_file(output_path):
is_valid = file_corruption_detector.is_valid_video_file(
output_path
)
# Also verify checksum if available
integrity_mgr = get_integrity_manager()
checksum_valid = True
if integrity_mgr.has_checksum(Path(output_path)):
checksum_valid = integrity_mgr.verify_checksum(
Path(output_path)
)
if not checksum_valid:
self.logger.warning(
f"Checksum verification failed for {output_file}"
)
if is_valid and checksum_valid:
msg = (
f"File already exists and is valid: "
f"{output_file}"
@@ -403,6 +421,8 @@ class EnhancedAniWorldLoader(Loader):
self.logger.warning(warning_msg)
try:
os.remove(output_path)
# Remove checksum entry
integrity_mgr.remove_checksum(Path(output_path))
except OSError as e:
error_msg = f"Failed to remove corrupted file: {e}"
self.logger.error(error_msg)
@@ -463,7 +483,9 @@ class EnhancedAniWorldLoader(Loader):
for provider_name in self.SUPPORTED_PROVIDERS:
try:
info_msg = f"Attempting download with provider: {provider_name}"
info_msg = (
f"Attempting download with provider: {provider_name}"
)
self.logger.info(info_msg)
# Get download link and headers for provider
@@ -514,6 +536,22 @@ class EnhancedAniWorldLoader(Loader):
# Move to final location
shutil.copy2(temp_path, output_path)
# Calculate and store checksum for integrity
integrity_mgr = get_integrity_manager()
try:
checksum = integrity_mgr.store_checksum(
Path(output_path)
)
filename = Path(output_path).name
self.logger.info(
f"Stored checksum for {filename}: "
f"{checksum[:16]}..."
)
except Exception as e:
self.logger.warning(
f"Failed to store checksum: {e}"
)
# Clean up temp file
try:
os.remove(temp_path)