Catch UnidentifiedImageError if non-image uploaded

This commit is contained in:
freamon 2025-01-17 22:47:40 +00:00
parent 6df39c02f3
commit ec232d9a17

View file

@ -12,7 +12,7 @@ from app.constants import DOWNVOTE_ACCEPT_ALL, DOWNVOTE_ACCEPT_MEMBERS, DOWNVOTE
DOWNVOTE_ACCEPT_TRUSTED
from app.models import Community, utcnow
from app.utils import domain_from_url, MultiCheckboxField
from PIL import Image, ImageOps
from PIL import Image, ImageOps, UnidentifiedImageError
from io import BytesIO
import pytesseract
@ -173,6 +173,8 @@ class CreateImageForm(CreatePostForm):
image_text = pytesseract.image_to_string(Image.open(BytesIO(uploaded_file.read())).convert('L'))
except FileNotFoundError as e:
image_text = ''
except UnidentifiedImageError as e:
image_text = ''
if 'Anonymous' in image_text and (
'No.' in image_text or ' N0' in image_text): # chan posts usually contain the text 'Anonymous' and ' No.12345'
self.image_file.errors.append(