|
7 | 7 | import flask_cors |
8 | 8 | from .. import geode_functions |
9 | 9 | import werkzeug |
| 10 | +import uuid |
10 | 11 |
|
11 | 12 |
|
12 | 13 | routes = flask.Blueprint("routes", __name__) |
@@ -204,3 +205,55 @@ def geode_objects_and_output_extensions(): |
204 | 205 | {"geode_objects_and_output_extensions": geode_objects_and_output_extensions}, |
205 | 206 | 200, |
206 | 207 | ) |
| 208 | + |
| 209 | + |
| 210 | +with open( |
| 211 | + os.path.join(schemas, "save_viewable_file.json"), |
| 212 | + "r", |
| 213 | +) as file: |
| 214 | + save_viewable_file_json = json.load(file) |
| 215 | + |
| 216 | + |
| 217 | +@routes.route( |
| 218 | + save_viewable_file_json["route"], |
| 219 | + methods=save_viewable_file_json["methods"], |
| 220 | +) |
| 221 | +def save_viewable_file(): |
| 222 | + UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"] |
| 223 | + secure_filename = werkzeug.utils.secure_filename(flask.request.json["filename"]) |
| 224 | + file_path = os.path.abspath(os.path.join(UPLOAD_FOLDER, secure_filename)) |
| 225 | + data = geode_functions.load(flask.request.json["input_geode_object"], file_path) |
| 226 | + generated_id = str(uuid.uuid4()).replace("-", "") |
| 227 | + |
| 228 | + if geode_functions.is_viewable(flask.request.json["input_geode_object"]): |
| 229 | + name = data.name() |
| 230 | + else: |
| 231 | + name = flask.request.json["filename"] |
| 232 | + |
| 233 | + native_extension = data.native_extension() |
| 234 | + |
| 235 | + absolute_native_file_path = os.path.join( |
| 236 | + UPLOAD_FOLDER, generated_id + "." + native_extension |
| 237 | + ) |
| 238 | + |
| 239 | + saved_viewable_file_path = geode_functions.save_viewable( |
| 240 | + flask.request.json["input_geode_object"], data, UPLOAD_FOLDER, generated_id |
| 241 | + ) |
| 242 | + geode_functions.save( |
| 243 | + flask.request.json["input_geode_object"], |
| 244 | + data, |
| 245 | + UPLOAD_FOLDER, |
| 246 | + generated_id + "." + native_extension, |
| 247 | + ) |
| 248 | + |
| 249 | + native_file_name = os.path.basename(absolute_native_file_path) |
| 250 | + viewable_file_name = os.path.basename(saved_viewable_file_path) |
| 251 | + return flask.make_response( |
| 252 | + { |
| 253 | + "name": name, |
| 254 | + "native_file_name": native_file_name, |
| 255 | + "viewable_file_name": viewable_file_name, |
| 256 | + "id": generated_id, |
| 257 | + }, |
| 258 | + 200, |
| 259 | + ) |
0 commit comments