Spaces:
Sleeping
Sleeping
Commit
·
1c8c19d
1
Parent(s):
9c15d37
Upd gracer drive module fallback
Browse files- utils/drive_saver.py +39 -4
utils/drive_saver.py
CHANGED
|
@@ -1,9 +1,30 @@
|
|
| 1 |
# Save final post-process to Google Drive
|
| 2 |
import os, json, logging
|
| 3 |
from typing import Optional
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
from utils.token import get_credentials
|
| 9 |
|
|
@@ -23,11 +44,21 @@ class DriveSaver:
|
|
| 23 |
self.folder_id = default_folder_id or os.getenv("GDRIVE_FOLDER_ID")
|
| 24 |
self.supports_all_drives = os.getenv("GDRIVE_FOLDER_IS_SHARED", "false").lower() in ("1","true","yes")
|
| 25 |
self.allow_sa_fallback = os.getenv("GDRIVE_ALLOW_SA_FALLBACK", "false").lower() in ("1","true","yes")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
if not self.folder_id:
|
| 27 |
logger.warning("📁 No GDRIVE_FOLDER_ID set; uploads must provide folder_id explicitly")
|
| 28 |
self._initialize_service()
|
| 29 |
|
| 30 |
def _initialize_service(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
creds = get_credentials()
|
| 32 |
if creds:
|
| 33 |
logger.info("✅ Using OAuth credentials")
|
|
@@ -59,6 +90,10 @@ class DriveSaver:
|
|
| 59 |
logger.info("✅ Google Drive service initialized")
|
| 60 |
|
| 61 |
def upload_file_to_drive(self, file_path: str, folder_id: Optional[str] = None, mimetype: Optional[str] = None) -> bool:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
if not self.service:
|
| 63 |
logger.error("❌ Drive service not initialized")
|
| 64 |
return False
|
|
@@ -81,7 +116,7 @@ class DriveSaver:
|
|
| 81 |
return False
|
| 82 |
|
| 83 |
def is_service_available(self) -> bool:
|
| 84 |
-
return self.service is not None
|
| 85 |
|
| 86 |
def set_folder_id(self, folder_id: str):
|
| 87 |
self.folder_id = folder_id
|
|
|
|
| 1 |
# Save final post-process to Google Drive
|
| 2 |
import os, json, logging
|
| 3 |
from typing import Optional
|
| 4 |
+
|
| 5 |
+
# Conditional imports for Google Drive (only when needed)
|
| 6 |
+
try:
|
| 7 |
+
from google.oauth2 import service_account
|
| 8 |
+
from googleapiclient.discovery import build
|
| 9 |
+
from googleapiclient.http import MediaFileUpload
|
| 10 |
+
GOOGLE_DRIVE_AVAILABLE = True
|
| 11 |
+
except ImportError:
|
| 12 |
+
GOOGLE_DRIVE_AVAILABLE = False
|
| 13 |
+
# Create dummy classes for when Google Drive is not available
|
| 14 |
+
class service_account:
|
| 15 |
+
class Credentials:
|
| 16 |
+
@staticmethod
|
| 17 |
+
def from_service_account_info(*args, **kwargs):
|
| 18 |
+
raise ImportError("Google Drive dependencies not available")
|
| 19 |
+
|
| 20 |
+
class build:
|
| 21 |
+
@staticmethod
|
| 22 |
+
def build(*args, **kwargs):
|
| 23 |
+
raise ImportError("Google Drive dependencies not available")
|
| 24 |
+
|
| 25 |
+
class MediaFileUpload:
|
| 26 |
+
def __init__(self, *args, **kwargs):
|
| 27 |
+
raise ImportError("Google Drive dependencies not available")
|
| 28 |
|
| 29 |
from utils.token import get_credentials
|
| 30 |
|
|
|
|
| 44 |
self.folder_id = default_folder_id or os.getenv("GDRIVE_FOLDER_ID")
|
| 45 |
self.supports_all_drives = os.getenv("GDRIVE_FOLDER_IS_SHARED", "false").lower() in ("1","true","yes")
|
| 46 |
self.allow_sa_fallback = os.getenv("GDRIVE_ALLOW_SA_FALLBACK", "false").lower() in ("1","true","yes")
|
| 47 |
+
|
| 48 |
+
# Check if Google Drive is available
|
| 49 |
+
if not GOOGLE_DRIVE_AVAILABLE:
|
| 50 |
+
logger.warning("⚠️ Google Drive dependencies not available - DriveSaver will be disabled")
|
| 51 |
+
return
|
| 52 |
+
|
| 53 |
if not self.folder_id:
|
| 54 |
logger.warning("📁 No GDRIVE_FOLDER_ID set; uploads must provide folder_id explicitly")
|
| 55 |
self._initialize_service()
|
| 56 |
|
| 57 |
def _initialize_service(self):
|
| 58 |
+
if not GOOGLE_DRIVE_AVAILABLE:
|
| 59 |
+
logger.warning("⚠️ Google Drive dependencies not available - skipping service initialization")
|
| 60 |
+
return
|
| 61 |
+
|
| 62 |
creds = get_credentials()
|
| 63 |
if creds:
|
| 64 |
logger.info("✅ Using OAuth credentials")
|
|
|
|
| 90 |
logger.info("✅ Google Drive service initialized")
|
| 91 |
|
| 92 |
def upload_file_to_drive(self, file_path: str, folder_id: Optional[str] = None, mimetype: Optional[str] = None) -> bool:
|
| 93 |
+
if not GOOGLE_DRIVE_AVAILABLE:
|
| 94 |
+
logger.warning("⚠️ Google Drive dependencies not available - upload skipped")
|
| 95 |
+
return False
|
| 96 |
+
|
| 97 |
if not self.service:
|
| 98 |
logger.error("❌ Drive service not initialized")
|
| 99 |
return False
|
|
|
|
| 116 |
return False
|
| 117 |
|
| 118 |
def is_service_available(self) -> bool:
|
| 119 |
+
return GOOGLE_DRIVE_AVAILABLE and self.service is not None
|
| 120 |
|
| 121 |
def set_folder_id(self, folder_id: str):
|
| 122 |
self.folder_id = folder_id
|