rairo commited on
Commit
467e35e
·
verified ·
1 Parent(s): d3ac6c9

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +24 -16
main.py CHANGED
@@ -408,22 +408,32 @@ def get_user_profile():
408
  return jsonify({'error': str(e)}), 500
409
 
410
  # Receipt media endpoints
 
 
411
  def get_blob_from_image_url(image_url):
412
  """
413
- Given an image_url from Firebase Storage (which typically looks like:
414
- https://firebasestorage.googleapis.com/v0/b/<bucket>/o/receipts%2F<uid>%2F<filename>?alt=media&token=...),
415
- extract and return the blob path (e.g., receipts/<uid>/<filename>).
 
416
  """
417
  parsed = urlparse(image_url)
418
- # Construct the expected prefix using your bucket's name.
 
 
 
 
 
 
 
 
419
  prefix = f"/v0/b/{bucket.name}/o/"
420
  if parsed.path.startswith(prefix):
421
- encoded_blob_path = parsed.path[len(prefix):] # e.g., receipts%2Fuid%2Ffilename
422
- blob_path = unquote(encoded_blob_path) # e.g., receipts/uid/filename
423
  return blob_path
424
  return None
425
 
426
-
427
  @app.route('/api/admin/receipt/<string:transaction_id>/view', methods=['GET'])
428
  def view_receipt(transaction_id):
429
  try:
@@ -438,19 +448,18 @@ def view_receipt(transaction_id):
438
  return jsonify({'error': 'No receipt image found for this transaction'}), 404
439
 
440
  blob_path = get_blob_from_image_url(image_url)
441
-
442
  if not blob_path:
443
  return jsonify({'error': 'Could not determine blob path from URL'}), 500
444
 
445
- print(f"**DEBUG: Blob path being used: {blob_path}**") # ADD THIS LINE
446
- blob = bucket.blob(blob_path)
447
 
 
448
  if not blob.exists():
449
  print("Blob does not exist at path:", blob_path)
450
  return jsonify({'error': 'Blob not found'}), 404
451
 
452
  signed_url = blob.generate_signed_url(expiration=timedelta(minutes=10))
453
- print(f"Signed URL for view: {signed_url}") # Debug log
454
  r = requests.get(signed_url)
455
  print(f"View endpoint response status: {r.status_code}") # Debug log
456
  if r.status_code != 200:
@@ -478,15 +487,15 @@ def download_receipt(transaction_id):
478
  if not blob_path:
479
  return jsonify({'error': 'Could not determine blob path from URL'}), 500
480
 
481
- print(f"**DEBUG: Blob path being used: {blob_path}**") # ADD THIS LINE
482
- blob = bucket.blob(blob_path) # Debug log
483
 
 
484
  if not blob.exists():
485
  print("Blob does not exist at path:", blob_path)
486
  return jsonify({'error': 'Blob not found'}), 404
487
 
488
  signed_url = blob.generate_signed_url(expiration=timedelta(minutes=10))
489
- print(f"Signed URL for download: {signed_url}") # Debug log
490
  r = requests.get(signed_url)
491
  print(f"Download endpoint response status: {r.status_code}") # Debug log
492
  if r.status_code != 200:
@@ -496,7 +505,7 @@ def download_receipt(transaction_id):
496
  io.BytesIO(r.content),
497
  mimetype='image/jpeg',
498
  as_attachment=True,
499
- attachment_file='receipt.jpg'
500
  )
501
  except Exception as e:
502
  print(f"Download receipt error: {str(e)}")
@@ -504,7 +513,6 @@ def download_receipt(transaction_id):
504
 
505
 
506
 
507
-
508
  # delete users
509
 
510
  @app.route('/api/admin/users/<string:uid>', methods=['DELETE'])
 
408
  return jsonify({'error': str(e)}), 500
409
 
410
  # Receipt media endpoints
411
+
412
+
413
  def get_blob_from_image_url(image_url):
414
  """
415
+ Extracts and returns the blob path from a Firebase Storage URL.
416
+ Supports URLs of the form:
417
+ https://storage.googleapis.com/<bucket>/<blob_path>
418
+ or the default Firebase URL format.
419
  """
420
  parsed = urlparse(image_url)
421
+ # If URL is in the storage.googleapis.com format:
422
+ if parsed.netloc == "storage.googleapis.com":
423
+ # Expected path: /<bucket>/<blob_path>
424
+ parts = parsed.path.strip("/").split("/", 1)
425
+ if len(parts) == 2:
426
+ bucket_name, blob_path = parts
427
+ # (Optionally, check if bucket_name matches our bucket.name)
428
+ return blob_path
429
+ # Otherwise, try the default format:
430
  prefix = f"/v0/b/{bucket.name}/o/"
431
  if parsed.path.startswith(prefix):
432
+ encoded_blob_path = parsed.path[len(prefix):]
433
+ blob_path = unquote(encoded_blob_path)
434
  return blob_path
435
  return None
436
 
 
437
  @app.route('/api/admin/receipt/<string:transaction_id>/view', methods=['GET'])
438
  def view_receipt(transaction_id):
439
  try:
 
448
  return jsonify({'error': 'No receipt image found for this transaction'}), 404
449
 
450
  blob_path = get_blob_from_image_url(image_url)
 
451
  if not blob_path:
452
  return jsonify({'error': 'Could not determine blob path from URL'}), 500
453
 
454
+ print(f"Blob path for view: {blob_path}") # Debug log
 
455
 
456
+ blob = bucket.blob(blob_path)
457
  if not blob.exists():
458
  print("Blob does not exist at path:", blob_path)
459
  return jsonify({'error': 'Blob not found'}), 404
460
 
461
  signed_url = blob.generate_signed_url(expiration=timedelta(minutes=10))
462
+ print(f"Signed URL for view: {signed_url}") # Debug log
463
  r = requests.get(signed_url)
464
  print(f"View endpoint response status: {r.status_code}") # Debug log
465
  if r.status_code != 200:
 
487
  if not blob_path:
488
  return jsonify({'error': 'Could not determine blob path from URL'}), 500
489
 
490
+ print(f"Blob path for download: {blob_path}") # Debug log
 
491
 
492
+ blob = bucket.blob(blob_path)
493
  if not blob.exists():
494
  print("Blob does not exist at path:", blob_path)
495
  return jsonify({'error': 'Blob not found'}), 404
496
 
497
  signed_url = blob.generate_signed_url(expiration=timedelta(minutes=10))
498
+ print(f"Signed URL for download: {signed_url}") # Debug log
499
  r = requests.get(signed_url)
500
  print(f"Download endpoint response status: {r.status_code}") # Debug log
501
  if r.status_code != 200:
 
505
  io.BytesIO(r.content),
506
  mimetype='image/jpeg',
507
  as_attachment=True,
508
+ attachment_filename='receipt.jpg' # Use download_name if using Flask 2.x
509
  )
510
  except Exception as e:
511
  print(f"Download receipt error: {str(e)}")
 
513
 
514
 
515
 
 
516
  # delete users
517
 
518
  @app.route('/api/admin/users/<string:uid>', methods=['DELETE'])