Support pathlib.Path on download/upload

This commit is contained in:
Lonami Exo 2018-06-16 17:01:20 +02:00
parent cbd5594dba
commit 7cfecfaf21
2 changed files with 8 additions and 1 deletions

View File

@ -2,10 +2,10 @@ import datetime
import io import io
import logging import logging
import os import os
import pathlib
from .users import UserMethods from .users import UserMethods
from .. import utils, helpers, errors from .. import utils, helpers, errors
from ..crypto import CdnDecrypter
from ..tl import TLObject, types, functions from ..tl import TLObject, types, functions
__log__ = logging.getLogger(__name__) __log__ = logging.getLogger(__name__)
@ -367,6 +367,9 @@ class DownloadMethods(UserMethods):
If any modification is made to the path, this method will If any modification is made to the path, this method will
ensure that no existing file will be overwritten. ensure that no existing file will be overwritten.
""" """
if isinstance(file, pathlib.Path):
file = str(file.absolute())
if file is not None and not isinstance(file, str): if file is not None and not isinstance(file, str):
# Probably a stream-like object, we cannot set a filename here # Probably a stream-like object, we cannot set a filename here
return file return file

View File

@ -2,6 +2,7 @@ import hashlib
import io import io
import logging import logging
import os import os
import pathlib
import warnings import warnings
from io import BytesIO from io import BytesIO
from mimetypes import guess_type from mimetypes import guess_type
@ -361,6 +362,9 @@ class UploadMethods(MessageParseMethods, UserMethods):
if not file: if not file:
return None, None return None, None
if isinstance(file, pathlib.Path):
file = str(file.absolute())
if not isinstance(file, (str, bytes, io.IOBase)): if not isinstance(file, (str, bytes, io.IOBase)):
# The user may pass a Message containing media (or the media, # The user may pass a Message containing media (or the media,
# or anything similar) that should be treated as a file. Try # or anything similar) that should be treated as a file. Try