|
@@ -445,6 +445,7 @@ async def update_order(
|
|
|
@router.post("/{order_id}/attach-file")
|
|
@router.post("/{order_id}/attach-file")
|
|
|
async def admin_attach_file(
|
|
async def admin_attach_file(
|
|
|
order_id: int,
|
|
order_id: int,
|
|
|
|
|
+ background_tasks: BackgroundTasks,
|
|
|
file: UploadFile = File(...),
|
|
file: UploadFile = File(...),
|
|
|
admin: dict = Depends(require_admin)
|
|
admin: dict = Depends(require_admin)
|
|
|
):
|
|
):
|
|
@@ -459,31 +460,18 @@ async def admin_attach_file(
|
|
|
sha256_hash.update(chunk)
|
|
sha256_hash.update(chunk)
|
|
|
buffer.write(chunk)
|
|
buffer.write(chunk)
|
|
|
|
|
|
|
|
- preview_path = None
|
|
|
|
|
- db_preview_path = None
|
|
|
|
|
- if file_path.lower().endswith(".stl"):
|
|
|
|
|
- preview_filename = f"{uuid.uuid4()}.png"
|
|
|
|
|
- preview_path = os.path.join(config.PREVIEW_DIR, preview_filename)
|
|
|
|
|
- db_preview_path = f"uploads/previews/{preview_filename}"
|
|
|
|
|
- preview_utils.generate_stl_preview(file_path, preview_path)
|
|
|
|
|
-
|
|
|
|
|
- filament_g = None
|
|
|
|
|
- print_time = None
|
|
|
|
|
- if file_path.lower().endswith(".stl"):
|
|
|
|
|
- result = slicer_utils.slice_model(file_path)
|
|
|
|
|
- if result and result.get('success'):
|
|
|
|
|
- filament_g = result.get('filament_g')
|
|
|
|
|
- print_time = result.get('print_time_str')
|
|
|
|
|
-
|
|
|
|
|
- query = "INSERT INTO order_files (order_id, filename, file_path, file_size, quantity, file_hash, print_time, filament_g, preview_path) VALUES (%s, %s, %s, %s, 1, %s, %s, %s, %s)"
|
|
|
|
|
- f_id = db.execute_commit(query, (order_id, file.filename, db_file_path, file.size, sha256_hash.hexdigest(), print_time, filament_g, db_preview_path))
|
|
|
|
|
|
|
+ query = "INSERT INTO order_files (order_id, filename, file_path, file_size, quantity, file_hash, print_time, filament_g, preview_path) VALUES (%s, %s, %s, %s, 1, %s, NULL, NULL, NULL)"
|
|
|
|
|
+ f_id = db.execute_commit(query, (order_id, file.filename, db_file_path, file.size, sha256_hash.hexdigest()))
|
|
|
|
|
+
|
|
|
|
|
+ # Run heavy processing in background to avoid worker timeouts
|
|
|
|
|
+ background_tasks.add_task(order_processing.process_single_file_async, f_id)
|
|
|
|
|
|
|
|
# NOTIFY USER VIA WEBSOCKET
|
|
# NOTIFY USER VIA WEBSOCKET
|
|
|
order_info = db.execute_query("SELECT user_id FROM orders WHERE id = %s", (order_id,))
|
|
order_info = db.execute_query("SELECT user_id FROM orders WHERE id = %s", (order_id,))
|
|
|
if order_info:
|
|
if order_info:
|
|
|
await global_manager.notify_order_update(order_info[0]['user_id'], order_id)
|
|
await global_manager.notify_order_update(order_info[0]['user_id'], order_id)
|
|
|
|
|
|
|
|
- return {"file_id": f_id, "filename": file.filename, "preview_path": db_preview_path, "filament_g": filament_g, "print_time": print_time}
|
|
|
|
|
|
|
+ return {"file_id": f_id, "filename": file.filename, "status": "processing"}
|
|
|
|
|
|
|
|
@router.delete("/{order_id}/files/{file_id}")
|
|
@router.delete("/{order_id}/files/{file_id}")
|
|
|
async def admin_delete_file(
|
|
async def admin_delete_file(
|
|
@@ -522,6 +510,24 @@ async def admin_delete_file(
|
|
|
await global_manager.notify_order_update(order_info[0]['user_id'], order_id)
|
|
await global_manager.notify_order_update(order_info[0]['user_id'], order_id)
|
|
|
|
|
|
|
|
return {"status": "success"}
|
|
return {"status": "success"}
|
|
|
|
|
+
|
|
|
|
|
+@router.patch("/{order_id}/files/{file_id}")
|
|
|
|
|
+async def admin_update_file(
|
|
|
|
|
+ order_id: int,
|
|
|
|
|
+ file_id: int,
|
|
|
|
|
+ data: dict,
|
|
|
|
|
+ admin: dict = Depends(require_admin)
|
|
|
|
|
+):
|
|
|
|
|
+ if "quantity" in data:
|
|
|
|
|
+ db.execute_commit("UPDATE order_files SET quantity = %s WHERE id = %s AND order_id = %s", (data['quantity'], file_id, order_id))
|
|
|
|
|
+
|
|
|
|
|
+ # Notify user/admin via WS
|
|
|
|
|
+ order_info = db.execute_query("SELECT user_id FROM orders WHERE id = %s", (order_id,))
|
|
|
|
|
+ if order_info:
|
|
|
|
|
+ await global_manager.notify_order_update(order_info[0]['user_id'], order_id)
|
|
|
|
|
+
|
|
|
|
|
+ return {"status": "success"}
|
|
|
|
|
+
|
|
|
|
|
|
|
|
class OrderItemSchema(BaseModel):
|
|
class OrderItemSchema(BaseModel):
|
|
|
description: str
|
|
description: str
|