From ffb92b62e3788c80b86ee3753297531f13938f56 Mon Sep 17 00:00:00 2001 From: Jaydin_MacBook Date: Mon, 15 Jan 2024 22:14:59 -0500 Subject: [PATCH] Refactored Browse, & file managment --- backend/app.py | 61 +++++++++++++++++++++----------------- backend/file_management.py | 38 +++++++++++++----------- frontend/app.py | 1 - 3 files changed, 53 insertions(+), 47 deletions(-) diff --git a/backend/app.py b/backend/app.py index 9a352b4..a178985 100644 --- a/backend/app.py +++ b/backend/app.py @@ -253,39 +253,44 @@ def list_directory(path): path = team_drive_id items = list_files_in_drive_folder(drive, path, team_drive_id) - # Check if there's only one file returned - if len(items) == 1 and items[0][3] == 'FILE': - # Assuming 'file_id' and 'file_name' are available based on the user selection - file_id = items[0][2] - file_name = items[0][0] - - # Update the session_files_path based on the current directory and create if it doesn't exist - current_dir = os.path.dirname(os.path.abspath(__file__)) - if os.path.basename(current_dir) != 'backend': - session_files_path = os.path.join( - current_dir, 'backend', 'Session Files') - else: - session_files_path = os.path.join(current_dir, 'Session Files') + if len(items) == 1: + item = items[0] + item_type, file_name, file_id = item[3], item[0], item[2] + + if item_type == 'FILE': + return handle_single_file(file_id, file_name) + elif item_type == 'FOLDER': + return jsonify({'error': 'Cannot download a folder.'}), 400 - # Check if the Session Files directory exists, if not, create it - if not os.path.exists(session_files_path): - os.makedirs(session_files_path) - full_path = os.path.join(session_files_path, file_name) + return jsonify(items) - file = drive.CreateFile({'id': file_id}) - print('Downloading file %s from Google Drive' % file_name) - file.GetContentFile(full_path) - @after_this_request - def trigger_post_download_operations(response): - thread = threading.Thread( - target=clean_up_and_upload_files_to_google_drive, args=(full_path,)) - thread.start() - return response +def handle_single_file(file_id, file_name): + session_files_path = get_session_files_path() + if not os.path.exists(session_files_path): + os.makedirs(session_files_path) + full_path = os.path.join(session_files_path, file_name) - return send_from_directory(session_files_path, file_name, as_attachment=True) + file = drive.CreateFile({'id': file_id}) + print('Downloading file %s from Google Drive' % file_name) + file.GetContentFile(full_path) - return jsonify(items) + @after_this_request + def trigger_post_download_operations(response): + thread = threading.Thread( + target=clean_up_and_upload_files_to_google_drive, args=(full_path,)) + thread.start() + return response + + return send_from_directory(session_files_path, file_name, as_attachment=True) + + +def get_session_files_path(): + current_dir = os.path.dirname(os.path.abspath(__file__)) + if os.path.basename(current_dir) != 'backend': + return os.path.join(current_dir, 'backend', 'Session Files') + else: + return os.path.join(current_dir, 'Session Files') @app.route('/browse') diff --git a/backend/file_management.py b/backend/file_management.py index 2a1b21d..f3d8461 100644 --- a/backend/file_management.py +++ b/backend/file_management.py @@ -1,3 +1,4 @@ +import logging import os import shutil from flask import app @@ -208,22 +209,23 @@ def update_drive_directory(drive, team_drive_id): def list_files_in_drive_folder(drive, folder_id, team_drive_id): - query = f"'{folder_id}' in parents and trashed=false" - - # Attempt to use the provided ID as a folder ID - if team_drive_id: - file_list = drive.ListFile({'q': query, 'supportsTeamDrives': True, 'includeTeamDriveItems': True, - 'corpora': 'teamDrive', 'teamDriveId': team_drive_id}).GetList() - else: - file_list = drive.ListFile({'q': query}).GetList() - - # Check if the file_list is empty, if so, try treating the ID as a file ID - if not file_list: - file = drive.CreateFile({'id': folder_id}) - file.FetchMetadata() - return [(file['title'], file['mimeType'], file['id'], 'FILE')] - - # Sort the files by title in alphabetical order - sorted_file_list = sorted(file_list, key=lambda file: file['title']) + try: + query = f"'{folder_id}' in parents and trashed=false" + + if team_drive_id: + file_list = drive.ListFile({'q': query, 'supportsTeamDrives': True, 'includeTeamDriveItems': True, + 'corpora': 'teamDrive', 'teamDriveId': team_drive_id}).GetList() + else: + file_list = drive.ListFile({'q': query}).GetList() + + if not file_list: + file = drive.CreateFile({'id': folder_id}) + file.FetchMetadata() + return [(file['title'], file['mimeType'], file['id'], 'FILE')] + + sorted_file_list = sorted(file_list, key=lambda file: file['title']) + return [(file['title'], file['mimeType'], file['id']) for file in sorted_file_list] - return [(file['title'], file['mimeType'], file['id']) for file in sorted_file_list] + except Exception as e: + logging.error(f"Error in list_files_in_drive_folder: {e}") + return [] \ No newline at end of file diff --git a/frontend/app.py b/frontend/app.py index cfbfe95..48a6a05 100644 --- a/frontend/app.py +++ b/frontend/app.py @@ -1,6 +1,5 @@ from flask import Flask, render_template from flask_cors import CORS, cross_origin -import os import logging import config