import os from flask import jsonify, Response from googleapiclient.discovery import build from googleapiclient.http import MediaFileUpload from google.oauth2 import service_account class GDriveController: def __init__(self): SERVICE_ACCOUNT_FILE = "service_account.json" SCOPES = ["https://www.googleapis.com/auth/drive.file"] creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES) self.drive_service = build("drive", "v3", credentials=creds) def post_gdrive(self, local_file_path: str, folder_id: str) -> Response: if not local_file_path or not folder_id: return jsonify({"error": "Missing local_file_path or folder_id"}), 400 if not os.path.exists(local_file_path): return jsonify({"error": "File not found"}), 404 file_metadata = { "name": os.path.basename(local_file_path), "parents": [folder_id] } media = MediaFileUpload(local_file_path, resumable=True) file = self.drive_service.files().create( body=file_metadata, media_body=media, fields="id, webViewLink" ).execute() return jsonify({"file_url": file.get('webViewLink')})