Spaces:
Sleeping
Sleeping
| # File: backend/dropbox_utils.py | |
| import dropbox | |
| import requests | |
| import json | |
| import os | |
| REDIRECT_URI = "https://docusort.vercel.app/dropbox-callback" | |
| def get_dropbox_secrets(): | |
| """Securely load Dropbox keys from Hugging Face Environment Secrets""" | |
| secret_str = os.getenv("DROPBOX_SECRET_JSON") | |
| if not secret_str: | |
| print("WARNING: DROPBOX_SECRET_JSON not found in environment!") | |
| return {"app_key": "MISSING", "app_secret": "MISSING"} | |
| return json.loads(secret_str) | |
| def get_dropbox_auth_url(): | |
| secrets = get_dropbox_secrets() | |
| return f"https://www.dropbox.com/oauth2/authorize?client_id={secrets['app_key']}&response_type=code&redirect_uri={REDIRECT_URI}&token_access_type=offline&force_reapprove=true" | |
| def exchange_code_for_token(code: str): | |
| secrets = get_dropbox_secrets() | |
| token_url = "https://api.dropboxapi.com/oauth2/token" | |
| data = { | |
| "code": code, | |
| "grant_type": "authorization_code", | |
| "client_id": secrets['app_key'], | |
| "client_secret": secrets['app_secret'], | |
| "redirect_uri": REDIRECT_URI, | |
| } | |
| response = requests.post(token_url, data=data) | |
| response.raise_for_status() | |
| return response.json() | |
| def get_dropbox_client(access_token: str): | |
| return dropbox.Dropbox(access_token) | |
| def get_user_email(dbx): | |
| account = dbx.users_get_current_account() | |
| return account.email | |
| def list_files_in_folder(dbx, folder_path=""): | |
| if folder_path == "root": | |
| folder_path = "" | |
| res = dbx.files_list_folder(folder_path) | |
| items = [] | |
| for entry in res.entries: | |
| if isinstance(entry, dropbox.files.FolderMetadata): | |
| items.append({ | |
| "id": entry.path_display, | |
| "name": entry.name, | |
| "mimeType": "application/vnd.google-apps.folder", | |
| "webViewLink": "", | |
| "iconLink": "" | |
| }) | |
| elif isinstance(entry, dropbox.files.FileMetadata): | |
| items.append({ | |
| "id": entry.path_display, | |
| "name": entry.name, | |
| "mimeType": "application/octet-stream", | |
| "webViewLink": f"https://www.dropbox.com/preview{entry.path_display}?role=personal", | |
| "iconLink": "https://cfl.dropboxstatic.com/static/images/favicon-vflUeLeeY.ico", | |
| "size": entry.size, | |
| "content_hash": entry.content_hash | |
| }) | |
| return items |