Spaces:
Sleeping
Sleeping
File size: 8,329 Bytes
3d8e949 159faf0 3d8e949 159faf0 3d8e949 7682d30 159faf0 7682d30 3d8e949 159faf0 3d8e949 159faf0 3d8e949 159faf0 3d8e949 d123e01 3d8e949 159faf0 3d8e949 159faf0 3d8e949 d123e01 3d8e949 d123e01 3d8e949 159faf0 3d8e949 159faf0 3d8e949 d123e01 3d8e949 159faf0 3d8e949 159faf0 3d8e949 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
"""
Upload Service - Handle file uploads and validation
Provides upload management functionality that integrates with
the Flask app factory pattern and existing services.
"""
import logging
from typing import Any, Dict, List, Tuple
from werkzeug.datastructures import FileStorage
class UploadService:
"""
File upload service that handles multi-file uploads with validation.
Integrates with DocumentService for file management and ProcessingService
for async processing workflow.
"""
def __init__(self, document_service, processing_service):
"""
Initialize upload service.
Args:
document_service: DocumentService instance
processing_service: ProcessingService instance
"""
self.document_service = document_service
self.processing_service = processing_service
logging.info("UploadService initialized")
def handle_upload_request(self, request_files, metadata: Dict[str, Any] = None) -> Dict[str, Any]:
"""
Handle multi-file upload request.
Args:
request_files: Files from Flask request
metadata: Optional metadata for files
Returns:
Upload results with status and file information
"""
if not request_files:
return {"status": "error", "message": "No files provided", "files": []}
results = {
"status": "success",
"files": [],
"job_ids": [],
"total_files": 0,
"successful_uploads": 0,
"failed_uploads": 0,
"errors": [],
}
# Handle multiple files
files = request_files.getlist("files") if hasattr(request_files, "getlist") else [request_files.get("file")]
files = [f for f in files if f] # Remove None values
results["total_files"] = len(files)
for file_obj in files:
try:
file_result = self._process_single_file(file_obj, metadata or {})
results["files"].append(file_result)
if file_result["status"] == "success":
results["successful_uploads"] += 1
if file_result.get("job_id"):
results["job_ids"].append(file_result["job_id"])
else:
results["failed_uploads"] += 1
if file_result.get("error"):
results["errors"].append(file_result["error"])
except Exception as e:
error_msg = f"Failed to process file: {str(e)}"
results["errors"].append(error_msg)
results["failed_uploads"] += 1
results["files"].append(
{
"filename": getattr(file_obj, "filename", "unknown"),
"status": "error",
"error": error_msg,
}
)
# Update overall status
if results["failed_uploads"] > 0:
if results["successful_uploads"] == 0:
results["status"] = "error"
results["message"] = "All uploads failed"
else:
results["status"] = "partial"
results["message"] = (
f"{results['successful_uploads']} files uploaded, " f"{results['failed_uploads']} failed"
)
else:
results["message"] = f"Successfully uploaded {results['successful_uploads']} files"
return results
def _process_single_file(self, file_obj: FileStorage, metadata: Dict[str, Any]) -> Dict[str, Any]:
"""
Process a single uploaded file.
Args:
file_obj: File object from request
metadata: File metadata
Returns:
Processing result for the file
"""
filename = file_obj.filename or "unknown"
try:
# Get file size
file_obj.seek(0, 2) # Seek to end
file_size = file_obj.tell()
file_obj.seek(0) # Reset to beginning
# Validate file
validation_result = self.document_service.validate_file(filename, file_size)
if not validation_result["valid"]:
error_msg = f"Validation failed: {', '.join(validation_result['errors'])}"
return {
"filename": filename,
"status": "error",
"error": error_msg,
"validation": validation_result,
}
# Save file
file_info = self.document_service.save_uploaded_file(file_obj, filename)
# Add metadata
file_info.update(metadata)
# Extract file metadata
file_metadata = self.document_service.get_file_metadata(file_info["file_path"])
file_info["metadata"] = file_metadata
# Submit for processing
processing_options = {
"chunk_size": metadata.get("chunk_size", 1000),
"overlap": metadata.get("overlap", 200),
"auto_process": metadata.get("auto_process", True),
}
job_id = None
if processing_options.get("auto_process", True):
job_id = self.processing_service.submit_job(file_info, processing_options)
upload_msg = "File uploaded"
if job_id:
upload_msg += " and submitted for processing"
return {
"filename": filename,
"status": "success",
"file_info": file_info,
"job_id": job_id,
"validation": validation_result,
"message": upload_msg,
}
except Exception as e:
logging.error(f"Error processing file {filename}: {e}", exc_info=True)
return {"filename": filename, "status": "error", "error": str(e)}
def get_upload_summary(self) -> Dict[str, Any]:
"""
Get summary of upload system status.
Returns:
Upload system summary
"""
try:
upload_stats = self.document_service.get_upload_stats()
queue_status = self.processing_service.get_queue_status()
return {
"upload_stats": upload_stats,
"processing_queue": queue_status,
"service_status": {
"document_service": "active",
"processing_service": ("active" if queue_status["service_running"] else "inactive"),
},
}
except Exception as e:
logging.error(f"Error getting upload summary: {e}")
return {"error": str(e)}
def validate_batch_upload(self, files: List[FileStorage]) -> Tuple[List[FileStorage], List[str]]:
"""
Validate a batch of files before upload.
Args:
files: List of file objects
Returns:
Tuple of (valid_files, error_messages)
"""
valid_files = []
errors = []
if len(files) > self.document_service.max_batch_size:
max_batch = self.document_service.max_batch_size
errors.append(f"Too many files: {len(files)} (max: {max_batch})")
return [], errors
total_size = 0
for file_obj in files:
if not file_obj or not file_obj.filename:
errors.append("Empty file or missing filename")
continue
# Get file size
file_obj.seek(0, 2)
file_size = file_obj.tell()
file_obj.seek(0)
total_size += file_size
# Validate individual file
validation = self.document_service.validate_file(file_obj.filename, file_size)
if validation["valid"]:
valid_files.append(file_obj)
else:
errors.extend([f"{file_obj.filename}: {error}" for error in validation["errors"]])
# Check total batch size
max_total_size = self.document_service.max_file_size * len(files)
if total_size > max_total_size:
errors.append(f"Total batch size too large: {total_size} bytes")
return valid_files, errors
|