Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions backend/apps/datasource/api/datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from ..crud.table import get_tables_by_ds_id
from ..models.datasource import CoreDatasource, CreateDatasource, TableObj, CoreTable, CoreField, FieldObj, \
TableSchemaResponse, ColumnSchemaResponse, PreviewResponse, ImportRequest
from ..utils.excel import parse_excel_preview, read_import_dataframe
from ..utils.excel import is_csv_file, parse_excel_preview, read_import_dataframe

router = APIRouter(tags=["Datasource"], prefix="/datasource")
path = settings.EXCEL_PATH
Expand Down Expand Up @@ -363,7 +363,7 @@ async def upload_excel(trans: Trans, session: SessionDep,
def inner():
sheets = []
engine = get_engine_conn()
if filename.endswith(".csv"):
if is_csv_file(filename):
df = pd.read_csv(save_path, engine='c')
tableName = f"sheet1_{hashlib.sha256(uuid.uuid4().bytes).hexdigest()[:10]}"
sheets.append({"tableName": tableName, "tableComment": ""})
Expand Down Expand Up @@ -633,7 +633,7 @@ def inner():
field_mapping = {f.fieldName: f.fieldType for f in fields}
try:
df = read_import_dataframe(save_path, sheet_name, field_mapping)
if save_path.endswith(".csv"):
if is_csv_file(save_path):
sheet_name = "Sheet1"
except Exception as e:
raise HTTPException(500, f"{trans('i18n_ds_upload_error')}: {str(e)}")
Expand Down
8 changes: 6 additions & 2 deletions backend/apps/datasource/utils/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
}


def is_csv_file(file_path: str) -> bool:
return file_path.lower().endswith(".csv")


def infer_field_type(dtype) -> str:
dtype_str = str(dtype)
return FIELD_TYPE_MAP.get(dtype_str, 'string')
Expand Down Expand Up @@ -51,7 +55,7 @@ def read_import_dataframe(save_path: str, sheet_name: str, field_mapping: dict):
column: 'object' if field_type == 'int' else USER_TYPE_TO_PANDAS.get(field_type, 'string')
for column, field_type in field_mapping.items()
}
if save_path.endswith('.csv'):
if is_csv_file(save_path):
df = pd.read_csv(save_path, engine='c', dtype=dtype_dict)
else:
df = pd.read_excel(save_path, sheet_name=sheet_name, engine='calamine', dtype=dtype_dict)
Expand All @@ -68,7 +72,7 @@ def read_import_dataframe(save_path: str, sheet_name: str, field_mapping: dict):

def parse_excel_preview(save_path: str, max_rows: int = 10):
sheets_data = []
if save_path.endswith(".csv"):
if is_csv_file(save_path):
df = pd.read_csv(save_path, engine='c')
fields = []
for col in df.columns:
Expand Down
39 changes: 39 additions & 0 deletions backend/tests/test_datasource_csv_extension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import pandas as pd
import pytest

from apps.datasource.utils.excel import parse_excel_preview, read_import_dataframe


@pytest.fixture(params=["csv", "CSV", "CsV"])
def stored_csv(tmp_path, request):
path = tmp_path / f"sales_0123456789.{request.param}"
path.write_text("region,units\nEast,3\nWest,7\n", encoding="utf-8")
return str(path)


def test_preview_csv_with_any_extension_case(stored_csv):
[sheet] = parse_excel_preview(stored_csv)
assert sheet["sheetName"] == "Sheet1"
assert sheet["rows"] == 2
assert sheet["data"] == [
{"region": "East", "units": 3},
{"region": "West", "units": 7},
]


def test_import_stored_csv_with_any_extension_case(stored_csv):
frame = read_import_dataframe(
stored_csv, "Sheet1", {"region": "string", "units": "int"}
)
assert frame["region"].tolist() == ["East", "West"]
assert frame["units"].tolist() == [3, 7]


def test_excel_reader_preserves_named_sheet(tmp_path):
path = tmp_path / "sales.CSV.xlsx"
pd.DataFrame({"units": [3]}).to_excel(path, sheet_name="Sales", index=False)

[sheet] = parse_excel_preview(str(path))
assert sheet["sheetName"] == "Sales"
frame = read_import_dataframe(str(path), "Sales", {"units": "int"})
assert frame["units"].tolist() == [3]
12 changes: 12 additions & 0 deletions backend/tests/test_datasource_upload_path_traversal.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,15 @@ def test_normal_names_keep_their_own_stem_and_extension(parse_harness):
stored = response.json()["filePath"]
assert stored.startswith("a.b_") and stored.endswith(".xlsx"), stored
assert (parse_harness.upload_dir / stored).is_file()


def test_legacy_upload_imports_mixed_case_csv(harness, monkeypatch):
insert = Mock()
monkeypatch.setattr(datasource, "insert_pg", insert)
response = harness.client.post(
UPLOAD_URL, files={"file": ("orders.CsV", b"units\n3\n7\n", "text/csv")}
)
assert response.status_code == 200, response.text
insert.assert_called_once()
frame = insert.call_args.args[0]
assert frame.to_dict("records") == [{"units": 3}, {"units": 7}]