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:
@@ -19,10 +19,10 @@
|
|||||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import NamedTuple
|
from typing import NamedTuple, Optional
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from gi.repository import GdkPixbuf, Gio
|
from gi.repository import GdkPixbuf, Gio, GLib
|
||||||
from requests.exceptions import HTTPError, SSLError
|
from requests.exceptions import HTTPError, SSLError
|
||||||
|
|
||||||
from cartridges import shared
|
from cartridges import shared
|
||||||
@@ -128,9 +128,20 @@ class CoverManager(Manager):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# Load source image
|
# Load source image
|
||||||
|
try:
|
||||||
source = GdkPixbuf.Pixbuf.new_from_file(
|
source = GdkPixbuf.Pixbuf.new_from_file(
|
||||||
str(convert_cover(image_path, resize=False))
|
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())
|
source_size = ImageSize(source.get_width(), source.get_height())
|
||||||
cover_size = ImageSize._make(shared.image_size)
|
cover_size = ImageSize._make(shared.image_size)
|
||||||
|
|
||||||
|
|||||||
@@ -35,14 +35,6 @@ def convert_cover(
|
|||||||
if not cover_path and not pixbuf:
|
if not cover_path and not pixbuf:
|
||||||
return None
|
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:
|
try:
|
||||||
with Image.open(cover_path) as image:
|
with Image.open(cover_path) as image:
|
||||||
if getattr(image, "is_animated", False):
|
if getattr(image, "is_animated", False):
|
||||||
|
|||||||
Reference in New Issue
Block a user