Skip to content

Commit 8896136

Browse files
mattttechnillogue
authored andcommitted
Add missing tests for data: URIs
1 parent 6a0b5a6 commit 8896136

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

Diff for: python/cog/types.py

+8
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,14 @@ def get_filename_from_urlopen(resp: urllib.response.addinfourl) -> str: # type:
319319
def get_filename_from_url(url: str) -> str:
320320
parsed_url = urllib.parse.urlparse(url)
321321

322+
if parsed_url.scheme == "data":
323+
resp = urllib.request.urlopen(url) # noqa: S310
324+
mime_type = resp.headers.get_content_type()
325+
extension = mimetypes.guess_extension(mime_type)
326+
if extension is None:
327+
return "file"
328+
return "file" + extension
329+
322330
filename = os.path.basename(parsed_url.path)
323331
filename = urllib.parse.unquote_plus(filename)
324332

Diff for: python/tests/test_types.py

+13
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,19 @@ def test_urlfile_can_be_pickled_even_once_loaded():
9090
"https://example.com/%E1%9E%A0_%E1%9E%8F_%E1%9E%A2_%E1%9E%9C_%E1%9E%94_%E1%9E%93%E1%9E%87_%E1%9E%80_%E1%9E%9A%E1%9E%9F_%E1%9E%82%E1%9E%8F%E1%9E%9A%E1%9E%94%E1%9E%9F_%E1%9E%96_%E1%9E%9A_%E1%9E%99_%E1%9E%9F_%E1%9E%98_%E1%9E%93%E1%9E%A2_%E1%9E%8E_%E1%9E%85%E1%9E%98_%E1%9E%9B_Why_Was_The_Death_Of_Jesus_So_Powerful_.m4a",
9191
"ហ_ត_អ_វ_ប_នជ_ក_រស_គតរបស_ព_រ_យ_ស_ម_នអ_ណ_ចម_ល_Why_Was_The_Death_Of_Jesus_So_Powerful_.m4a",
9292
),
93+
# Data URIs
94+
(
95+
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==",
96+
"file.png",
97+
),
98+
(
99+
"data:text/plain,hello world",
100+
"file.txt",
101+
),
102+
(
103+
"data:application/data;base64,aGVsbG8gd29ybGQ=",
104+
"file",
105+
),
93106
# Illegal characters
94107
("https://example.com/nulbytes\u0000.wav", "nulbytes_.wav"),
95108
("https://example.com/nulbytes%00.wav", "nulbytes_.wav"),

0 commit comments

Comments
 (0)