From 45769bc5b440183dd5e46b40ec2cdd364a3a01db Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Thu, 18 Apr 2024 18:42:42 +0200 Subject: ✨ imports: allow media import from a web location MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ishtar_common/utils.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'ishtar_common/utils.py') diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index f734a9b2d..08ef84831 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -644,6 +644,23 @@ def is_downloadable(curl): return True +def get_file_from_link(file_link): + """ + return filename and temp_file object from a web link + """ + try: + request = requests.get(file_link, stream=True) + except requests.exceptions.RequestException: + raise ValueError() + ntf = tempfile.NamedTemporaryFile() + for block in request.iter_content(1024 * 8): + if not block: + break + ntf.write(block) + file_name = file_link.split("/")[-1] + return file_name, ntf + + def get_current_year(): return datetime.datetime.now().year -- cgit v1.2.3