Просмотр исходного кода

Check if image exists and if content-length is available

Fixes #541
Peter Justin 4 лет назад
Родитель
Сommit
d76000f431
1 измененных файлов с 14 добавлено и 2 удалено
  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.
     """
-    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_max_size = 10000
     image_data = {
@@ -558,6 +566,10 @@ def check_image(url):
     img_info = get_image_info(url)
     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"]:
         error = "Image is too big! {}kb are allowed.".format(
             flaskbb_config["AVATAR_SIZE"]