From ee8a8471518f3a3ea49316d8d7d56b357049b648 Mon Sep 17 00:00:00 2001 From: Rodolphe Houdas Date: Thu, 26 Aug 2021 22:13:28 +0100 Subject: [PATCH] :sparkles: Add multi file upload --- Dockerfile | 3 +- openscad_remote.py | 105 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 103 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 8c75732..09f264e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,7 +10,8 @@ COPY requirements.txt /app/requirements.txt RUN pip install -r requirements.txt \ && rm -rf ~/.cache/pip/* \ && mkdir -p /data/upload \ - && mkdir -p /data/build + && mkdir -p /data/build \ + && mkdir -p /data/temp COPY . /app diff --git a/openscad_remote.py b/openscad_remote.py index d6951f1..fe45f25 100644 --- a/openscad_remote.py +++ b/openscad_remote.py @@ -2,23 +2,26 @@ from flask import ( Flask, request, jsonify, - send_file + send_file, + abort ) +from flask_cors import cross_origin import os from uuid import uuid4 import subprocess +import base64 app = Flask(__name__) -@app.route("/") +@app.route("/", methods=["GET"]) def hello_world(): return "

Hello, World!

" @app.route("/upload", methods=['POST']) def upload_file(): - f = request.files['file'] + f = request.files['main-file'] uuid = uuid4() filepath = os.path.join("/data/upload", f"{uuid}.scad") f.save(filepath) @@ -27,7 +30,7 @@ def upload_file(): }) -@app.route("/generate/") +@app.route("/generate/", methods=["GET"]) def generate(id): subprocess.run([ "openscad", @@ -37,5 +40,99 @@ def generate(id): return send_file(f"/data/build/{id}.stl") +@app.route("/generate", methods=['POST']) +@cross_origin() +def make_model(): + content = request.json + parameters = content["parameters"] if "parameters" in content else "{}" + scad = content["scad"] + uuid = uuid4() + with open(f"/data/temp/{uuid}.scad", "wb") as file: + file.write(base64.b64decode(scad)) + with open(f"/data/temp/{uuid}.json", "w") as file: + file.write(parameters) + subprocess.run([ + "openscad", + # "-p", f"/data/temp/{uuid}.json" + "-o", f"/data/build/{uuid}.stl", + f"/data/temp/{uuid}.scad" + ]) + return send_file(f"/data/build/{uuid}.stl") + + +@app.route("/", methods=['POST']) +def upload_files(): + project_id = str(uuid4()) + + main_file = get_and_save_file(request, "main-file", project_id) + + if main_file is False: + abort(400) + + project_folder = f"/data/build/{project_id}" + if not os.path.exists(project_folder): + os.makedirs(project_folder) + + file_name = ".".join(main_file.split("/")[-1].split(".")[:-1]) + result_file_path = f"{project_folder}/{file_name}.stl" + + command = [ + "openscad", + "-o", result_file_path + ] + + param_file = get_and_save_file(request, "param-file", project_id) + + if param_file is not False: + command.extend(["-p", param_file]) + + keys = list(request.files.keys()) + keys.remove("main-file") + + try: + keys.remove("param-file") + except ValueError: + print("List does not contain value") + + # Add other files + for key in keys: + get_and_save_file(request, key, project_id) + + # Add main file at the end of the command + command.append(main_file) + + print(command) + + subprocess.run(command, cwd=project_folder) + return send_file(result_file_path) + + +def get_and_save_file(request, fileId, projectId): + # Check if the file we're requesting is in the request + if fileId not in request.files: + return False + + project_path = os.path.join("/data/upload", projectId) + + # Check if folder for project exist, create it if it doesn't exist + if not os.path.exists(project_path): + os.makedirs(project_path) + + f = request.files[fileId] + + # Check if the uploaded file is a file or an empty field + if f.filename == "": + return False + + file_path = os.path.join(project_path, f.filename) + + # Check if the file exists already + if os.path.exists(file_path): + return False + + f.save(file_path) + return file_path + + if __name__ == "__main__": app.run()