Skip to content

Commit

Permalink
Add explicit path resolution for __load_media_file to maybe circumven…
Browse files Browse the repository at this point in the history
…t a mystery problem in some configurations.
  • Loading branch information
halcy authored and halcy committed Apr 23, 2023
1 parent 6f7d457 commit 34e97a9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 3 additions & 1 deletion mastodon/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
magic = None

try:
from pathlib import PurePath
from pathlib import PurePath, Path
except:
class PurePath:
pass
class Path:
pass

9 changes: 6 additions & 3 deletions mastodon/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from .errors import MastodonNetworkError, MastodonIllegalArgumentError, MastodonRatelimitError, MastodonNotFoundError, \
MastodonUnauthorizedError, MastodonInternalServerError, MastodonBadGatewayError, MastodonServiceUnavailableError, \
MastodonGatewayTimeoutError, MastodonServerError, MastodonAPIError, MastodonMalformedEventError
from .compat import urlparse, magic, PurePath
from .compat import urlparse, magic, PurePath, Path
from .defaults import _DEFAULT_STREAM_TIMEOUT, _DEFAULT_STREAM_RECONNECT_WAIT_SEC


Expand Down Expand Up @@ -625,11 +625,14 @@ def __guess_type(self, media_file):
def __load_media_file(self, media_file, mime_type=None, file_name=None):
if isinstance(media_file, PurePath):
media_file = str(media_file)
if isinstance(media_file, str):
try: # Explicitly resolve to canonical for robustness. This can and will fail if Path isn't available because python too old.
media_file = Path(media_file).resolve()
except:
pass
if isinstance(media_file, str) and os.path.isfile(media_file):
mime_type = self.__guess_type(media_file)
media_file = open(media_file, 'rb')
elif isinstance(media_file, str) and os.path.isfile(media_file):
media_file = open(media_file, 'rb')
if mime_type is None:
raise MastodonIllegalArgumentError('Could not determine mime type or data passed directly without mime type.')
if file_name is None:
Expand Down

0 comments on commit 34e97a9

Please sign in to comment.