Bhargav3000 commited on
Commit
9b277b9
·
verified ·
1 Parent(s): d9ad68f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -48
app.py CHANGED
@@ -28,63 +28,55 @@ temp_dir = tempfile.mkdtemp()
28
 
29
  # Function to retrieve all files from the Salesforce custom object
30
  def get_all_salesforce_files(custom_object_api_name):
31
- # Step 1: Query all record IDs from the custom object
32
- query_all_records = f"SELECT Id FROM {custom_object_api_name}"
33
  all_records = sf.query(query_all_records)
34
- record_ids = [record['Id'] for record in all_records['records']]
35
 
36
- all_files = []
37
-
38
- # Step 2: Loop through each record ID and get linked files from the Files object
39
- for record_id in record_ids:
40
- query_files = f"SELECT Id, ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = '{record_id}'"
41
- results = sf.query(query_files)
42
-
43
- if results['records']:
44
- all_files.extend(results['records'])
45
-
46
- return all_files
47
 
48
  # Function to download a file from Google Drive and save it to a temporary location
49
  def download_google_drive_file(file_id, file_name):
50
- request = drive_service.files().get_media(fileId=file_id)
51
- file_path = os.path.join(temp_dir, file_name)
52
- with open(file_path, 'wb') as f:
53
- downloader = MediaIoBaseDownload(f, request)
54
- done = False
55
- while not done:
56
- status, done = downloader.next_chunk()
57
- if status:
58
- print(f"Download progress: {int(status.progress() * 100)}%")
59
- return file_path
 
 
 
 
60
 
61
  # Function to save all files linked to records from the Salesforce custom object
62
  def save_salesforce_files(custom_object_api_name):
63
- files = get_all_salesforce_files(custom_object_api_name)
64
- for file in files:
65
- content_doc_id = file['ContentDocumentId']
66
-
67
- # Query ContentDocument to get the Title and FileExtension
68
- content_doc_query = f"SELECT Title, FileExtension FROM ContentDocument WHERE Id = '{content_doc_id}'"
69
- content_doc_result = sf.query(content_doc_query)
70
-
71
- if not content_doc_result['records']:
72
- print(f"No details found for ContentDocumentId: {content_doc_id}")
73
- continue
74
-
75
- content_doc = content_doc_result['records'][0]
76
- file_name = content_doc['Title'] + '.' + content_doc['FileExtension']
77
 
78
- # Download the file from Google Drive using the ContentDocument ID (assuming the file is stored in Google Drive)
79
- try:
80
- print(f"Downloading file {file_name} from Google Drive...")
81
- file_path = download_google_drive_file(content_doc_id, file_name)
82
- if os.path.exists(file_path):
83
- print(f"File {file_name} saved successfully to {file_path}")
84
- else:
85
- print(f"Failed to save file {file_name}")
86
- except Exception as e:
87
- print(f"Error downloading file {file_name}: {e}")
 
 
 
 
 
 
 
 
88
 
89
  if __name__ == "__main__":
90
  # Replace 'YOUR_CUSTOM_OBJECT_API_NAME' with the Salesforce custom object API name
 
28
 
29
  # Function to retrieve all files from the Salesforce custom object
30
  def get_all_salesforce_files(custom_object_api_name):
31
+ # Step 1: Query all record IDs from the custom object, including the Google Drive File ID field
32
+ query_all_records = f"SELECT Id, Google_Fille_ID__c FROM {custom_object_api_name} WHERE Google_Fille_ID__c != NULL"
33
  all_records = sf.query(query_all_records)
34
+ records = all_records['records']
35
 
36
+ return records
 
 
 
 
 
 
 
 
 
 
37
 
38
  # Function to download a file from Google Drive and save it to a temporary location
39
  def download_google_drive_file(file_id, file_name):
40
+ try:
41
+ request = drive_service.files().get_media(fileId=file_id)
42
+ file_path = os.path.join(temp_dir, file_name)
43
+ with open(file_path, 'wb') as f:
44
+ downloader = MediaIoBaseDownload(f, request)
45
+ done = False
46
+ while not done:
47
+ status, done = downloader.next_chunk()
48
+ if status:
49
+ print(f"Download progress: {int(status.progress() * 100)}%")
50
+ return file_path
51
+ except Exception as e:
52
+ print(f"Error downloading file {file_name}: {e}")
53
+ return None
54
 
55
  # Function to save all files linked to records from the Salesforce custom object
56
  def save_salesforce_files(custom_object_api_name):
57
+ records = get_all_salesforce_files(custom_object_api_name)
58
+ for record in records:
59
+ google_drive_file_id = record['Google_Drive_File_ID__c']
60
+ record_id = record['Id']
 
 
 
 
 
 
 
 
 
 
61
 
62
+ # Check if the Google Drive File ID exists
63
+ if google_drive_file_id:
64
+ # Get the file metadata from Google Drive to obtain the filename
65
+ try:
66
+ file_metadata = drive_service.files().get(fileId=google_drive_file_id, fields='name').execute()
67
+ file_name = file_metadata['name']
68
+
69
+ print(f"Downloading file {file_name} from Google Drive...")
70
+ file_path = download_google_drive_file(google_drive_file_id, file_name)
71
+
72
+ if file_path and os.path.exists(file_path):
73
+ print(f"File {file_name} saved successfully to {file_path}")
74
+ else:
75
+ print(f"Failed to save file {file_name}")
76
+ except Exception as e:
77
+ print(f"Error retrieving file metadata for Google Drive ID {google_drive_file_id}: {e}")
78
+ else:
79
+ print(f"No Google Drive File ID found for record: {record_id}")
80
 
81
  if __name__ == "__main__":
82
  # Replace 'YOUR_CUSTOM_OBJECT_API_NAME' with the Salesforce custom object API name