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(download-utils): file name with a forward slash #329

Merged
merged 2 commits into from
Oct 18, 2024
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
4 changes: 4 additions & 0 deletions projects/fal/src/fal/toolkit/utils/download_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ def _get_remote_file_properties(
url_path = parsed_url.path
file_name = Path(url_path).name or _hash_url(url)

# file name can still contain a forward slash if the server returns a relative path
file_name = Path(file_name).name

return file_name, content_length


Expand Down Expand Up @@ -159,6 +162,7 @@ def download_file(
try:
file_name = _get_remote_file_properties(url, request_headers)[0]
except Exception as e:
print(f"GOt error: {e}")
raise DownloadError(f"Failed to get remote file properties for {url}") from e

if "/" in file_name:
Expand Down
19 changes: 19 additions & 0 deletions projects/fal/tests/integration_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import tempfile
import unittest.mock
from pathlib import Path
from typing import Callable
from uuid import uuid4
Expand Down Expand Up @@ -562,3 +563,21 @@ def upload_to_fal_cdn() -> FalImage:
uploaded_image = upload_to_fal_cdn()

assert uploaded_image


def test_download_file_with_slash_in_filename():
from fal.toolkit.utils.download_utils import _get_remote_file_properties

test_url = "https://example.com/file/with/slash.txt"

with unittest.mock.patch(
"fal.toolkit.utils.download_utils.urlopen"
) as mock_urlopen:
# urlopen is a context manager, so we need to mock the __enter__ method
mock_response = mock_urlopen.return_value.__enter__.return_value
mock_response.headers.get_filename.return_value = "file/with/slash.txt"
mock_response.headers.get.return_value = "100"

file_name, _ = _get_remote_file_properties(test_url)

assert "/" not in file_name
Loading