Skip to content

Commit

Permalink
Improve thumbnailing in Flatpak
Browse files Browse the repository at this point in the history
Manually fall back to GdkPixbuf if possible
  • Loading branch information
kra-mo committed Jan 31, 2024
1 parent 904d96a commit 427c269
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
4 changes: 3 additions & 1 deletion hyperplane/utils/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def get_paste_gfile(gfile: Gio.File, number_only: bool = False) -> Gio.File:
logging.error(
'Cannot get copy GFile for "%s": File has no path.', gfile.get_uri()
)
return
return gfile

if (
gfile.query_info(
Expand Down Expand Up @@ -365,6 +365,8 @@ def get_paste_gfile(gfile: Gio.File, number_only: bool = False) -> Gio.File:
).exists():
return Gio.File.new_for_path(str(copy_path))

return gfile


def get_gfile_display_name(gfile: Gio.File) -> str:
"""Gets the display name for `gfile`."""
Expand Down
55 changes: 33 additions & 22 deletions hyperplane/utils/thumbnail.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import logging
from typing import Any, Callable

from gi.repository import Gdk, Gio, GLib, GnomeDesktop
from gi.repository import Gdk, GdkPixbuf, Gio, GLib, GnomeDesktop

from hyperplane.utils.files import get_gfile_path


def generate_thumbnail(
Expand All @@ -32,7 +34,9 @@ def generate_thumbnail(
If the thumbnail generation fails, `callback` is called with None and *args.
"""
factory = GnomeDesktop.DesktopThumbnailFactory()
factory = GnomeDesktop.DesktopThumbnailFactory.new(
GnomeDesktop.DesktopThumbnailSize.LARGE
)
uri = gfile.get_uri()

try:
Expand All @@ -54,32 +58,39 @@ def generate_thumbnail(
try:
thumbnail = factory.generate_thumbnail(uri, content_type)
except GLib.Error as error:
if not error.matches(Gio.io_error_quark(), Gio.IOErrorEnum.NOT_FOUND):
logging.debug("Cannot thumbnail: %s", error)
callback(None, *args)
factory.create_failed_thumbnail(uri, mtime)
return
if error.matches(Gio.io_error_quark(), Gio.IOErrorEnum.NOT_FOUND):
# If it cannot get a path for the URI, try the target URI
try:
target_uri = gfile.query_info(
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI,
Gio.FileQueryInfoFlags.NONE,
).get_attribute_string(Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI)

# If it cannot get a path for the URI, try the target URI
# TODO: I cannot currently test thumbnailing. Do I need this?
try:
target_uri = gfile.query_info(
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI,
Gio.FileQueryInfoFlags.NONE,
).get_attribute_string(Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI)
if not target_uri:
logging.debug("Cannot thumbnail: %s", error)
callback(None, *args)
return

if not target_uri:
logging.debug("Cannot thumbnail: %s", error)
thumbnail = factory.generate_thumbnail(target_uri, content_type)
except GLib.Error as new_error:
logging.debug("Cannot thumbnail: %s", new_error)
callback(None, *args)
if not new_error.matches(
Gio.io_error_quark(), Gio.IOErrorEnum.NOT_FOUND
):
factory.create_failed_thumbnail(uri, mtime)
factory.create_failed_thumbnail(target_uri, mtime)
return

thumbnail = factory.generate_thumbnail(target_uri, content_type)
except GLib.Error as new_error:
logging.debug("Cannot thumbnail: %s", new_error)
try:
# Fallback to GdkPixbuf
thumbnail = GdkPixbuf.Pixbuf.new_from_file_at_size(
str(get_gfile_path(gfile)), 256, 256
)
except (GLib.Error, FileNotFoundError):
logging.debug("Cannot thumbnail: %s", error)
callback(None, *args)
if not new_error.matches(Gio.io_error_quark(), Gio.IOErrorEnum.NOT_FOUND):
factory.create_failed_thumbnail(uri, mtime)
factory.create_failed_thumbnail(target_uri, mtime)
factory.create_failed_thumbnail(uri, mtime)
return

if not thumbnail:
Expand Down
1 change: 1 addition & 0 deletions page.kramo.Hyperplane.Devel.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"--talk-name=org.gtk.vfs.*",
"--talk-name=org.freedesktop.Flatpak",
"--filesystem=xdg-run/gvfsd",
"--filesystem=xdg-cache/thumbnails",
"--own-name=org.freedesktop.FileManager1"
],
"cleanup" : [
Expand Down

0 comments on commit 427c269

Please sign in to comment.