Skip to content

Commit

Permalink
fix: webhook and gdrive_upload
Browse files Browse the repository at this point in the history
Updated error to message in json
Fix for process_gdrive_upload' is not defined
  • Loading branch information
rainmanjam committed Sep 8, 2024
1 parent 26307c5 commit b277584
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 18 deletions.
10 changes: 5 additions & 5 deletions routes/audio_mixing.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ def audio_mixing():

# Input validation
if not video_url or not audio_url:
return jsonify({"error": "Missing video_url or audio_url parameter"}), 400
return jsonify({"message": "Missing video_url or audio_url parameter"}), 400
if not (0 <= video_vol <= 100):
return jsonify({"error": "video_vol must be between 0 and 100"}), 400
return jsonify({"message": "video_vol must be between 0 and 100"}), 400
if not (0 <= audio_vol <= 100):
return jsonify({"error": "audio_vol must be between 0 and 100"}), 400
return jsonify({"message": "audio_vol must be between 0 and 100"}), 400
if output_length not in ['video', 'audio']:
return jsonify({"error": "output_length must be either 'video' or 'audio'"}), 400
return jsonify({"message": "output_length must be either 'video' or 'audio'"}), 400
if webhook_url and not id:
return jsonify({"error": "Missing id parameter for webhook"}), 400
return jsonify({"message": "Missing id parameter for webhook"}), 400

job_id = str(uuid.uuid4())
logger.info(f"Job {job_id}: Received audio mixing request for {video_url} and {audio_url}")
Expand Down
2 changes: 1 addition & 1 deletion routes/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def authenticate(func):
def wrapper(*args, **kwargs):
api_key = request.headers.get('X-API-Key')
if api_key != API_KEY:
return jsonify({"error": "Unauthorized"}), 401
return jsonify({"message": "Unauthorized"}), 401
return func(*args, **kwargs)
return wrapper

Expand Down
4 changes: 2 additions & 2 deletions routes/combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def process_and_notify(media_urls, webhook_url, id, job_id):
send_webhook(webhook_url, {
"endpoint": "/combine-videos",
"id": id,
"error": str(e),
"message": str(e),
"code": 500,
"message": "failed"
})
Expand All @@ -45,7 +45,7 @@ def combine_videos():

if not media_urls or not isinstance(media_urls, list):
logger.error("Invalid or missing media_urls parameter in request")
return jsonify({"error": "Invalid or missing media_urls parameter"}), 400
return jsonify({"message": "Invalid or missing media_urls parameter"}), 400

if webhook_url and not id:
logger.warning("id is missing when webhook_url is provided")
Expand Down
12 changes: 6 additions & 6 deletions routes/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from services.ffmpeg_processing import process_conversion
from services.authentication import authenticate
from services.webhook import send_webhook
from services.gdrive_service import upload_to_gdrive, upload_to_gcs, move_to_local_storage
from services.gdrive_service import process_gdrive_upload, upload_to_gdrive, upload_to_gcs, move_to_local_storage

# Configure logging
logging.basicConfig(level=logging.INFO)
Expand Down Expand Up @@ -95,7 +95,7 @@ def process_and_notify(media_url, job_id, id, webhook_url):
send_webhook(webhook_url, {
"endpoint": "/media-to-mp3",
"id": id,
"error": str(e),
"message": str(e),
"code": 500,
"message": "failed"
})
Expand All @@ -104,7 +104,6 @@ def process_and_notify(media_url, job_id, id, webhook_url):
finally:
logger.info(f"Job {job_id}: Exiting process_and_notify function.")


@convert_bp.route('/media-to-mp3', methods=['POST'])
@authenticate
def convert_media_to_mp3():
Expand All @@ -116,12 +115,12 @@ def convert_media_to_mp3():
# Ensure media_url is present
if not media_url:
logger.error("Received API call with missing media_url parameter.")
return jsonify({"error": "Missing media_url parameter"}), 400
return jsonify({"message": "Missing media_url parameter"}), 400

# Only check for id if webhook_url is provided
if webhook_url and not id:
logger.error("Received API call with webhook_url but missing id parameter.")
return jsonify({"error": "Missing id parameter for webhook"}), 400
return jsonify({"message": "Missing id parameter for webhook"}), 400

job_id = str(uuid.uuid4())
logger.info(f"Job {job_id}: Received conversion request for {media_url}")
Expand All @@ -143,7 +142,8 @@ def convert_media_to_mp3():
if uploaded_file_url:
return jsonify({"response": uploaded_file_url, "message": "success"}), 200
else:
return jsonify({"error": "File processing failed, no URL returned"}), 500
return jsonify({"message": "File processing failed, no URL returned"}), 500
except Exception as e:
logger.error(f"Job {job_id}: Error during synchronous processing - {e}")
return jsonify({"message": str(e)}), 500

2 changes: 1 addition & 1 deletion routes/transcribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def transcribe():

if not media_url:
logger.error("Missing media_url parameter in request")
return jsonify({"error": "Missing media_url parameter"}), 400
return jsonify({"message": "Missing media_url parameter"}), 400

# Check if either webhook_url or id is missing and return the appropriate message
if webhook_url and not id:
Expand Down
2 changes: 1 addition & 1 deletion services/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ def wrapper(*args, **kwargs):
api_key = request.headers.get('X-API-Key')

if api_key != API_KEY:
return jsonify({"error": "Unauthorized"}), 401
return jsonify({"message": "Unauthorized"}), 401
return func(*args, **kwargs)
return wrapper
42 changes: 41 additions & 1 deletion services/gdrive_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,44 @@ def move_to_local_storage(file_url, local_path):
return local_path
except Exception as e:
logger.error(f"Error moving file to local storage: {e}")
raise
raise

def process_gdrive_upload(file_url, filename, folder_id, webhook_url, job_id):
try:
# Download the file from the provided URL
file_path = download_file(file_url, STORAGE_PATH)

# Upload the file to Google Drive
file_id = upload_to_gdrive(file_path, filename, folder_id)

# Get the public URL of the uploaded file
service = build('drive', 'v3', credentials=drive_credentials)
set_file_public(service, file_id)
uploaded_file_url = f"https://drive.google.com/file/d/{file_id}/view?usp=sharing"

# Send success webhook if applicable
if webhook_url:
send_webhook(webhook_url, {
"endpoint": "/media-to-mp3",
"id": job_id,
"response": uploaded_file_url,
"code": 200,
"message": "success"
})

return uploaded_file_url

except Exception as e:
logger.error(f"Job {job_id}: Error during processing - {e}")
if webhook_url:
send_webhook(webhook_url, {
"endpoint": "/media-to-mp3",
"id": job_id,
"message": str(e),
"code": 500,
"message": "failed"
})
return None

finally:
logger.info(f"Job {job_id}: Exiting process_gdrive_upload function.")
2 changes: 1 addition & 1 deletion tests/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_authenticate_endpoint(client):
def test_authenticate_fail(client):
response = client.post('/authenticate', headers={'X-API-Key': 'wrong-key'})
assert response.status_code == 401
assert response.json == {"error": "Unauthorized"}
assert response.json == {"message": "Unauthorized"}

def test_convert_media_to_mp3(client, mocker):
data = {
Expand Down

0 comments on commit b277584

Please sign in to comment.