commit 893b229cc6b35c09181a84050f34fb79024e41c2 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 22:14:08 2025 -0500 j commit 06f539aea2f4fff9da7038d43d0de553c4423796 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:55:30 2025 -0500 fk commit d0c2960ec9f334448d2eb3573b9d7817482abf46 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:50:53 2025 -0500 frick commit 6b1362c166fc5f51c3bcf316a99116f0d11074a5 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:49:40 2025 -0500 database commit 8cadc6df4c817d9d05503807e56287fd00e5e939 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:38:09 2025 -0500 asdf commit 1ca6f9868452e34143b8df4a412be35e6902a31e Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:32:50 2025 -0500 fffff commit 8bb337a9c35e830ef9ce3dac0a0f2df3fe9bc5a0 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:31:13 2025 -0500 ffff commit 65aba280c55fa09c6a37f688f485efab1f70792b Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:26:16 2025 -0500 aa commit 59ef03a59ee4a15c30e080a1aef7c31c0214a2e3 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:24:21 2025 -0500 asdf commit f44d5740fc9315ccb0792ecac3e8ec9f28f171be Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:23:32 2025 -0500 aaa commit 13c96b164316b4908d9d01e454cbdc9103157558 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:18:54 2025 -0500 sdf commit 949c795fd13d93c9618613740fb093f6bb7b7710 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:17:53 2025 -0500 asdf commit 8c3cd423fe228e8aff112a050170246a5fc9f8bd Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:56:01 2025 -0500 app2 commit 78eafc739ebb7f100f657964b3ad8f4937a4046b Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:54:55 2025 -0500 app commit dc47eced143e77ebec415bdfbe209d9466b7bcf1 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:43:15 2025 -0500 asdfasdfasdf commit e24bcae88cf8c14ea543f49b639b2976c627d201 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:39:44 2025 -0500 a commit c894451bfe790c97ac0e01085615d7c7288a39da Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:38:20 2025 -0500 req commit 3d09869562a96b5adc7c4be279bc8c003bbb37b2 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 commit 4c93a1271b8aea159cf53f8d7879b00513886d6f Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:29:39 2025 -0500 q commit 1f5361da88fe3903a1e92a345fa56bb390f69d92 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 commit 511b070cbbcd29b4e784e9a09d58481e50e6e82f Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 13:52:28 2025 -0500 pricey worky commit 964fdd641b63530c59e038ebc7d1e01e9570d75c Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 11:37:29 2025 -0500 prep for pricing service work commit a78c3bcba303c2605b6277c1db33b155abe4db1b Author: zman <joshua.k.rzemien@gmail.com> Date: Wed Feb 5 21:51:22 2025 -0500 more stuff yay commit bd9cfca7a95c89b2140eec57bf52bc84432b9a4e Author: zman <joshua.k.rzemien@gmail.com> Date: Tue Feb 4 22:30:33 2025 -0500 GIGA FIXED EVERYTHING OMG commit 85510a46713e0ac660e70c7befb4e94ccf11912e 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}") |