Skip to content

Commit

Permalink
Google drive related function is transfered into new file named gdriv…
Browse files Browse the repository at this point in the history
…e_utils.py

Google drive related file is being transfer into a new file named gdrive_utils.py
  • Loading branch information
ai-naymul committed Sep 12, 2023
1 parent 128587f commit ce79a40
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 27 deletions.
33 changes: 6 additions & 27 deletions SlideServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand Down Expand Up @@ -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)
45 changes: 45 additions & 0 deletions gdrive_utils.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit ce79a40

Please sign in to comment.