def upload(): token = request.headers.get("x-api-key", None) if not token: return abort(403) user_uuid = None try: decoded = decode_token(token) user_uuid = decoded["identity"] except Exception as e: abort(403) if decoded["user_claims"].get("organization_uuid") is None: abort(403) if "file" not in request.files: return abort(400) incoming = request.files["file"] if incoming.filename == "": return abort(400) if not re.search(r"\.gco(de)?$", incoming.filename): return abort(415) try: org_uuid = decoded["user_claims"]["organization_uuid"] saved = files.save(org_uuid, incoming, request.form.get("path", "")) gcode_id = gcodes.add_gcode( uuid=guid.uuid4(), path=saved["path"], filename=saved["filename"], display=saved["display"], absolute_path=saved["absolute_path"], organization_uuid=org_uuid, size=saved["size"], user_uuid=user_uuid, ) analyze_gcode.delay(gcode_id) except (IOError, OSError) as e: return abort(500, "upload IOError;\t" + str(e)) return ( jsonify({ "files": { "local": { "name": saved["filename"], "display": saved["display"], "path": saved["absolute_path"], "origin": "local", } } }), 201, )
def gcode_create(org_uuid): if "file" not in request.files: return abort(make_response(jsonify(message="No file uploaded"), 400)) incoming = request.files["file"] if incoming.filename == "": return abort( make_response(jsonify(message="Uploaded file has to have a name"), 400)) if not re.search(r"\.gco(de)?$", incoming.filename): return abort( make_response( jsonify(message="Uploaded file does not look like gcode"), 415)) try: saved = files.save(org_uuid, incoming, request.form.get("path", "/")) gcode_id = gcodes.add_gcode( uuid=guid.uuid4(), path=saved["path"], filename=saved["filename"], display=saved["display"], absolute_path=saved["absolute_path"], size=saved["size"], user_uuid=get_current_user()["uuid"], organization_uuid=org_uuid, ) analyze_gcode.delay(gcode_id) except (IOError, OSError) as e: return abort(make_response(jsonify(message=str(e)), 500)) return ( jsonify( make_gcode_response({ "uuid": gcode_id, "organization_uuid": org_uuid, "user_uuid": get_current_user()["uuid"], "username": get_current_user()["username"], "path": saved["path"], "filename": saved["filename"], "display": saved["display"], "absolute_path": saved["absolute_path"], "uploaded": datetime.datetime.now(), "size": saved["size"], })), 201, )