| 535 | @router.post("/parseExcel", response_model=None, summary=f"{PLACEHOLDER_PREFIX}ds_parse_excel") |
| 536 | @require_permissions(permission=SqlbotPermission(role=['ws_admin'])) |
| 537 | async def parse_excel(file: UploadFile = File(..., description=f"{PLACEHOLDER_PREFIX}ds_excel")): |
| 538 | ALLOWED_EXTENSIONS = {".xlsx", ".xls", ".csv"} |
| 539 | if not file.filename.lower().endswith(tuple(ALLOWED_EXTENSIONS)): |
| 540 | raise HTTPException(400, "Only support .xlsx/.xls/.csv") |
| 541 | |
| 542 | os.makedirs(path, exist_ok=True) |
| 543 | filename = f"{file.filename.split('.')[0].split('/')[-1]}_{hashlib.sha256(uuid.uuid4().bytes).hexdigest()[:10]}.{file.filename.split('.')[-1]}" |
| 544 | save_path = os.path.join(path, filename) |
| 545 | with open(save_path, "wb") as f: |
| 546 | f.write(await file.read()) |
| 547 | |
| 548 | def inner(): |
| 549 | sheets_data = parse_excel_preview(save_path) |
| 550 | return { |
| 551 | "filePath": filename, |
| 552 | "data": sheets_data |
| 553 | } |
| 554 | |
| 555 | return await asyncio.to_thread(inner) |
| 556 | |
| 557 | |
| 558 | @router.post("/importToDb", response_model=None, summary=f"{PLACEHOLDER_PREFIX}ds_import_to_db") |