
- Set up FastAPI project structure - Implement database models and migrations for file metadata - Create file upload endpoint with size validation - Implement file download and listing functionality - Add health check and API information endpoints - Create comprehensive documentation
39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
import datetime
|
|
from sqlalchemy import Column, Integer, String, DateTime
|
|
from uuid import uuid4
|
|
|
|
from app.db.base import Base
|
|
|
|
|
|
class File(Base):
|
|
"""
|
|
SQLAlchemy model for the file table.
|
|
Stores metadata about uploaded files.
|
|
"""
|
|
|
|
__tablename__ = "files"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
filename = Column(String, nullable=False)
|
|
original_filename = Column(String, nullable=False)
|
|
content_type = Column(String, nullable=False)
|
|
file_size = Column(Integer, nullable=False) # in bytes
|
|
file_path = Column(String, nullable=False, unique=True)
|
|
created_at = Column(DateTime, default=datetime.datetime.utcnow)
|
|
updated_at = Column(
|
|
DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow
|
|
)
|
|
|
|
@staticmethod
|
|
def generate_unique_filename(original_filename: str) -> str:
|
|
"""
|
|
Generate a unique filename to prevent collisions and security issues.
|
|
"""
|
|
# Extract file extension from the original filename
|
|
if "." in original_filename:
|
|
extension = original_filename.rsplit(".", 1)[1].lower()
|
|
return f"{uuid4().hex}.{extension}"
|
|
|
|
# No extension in the original filename
|
|
return f"{uuid4().hex}"
|