# 文件类型验证器
from fastapi import UploadFile, HTTPException

def validate_file_type(file: UploadFile) -> str:
    """验证文件类型并返回文件类型"""
    filename = file.filename.lower()
    
    SUPPORTED_RASTER_TYPES = {'.tif', '.tiff', '.img'}
    SUPPORTED_VECTOR_TYPES = {'.geojson', '.json', '.shp'}
    
    file_ext = filename.split('.')[-1]
    
    if f'.{file_ext}' in SUPPORTED_RASTER_TYPES:
        return 'raster'
    elif f'.{file_ext}' in SUPPORTED_VECTOR_TYPES:
        return 'vector'
    else:
        supported_formats = list(SUPPORTED_RASTER_TYPES) + list(SUPPORTED_VECTOR_TYPES)
        raise HTTPException(
            status_code=400,
            detail=f"不支持的文件类型。支持的格式有: {', '.join(supported_formats)}"
        )
