feat(dwg): switch to ODA File Converter (libredwg too unstable)
ODA File Converter handles all DWG versions reliably. Uses xvfb for headless Qt operation in Docker. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
+32
-26
@@ -1,6 +1,8 @@
|
||||
"""Minimal DWG-to-DXF conversion microservice via libredwg."""
|
||||
"""DWG-to-DXF conversion microservice via ODA File Converter."""
|
||||
|
||||
import glob
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import uuid
|
||||
@@ -11,15 +13,16 @@ app = Flask(__name__)
|
||||
|
||||
MAX_FILE_SIZE = 100 * 1024 * 1024 # 100 MB
|
||||
|
||||
# ODA File Converter installs here on Ubuntu
|
||||
ODA_BIN = "/usr/bin/ODAFileConverter"
|
||||
|
||||
|
||||
@app.route("/health", methods=["GET"])
|
||||
def health():
|
||||
"""Health check — verifies dwg2dxf binary is callable."""
|
||||
try:
|
||||
subprocess.run(["dwg2dxf", "--version"], capture_output=True, timeout=5)
|
||||
return jsonify({"status": "ok", "dwg2dxf": "available"}), 200
|
||||
except Exception as e:
|
||||
return jsonify({"status": "error", "detail": str(e)}), 503
|
||||
"""Health check — verifies ODA File Converter is installed."""
|
||||
if os.path.isfile(ODA_BIN) and os.access(ODA_BIN, os.X_OK):
|
||||
return jsonify({"status": "ok", "converter": "ODAFileConverter"}), 200
|
||||
return jsonify({"status": "error", "detail": "ODAFileConverter not found"}), 503
|
||||
|
||||
|
||||
@app.route("/convert", methods=["POST"])
|
||||
@@ -38,11 +41,13 @@ def convert():
|
||||
c if c.isalnum() or c in "._-" else "_" for c in original_name
|
||||
)
|
||||
|
||||
tmp_dir = os.path.join(tempfile.gettempdir(), f"dwg-{uuid.uuid4().hex}")
|
||||
os.makedirs(tmp_dir, exist_ok=True)
|
||||
work_dir = os.path.join(tempfile.gettempdir(), f"dwg-{uuid.uuid4().hex}")
|
||||
input_dir = os.path.join(work_dir, "in")
|
||||
output_dir = os.path.join(work_dir, "out")
|
||||
os.makedirs(input_dir, exist_ok=True)
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
|
||||
input_path = os.path.join(tmp_dir, safe_name)
|
||||
output_path = input_path.rsplit(".", 1)[0] + ".dxf"
|
||||
input_path = os.path.join(input_dir, safe_name)
|
||||
|
||||
try:
|
||||
uploaded.save(input_path)
|
||||
@@ -51,21 +56,30 @@ def convert():
|
||||
if file_size > MAX_FILE_SIZE:
|
||||
return jsonify({"error": f"File too large ({file_size} bytes)."}), 413
|
||||
|
||||
# ODA File Converter: input_dir output_dir version type recurse audit
|
||||
# ACAD2018 = output DXF version, DXF = output format, 0 = no recurse, 1 = audit
|
||||
result = subprocess.run(
|
||||
["dwg2dxf", input_path],
|
||||
[
|
||||
"xvfb-run", "--auto-servernum", "--server-args=-screen 0 1x1x24",
|
||||
ODA_BIN, input_dir, output_dir, "ACAD2018", "DXF", "0", "1",
|
||||
],
|
||||
capture_output=True,
|
||||
timeout=120,
|
||||
)
|
||||
|
||||
if not os.path.exists(output_path):
|
||||
# Find the output DXF file
|
||||
dxf_files = glob.glob(os.path.join(output_dir, "*.dxf"))
|
||||
if not dxf_files:
|
||||
stderr = result.stderr.decode("utf-8", errors="replace")
|
||||
return jsonify({
|
||||
"error": f"Conversion failed: {stderr or 'no output file'}"
|
||||
}), 500
|
||||
stdout = result.stdout.decode("utf-8", errors="replace")
|
||||
detail = stderr or stdout or "No output file produced"
|
||||
return jsonify({"error": f"Conversion failed: {detail}"}), 500
|
||||
|
||||
dxf_path = dxf_files[0]
|
||||
dxf_name = safe_name.rsplit(".", 1)[0] + ".dxf"
|
||||
|
||||
return send_file(
|
||||
output_path,
|
||||
dxf_path,
|
||||
mimetype="application/dxf",
|
||||
as_attachment=True,
|
||||
download_name=dxf_name,
|
||||
@@ -78,15 +92,7 @@ def convert():
|
||||
return jsonify({"error": f"Unexpected error: {str(e)}"}), 500
|
||||
|
||||
finally:
|
||||
for f in os.listdir(tmp_dir):
|
||||
try:
|
||||
os.unlink(os.path.join(tmp_dir, f))
|
||||
except OSError:
|
||||
pass
|
||||
try:
|
||||
os.rmdir(tmp_dir)
|
||||
except OSError:
|
||||
pass
|
||||
shutil.rmtree(work_dir, ignore_errors=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user