✨ Add multi file upload
This commit is contained in:
parent
359d396123
commit
ee8a847151
|
@ -10,7 +10,8 @@ COPY requirements.txt /app/requirements.txt
|
||||||
RUN pip install -r requirements.txt \
|
RUN pip install -r requirements.txt \
|
||||||
&& rm -rf ~/.cache/pip/* \
|
&& rm -rf ~/.cache/pip/* \
|
||||||
&& mkdir -p /data/upload \
|
&& mkdir -p /data/upload \
|
||||||
&& mkdir -p /data/build
|
&& mkdir -p /data/build \
|
||||||
|
&& mkdir -p /data/temp
|
||||||
|
|
||||||
COPY . /app
|
COPY . /app
|
||||||
|
|
||||||
|
|
|
@ -2,23 +2,26 @@ from flask import (
|
||||||
Flask,
|
Flask,
|
||||||
request,
|
request,
|
||||||
jsonify,
|
jsonify,
|
||||||
send_file
|
send_file,
|
||||||
|
abort
|
||||||
)
|
)
|
||||||
|
from flask_cors import cross_origin
|
||||||
import os
|
import os
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import base64
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/", methods=["GET"])
|
||||||
def hello_world():
|
def hello_world():
|
||||||
return "<p>Hello, World!</p>"
|
return "<p>Hello, World!</p>"
|
||||||
|
|
||||||
|
|
||||||
@app.route("/upload", methods=['POST'])
|
@app.route("/upload", methods=['POST'])
|
||||||
def upload_file():
|
def upload_file():
|
||||||
f = request.files['file']
|
f = request.files['main-file']
|
||||||
uuid = uuid4()
|
uuid = uuid4()
|
||||||
filepath = os.path.join("/data/upload", f"{uuid}.scad")
|
filepath = os.path.join("/data/upload", f"{uuid}.scad")
|
||||||
f.save(filepath)
|
f.save(filepath)
|
||||||
|
@ -27,7 +30,7 @@ def upload_file():
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@app.route("/generate/<id>")
|
@app.route("/generate/<id>", methods=["GET"])
|
||||||
def generate(id):
|
def generate(id):
|
||||||
subprocess.run([
|
subprocess.run([
|
||||||
"openscad",
|
"openscad",
|
||||||
|
@ -37,5 +40,99 @@ def generate(id):
|
||||||
return send_file(f"/data/build/{id}.stl")
|
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__":
|
if __name__ == "__main__":
|
||||||
app.run()
|
app.run()
|
||||||
|
|
Loading…
Reference in a new issue