rairo commited on
Commit
eb6fbc0
·
verified ·
1 Parent(s): bc8b6e7

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +79 -0
main.py CHANGED
@@ -826,6 +826,85 @@ def extract_firebase_path(public_url: str) -> str:
826
  return ""
827
 
828
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
829
  # ---------- Credit Request Endpoints ----------
830
 
831
  @app.route('/api/user/request-credits', methods=['POST'])
 
826
  return ""
827
 
828
 
829
+ # Delete endpoints
830
+ #projects
831
+ @app.route('/api/projects/<string:story_id>', methods=['DELETE'])
832
+ def delete_project(story_id):
833
+ """
834
+ Deletes the entire project (story) from the database (and optionally its assets in storage).
835
+ """
836
+ try:
837
+ # --- Authentication ---
838
+ auth_header = request.headers.get('Authorization', '')
839
+ if not auth_header or not auth_header.startswith('Bearer '):
840
+ return jsonify({'error': 'Missing or invalid token'}), 401
841
+ token = auth_header.split(' ')[1]
842
+ uid = verify_token(token)
843
+ if not uid:
844
+ return jsonify({'error': 'Invalid or expired token'}), 401
845
+
846
+ # --- Fetch Story ---
847
+ story_ref = db.reference(f"stories/{story_id}")
848
+ story_data = story_ref.get()
849
+ if not story_data:
850
+ return jsonify({'error': 'Story not found'}), 404
851
+
852
+ if story_data.get('uid') != uid:
853
+ return jsonify({'error': 'Unauthorized'}), 403
854
+
855
+ # --- Delete the story from DB ---
856
+ story_ref.delete()
857
+
858
+ return jsonify({'success': True, 'message': f'Story {story_id} deleted.'}), 200
859
+
860
+ except Exception as e:
861
+ return jsonify({'error': str(e)}), 500
862
+
863
+ #videos
864
+ @app.route('/api/videos/<string:story_id>', methods=['DELETE'])
865
+ def delete_video(story_id):
866
+ """
867
+ Deletes only the video from a project (removes video_url from DB),
868
+ optionally deleting the file in storage.
869
+ """
870
+ try:
871
+ # --- Authentication ---
872
+ auth_header = request.headers.get('Authorization', '')
873
+ if not auth_header or not auth_header.startswith('Bearer '):
874
+ return jsonify({'error': 'Missing or invalid token'}), 401
875
+ token = auth_header.split(' ')[1]
876
+ uid = verify_token(token)
877
+ if not uid:
878
+ return jsonify({'error': 'Invalid or expired token'}), 401
879
+
880
+ # --- Fetch Story ---
881
+ story_ref = db.reference(f"stories/{story_id}")
882
+ story_data = story_ref.get()
883
+ if not story_data:
884
+ return jsonify({'error': 'Story not found'}), 404
885
+
886
+ if story_data.get('uid') != uid:
887
+ return jsonify({'error': 'Unauthorized'}), 403
888
+
889
+ video_url = story_data.get('video_url')
890
+ if not video_url:
891
+ return jsonify({'error': 'No video to delete'}), 400
892
+
893
+ # Delete the video file from Firebase Storage
894
+ bucket = storage.bucket()
895
+ file_path = extract_firebase_path(video_url)
896
+ if file_path:
897
+ blob = bucket.blob(file_path)
898
+ blob.delete()
899
+
900
+ # --- Remove video_url from DB ---
901
+ story_ref.update({'video_url': None})
902
+
903
+ return jsonify({'success': True, 'message': f'Video removed from story {story_id}.'}), 200
904
+
905
+ except Exception as e:
906
+ return jsonify({'error': str(e)}), 500
907
+
908
  # ---------- Credit Request Endpoints ----------
909
 
910
  @app.route('/api/user/request-credits', methods=['POST'])