Enhance README and backend functionality for dashboard access and image upload validation

- Added a new section in README detailing the dashboard access method, features, and troubleshooting tips.
- Updated backend server startup message to allow access from mobile/emulator.
- Refactored image upload handling in the report route to validate file types and extensions, ensuring only supported image formats are accepted.
- Adjusted upload directory path for consistency.
This commit is contained in:
2025-09-26 19:54:07 +08:00
parent 11ea469b6d
commit 2a46ecb7d2
5 changed files with 55 additions and 80 deletions

View File

@@ -11,7 +11,7 @@ router = APIRouter()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
UPLOAD_DIR = "app/static/uploads"
UPLOAD_DIR = "static/uploads"
os.makedirs(UPLOAD_DIR, exist_ok=True)
@router.post("/report")
@@ -33,8 +33,23 @@ async def report_issue(
raise HTTPException(status_code=404, detail=f"User with id {user_id} not found")
logger.debug(f"User found: {user.name} ({user.email})")
# Validate file type
allowed_extensions = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp'}
allowed_content_types = {
'image/jpeg', 'image/png', 'image/gif', 'image/bmp', 'image/webp',
'application/octet-stream' # Some cameras/mobile devices use this
}
file_ext = os.path.splitext(image.filename.lower())[1]
if file_ext not in allowed_extensions:
logger.error(f"Invalid file extension: {file_ext}")
raise HTTPException(status_code=400, detail="Only image files are allowed")
if image.content_type not in allowed_content_types:
logger.error(f"Invalid content type: {image.content_type}")
raise HTTPException(status_code=400, detail="Invalid file type")
# Save uploaded image
file_ext = os.path.splitext(image.filename)[1]
filename = f"{uuid.uuid4()}{file_ext}"
file_path = os.path.join(UPLOAD_DIR, filename)
try:

View File

@@ -1,74 +0,0 @@
import uuid
from sqlalchemy import Column, String, Float, Enum, DateTime, ForeignKey, Index
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from app.database import Base
import enum
# ----------------------
# Enums
# ----------------------
class TicketStatus(str, enum.Enum):
NEW = "New"
IN_PROGRESS = "In Progress"
FIXED = "Fixed"
class SeverityLevel(str, enum.Enum):
LOW = "Low"
MEDIUM = "Medium"
HIGH = "High"
NA = "N/A"
# ----------------------
# User Model
# ----------------------
class User(Base):
__tablename__ = "users"
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()), index=True)
name = Column(String, nullable=False)
email = Column(String, unique=True, nullable=False)
tickets = relationship("Ticket", back_populates="user", cascade="all, delete-orphan")
def __repr__(self):
return f"<User(id={self.id}, name={self.name}, email={self.email})>"
# ----------------------
# Ticket Model
# ----------------------
class Ticket(Base):
__tablename__ = "tickets"
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()), index=True)
user_id = Column(String, ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
image_path = Column(String, nullable=False)
category = Column(String, nullable=False)
severity = Column(Enum(SeverityLevel), nullable=False, default=SeverityLevel.NA)
description = Column(String, default="")
status = Column(Enum(TicketStatus), nullable=False, default=TicketStatus.NEW)
latitude = Column(Float, nullable=False)
longitude = Column(Float, nullable=False)
created_at = Column(DateTime(timezone=True), server_default=func.now())
updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())
user = relationship("User", back_populates="tickets")
__table_args__ = (
Index("idx_category_status", "category", "status"),
)
def __repr__(self):
return f"<Ticket(id={self.id}, category={self.category}, severity={self.severity}, status={self.status}, user_id={self.user_id})>"
# ----------------------
# Ticket Audit Model
# ----------------------
class TicketAudit(Base):
__tablename__ = "ticket_audit"
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
ticket_id = Column(String, ForeignKey("tickets.id", ondelete="CASCADE"))
old_status = Column(Enum(TicketStatus))
new_status = Column(Enum(TicketStatus))
updated_at = Column(DateTime(timezone=True), server_default=func.now())