supabase image url and file name mapping
Browse files- supabase_service.py +24 -11
supabase_service.py
CHANGED
|
@@ -18,30 +18,43 @@ BUCKET_NAME = "csvcharts"
|
|
| 18 |
async def upload_image_to_supabase(file_path: str, file_name: str) -> str:
|
| 19 |
"""
|
| 20 |
Uploads an image to Supabase Storage and returns the public URL.
|
| 21 |
-
|
| 22 |
-
:param file_path: Path to the image file on your local machine.
|
| 23 |
-
:param file_name: Name to save the file as in Supabase Storage.
|
| 24 |
-
:return: Public URL of the uploaded image.
|
| 25 |
"""
|
| 26 |
-
# Check if the file exists
|
| 27 |
if not os.path.exists(file_path):
|
| 28 |
raise FileNotFoundError(f"The file {file_path} does not exist.")
|
| 29 |
|
| 30 |
-
# Read the file in binary mode
|
| 31 |
with open(file_path, "rb") as f:
|
| 32 |
file_data = f.read()
|
| 33 |
|
| 34 |
-
# Upload the file to Supabase Storage
|
| 35 |
try:
|
| 36 |
res = supabase.storage.from_(BUCKET_NAME).upload(file_name, file_data)
|
| 37 |
-
|
| 38 |
except Exception as e:
|
| 39 |
-
print(f"Error uploading file: {e}")
|
| 40 |
raise Exception(f"Failed to upload file: {e}")
|
| 41 |
|
| 42 |
-
# Get the public URL of the uploaded file
|
| 43 |
public_url = supabase.storage.from_(BUCKET_NAME).get_public_url(file_name)
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
return public_url
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
async def upload_image_to_supabase(file_path: str, file_name: str) -> str:
|
| 19 |
"""
|
| 20 |
Uploads an image to Supabase Storage and returns the public URL.
|
| 21 |
+
Also saves the mapping between public_url and chart_name in the database.
|
|
|
|
|
|
|
|
|
|
| 22 |
"""
|
|
|
|
| 23 |
if not os.path.exists(file_path):
|
| 24 |
raise FileNotFoundError(f"The file {file_path} does not exist.")
|
| 25 |
|
|
|
|
| 26 |
with open(file_path, "rb") as f:
|
| 27 |
file_data = f.read()
|
| 28 |
|
|
|
|
| 29 |
try:
|
| 30 |
res = supabase.storage.from_(BUCKET_NAME).upload(file_name, file_data)
|
| 31 |
+
print("Upload response:", res)
|
| 32 |
except Exception as e:
|
|
|
|
| 33 |
raise Exception(f"Failed to upload file: {e}")
|
| 34 |
|
|
|
|
| 35 |
public_url = supabase.storage.from_(BUCKET_NAME).get_public_url(file_name)
|
| 36 |
+
print("Public URL:", public_url)
|
| 37 |
+
|
| 38 |
+
# Save the mapping to the database
|
| 39 |
+
try:
|
| 40 |
+
supabase.table("chart_mappings").insert({
|
| 41 |
+
"public_url": public_url,
|
| 42 |
+
"chart_name": file_name
|
| 43 |
+
}).execute()
|
| 44 |
+
except Exception as e:
|
| 45 |
+
raise Exception(f"Failed to save mapping to database: {e}")
|
| 46 |
|
| 47 |
return public_url
|
| 48 |
|
| 49 |
+
async def get_chart_name_by_url(public_url: str) -> str:
|
| 50 |
+
"""
|
| 51 |
+
Retrieves the chart_name using the public_url from the database.
|
| 52 |
+
"""
|
| 53 |
+
try:
|
| 54 |
+
response = supabase.table("chart_mappings").select("chart_name").eq("public_url", public_url).execute()
|
| 55 |
+
if response.data:
|
| 56 |
+
return response.data[0]["chart_name"]
|
| 57 |
+
else:
|
| 58 |
+
return None
|
| 59 |
+
except Exception as e:
|
| 60 |
+
raise Exception(f"Failed to retrieve chart_name: {e}")
|