Skip to content

Commit

Permalink
Simplify URLs in fxn.storage.upload
Browse files Browse the repository at this point in the history
  • Loading branch information
olokobayusuf committed Feb 20, 2024
1 parent 10b8bd8 commit d2b860c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
13 changes: 11 additions & 2 deletions fxn/services/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from requests import put
from rich.progress import open as open_progress, wrap_file
from typing import Union
from urllib.parse import urlparse, urlunparse

from ..graph import GraphClient
from ..types import UploadType
Expand Down Expand Up @@ -97,7 +98,7 @@ def __upload_file (
with open_progress(file, mode="rb", description=name, disable=not verbose) as f:
put(url, data=f, headers={ "Content-Type": mime }).raise_for_status()
# Return
return url
return self.__simplify_url(url)

def __upload_buffer (
self,
Expand All @@ -122,9 +123,17 @@ def __upload_buffer (
with wrap_file(file, total=size, description=name, disable=not verbose) as f:
put(url, data=f, headers={ "Content-Type": mime }).raise_for_status()
# Return
return url
return self.__simplify_url(url)

def __create_data_url (self, file: BytesIO, *, mime: str) -> str:
encoded_data = b64encode(file.getvalue()).decode("ascii")
url = f"data:{mime};base64,{encoded_data}"
return url

def __simplify_url (self, url: str) -> str:
if url.startswith("data:"):
return url
parsed_url = urlparse(url)
parsed_url = parsed_url._replace(netloc="cdn.fxn.ai", query="")
url = urlunparse(parsed_url)
return url
6 changes: 3 additions & 3 deletions test/storage_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

def test_file_upload ():
fxn = Function()
url = fxn.storage.upload("test/media/cat.jpg", UploadType.Value, verbose=True)
assert url.startswith("https://")
url = fxn.storage.upload("test/media/cat.jpg", type=UploadType.Value, verbose=True)
assert url.startswith("https://cdn.fxn.ai")

def test_file_upload_data_url ():
fxn = Function()
Expand All @@ -25,7 +25,7 @@ def test_buffer_upload ():
with open(path, mode="rb") as f:
buffer = BytesIO(f.read())
url = fxn.storage.upload(buffer, type=UploadType.Value, name=path.name, verbose=True)
assert url.startswith("https://")
assert url.startswith("https://cdn.fxn.ai")

def test_buffer_upload_data_url ():
fxn = Function()
Expand Down

0 comments on commit d2b860c

Please sign in to comment.