rairo commited on
Commit
ef2c475
·
verified ·
1 Parent(s): 8cb2611

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +49 -0
main.py CHANGED
@@ -610,6 +610,55 @@ def view_videos():
610
  except Exception as e:
611
  return jsonify({'error': str(e)}), 500
612
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
613
  # ---------- Credit Request Endpoints ----------
614
 
615
  @app.route('/api/user/request-credits', methods=['POST'])
 
610
  except Exception as e:
611
  return jsonify({'error': str(e)}), 500
612
 
613
+ @app.route('/api/download/story_asset/<story_id>', methods=['GET'])
614
+ def download_story_asset(story_id):
615
+ try:
616
+ # --- Authentication ---
617
+ auth_header = request.headers.get('Authorization', '')
618
+ if not auth_header.startswith('Bearer '):
619
+ return jsonify({'error': 'Missing or invalid token'}), 401
620
+ token = auth_header.split(' ')[1]
621
+ uid = verify_token(token)
622
+ if not uid:
623
+ return jsonify({'error': 'Invalid or expired token'}), 401
624
+
625
+ # --- Check if the Story Exists and Belongs to the User ---
626
+ stories_ref = db.reference('stories')
627
+ story_record = stories_ref.child(story_id).get()
628
+
629
+ if not story_record:
630
+ return jsonify({'error': 'Story not found'}), 404
631
+ if story_record.get('uid') != uid:
632
+ return jsonify({'error': 'Unauthorized'}), 403
633
+
634
+ # --- Determine the Asset Type ---
635
+ video_url = story_record.get("video_url", None)
636
+ audio_url = story_record.get("audio_url", None) # Add if audio files exist
637
+ image_url = story_record.get("image_url", None) # Add if images exist
638
+
639
+ # Prioritize video > audio > image
640
+ file_url = video_url or audio_url or image_url
641
+ if not file_url:
642
+ return jsonify({'error': 'No asset found for this story'}), 404
643
+
644
+ # --- If Stored in Firebase Storage, Generate a Signed URL ---
645
+ if "firebasestorage.googleapis.com" in file_url:
646
+ bucket = storage.bucket()
647
+ blob_path = file_url.split('/o/')[-1].split('?')[0] # Extract path
648
+ blob = bucket.blob(blob_path)
649
+ signed_url = blob.generate_signed_url(expiration=600) # Valid for 10 minutes
650
+ return jsonify({"download_url": signed_url})
651
+
652
+ # --- If Stored Locally, Serve the File ---
653
+ local_path = os.path.join("uploads", file_url) # Adjust path accordingly
654
+ if os.path.exists(local_path):
655
+ return send_file(local_path, as_attachment=True)
656
+
657
+ return jsonify({'error': 'File not found'}), 404
658
+
659
+ except Exception as e:
660
+ return jsonify({'error': str(e)}), 500
661
+
662
  # ---------- Credit Request Endpoints ----------
663
 
664
  @app.route('/api/user/request-credits', methods=['POST'])