Skip to content

Commit

Permalink
Refactored Browse, & file managment
Browse files Browse the repository at this point in the history
  • Loading branch information
TheManWhoLikesToCode committed Jan 16, 2024
1 parent f019c32 commit ffb92b6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 47 deletions.
61 changes: 33 additions & 28 deletions backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
38 changes: 20 additions & 18 deletions backend/file_management.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
import shutil
from flask import app
Expand Down Expand Up @@ -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 []
1 change: 0 additions & 1 deletion frontend/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from flask import Flask, render_template
from flask_cors import CORS, cross_origin
import os
import logging
import config

Expand Down

0 comments on commit ffb92b6

Please sign in to comment.