From ce79a40acc931abfd3e4aabbb3f5b162241f2820 Mon Sep 17 00:00:00 2001 From: Naymul Islam Date: Tue, 12 Sep 2023 19:12:57 +0600 Subject: [PATCH] Google drive related function is transfered into new file named gdrive_utils.py Google drive related file is being transfer into a new file named gdrive_utils.py --- SlideServer.py | 33 ++++++--------------------------- gdrive_utils.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 27 deletions(-) create mode 100644 gdrive_utils.py diff --git a/SlideServer.py b/SlideServer.py index c1dc9c4..4c41a6d 100644 --- a/SlideServer.py +++ b/SlideServer.py @@ -22,7 +22,7 @@ import csv import pathlib import logging -from gDriveDownload import start, afterUrlAuth, callApi +from gdrive_utils import getFileFromGdrive, gDriveGetFile, checkDownloadStatus from threading import Thread try: @@ -48,7 +48,7 @@ app.config['ROI_FOLDER'] = "/images/roiDownload" -ALLOWED_EXTENSIONS = set(['svs', 'tif', 'tiff', 'vms', 'vmu', 'ndpi', 'scn', 'mrxs', 'bif', 'svslide', 'png', 'jpg']) +ALLOWED_EXTENSIONS = set(['svs', 'tif', 'tiff', 'vms', 'vmu', 'ndpi', 'scn', 'mrxs', 'bif', 'svslide', 'png', 'jpg']) def allowed_file(filename): @@ -570,37 +570,16 @@ def run(self): # Route to start the OAuth Server(to listen if user is Authenticated) and start the file Download after Authentication @app.route('/googleDriveUpload/getFile', methods=['POST']) -def gDriveGetFile(): +def gDriveGetFileRoute(): body = flask.request.get_json() if not body: return flask.Response(json.dumps({"error": "Missing JSON body"}), status=400) - - token = "".join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10)) - token = secure_filename(token) - tmppath = os.path.join("/images/uploading/", token) - # regenerate if we happen to collide - while os.path.isfile(tmppath): - token = "".join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10)) - token = secure_filename(token) - tmppath = os.path.join("/images/uploading/", token) - - try: - params = start(body['userId']) - except: - return flask.Response(json.dumps({'error': str(sys.exc_info()[0])}), status=400) - thread_a = getFileFromGdrive(params, body['userId'], body['fileId'], token) - thread_a.start() - return flask.Response(json.dumps({"authURL": params["auth_url"], "token": token}), status=200) + return gDriveGetFile(body) # To check if a particular file is downloaded from Gdrive @app.route('/googleDriveUpload/checkStatus', methods=['POST']) -def checkDownloadStatus(): +def checkDownloadStatusRoute(): body = flask.request.get_json() if not body: return flask.Response(json.dumps({"error": "Missing JSON body"}), status=400) - token = body['token'] - path = app.config['TEMP_FOLDER']+'/'+token - if os.path.isfile(path): - return flask.Response(json.dumps({"downloadDone": True}), status=200) - return flask.Response(json.dumps({"downloadDone": False}), status=200) - + return checkDownloadStatus(body) \ No newline at end of file diff --git a/gdrive_utils.py b/gdrive_utils.py new file mode 100644 index 0000000..e1de7a4 --- /dev/null +++ b/gdrive_utils.py @@ -0,0 +1,45 @@ +from threading import Thread +from gDriveDownload import start, afterUrlAuth, callApi +import flask +import os +import random +import string +from werkzeug.utils import secure_filename +import sys + +# A new Thread to call the Gdrive API after an Auth Response is returned to the user. +class getFileFromGdrive(Thread): + def __init__(self, params, userId, fileId, token): + Thread.__init__(self) + self.params, self.userId, self.fileId , self.token = params, userId, fileId, token + + def run(self): + if(self.params["auth_url"] != None): + self.params["creds"] = afterUrlAuth(self.params["local_server"], self.params["flow"], self.params["wsgi_app"], self.userId) + call = callApi(self.params["creds"], self.fileId, self.token) + app.logger.info(call) + +def gDriveGetFile(body): + token = "".join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10)) + token = secure_filename(token) + tmppath = os.path.join("/images/uploading/", token) + # regenerate if we happen to collide + while os.path.isfile(tmppath): + token = "".join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10)) + token = secure_filename(token) + tmppath = os.path.join("/images/uploading/", token) + + try: + params = start(body['userId']) + except: + return flask.Response(json.dumps({'error': str(sys.exc_info()[0])}), status=400) + thread_a = getFileFromGdrive(params, body['userId'], body['fileId'], token) + thread_a.start() + return flask.Response(json.dumps({"authURL": params["auth_url"], "token": token}), status=200) + +def checkDownloadStatus(body): + token = body['token'] + path = app.config['TEMP_FOLDER']+'/'+token + if os.path.isfile(path): + return flask.Response(json.dumps({"downloadDone": True}), status=200) + return flask.Response(json.dumps({"downloadDone": False}), status=200) \ No newline at end of file