Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure all uploads avoid clashes and respect the extension #115

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions projects/fal/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ test = [
"pytest-asyncio",
"pytest-xdist",
"flaky",
"boto3",
]
dev = [
"fal[test]",
Expand Down
1 change: 1 addition & 0 deletions projects/fal/src/fal/toolkit/file/providers/fal.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def save(
**self.auth_headers,
"Accept": "application/json",
"Content-Type": file.content_type,
"X-Fal-File-Name": file.file_name,
"X-Fal-Object-Lifecycle-Preference": json.dumps(
dataclasses.asdict(GLOBAL_LIFECYCLE_PREFERENCE)
),
Expand Down
34 changes: 33 additions & 1 deletion projects/fal/tests/toolkit/file_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from base64 import b64encode

import pytest
from fal.toolkit.file.file import File, GoogleStorageRepository
from fal.toolkit.file.file import BUILT_IN_REPOSITORIES, File, GoogleStorageRepository
from fal.toolkit.file.types import FileRepository, RepositoryId


def test_binary_content_matches():
Expand Down Expand Up @@ -64,3 +65,34 @@ def test_gcp_storage_if_available():
assert file.url.startswith(
"https://storage.googleapis.com/fal_registry_image_results/"
)


@pytest.mark.parametrize(
"repo",
BUILT_IN_REPOSITORIES.keys(),
)
def test_uniqueness_of_file_name(repo: RepositoryId | FileRepository):
if repo == "in_memory":
return

if repo == "gcp_storage":
gcp_sa_json = os.environ.get("GCLOUD_SA_JSON")
if gcp_sa_json is None:
pytest.skip(reason="GCLOUD_SA_JSON environment variable is not set")
repo = GoogleStorageRepository(bucket_name="fal_registry_image_results")

if repo == "r2":
r2_account_json = os.environ.get("R2_CREDS_JSON")
if r2_account_json is None:
pytest.skip(reason="R2_CREDS_JSON environment variable is not set")

file = File.from_bytes(b"print('Hello!')", repository=repo, file_name="hello.py")

host_and_path = file.url.split("?")[0]
last_path = host_and_path.split("/")[-1]
assert (
last_path.endswith(".py")
), f"The file name {last_path} should end with the same extension"
assert (
len(last_path) > 10
), f"There should be a long enough random string in the file name {last_path}"
Loading