|
@@ -5,18 +5,72 @@ from .. import core, helpers
|
|
from ..context import context
|
|
from ..context import context
|
|
|
|
|
|
|
|
|
|
-def download(src: Union[str, Path, bytes], filename: Optional[str] = None, media_type: str = '') -> None:
|
|
|
|
- """Download
|
|
|
|
|
|
+class Download:
|
|
|
|
+ """Download functions
|
|
|
|
|
|
- Function to trigger the download of a file, URL or bytes.
|
|
|
|
|
|
+ These functions allow you to download files, URLs or raw data.
|
|
|
|
|
|
- :param src: target URL, local path of a file or raw data which should be downloaded
|
|
|
|
- :param filename: name of the file to download (default: name of the file on the server)
|
|
|
|
- :param media_type: media type of the file to download (default: "")
|
|
|
|
|
|
+ *Added in version 2.x.0*
|
|
"""
|
|
"""
|
|
- if not isinstance(src, bytes):
|
|
|
|
- if helpers.is_file(src):
|
|
|
|
- src = core.app.add_static_file(local_file=src, single_use=True)
|
|
|
|
|
|
+
|
|
|
|
+ def __call__(self, src: Union[str, Path, bytes], filename: Optional[str] = None, media_type: str = '') -> None:
|
|
|
|
+ """Download
|
|
|
|
+
|
|
|
|
+ Function to trigger the download of a file, URL or bytes.
|
|
|
|
+
|
|
|
|
+ :param src: target URL, local path of a file or raw data which should be downloaded
|
|
|
|
+ :param filename: name of the file to download (default: name of the file on the server)
|
|
|
|
+ :param media_type: media type of the file to download (default: "")
|
|
|
|
+ """
|
|
|
|
+ if isinstance(src, bytes):
|
|
|
|
+ self.content(src, filename, media_type)
|
|
|
|
+ elif helpers.is_file(src):
|
|
|
|
+ self.file(src, filename, media_type)
|
|
else:
|
|
else:
|
|
- src = str(src)
|
|
|
|
- context.client.download(src, filename, media_type)
|
|
|
|
|
|
+ assert isinstance(src, str)
|
|
|
|
+ self.from_url(src, filename, media_type)
|
|
|
|
+
|
|
|
|
+ def file(self, path: Union[str, Path], filename: Optional[str] = None, media_type: str = '') -> None:
|
|
|
|
+ """Download file from local path
|
|
|
|
+
|
|
|
|
+ Function to trigger the download of a file.
|
|
|
|
+
|
|
|
|
+ *Added in version 2.x.0*
|
|
|
|
+
|
|
|
|
+ :param path: local path of the file
|
|
|
|
+ :param filename: name of the file to download (default: name of the file on the server)
|
|
|
|
+ :param media_type: media type of the file to download (default: "")
|
|
|
|
+ """
|
|
|
|
+ src = core.app.add_static_file(local_file=path, single_use=True)
|
|
|
|
+ context.client.download(src, filename, media_type)
|
|
|
|
+
|
|
|
|
+ def from_url(self, url: str, filename: Optional[str] = None, media_type: str = '') -> None:
|
|
|
|
+ """Download from a URL
|
|
|
|
+
|
|
|
|
+ Function to trigger the download from a URL.
|
|
|
|
+
|
|
|
|
+ *Added in version 2.x.0*
|
|
|
|
+
|
|
|
|
+ :param url: URL
|
|
|
|
+ :param filename: name of the file to download (default: name of the file on the server)
|
|
|
|
+ :param media_type: media type of the file to download (default: "")
|
|
|
|
+ """
|
|
|
|
+ context.client.download(url, filename, media_type)
|
|
|
|
+
|
|
|
|
+ def content(self, content: Union[bytes, str], filename: Optional[str] = None, media_type: str = '') -> None:
|
|
|
|
+ """Download raw bytes or string content
|
|
|
|
+
|
|
|
|
+ Function to trigger the download of raw data.
|
|
|
|
+
|
|
|
|
+ *Added in version 2.x.0*
|
|
|
|
+
|
|
|
|
+ :param content: raw bytes or string
|
|
|
|
+ :param filename: name of the file to download (default: name of the file on the server)
|
|
|
|
+ :param media_type: media type of the file to download (default: "")
|
|
|
|
+ """
|
|
|
|
+ if isinstance(content, str):
|
|
|
|
+ content = content.encode('utf-8')
|
|
|
|
+ context.client.download(content, filename, media_type)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+download = Download()
|