Make sure that image can be opened

Checking for file extension does not ensure that the image file can be
actually opened. Check if it can be loaded into a pixbuf, and convert it if
necessary.
This commit is contained in:
Balló György
2024-11-02 15:32:45 +01:00
parent 31c2a1dfee
commit fa86a25870
2 changed files with 16 additions and 13 deletions

View File

@@ -19,10 +19,10 @@
# SPDX-License-Identifier: GPL-3.0-or-later
from pathlib import Path
from typing import NamedTuple
from typing import NamedTuple, Optional
import requests
from gi.repository import GdkPixbuf, Gio
from gi.repository import GdkPixbuf, Gio, GLib
from requests.exceptions import HTTPError, SSLError
from cartridges import shared
@@ -128,9 +128,20 @@ class CoverManager(Manager):
"""
# Load source image
source = GdkPixbuf.Pixbuf.new_from_file(
str(convert_cover(image_path, resize=False))
)
try:
source = GdkPixbuf.Pixbuf.new_from_file(
str(image_path)
)
except GLib.Error:
new_path = convert_cover(image_path, resize=False)
if new_path:
source = GdkPixbuf.Pixbuf.new_from_file(
str(new_path)
)
else:
return None
source_size = ImageSize(source.get_width(), source.get_height())
cover_size = ImageSize._make(shared.image_size)

View File

@@ -35,14 +35,6 @@ def convert_cover(
if not cover_path and not pixbuf:
return None
pixbuf_extensions = set()
for pixbuf_format in GdkPixbuf.Pixbuf.get_formats():
for pixbuf_extension in pixbuf_format.get_extensions():
pixbuf_extensions.add(pixbuf_extension)
if not resize and cover_path and cover_path.suffix.lower()[1:] in pixbuf_extensions:
return cover_path
try:
with Image.open(cover_path) as image:
if getattr(image, "is_animated", False):