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

Determine filetype from URL through HEAD request #270

Merged
merged 1 commit into from
Oct 17, 2023
Merged
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
22 changes: 18 additions & 4 deletions renumics/spotlight/data_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
from typing import Any, List, Optional, Set, Union, cast
import numpy as np
import filetype
import requests
import trimesh
import PIL.Image
import validators

from renumics.spotlight.cache import external_data_cache
from renumics.spotlight.data_source import DataSource
Expand Down Expand Up @@ -231,10 +233,22 @@ def _guess_value_dtype(value: Any) -> Optional[DType]:
if isinstance(value, np.ndarray):
return ArrayDType(value.shape)

if isinstance(value, bytes) or (is_pathtype(value) and os.path.isfile(value)):
kind = filetype.guess(value)
if kind is not None:
mime_group = kind.mime.split("/")[0]
if isinstance(value, bytes) or is_pathtype(value):
mimetype: Optional[str] = None
if isinstance(value, bytes) or (is_pathtype(value) and os.path.isfile(value)):
kind = filetype.guess(value)
if kind is not None:
mimetype = kind.mime
elif isinstance(value, str) and validators.url(value):
try:
response = requests.head(value, timeout=1)
except requests.ReadTimeout:
...
else:
if response.ok:
mimetype = response.headers.get("Content-Type")
if mimetype is not None:
mime_group = mimetype.split("/")[0]
if mime_group == "image":
return image_dtype
if mime_group == "audio":
Expand Down