Spaces:
Sleeping
Sleeping
Bahawal Ali commited on
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,19 +1,22 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import time
|
|
|
|
| 4 |
import firebase_admin
|
| 5 |
from firebase_admin import credentials, firestore
|
| 6 |
from flask import Flask, request, jsonify
|
| 7 |
from flask_cors import CORS
|
| 8 |
-
from google.oauth2 import service_account
|
| 9 |
from googleapiclient.discovery import build
|
| 10 |
from googleapiclient.http import MediaFileUpload
|
|
|
|
| 11 |
|
| 12 |
app = Flask(__name__)
|
| 13 |
CORS(app)
|
| 14 |
|
| 15 |
-
# ---
|
| 16 |
print("--- SYSTEM STARTUP ---", flush=True)
|
|
|
|
|
|
|
| 17 |
try:
|
| 18 |
cred_json = json.loads(os.environ.get('FIREBASE_CREDENTIALS'))
|
| 19 |
cred = credentials.Certificate(cred_json)
|
|
@@ -23,47 +26,53 @@ try:
|
|
| 23 |
except Exception as e:
|
| 24 |
print(f"β Firebase Failed: {e}", flush=True)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
FOLDER_ID = os.environ.get('GDRIVE_FOLDER_ID')
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def get_drive_service():
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
return build('drive', 'v3', credentials=creds)
|
| 35 |
|
| 36 |
def upload_to_drive(file_path, filename):
|
| 37 |
-
print("π Uploading to Google Drive...", flush=True)
|
| 38 |
service = get_drive_service()
|
| 39 |
|
| 40 |
file_metadata = {
|
| 41 |
'name': filename,
|
| 42 |
-
'parents': [FOLDER_ID]
|
| 43 |
}
|
| 44 |
|
| 45 |
media = MediaFileUpload(file_path, resumable=True)
|
| 46 |
|
| 47 |
-
#
|
| 48 |
file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
|
| 49 |
file_id = file.get('id')
|
| 50 |
print(f"β
Uploaded! File ID: {file_id}", flush=True)
|
| 51 |
|
| 52 |
-
#
|
| 53 |
service.permissions().create(
|
| 54 |
fileId=file_id,
|
| 55 |
body={'type': 'anyone', 'role': 'reader'},
|
| 56 |
fields='id',
|
| 57 |
).execute()
|
| 58 |
|
| 59 |
-
#
|
| 60 |
result = service.files().get(fileId=file_id, fields='webViewLink').execute()
|
| 61 |
return result.get('webViewLink')
|
| 62 |
|
| 63 |
-
# --- ROUTES ---
|
| 64 |
@app.route('/')
|
| 65 |
def home():
|
| 66 |
-
return "Google Drive Backend
|
| 67 |
|
| 68 |
@app.route('/upload', methods=['POST'])
|
| 69 |
def upload_note():
|
|
@@ -77,15 +86,12 @@ def upload_note():
|
|
| 77 |
uploader = request.form.get('uploaderName', 'Anonymous')
|
| 78 |
|
| 79 |
if file:
|
| 80 |
-
# Temp Save (Server par 2 pal ke liye)
|
| 81 |
local_filename = f"/tmp/{int(time.time())}_{file.filename}"
|
| 82 |
file.save(local_filename)
|
| 83 |
|
| 84 |
try:
|
| 85 |
-
# 1. Drive Upload
|
| 86 |
link = upload_to_drive(local_filename, file.filename)
|
| 87 |
|
| 88 |
-
# 2. Firebase Save
|
| 89 |
if link:
|
| 90 |
db.collection('notes').add({
|
| 91 |
'title': title,
|
|
@@ -94,17 +100,15 @@ def upload_note():
|
|
| 94 |
'uploaderName': uploader,
|
| 95 |
'timestamp': firestore.SERVER_TIMESTAMP
|
| 96 |
})
|
| 97 |
-
|
| 98 |
-
if os.path.exists(local_filename): os.remove(local_filename)
|
| 99 |
return jsonify({'success': True, 'link': link}), 200
|
| 100 |
|
| 101 |
except Exception as e:
|
| 102 |
-
print(f"β Error: {e}", flush=True)
|
| 103 |
if os.path.exists(local_filename): os.remove(local_filename)
|
| 104 |
return jsonify({'error': str(e)}), 500
|
| 105 |
|
| 106 |
except Exception as e:
|
| 107 |
-
print(f"π₯ Critical Error: {e}", flush=True)
|
| 108 |
return jsonify({'error': str(e)}), 500
|
| 109 |
|
| 110 |
if __name__ == '__main__':
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import time
|
| 4 |
+
import requests
|
| 5 |
import firebase_admin
|
| 6 |
from firebase_admin import credentials, firestore
|
| 7 |
from flask import Flask, request, jsonify
|
| 8 |
from flask_cors import CORS
|
|
|
|
| 9 |
from googleapiclient.discovery import build
|
| 10 |
from googleapiclient.http import MediaFileUpload
|
| 11 |
+
from google.oauth2.credentials import Credentials
|
| 12 |
|
| 13 |
app = Flask(__name__)
|
| 14 |
CORS(app)
|
| 15 |
|
| 16 |
+
# --- CONFIGURATION ---
|
| 17 |
print("--- SYSTEM STARTUP ---", flush=True)
|
| 18 |
+
|
| 19 |
+
# 1. Firebase Setup
|
| 20 |
try:
|
| 21 |
cred_json = json.loads(os.environ.get('FIREBASE_CREDENTIALS'))
|
| 22 |
cred = credentials.Certificate(cred_json)
|
|
|
|
| 26 |
except Exception as e:
|
| 27 |
print(f"β Firebase Failed: {e}", flush=True)
|
| 28 |
|
| 29 |
+
# 2. Google Drive Config (Secrets se utha raha hai)
|
| 30 |
FOLDER_ID = os.environ.get('GDRIVE_FOLDER_ID')
|
| 31 |
+
CLIENT_ID = os.environ.get('GDRIVE_CLIENT_ID')
|
| 32 |
+
CLIENT_SECRET = os.environ.get('GDRIVE_CLIENT_SECRET')
|
| 33 |
+
REFRESH_TOKEN = os.environ.get('GDRIVE_REFRESH_TOKEN')
|
| 34 |
|
| 35 |
def get_drive_service():
|
| 36 |
+
# Refresh Token se Naya Access Token bana raha hai
|
| 37 |
+
creds = Credentials(
|
| 38 |
+
None, # Access token (temporary)
|
| 39 |
+
refresh_token=REFRESH_TOKEN,
|
| 40 |
+
token_uri="https://oauth2.googleapis.com/token",
|
| 41 |
+
client_id=CLIENT_ID,
|
| 42 |
+
client_secret=CLIENT_SECRET
|
| 43 |
+
)
|
| 44 |
return build('drive', 'v3', credentials=creds)
|
| 45 |
|
| 46 |
def upload_to_drive(file_path, filename):
|
| 47 |
+
print("π Uploading to Google Drive (User Storage)...", flush=True)
|
| 48 |
service = get_drive_service()
|
| 49 |
|
| 50 |
file_metadata = {
|
| 51 |
'name': filename,
|
| 52 |
+
'parents': [FOLDER_ID]
|
| 53 |
}
|
| 54 |
|
| 55 |
media = MediaFileUpload(file_path, resumable=True)
|
| 56 |
|
| 57 |
+
# Upload File
|
| 58 |
file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
|
| 59 |
file_id = file.get('id')
|
| 60 |
print(f"β
Uploaded! File ID: {file_id}", flush=True)
|
| 61 |
|
| 62 |
+
# Make Public (Taake user download kar sake)
|
| 63 |
service.permissions().create(
|
| 64 |
fileId=file_id,
|
| 65 |
body={'type': 'anyone', 'role': 'reader'},
|
| 66 |
fields='id',
|
| 67 |
).execute()
|
| 68 |
|
| 69 |
+
# Get Link
|
| 70 |
result = service.files().get(fileId=file_id, fields='webViewLink').execute()
|
| 71 |
return result.get('webViewLink')
|
| 72 |
|
|
|
|
| 73 |
@app.route('/')
|
| 74 |
def home():
|
| 75 |
+
return "Google Drive OAuth Backend Online"
|
| 76 |
|
| 77 |
@app.route('/upload', methods=['POST'])
|
| 78 |
def upload_note():
|
|
|
|
| 86 |
uploader = request.form.get('uploaderName', 'Anonymous')
|
| 87 |
|
| 88 |
if file:
|
|
|
|
| 89 |
local_filename = f"/tmp/{int(time.time())}_{file.filename}"
|
| 90 |
file.save(local_filename)
|
| 91 |
|
| 92 |
try:
|
|
|
|
| 93 |
link = upload_to_drive(local_filename, file.filename)
|
| 94 |
|
|
|
|
| 95 |
if link:
|
| 96 |
db.collection('notes').add({
|
| 97 |
'title': title,
|
|
|
|
| 100 |
'uploaderName': uploader,
|
| 101 |
'timestamp': firestore.SERVER_TIMESTAMP
|
| 102 |
})
|
| 103 |
+
os.remove(local_filename)
|
|
|
|
| 104 |
return jsonify({'success': True, 'link': link}), 200
|
| 105 |
|
| 106 |
except Exception as e:
|
| 107 |
+
print(f"β Upload Error: {e}", flush=True)
|
| 108 |
if os.path.exists(local_filename): os.remove(local_filename)
|
| 109 |
return jsonify({'error': str(e)}), 500
|
| 110 |
|
| 111 |
except Exception as e:
|
|
|
|
| 112 |
return jsonify({'error': str(e)}), 500
|
| 113 |
|
| 114 |
if __name__ == '__main__':
|