add data source

This commit is contained in:
qixinbo
2026-03-15 20:48:40 +08:00
parent f1db709aae
commit a7f1c77787
10 changed files with 119 additions and 29 deletions
+20 -6
View File
@@ -11,9 +11,10 @@ upload_dir.mkdir(parents=True, exist_ok=True)
@router.post("/upload/file")
async def upload_file(file: UploadFile = File(...)):
allowed_extensions = ('.csv', '.xls', '.xlsx')
if not file.filename.lower().endswith(allowed_extensions):
raise HTTPException(status_code=400, detail="Invalid file type. Only CSV and Excel files allowed.")
allowed_extensions = ('.csv', '.xls', '.xlsx', '.parquet', '.db', '.sqlite', '.sqlite3')
filename_lower = file.filename.lower()
if not filename_lower.endswith(allowed_extensions):
raise HTTPException(status_code=400, detail="Invalid file type. Allowed: CSV, Excel, Parquet, SQLite.")
try:
content = await file.read()
@@ -29,11 +30,24 @@ async def upload_file(file: UploadFile = File(...)):
file_obj.seek(0)
try:
if file.filename.lower().endswith('.csv'):
if filename_lower.endswith('.csv'):
df = pd.read_csv(file_obj)
else:
elif filename_lower.endswith(('.xls', '.xlsx')):
df = pd.read_excel(file_obj)
elif filename_lower.endswith('.parquet'):
df = pd.read_parquet(file_obj)
elif filename_lower.endswith(('.db', '.sqlite', '.sqlite3')):
# For SQLite, we don't load into DF immediately for analysis here
# Just return success
return {
"filename": unique_filename,
"url": file_url,
"rows": 0,
"columns": [],
"summary": "SQLite database uploaded"
}
# For DF supported types
duckdb_conn = duckdb.connect(database=':memory:')
duckdb_conn.register('uploaded_file', df)
summary = duckdb_conn.execute("DESCRIBE uploaded_file").fetchall()