Browse Source

Check if image exists and if content-length is available

Fixes #541
Peter Justin 4 years ago
parent
commit
d76000f431
1 changed files with 14 additions and 2 deletions
  1. 14 2
      flaskbb/utils/helpers.py

+ 14 - 2
flaskbb/utils/helpers.py

@@ -512,8 +512,16 @@ def get_image_info(url):
 
 
     :param url: The URL of the image.
     :param url: The URL of the image.
     """
     """
-    r = requests.get(url, stream=True)
-    image_size = r.headers.get("content-length")
+
+    try:
+        r = requests.get(url, stream=True)
+    except requests.ConnectionError:
+        return None
+
+    image_size = r.headers.get("content-length", None)
+    if image_size is None:
+        return None
+
     image_size = float(image_size) / 1000  # in kilobyte
     image_size = float(image_size) / 1000  # in kilobyte
     image_max_size = 10000
     image_max_size = 10000
     image_data = {
     image_data = {
@@ -558,6 +566,10 @@ def check_image(url):
     img_info = get_image_info(url)
     img_info = get_image_info(url)
     error = None
     error = None
 
 
+    if img_info is None:
+        error = "Couldn't get image info. Try a different hoster and/or image."
+        return error, False
+
     if img_info["size"] > flaskbb_config["AVATAR_SIZE"]:
     if img_info["size"] > flaskbb_config["AVATAR_SIZE"]:
         error = "Image is too big! {}kb are allowed.".format(
         error = "Image is too big! {}kb are allowed.".format(
             flaskbb_config["AVATAR_SIZE"]
             flaskbb_config["AVATAR_SIZE"]