|
@@ -1,16 +1,17 @@
|
|
|
import os
|
|
import os
|
|
|
import uuid
|
|
import uuid
|
|
|
import hashlib
|
|
import hashlib
|
|
|
-from fastapi import APIRouter, UploadFile, File
|
|
|
|
|
|
|
+from fastapi import APIRouter, UploadFile, File, BackgroundTasks
|
|
|
from typing import List
|
|
from typing import List
|
|
|
import db
|
|
import db
|
|
|
import config
|
|
import config
|
|
|
import preview_utils
|
|
import preview_utils
|
|
|
|
|
+from services import order_processing
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/files", tags=["files"])
|
|
router = APIRouter(prefix="/files", tags=["files"])
|
|
|
|
|
|
|
|
@router.post("/upload")
|
|
@router.post("/upload")
|
|
|
-async def upload_files(files: List[UploadFile] = File(...)):
|
|
|
|
|
|
|
+async def upload_files(background_tasks: BackgroundTasks, files: List[UploadFile] = File(...)):
|
|
|
if not files: return {"uploaded": []}
|
|
if not files: return {"uploaded": []}
|
|
|
uploaded_data = []
|
|
uploaded_data = []
|
|
|
for file in files:
|
|
for file in files:
|
|
@@ -58,6 +59,10 @@ async def upload_files(files: List[UploadFile] = File(...)):
|
|
|
query = "INSERT INTO order_files (order_id, filename, file_path, file_size, quantity, file_hash, print_time, filament_g, preview_path) VALUES (NULL, %s, %s, %s, 1, %s, %s, %s, %s)"
|
|
query = "INSERT INTO order_files (order_id, filename, file_path, file_size, quantity, file_hash, print_time, filament_g, preview_path) VALUES (NULL, %s, %s, %s, 1, %s, %s, %s, %s)"
|
|
|
f_id = db.execute_commit(query, (file.filename, db_file_path, file.size, file_hash, print_time, filament_g, db_preview_path))
|
|
f_id = db.execute_commit(query, (file.filename, db_file_path, file.size, file_hash, print_time, filament_g, db_preview_path))
|
|
|
|
|
|
|
|
|
|
+ # If preview/slicing didn't happen (not cached and not sync), run in background
|
|
|
|
|
+ if not print_time or not db_preview_path:
|
|
|
|
|
+ background_tasks.add_task(order_processing.process_single_file_async, f_id)
|
|
|
|
|
+
|
|
|
uploaded_data.append({
|
|
uploaded_data.append({
|
|
|
"id": f_id, "filename": file.filename, "size": file.size,
|
|
"id": f_id, "filename": file.filename, "size": file.size,
|
|
|
"print_time": print_time, "filament_g": filament_g, "preview_path": db_preview_path
|
|
"print_time": print_time, "filament_g": filament_g, "preview_path": db_preview_path
|