From 37526800c12c4b592e10fe7513abd8a1958a3531 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 10 Jun 2020 15:16:00 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=20Move=20download=5Ffile=20into=20oct?= =?UTF-8?q?oprint.util.net?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We can use it elsewhere. --- .../plugins/pluginmanager/__init__.py | 22 ++-------------- src/octoprint/util/net.py | 26 ++++++++++++++++++- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/octoprint/plugins/pluginmanager/__init__.py b/src/octoprint/plugins/pluginmanager/__init__.py index 1fa763d3e..af9d6fb98 100644 --- a/src/octoprint/plugins/pluginmanager/__init__.py +++ b/src/octoprint/plugins/pluginmanager/__init__.py @@ -18,6 +18,7 @@ from octoprint.util import to_bytes from octoprint.util.pip import LocalPipCaller from octoprint.util.version import get_octoprint_version_string, get_octoprint_version, is_octoprint_compatible, is_python_compatible from octoprint.util.platform import get_os, is_os_compatible +from octoprint.util.net import download_file from octoprint.events import Events from flask import jsonify, make_response @@ -422,25 +423,6 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin, plugin = self._plugin_manager.plugins[plugin_name] return self.command_toggle(plugin, command) - @staticmethod - def _download_file(url, folder): - with requests.get(url, stream=True) as r: - r.raise_for_status() - if "Content-Disposition" in r.headers.keys(): - filename = re.findall("filename=(.+)", r.headers["Content-Disposition"])[0] - else: - filename = url.split("/")[-1] - - assert len(filename) > 0 - - # TODO check content-length against safety limit - - path = os.path.join(folder, filename) - with io.open(path, 'wb') as f: - for chunk in r.iter_content(chunk_size=8192): - f.write(chunk) - return path - # noinspection PyMethodMayBeStatic def _is_archive(self, path): _, ext = os.path.splitext(path) @@ -475,7 +457,7 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin, if url is not None: # fetch URL folder = tempfile.TemporaryDirectory() - path = self._download_file(url, folder.name) + path = download_file(url, folder.name) source = url source_type = "url" diff --git a/src/octoprint/util/net.py b/src/octoprint/util/net.py index e0f657924..8e62fd3fa 100644 --- a/src/octoprint/util/net.py +++ b/src/octoprint/util/net.py @@ -7,9 +7,14 @@ __copyright__ = "Copyright (C) 2018 The OctoPrint Project - Released under terms import socket import sys import netaddr -import netifaces import logging import threading +import io +import os +import re + +import netifaces +import requests _cached_check_v6 = None def check_v6(): @@ -191,3 +196,22 @@ def resolve_host(host): return [to_unicode(x[4][0]) for x in socket.getaddrinfo(host, 80)] except Exception: return [] + + +def download_file(url, folder, max_length=None): + with requests.get(url, stream=True) as r: + r.raise_for_status() + if "Content-Disposition" in r.headers.keys(): + filename = re.findall("filename=(.+)", r.headers["Content-Disposition"])[0] + else: + filename = url.split("/")[-1] + + assert len(filename) > 0 + + # TODO check content-length against safety limit + + path = os.path.join(folder, filename) + with io.open(path, 'wb') as f: + for chunk in r.iter_content(chunk_size=8192): + f.write(chunk) + return path