Skip to content

Commit

Permalink
Gestion Erreur création de thumbnail : retourne 404
Browse files Browse the repository at this point in the history
  • Loading branch information
amandine-sahl committed Aug 7, 2024
1 parent f7e63c8 commit 90f6108
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
22 changes: 15 additions & 7 deletions apptax/taxonomie/filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from flask import current_app

import urllib.request

from urllib.error import HTTPError
from apptax.utils.errors import TaxhubError

logger = logging.getLogger()

Expand Down Expand Up @@ -92,7 +93,11 @@ def create_thumb(self, media, size, force=False, regenerate=False):
return thumbpath_full

# Get Image
img = self._get_image_object(media)
try:
img = self._get_image_object(media)
except TaxhubError as e:
return None

# Création du thumbnail
resizeImg = resize_thumbnail(img, (size[0], size[1], force))
# Sauvegarde de l'image
Expand All @@ -112,24 +117,27 @@ def url_to_image(url):
"""
Récupération d'une image à partir d'une url
"""
local_filename, headers = urllib.request.urlretrieve(url)
try:
local_filename, headers = urllib.request.urlretrieve(url)
except HTTPError as e:
raise TaxhubError(e.reason)
try:
img = Image.open(local_filename)
urllib.request.urlcleanup()
return img
except IOError:
raise Exception("Media is not an image")
return img
raise TaxhubError("Media is not an image")


def resize_thumbnail(image, size):
(width, height, force) = size

if image.size[0] > width or image.size[1] > height:
if force:
return ImageOps.fit(image, (width, height), Image.ANTIALIAS)
return ImageOps.fit(image, (width, height))
else:
thumb = image.copy()
thumb.thumbnail((width, height), Image.ANTIALIAS)
thumb.thumbnail((width, height))
return thumb

return image
11 changes: 7 additions & 4 deletions apptax/taxonomie/routestmedias.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
from pathlib import Path
import os
from flask import json, Blueprint, request, current_app, send_file
from flask import json, Blueprint, request, current_app, send_file, abort


from .models import TMedias
Expand Down Expand Up @@ -83,6 +83,9 @@ def getThumbnail_tmedias(id_media):
regenerate = True

thumbpath = FILEMANAGER.create_thumb(media, size, force, regenerate)
return send_file(
os.path.join(Path(current_app.config["MEDIA_FOLDER"]).absolute(), "taxhub", thumbpath)
)
if thumbpath:
return send_file(
os.path.join(Path(current_app.config["MEDIA_FOLDER"]).absolute(), "taxhub", thumbpath)
)
else:
abort(404)
7 changes: 7 additions & 0 deletions apptax/utils/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
Erreurs propres à Taxhub
"""


class TaxhubError(Exception):
pass

0 comments on commit 90f6108

Please sign in to comment.