From 4e25bd8c271cd2e6fda11b9534eb8a453274d0a8 Mon Sep 17 00:00:00 2001 From: Vedant Nandoskar Date: Tue, 14 Apr 2020 07:05:38 +0530 Subject: [PATCH] Added route for deleting slide from file system (#25) * Added route for deleting slide * removed incorrect comment * Secured delete slide route * Removed auth checker from SlideLoader * removed globals for auth checker Co-Authored-By: Ryan Birmingham * Fixed typo Co-Authored-By: Ryan Birmingham Co-authored-by: Ryan Birmingham --- Dockerfile | 4 ++-- SlideServer.py | 35 ++++++++++++++++++++++++++++++++--- requirements.txt | 2 +- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index b04352d..c8b43a3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -26,8 +26,8 @@ RUN pip3 install -r requirements.txt EXPOSE 4000 #debug/dev only -#ENV FLASK_APP SlideServer.py -#CMD python -m flask run --host=0.0.0.0 --port=4000 +# ENV FLASK_APP SlideServer.py +# CMD python -m flask run --host=0.0.0.0 --port=4000 #prod only CMD gunicorn -w 4 -b 0.0.0.0:4000 SlideServer:app --timeout 400 diff --git a/SlideServer.py b/SlideServer.py index 91b394b..7b7ebaf 100644 --- a/SlideServer.py +++ b/SlideServer.py @@ -6,6 +6,10 @@ import string import sys import pyvips +from os import listdir +from os.path import isfile, join + + import urllib import flask import flask_cors @@ -14,6 +18,7 @@ import dev_utils import requests + try: from io import BytesIO except ImportError: @@ -28,7 +33,7 @@ app.config['TOKEN_SIZE'] = 10 app.config['SECRET_KEY'] = os.urandom(24) -ALLOWED_EXTENSIONS = set(['svs', 'tif', 'tiff', 'vms', 'vmu', 'ndpi', 'scn', 'mrxs', 'bif', 'svslide']) +ALLOWED_EXTENSIONS = set(['svs', 'tif', 'tiff', 'vms', 'vmu', 'ndpi', 'scn', 'mrxs', 'bif', 'svslide']) def allowed_file(filename): @@ -131,6 +136,29 @@ def finish_upload(token): # get info associated with token # move the file out of temp to upload dir +# Delete the requested slide +@app.route('/slide/delete', methods=['POST']) +def slide_delete(): + body = flask.request.get_json() + + if not body: + return flask.Response(json.dumps({"error": "Missing JSON body"}), status=400) + + filename = body['filename'] + if filename and allowed_file(filename): + filename = secure_filename(filename) + filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) + if os.path.isfile(filepath): + os.remove(os.path.join(app.config['UPLOAD_FOLDER'], filename)) + return flask.Response(json.dumps({"deleted": filename, "success": True})) + else: + return flask.Response(json.dumps({"error": "File with name '" + filename + "' does not exist"}), status=400) + + else: + return flask.Response(json.dumps({"error": "Invalid filename"}), status=400) + + # check for file if it exists or not + # delete the file @app.route("/test", methods=['GET']) def testRoute(): @@ -152,13 +180,13 @@ def singleThumb(filepath): def multiSlide(filepathlist): return json.dumps(dev_utils.getMetadataList(json.loads(filepathlist), app.config['UPLOAD_FOLDER'])) + @app.route("/getSlide/") def getSlide(image_name): if(os.path.isfile("/images/"+image_name)): return flask.send_from_directory(app.config["UPLOAD_FOLDER"], filename=image_name, as_attachment=True) else: - return flask.Response(json.dumps({"error": "File does not exist"}), status=404) - + return flask.Response(json.dumps({"error": "File does not exist"}), status=404) # using the token from the start url upload endpoint @app.route('/urlupload/continue/', methods=['POST']) @@ -196,3 +224,4 @@ def urlUploadStatus(): return flask.Response(json.dumps({"uploaded": "True"}), status=200) else: return flask.Response(json.dumps({"uploaded": "False"}), status=200) + diff --git a/requirements.txt b/requirements.txt index 823b15e..3c43dc0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,4 @@ openslide-python flask Werkzeug flask-cors -requests \ No newline at end of file +requests