Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/thumbnails.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "thumbnails.h"
#include "mounter.h"

#include <QBuffer>
#include <QCryptographicHash>
Expand Down Expand Up @@ -81,10 +82,12 @@ QStringList expandCommand(const QString &command, const QString &input, const QS

} // namespace

Thumbnails::Thumbnails(const QString &cacheDirectory, const QStringList &thumbnailerDirectories)
Thumbnails::Thumbnails(const QString &cacheDirectory, const QStringList &thumbnailerDirectories,
const QString &remoteRoot)
: m_cache(cacheDirectory.isEmpty()
? QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/thumbnails"
: cacheDirectory) {
: cacheDirectory),
m_remoteRoot(remoteRoot.isEmpty() ? Mounter::root() : remoteRoot) {
QStringList directories = thumbnailerDirectories;
if (directories.isEmpty()) {
// Later directories win, so the user's own thumbnailers override the
Expand Down Expand Up @@ -342,7 +345,18 @@ QImage Thumbnails::runThumbnailer(const QString &command, const QString &path, i
if (!scratch.isValid())
return {};
const QString output = scratch.filePath("thumbnail.png");
QStringList args = expandCommand(command, path, output, pixels);
// A thumbnailer given a URI asks GIO for the file, and GIO turns one on
// a GVfs mount (another machine's) back into its network address,
// sftp://…, which most can't open, Papers' among them. A link here reads
// through the mount the same, and GIO leaves it a file.
QString input = path;
if (path.startsWith(m_remoteRoot + "/")) {
const QString suffix = QFileInfo(path).suffix();
const QString link = scratch.filePath(suffix.isEmpty() ? "input" : "input." + suffix);
if (QFile::link(path, link))
input = link;
}
QStringList args = expandCommand(command, input, output, pixels);
if (args.isEmpty())
return {};
const QString program = args.takeFirst();
Expand Down Expand Up @@ -370,14 +384,20 @@ void Thumbnails::remember(QImage image, const QString &path, int size, const QDa
save(std::move(image), cachePath(path, size), path, modified);
}

// Markers say which round of thumbnailing failed: ones from before a fix
// (round 2: files on other machines) are tried again once.
static const QString failedRound = QStringLiteral("2");

void Thumbnails::rememberFailure(const QString &path, const QDateTime &modified) const {
QImage marker(1, 1, QImage::Format_ARGB32);
marker.fill(Qt::transparent);
marker.setText("X-Folio::Round", failedRound);
save(std::move(marker), failPath(path), path, modified);
}

bool Thumbnails::failedBefore(const QString &path, const QDateTime &modified) const {
return madeFor(QImage(failPath(path)), modified);
const QImage marker(failPath(path));
return madeFor(marker, modified) && marker.text("X-Folio::Round") == failedRound;
}

// Whether a cached thumbnail (or failure marker) was made from the file as it
Expand Down
6 changes: 5 additions & 1 deletion src/thumbnails.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
// tile asks for it: there is no background crawl.
class Thumbnails {
public:
explicit Thumbnails(const QString &cacheDirectory = {}, const QStringList &thumbnailerDirectories = {});
// `remoteRoot` is where GVfs mounts other machines (Mounter::root());
// tests give their own.
explicit Thumbnails(const QString &cacheDirectory = {}, const QStringList &thumbnailerDirectories = {},
const QString &remoteRoot = {});

// Returns a thumbnail no larger than `size` on its longest side, from the
// cache when it is fresh, or a null image when none can be made.
Expand Down Expand Up @@ -56,6 +59,7 @@ class Thumbnails {
static bool madeFor(const QImage &image, const QDateTime &modified);

QString m_cache;
QString m_remoteRoot;
QHash<QString, QString> m_thumbnailers;
};

Expand Down
56 changes: 56 additions & 0 deletions tests/folio_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6231,6 +6231,62 @@ private slots:
// the one it names.
write(dir.filePath("photo.heic"), "not a real heic");
QCOMPARE(thumbnails.load(dir.filePath("photo.heic"), "image/*", 128).size(), QSize(64, 32));

// What failed is remembered with the round it failed in, and one
// remembered from before a fix (files on other machines) is tried
// again.
write(dir.filePath("broken.thumbnailer"), "[Thumbnailer Entry]\nExec=/bin/false %o\nMimeType=image/x-broken;\n");
Thumbnails withBroken(dir.filePath("cache"), {dir.path()});
write(dir.filePath("never.broken"), "x");
QVERIFY(withBroken.load(dir.filePath("never.broken"), "image/x-broken", 128).isNull());
const QImage marker(dir.filePath("cache/fail/folio/" + Thumbnails::cacheKey(dir.filePath("never.broken"))));
QCOMPARE(marker.text("X-Folio::Round"), QString("2"));
// An old marker, without its round, for a picture that can be made.
const QFileInfo picture(dir.filePath("frame.png"));
QImage old(1, 1, QImage::Format_ARGB32);
old.setText("Thumb::MTime", QString::number(picture.lastModified().toSecsSinceEpoch()));
old.setText("X-Folio::MTime", QString::number(picture.lastModified().toMSecsSinceEpoch()));
QVERIFY(old.save(dir.filePath("cache/fail/folio/" + Thumbnails::cacheKey(picture.filePath()))));
QVERIFY(!withBroken.load(picture.filePath(), "image/png", 128).isNull());
}

// A file on another machine goes to a thumbnailer given a URI by a link
// to it, a file:// one with its extension: GIO would turn the mount's
// own path back into sftp://, which most thumbnailers can't open. The
// thumbnail is still kept under the file's own path.
void thumbnailsReachOtherMachinesByALink()
{
QTemporaryDir dir;
QImage frame(64, 32, QImage::Format_RGB32);
frame.fill(Qt::green);
QVERIFY(frame.save(dir.filePath("frame.png")));
// Says what it was given, and makes the frame its thumbnail.
write(dir.filePath("record"), "#!/bin/sh\necho \"$1\" > \"" + dir.filePath("given").toUtf8()
+ "\"\ncp \"" + dir.filePath("frame.png").toUtf8() + "\" \"$2\"\n");
QVERIFY(QFile::setPermissions(dir.filePath("record"), QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner));
write(dir.filePath("record.thumbnailer"),
"[Thumbnailer Entry]\nExec=" + dir.filePath("record").toUtf8() + " %u %o\nMimeType=application/pdf;\n");
QDir(dir.path()).mkpath("gvfs/sftp:host=far/home");
const QString remote = dir.filePath("gvfs/sftp:host=far/home/Order 42.pdf");
write(remote, "%PDF");
Thumbnails thumbnails(dir.filePath("cache"), {dir.path()}, dir.filePath("gvfs"));

QVERIFY(!thumbnails.load(remote, "application/pdf", 128).isNull());
QFile given(dir.filePath("given"));
QVERIFY(given.open(QIODevice::ReadOnly));
const QUrl url(QString::fromUtf8(given.readAll()).trimmed());
QCOMPARE(url.scheme(), QString("file"));
QVERIFY2(!url.toLocalFile().startsWith(dir.filePath("gvfs")), qPrintable(url.toString()));
QCOMPARE(QFileInfo(url.toLocalFile()).fileName(), QString("input.pdf"));
QVERIFY(QFile::exists(dir.filePath("cache/normal/" + Thumbnails::cacheKey(remote))));

// A file here goes as it is.
const QString local = dir.filePath("Order 43.pdf");
write(local, "%PDF");
QVERIFY(!thumbnails.load(local, "application/pdf", 128).isNull());
given.close();
QVERIFY(given.open(QIODevice::ReadOnly));
QCOMPARE(QUrl(QString::fromUtf8(given.readAll()).trimmed()).toLocalFile(), local);
}

void thumbnailsSetTextOnAPage()
Expand Down
Loading