commit893b229cc6
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 22:14:08 2025 -0500 j commit06f539aea2
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:55:30 2025 -0500 fk commitd0c2960ec9
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:50:53 2025 -0500 frick commit6b1362c166
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:49:40 2025 -0500 database commit8cadc6df4c
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:38:09 2025 -0500 asdf commit1ca6f98684
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:32:50 2025 -0500 fffff commit8bb337a9c3
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:31:13 2025 -0500 ffff commit65aba280c5
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:26:16 2025 -0500 aa commit59ef03a59e
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:24:21 2025 -0500 asdf commitf44d5740fc
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:23:32 2025 -0500 aaa commit13c96b1643
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:18:54 2025 -0500 sdf commit949c795fd1
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:17:53 2025 -0500 asdf commit8c3cd423fe
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:56:01 2025 -0500 app2 commit78eafc739e
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:54:55 2025 -0500 app commitdc47eced14
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:43:15 2025 -0500 asdfasdfasdf commite24bcae88c
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:39:44 2025 -0500 a commitc894451bfe
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:38:20 2025 -0500 req commit3d09869562
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:33:27 2025 -0500 wrong number = code dont work lol i love computers commit4c93a1271b
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:29:39 2025 -0500 q commit1f5361da88
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 18:27:20 2025 -0500 same as original code now -5 days of my life commit511b070cbb
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 13:52:28 2025 -0500 pricey worky commit964fdd641b
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 11:37:29 2025 -0500 prep for pricing service work commita78c3bcba3
Author: zman <joshua.k.rzemien@gmail.com> Date: Wed Feb 5 21:51:22 2025 -0500 more stuff yay commitbd9cfca7a9
Author: zman <joshua.k.rzemien@gmail.com> Date: Tue Feb 4 22:30:33 2025 -0500 GIGA FIXED EVERYTHING OMG commit85510a4671
Author: zman <joshua.k.rzemien@gmail.com> Date: Tue Feb 4 00:01:34 2025 -0500 data model change and some new services
156 lines
5.8 KiB
Python
156 lines
5.8 KiB
Python
from sqlalchemy.orm import Session
|
|
from typing import Optional, List, Dict, Any
|
|
from uuid import uuid4
|
|
import csv
|
|
import logging
|
|
import os
|
|
from io import StringIO
|
|
|
|
from app.db.utils import db_transaction
|
|
from app.db.models import File, StagedFileProduct
|
|
from app.schemas.file import CreateFileRequest
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class FileConfig:
|
|
"""Configuration constants for file processing"""
|
|
TEMP_DIR = os.path.join(os.getcwd(), 'app/' + 'temp')
|
|
|
|
MANABOX_HEADERS = [
|
|
'Name', 'Set code', 'Set name', 'Collector number', 'Foil',
|
|
'Rarity', 'Quantity', 'ManaBox ID', 'Scryfall ID', 'Purchase price',
|
|
'Misprint', 'Altered', 'Condition', 'Language', 'Purchase price currency'
|
|
]
|
|
|
|
SOURCES = {
|
|
"manabox": {
|
|
"required_headers": MANABOX_HEADERS,
|
|
"allowed_extensions": ['.csv'],
|
|
"allowed_types": ['scan_export_common', 'scan_export_rare']
|
|
}
|
|
}
|
|
|
|
class FileValidationError(Exception):
|
|
"""Custom exception for file validation errors"""
|
|
pass
|
|
|
|
class FileService:
|
|
def __init__(self, db: Session):
|
|
self.db = db
|
|
|
|
def get_config(self, source: str) -> Dict[str, Any]:
|
|
"""Get configuration for a specific source"""
|
|
config = FileConfig.SOURCES.get(source)
|
|
if not config:
|
|
raise FileValidationError(f"Unsupported source: {source}")
|
|
return config
|
|
|
|
def validate_file_extension(self, filename: str, config: Dict[str, Any]) -> bool:
|
|
"""Validate file extension against allowed extensions"""
|
|
return any(filename.endswith(ext) for ext in config["allowed_extensions"])
|
|
|
|
def validate_file_type(self, metadata: CreateFileRequest, config: Dict[str, Any]) -> bool:
|
|
"""Validate file type against allowed types"""
|
|
return metadata.type in config["allowed_types"]
|
|
|
|
def validate_csv(self, content: bytes, required_headers: Optional[List[str]] = None) -> bool:
|
|
"""Validate CSV content and headers"""
|
|
try:
|
|
csv_text = content.decode('utf-8')
|
|
csv_file = StringIO(csv_text)
|
|
csv_reader = csv.reader(csv_file)
|
|
|
|
if required_headers:
|
|
headers = next(csv_reader, None)
|
|
if not headers or not all(header in headers for header in required_headers):
|
|
return False
|
|
return True
|
|
|
|
except (UnicodeDecodeError, csv.Error) as e:
|
|
logger.error(f"CSV validation error: {str(e)}")
|
|
return False
|
|
|
|
def validate_file_content(self, content: bytes, metadata: CreateFileRequest, config: Dict[str, Any]) -> bool:
|
|
"""Validate file content based on file type"""
|
|
extension = os.path.splitext(metadata.filename)[1].lower()
|
|
if extension == '.csv':
|
|
return self.validate_csv(content, config.get("required_headers"))
|
|
return False
|
|
|
|
def validate_file(self, content: bytes, metadata: CreateFileRequest) -> bool:
|
|
"""Validate file against all criteria"""
|
|
config = self.get_config(metadata.source)
|
|
|
|
if not self.validate_file_extension(metadata.filename, config):
|
|
raise FileValidationError("Invalid file extension")
|
|
|
|
if not self.validate_file_type(metadata, config):
|
|
raise FileValidationError("Invalid file type")
|
|
|
|
if not self.validate_file_content(content, metadata, config):
|
|
raise FileValidationError("Invalid file content or headers")
|
|
|
|
return True
|
|
|
|
def create_file(self, content: bytes, metadata: CreateFileRequest) -> File:
|
|
"""Create a new file record and save the file"""
|
|
with db_transaction(self.db):
|
|
file = File(
|
|
id=str(uuid4()),
|
|
filename=metadata.filename,
|
|
filepath=os.path.join(FileConfig.TEMP_DIR, metadata.filename),
|
|
type=metadata.type,
|
|
source=metadata.source,
|
|
filesize_kb=round(len(content) / 1024, 2),
|
|
status='pending',
|
|
service=metadata.service
|
|
)
|
|
self.db.add(file)
|
|
|
|
os.makedirs(FileConfig.TEMP_DIR, exist_ok=True)
|
|
with open(file.filepath, 'wb') as f:
|
|
f.write(content)
|
|
|
|
return file
|
|
|
|
def get_file(self, file_id: str) -> File:
|
|
"""Get a file by ID"""
|
|
file = self.db.query(File).filter(File.id == file_id).first()
|
|
if not file:
|
|
raise FileValidationError(f"File with id {file_id} not found")
|
|
return file
|
|
|
|
def get_files(self, status: Optional[str] = None) -> List[File]:
|
|
"""Get all files, optionally filtered by status"""
|
|
query = self.db.query(File)
|
|
if status:
|
|
query = query.filter(File.status == status)
|
|
return query.all()
|
|
|
|
def get_staged_products(self, file_id: str) -> List[StagedFileProduct]:
|
|
"""Get staged products for a file"""
|
|
return self.db.query(StagedFileProduct).filter(
|
|
StagedFileProduct.file_id == file_id
|
|
).all()
|
|
|
|
def delete_file(self, file_id: str) -> File:
|
|
"""Mark a file as deleted and remove associated staged products"""
|
|
file = self.get_file(file_id)
|
|
staged_products = self.get_staged_products(file_id)
|
|
|
|
with db_transaction(self.db):
|
|
file.status = 'deleted'
|
|
for staged_product in staged_products:
|
|
self.db.delete(staged_product)
|
|
|
|
return file
|
|
|
|
def get_file_content(self, file_id: str) -> bytes:
|
|
"""Get the content of a file"""
|
|
file = self.get_file(file_id)
|
|
try:
|
|
with open(file.filepath, 'rb') as f:
|
|
return f.read()
|
|
except IOError as e:
|
|
logger.error(f"Error reading file {file_id}: {str(e)}")
|
|
raise FileValidationError(f"Could not read file content for {file_id}") |