From a9f9ec7b4568e949775c98a80334ac7b6b2bd978 Mon Sep 17 00:00:00 2001 From: GM Date: Wed, 11 Nov 2026 00:45:58 -0400 Subject: [PATCH 01/12] Check a cached thumbnail belongs to its file A cache entry or failure marker was trusted on its modified time alone, so one made for another file with the same time was shown as this file's. It now has to carry this file's Thumb::URI as well, as the thumbnail spec asks a reader to check. --- src/thumbnails.cpp | 10 ++++++---- src/thumbnails.h | 2 +- tests/folio_tests.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/thumbnails.cpp b/src/thumbnails.cpp index 35fcf1ed..47004cf5 100644 --- a/src/thumbnails.cpp +++ b/src/thumbnails.cpp @@ -157,7 +157,7 @@ QImage Thumbnails::load(const QString &path, const QString &mime, int size) cons return {}; const QDateTime modified = info.lastModified(); - if (const QImage image(cachePath(path, size)); madeFor(image, modified)) + if (const QImage image(cachePath(path, size)); madeFor(image, path, modified)) return image; if (failedBefore(path, modified)) return {}; @@ -317,7 +317,7 @@ void Thumbnails::rememberFailure(const QString &path, const QDateTime &modified) } bool Thumbnails::failedBefore(const QString &path, const QDateTime &modified) const { - return madeFor(QImage(failPath(path)), modified); + return madeFor(QImage(failPath(path)), path, modified); } // Whether a cached thumbnail (or failure marker) was made from the file as it @@ -326,9 +326,11 @@ bool Thumbnails::failedBefore(const QString &path, const QDateTime &modified) co // the milliseconds. One another app made goes by the seconds alone. // QImageReader::text() splits keys at ':', so these are only readable from // the decoded image. -bool Thumbnails::madeFor(const QImage &image, const QDateTime &modified) { +bool Thumbnails::madeFor(const QImage &image, const QString &path, const QDateTime &modified) { const QString exact = image.text("X-Folio::MTime"); - return !image.isNull() && image.text("Thumb::MTime") == QString::number(modified.toSecsSinceEpoch()) + // The spec asks a reader to check that Thumb::URI names the file too. + return !image.isNull() && image.text("Thumb::URI") == fileUri(path) + && image.text("Thumb::MTime") == QString::number(modified.toSecsSinceEpoch()) && (exact.isEmpty() || exact == QString::number(modified.toMSecsSinceEpoch())); } diff --git a/src/thumbnails.h b/src/thumbnails.h index 919cc2da..935ac586 100644 --- a/src/thumbnails.h +++ b/src/thumbnails.h @@ -41,7 +41,7 @@ class Thumbnails { void rememberFailure(const QString &path, const QDateTime &modified) const; bool failedBefore(const QString &path, const QDateTime &modified) const; void save(QImage image, const QString &target, const QString &path, const QDateTime &modified) const; - static bool madeFor(const QImage &image, const QDateTime &modified); + static bool madeFor(const QImage &image, const QString &path, const QDateTime &modified); QString m_cache; QHash m_thumbnailers; diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index c26edf31..3cd065f1 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -5038,6 +5038,48 @@ private slots: QCOMPARE(Thumbnails::bucketFor(512), QString("x-large")); } + void thumbnailsRejectACacheEntryForAnotherFile() + { + QTemporaryDir dir; + QImage picture(800, 400, QImage::Format_RGB32); + picture.fill(Qt::red); + const QString file = dir.filePath("wide.png"); + QVERIFY(picture.save(file)); + + Thumbnails thumbnails(dir.filePath("cache"), {dir.filePath("none")}); + thumbnails.load(file, "image/png", 256); + const QString cached = dir.filePath("cache/large/" + Thumbnails::cacheKey(file)); + + // A fresh entry tagged for another file, or for none, is made again. + for (const QString &uri : {QString("file:///elsewhere/wide.png"), QString()}) { + QImage foreign(cached); + foreign.fill(Qt::blue); + foreign.setText("Thumb::URI", uri); + QVERIFY(foreign.save(cached)); + QCOMPARE(thumbnails.load(file, "image/png", 256).pixelColor(0, 0), QColor(Qt::red)); + QCOMPARE(QImage(cached).text("Thumb::URI"), QUrl::fromLocalFile(file).toString(QUrl::FullyEncoded)); + } + } + + void thumbnailsRejectAFailureMarkerForAnotherFile() + { + QTemporaryDir dir; + Thumbnails thumbnails(dir.filePath("cache"), {dir.filePath("none")}); + + // A marker with the file's time but another file's URI, or none, doesn't stop its thumbnail. + for (const QString &uri : {QString("file:///elsewhere/photo.png"), QString()}) { + const QString file = dir.filePath(uri.isEmpty() ? "unnamed.png" : "named.png"); + QVERIFY(QImage(64, 32, QImage::Format_RGB32).save(file)); + const QString marker = dir.filePath("cache/fail/folio/" + Thumbnails::cacheKey(file)); + QVERIFY(QDir().mkpath(QFileInfo(marker).absolutePath())); + QImage foreign(1, 1, QImage::Format_ARGB32); + foreign.setText("Thumb::URI", uri); + foreign.setText("Thumb::MTime", QString::number(QFileInfo(file).lastModified().toSecsSinceEpoch())); + QVERIFY(foreign.save(marker)); + QVERIFY(!thumbnails.load(file, "image/png", 128).isNull()); + } + } + void thumbnailsFollowAPictureSavedOver() { // Qt keeps the images it has shown by their URL, and a re-read makes From cabd11e6ca33e02e898e4f06d06babe744574d35 Mon Sep 17 00:00:00 2001 From: GM Date: Wed, 11 Nov 2026 00:45:58 -0400 Subject: [PATCH 02/12] Try again what an older Folio couldn't thumbnail Failure markers went to fail/folio, so a file Folio could not thumbnail stayed blank after it learned to, as it does for camera RAW here. They now go to the spec's fail/-, as fail/folio-2: the number is raised whenever Folio learns to draw more. --- src/thumbnails.cpp | 2 +- src/thumbnails.h | 2 ++ tests/folio_tests.cpp | 32 +++++++++++++++++++++++++++++--- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/thumbnails.cpp b/src/thumbnails.cpp index 47004cf5..db665591 100644 --- a/src/thumbnails.cpp +++ b/src/thumbnails.cpp @@ -144,7 +144,7 @@ QString Thumbnails::cachePath(const QString &path, int size) const { } QString Thumbnails::failPath(const QString &path) const { - return m_cache + "/fail/folio/" + cacheKey(path); + return m_cache + "/" + failures + "/" + cacheKey(path); } QImage Thumbnails::load(const QString &path, const QString &mime, int size) const { diff --git a/src/thumbnails.h b/src/thumbnails.h index 935ac586..819cb585 100644 --- a/src/thumbnails.h +++ b/src/thumbnails.h @@ -24,6 +24,8 @@ class Thumbnails { QImage load(const QString &path, const QString &mime, int size) const; static QString cacheKey(const QString &path); + // The spec's fail/-: the number is raised when Folio learns to draw more, so old failures are tried again. + static inline const QString failures = QStringLiteral("fail/folio-2"); static QString bucketFor(int size); // A text file's first lines set on a page in the given colors. Made // fresh each time rather than cached, since the colors follow the theme. diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 3cd065f1..19bb5a03 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -5070,7 +5070,7 @@ private slots: for (const QString &uri : {QString("file:///elsewhere/photo.png"), QString()}) { const QString file = dir.filePath(uri.isEmpty() ? "unnamed.png" : "named.png"); QVERIFY(QImage(64, 32, QImage::Format_RGB32).save(file)); - const QString marker = dir.filePath("cache/fail/folio/" + Thumbnails::cacheKey(file)); + const QString marker = dir.filePath("cache/" + Thumbnails::failures + "/" + Thumbnails::cacheKey(file)); QVERIFY(QDir().mkpath(QFileInfo(marker).absolutePath())); QImage foreign(1, 1, QImage::Format_ARGB32); foreign.setText("Thumb::URI", uri); @@ -5132,7 +5132,33 @@ private slots: // A thumbnailer that fails leaves a failure marker, so it isn't retried. write(dir.filePath("clip.mp4"), "not a video"); QVERIFY(thumbnails.load(dir.filePath("clip.mp4"), "video/mp4", 128).isNull()); - QVERIFY(QFile::exists(dir.filePath("cache/fail/folio/" + Thumbnails::cacheKey(dir.filePath("clip.mp4"))))); + QVERIFY(QFile::exists(dir.filePath("cache/" + Thumbnails::failures + "/" + Thumbnails::cacheKey(dir.filePath("clip.mp4"))))); + } + + void thumbnailsTryAgainWhatAnOlderVersionCouldnt() + { + QTemporaryDir dir; + // The thumbnailer is a script that notes each run beside itself and fails. + const QString script = dir.filePath("fail.sh"), runs = script + ".runs"; + write(script, "echo run >> \"$0.runs\"\nexit 1\n"); + write(dir.filePath("demo.thumbnailer"), + "[Thumbnailer Entry]\nExec=/bin/sh \"" + script.toUtf8() + "\" %i %o %s\nMimeType=video/mp4;\n"); + Thumbnails thumbnails(dir.filePath("cache"), {dir.path()}); + const QString clip = dir.filePath("clip.mp4"); + write(clip, "not a video"); + + // A marker of this Folio's holds the file back, so the thumbnailer runs once; each run notes four bytes. + QVERIFY(thumbnails.load(clip, "video/mp4", 128).isNull()); + QVERIFY(thumbnails.load(clip, "video/mp4", 128).isNull()); + const QString marker = dir.filePath("cache/" + Thumbnails::failures + "/" + Thumbnails::cacheKey(clip)); + QVERIFY(QFile::exists(marker)); + QCOMPARE(QFileInfo(runs).size(), 4); + + // The same marker where 0.2.0 kept them, in fail/folio, holds nothing back: the file is tried once more. + QVERIFY(QDir(dir.filePath("cache")).mkpath("fail/folio")); + QVERIFY(QFile::rename(marker, dir.filePath("cache/fail/folio/" + Thumbnails::cacheKey(clip)))); + QVERIFY(thumbnails.load(clip, "video/mp4", 128).isNull()); + QCOMPARE(QFileInfo(runs).size(), 8); } void thumbnailsRememberOnlyWhatFailed() @@ -5149,7 +5175,7 @@ private slots: // later still gets its turn. write(dir.filePath("bundle.zip"), "not a zip"); QVERIFY(thumbnails.load(dir.filePath("bundle.zip"), "application/zip", 128).isNull()); - QVERIFY(!QFile::exists(dir.filePath("cache/fail/folio/" + Thumbnails::cacheKey(dir.filePath("bundle.zip"))))); + QVERIFY(!QFile::exists(dir.filePath("cache/" + Thumbnails::failures + "/" + Thumbnails::cacheKey(dir.filePath("bundle.zip"))))); // A folder's peek asks for any picture, and gets the thumbnailer for // the one it names. From ddb2335b1356521bbca2bae17d607c02f29b9c73 Mon Sep 17 00:00:00 2001 From: GM Date: Wed, 11 Nov 2026 00:45:58 -0400 Subject: [PATCH 03/12] Find a thumbnailer through its type's aliases A thumbnailer that names a type only by an alias never matched the canonical name Folio asks with, so glycin's image/x-dds left a DDS file blank. The declared types are resolved to canonical names once, when the thumbnailers are read. --- src/thumbnails.cpp | 14 ++++++++++++-- tests/folio_tests.cpp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/thumbnails.cpp b/src/thumbnails.cpp index db665591..3d51199e 100644 --- a/src/thumbnails.cpp +++ b/src/thumbnails.cpp @@ -94,7 +94,10 @@ Thumbnails::Thumbnails(const QString &cacheDirectory, const QStringList &thumbna for (const QString &base : data) directories << base + "/thumbnailers"; } + QMimeDatabase mimes; for (const QString &directory : directories) { + // A spec may name a type by an alias, as glycin names DDS pictures image/x-dds. + QHash named, aliased; QDirIterator it(directory, {"*.thumbnailer"}, QDir::Files); while (it.hasNext()) { // A tiny desktop-entry reader: QSettings would treat the @@ -115,9 +118,16 @@ Thumbnails::Thumbnails(const QString &cacheDirectory, const QStringList &thumbna const QString tryExec = fields.value("TryExec"); if (exec.isEmpty() || (!tryExec.isEmpty() && QStandardPaths::findExecutable(tryExec).isEmpty())) continue; - for (const QString &mime : fields.value("MimeType").split(';', Qt::SkipEmptyParts)) - m_thumbnailers.insert(mime.trimmed(), exec); + for (const QString &declared : fields.value("MimeType").split(';', Qt::SkipEmptyParts)) { + const QString mime = declared.trimmed(); + named.insert(mime, exec); + if (const QString type = mimes.mimeTypeForName(mime).name(); !type.isEmpty()) + aliased.insert(type, exec); + } } + // Within one directory the spec that names a type itself wins over one that names an alias of it. + m_thumbnailers.insert(aliased); + m_thumbnailers.insert(named); } } diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 19bb5a03..b1e3e3a3 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -5227,6 +5228,36 @@ private slots: QVERIFY(qGray(page.pixel(page.width() - 3, page.height() - 3)) > 200); } + void thumbnailsMatchATypeNamedByItsAlias() + { + QTemporaryDir dir; + QImage page(64, 96, QImage::Format_RGB32); + page.fill(Qt::green); + QVERIFY(page.save(dir.filePath("page.png"))); + QVERIFY(QDir(dir.path()).mkdir("system") && QDir(dir.path()).mkdir("user")); + // Like evince, the user's spec names comic books and icons only by an alias. + write(dir.filePath("user/comics.thumbnailer"), + "[Thumbnailer Entry]\nExec=/bin/cp \"" + dir.filePath("page.png").toUtf8() + "\" %o\n" + "MimeType=application/x-cbz;image/x-icon;application/x-nothing-here;video/x-matroska;audio/vnd.wave;\n"); + write(dir.filePath("user/media.thumbnailer"), "[Thumbnailer Entry]\nExec=/bin/true %i\nMimeType=video/matroska;audio/x-wav;\n"); + write(dir.filePath("system/icons.thumbnailer"), "[Thumbnailer Entry]\nExec=/bin/false %i\nMimeType=image/vnd.microsoft.icon;\n"); + Thumbnails thumbnails(dir.filePath("cache"), {dir.filePath("system"), dir.filePath("user")}); + + write(dir.filePath("issue.cbz"), "not really a zip"); + const QImage thumb = thumbnails.load(dir.filePath("issue.cbz"), "application/vnd.comicbook+zip", 128); + QCOMPARE(thumb.pixelColor(0, 0), QColor(Qt::green)); + // The later directory still wins when its spec names the type by an alias. + QVERIFY(thumbnails.thumbnailerFor("image/vnd.microsoft.icon").startsWith("/bin/cp")); + // Within one directory the spec that names the type itself wins, whichever file is read first. + QCOMPARE(QMimeDatabase().mimeTypeForName("video/x-matroska").name(), QString("video/matroska")); + QCOMPARE(QMimeDatabase().mimeTypeForName("audio/x-wav").name(), QString("audio/vnd.wave")); + QVERIFY(thumbnails.thumbnailerFor("video/matroska").startsWith("/bin/true")); + QVERIFY(thumbnails.thumbnailerFor("audio/vnd.wave").startsWith("/bin/cp")); + // A type the MIME database doesn't know is matched by the name the spec gives, and by no other. + QVERIFY(thumbnails.thumbnailerFor("application/x-nothing-here").startsWith("/bin/cp")); + QVERIFY(thumbnails.thumbnailerFor("").isEmpty()); + } + void previewerPicksAViewerForEachType() { QCOMPARE(Previewer::viewerFor("image/png", false), QString("image")); From 3e8b4a9d3cfb955568b48e076ae3df3b38ee2782 Mon Sep 17 00:00:00 2001 From: GM Date: Wed, 11 Nov 2026 00:45:58 -0400 Subject: [PATCH 04/12] Thumbnail camera RAW from its embedded JPEG A CR2, NEF, NRW, ARW, SR2, DNG or PEF showed a blank tile unless a RAW thumbnailer was installed. The camera stores a finished JPEG in each, and a bounded reader of the TIFF directories now finds the largest one. An installed thumbnailer still answers first. --- src/rawembed.cpp | 322 ++++++++++++++++++++++++++++++++++++++++++ src/rawembed.h | 14 ++ src/thumbnails.cpp | 7 + tests/folio_tests.cpp | 193 +++++++++++++++++++++++++ 4 files changed, 536 insertions(+) create mode 100644 src/rawembed.cpp create mode 100644 src/rawembed.h diff --git a/src/rawembed.cpp b/src/rawembed.cpp new file mode 100644 index 00000000..5ba6d702 --- /dev/null +++ b/src/rawembed.cpp @@ -0,0 +1,322 @@ +#include "rawembed.h" + +#include +#include +#include +#include +#include +#include +#include + +namespace { + +// Header and directory bytes read per file in total. +constexpr quint64 kHeaderBudget = 1048576; +// Frame-header bytes read per file across all candidates; then validation stops. +constexpr qint64 kSofBudget = 1048576; +// One candidate's frame-header walk never reads past this. +constexpr qint64 kJpegPrefix = 262144; +// Frame-header walks read through a window this big, so a byte costs no system call. +constexpr qint64 kWindow = 8192; +// More candidates than this are never validated; a real file holds a handful. +constexpr int kMaxCandidates = 16; +// More directories than this, or deeper nesting, is a corrupt chain; the limit also bounds the recursion. +constexpr int kMaxIfds = 64; +constexpr int kMaxDepth = 4; +constexpr quint16 kMaxEntries = 1024; +constexpr quint64 kEntryLength = 12; +// A value longer than this is image data or a corrupt count. +constexpr quint64 kMaxValue = 65536; +// SubIFD fan-out is capped, so one count cannot queue unbounded work. +constexpr quint32 kMaxPointers = 64; +// The largest embedded JPEG read into memory; a real one is a few MiB. +constexpr quint64 kMaxJpeg = 64 * 1048576; +// SOI plus one frame header: anything shorter is no picture. +constexpr qint64 kMinJpeg = 20; +constexpr quint16 kTiffMagic = 42; +// A TIFF header is 8 bytes; a value of 4 bytes or fewer rides inline in its entry. +constexpr quint64 kTiffHeaderLength = 8, kInlineBytes = 4; + +// TIFF and EXIF tags, JPEG compression values, field types and JPEG markers. +constexpr quint16 kCompression = 0x0103, kStripOffsets = 0x0111, kOrientation = 0x0112, kStripByteCounts = 0x0117; +constexpr quint16 kSubIfds = 0x014A, kJpegOffset = 0x0201, kJpegLength = 0x0202; +// EXIF Orientation 1 is upright; 2 to 8 flip or turn the picture. +constexpr quint64 kFirstTurn = 2, kLastTurn = 8; +constexpr quint64 kOldJpeg = 6, kJpeg = 7; +constexpr quint16 kShort = 3, kLong = 4, kIfdType = 13; +constexpr quint8 kMarker = 0xFF, kSoi = 0xD8, kStuffed = 0x00, kTem = 0x01, kRst0 = 0xD0; +constexpr quint8 kSos = 0xDA, kEoi = 0xD9, kApp1 = 0xE1; +// A frame header opens with precision(1) height(2) width(2); SOF0 to SOF2 are the Huffman frames a preview uses. +constexpr quint8 kFirstFrame = 0xC0, kLastFrame = 0xCF, kLastHuffmanFrame = 0xC2, kDht = 0xC4, kJpg = 0xC8, kDac = 0xCC; +constexpr qsizetype kFrameHeadLength = 5; + +// Marker, length 34, "Exif\0\0", "II" 42 and IFD0 at 8, one entry: Orientation, SHORT, count 1, no next IFD. +constexpr unsigned char kExifOrientation[36] = {0xFF, 0xE1, 0x00, 0x22, 'E', 'x', 'i', 'f', 0, 0, 'I', 'I', 0x2A, 0, + 8, 0, 0, 0, 1, 0, 0x12, 0x01, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; +constexpr int kExifOrientationAt = 28; + +quint16 rd16(const char *p, bool le) { return le ? qFromLittleEndian(p) : qFromBigEndian(p); } +quint32 rd32(const char *p, bool le) { return le ? qFromLittleEndian(p) : qFromBigEndian(p); } + +// Bytes of one value of each TIFF type 0 to 13 (TIFF 6.0 section 2, 13 is an IFD offset); 0 is unknown. +quint64 typeSize(quint16 type) { + static const quint8 sizes[] = {0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8, 4}; + return type < sizeof sizes ? sizes[type] : 0; +} + +bool isLong(quint16 type) { return type == kLong || type == kIfdType; } + +// A JPEG frame header; DHT, JPG and DAC share the marker range but are not frames. +bool isFrame(quint8 m) { return m >= kFirstFrame && m <= kLastFrame && m != kDht && m != kJpg && m != kDac; } + +// The file through the header budget, or a slice in hand that is read at most its own length. +struct Source { + QFile *file; + QByteArray slice; + quint64 size; + quint64 left; + + bool get(quint64 offset, quint64 length, QByteArray &out) { + if (length > left || offset > size || length > size - offset) + return false; + left -= length; + if (!file) + out = slice.mid(qsizetype(offset), qsizetype(length)); + else + out = file->seek(qint64(offset)) ? file->read(qint64(length)) : QByteArray(); + return quint64(out.size()) == length; + } +}; + +// 49 49 2A 00 and the first IFD's offset, or 4D 4D 00 2A big-endian; BigTIFF (43) is refused, not misread. +bool tiffHeader(Source &src, quint64 at, bool &le, quint64 &ifd) { + QByteArray b; + if (!src.get(at, kTiffHeaderLength, b) || (!b.startsWith("II") && !b.startsWith("MM"))) + return false; + le = b.startsWith("II"); + ifd = rd32(b.constData() + 4, le); + return rd16(b.constData() + 2, le) == kTiffMagic; +} + +struct Walker { + explicit Walker(Source &source) : src(source) {} + Source &src; + QVector> candidates; + // Each directory is read once, so a cycle neither loops nor uses up the limit. + QVector seen; + std::optional orientation; + + // A value's bytes: the entry's last four when they fit, else that many bytes at the offset those four name. + bool value(bool le, quint16 type, quint32 count, const char *val, QByteArray &out) { + const quint64 total = typeSize(type) * count; + if (typeSize(type) == 0 || total > kMaxValue) + return false; + if (total <= kInlineBytes) + out = QByteArray(val, int(total)); + return total <= kInlineBytes || src.get(rd32(val, le), total, out); + } + + // The first value of a SHORT or LONG field. + std::optional one(bool le, quint16 type, quint32 count, const char *val) { + QByteArray b; + if (count == 0 || (type != kShort && !isLong(type)) || !value(le, type, count, val, b)) + return {}; + return type == kShort ? rd16(b.constData(), le) : rd32(b.constData(), le); + } + + // An IFD is count(2), then 12-byte entries tag(2) type(2) count(4) value-or-offset(4), then the next IFD(4). + void walk(bool le, int depth, quint64 at) { + QByteArray b, entries; + if (depth > kMaxDepth || seen.size() >= kMaxIfds || seen.contains(at) || !src.get(at, 2, b)) + return; + const quint16 count = rd16(b.constData(), le); + // A 0xFFFF count is the hostile case: this directory is skipped, never read or looped. + if (count > kMaxEntries || !src.get(at + 2, count * kEntryLength, entries)) + return; + seen.append(at); + std::optional jpegOffset, jpegLength, stripOffset, stripLength, compression; + QVector children; + for (int i = 0; i < count; ++i) { + const char *e = entries.constData() + i * kEntryLength; + const quint16 tag = rd16(e, le), type = rd16(e + 2, le); + const quint32 n = rd32(e + 4, le); + if (tag == kOrientation && !orientation) orientation = one(le, type, n, e + 8); + else if (tag == kJpegOffset) jpegOffset = one(le, type, n, e + 8); + else if (tag == kJpegLength) jpegLength = one(le, type, n, e + 8); + // Many strips are raw sensor data, never one preview. + else if (tag == kStripOffsets) stripOffset = n == 1 ? one(le, type, n, e + 8) : std::nullopt; + else if (tag == kStripByteCounts) stripLength = one(le, type, n, e + 8); + else if (tag == kCompression) compression = one(le, type, n, e + 8); + else if (tag == kSubIfds && n > 0 && (type == kShort || isLong(type)) + && value(le, type, qMin(n, kMaxPointers), e + 8, b)) + for (quint32 k = 0; k < qMin(n, kMaxPointers); ++k) + children.append(type == kShort ? rd16(b.constData() + k * 2, le) : rd32(b.constData() + k * 4, le)); + } + if (jpegOffset && jpegLength && *jpegLength > 0) + candidates.append({*jpegOffset, *jpegLength}); + // Any lone JPEG-compressed strip is a preview (a CR2 keeps its big one in IFD0); the frame rule keeps raw data out. + if (stripOffset && stripLength && compression && (*compression == kOldJpeg || *compression == kJpeg) + && *stripLength > 0) + candidates.append({*stripOffset, *stripLength}); + for (const quint64 child : children) + walk(le, depth + 1, child); + // A next pointer that cannot be read ends the chain and keeps what it collected. + if (src.get(at + 2 + count * kEntryLength, 4, b) && rd32(b.constData(), le) != 0) + walk(le, depth, rd32(b.constData(), le)); + } +}; + +// The first frame header's marker and size (FF C0 00 11 08 height width), reading at most `left` file bytes through a window. +bool firstFrame(QFile &file, qint64 &left, quint8 &marker, quint32 &width, quint32 &height) { + QByteArray window, b; + qsizetype used = 0; + const auto take = [&](qsizetype n) { + b.clear(); + while (b.size() < n) { + if (used == window.size()) { + window = left > 0 ? file.read(qMin(kWindow, left)) : QByteArray(); + left -= window.size(); + used = 0; + if (window.isEmpty()) + return false; + } + const qsizetype part = qMin(n - b.size(), window.size() - used); + b += window.mid(used, part); + used += part; + } + return true; + }; + if (!take(2) || quint8(b[0]) != kMarker || quint8(b[1]) != kSoi) + return false; + for (;;) { + do { if (!take(1)) return false; } while (quint8(b[0]) != kMarker); + do { if (!take(1)) return false; } while (quint8(b[0]) == kMarker); + const quint8 m = quint8(b[0]); + if (m == kStuffed || m == kSos || m == kEoi) + return false; + // TEM, the restart markers and SOI carry no length. + if (m == kTem || (m >= kRst0 && m <= kSoi)) + continue; + if (!take(2) || qFromBigEndian(b.constData()) < 2) + return false; + // A segment is skipped by reading past it, so the window still bounds the total. + if (!isFrame(m)) { + if (!take(qFromBigEndian(b.constData()) - 2)) + return false; + continue; + } + if (!take(kFrameHeadLength)) + return false; + marker = m; + height = qFromBigEndian(b.constData() + 1); + width = qFromBigEndian(b.constData() + 3); + return true; + } +} + +// The orientation the JPEG's own first Exif segment (FF E1, length, "Exif\0\0", a TIFF) names, if any. +std::optional ownOrientation(const QByteArray &jpeg) { + for (qsizetype at = 2; at + 4 <= jpeg.size() && quint8(jpeg[at]) == kMarker;) { + const quint8 marker = quint8(jpeg[at + 1]); + const qsizetype length = qFromBigEndian(jpeg.constData() + at + 2); + if (marker == kSos || marker == kEoi || isFrame(marker) || length < 2 || at + 2 + length > jpeg.size()) + return {}; + if (marker == kApp1 && length >= 8 && jpeg.mid(at + 4, 6) == QByteArray("Exif\0\0", 6)) { + const QByteArray tiff = jpeg.mid(at + 10, length - 8); + Source slice{nullptr, tiff, quint64(tiff.size()), quint64(tiff.size())}; + Walker walker(slice); + bool le = true; + quint64 ifd = 0; + if (tiffHeader(slice, 0, le, ifd)) + walker.walk(le, 0, ifd); + return walker.orientation; + } + at += 2 + length; + } + return {}; +} + +struct Preview { + quint64 offset = 0, length = 0, pixels = 0; +}; + +// Most pixels first, the longer of two equals ahead; only Huffman frames (SOF0 to SOF2) count, SOF3 is raw data. +QVector ranked(QFile &file, const QVector> &candidates) { + qint64 sofLeft = kSofBudget; + QVector previews; + for (int i = 0; i < qMin(int(candidates.size()), kMaxCandidates); ++i) { + const quint64 offset = candidates[i].first, length = candidates[i].second; + // A span running past the end of the file is a cut file, not a preview. + if (length < quint64(kMinJpeg) || sofLeft == 0 || offset > quint64(file.size()) + || length > quint64(file.size()) - offset) + continue; + const qint64 span = qMin(qint64(qMin(length, quint64(kJpegPrefix))), sofLeft); + qint64 left = span; + quint8 marker = 0; + quint32 width = 0, height = 0; + const bool found = span >= kMinJpeg && file.seek(qint64(offset)) && firstFrame(file, left, marker, width, height); + sofLeft -= span - left; + if (found && marker <= kLastHuffmanFrame) + previews.append({offset, length, quint64(width) * height}); + } + std::stable_sort(previews.begin(), previews.end(), [](const Preview &a, const Preview &b) { + return a.pixels != b.pixels ? a.pixels > b.pixels : a.length > b.length; + }); + return previews; +} + +// One preview read and scaled to fit `pixels`, turned by the directory's `turn` when it names none of its own. +QImage decoded(QFile &file, const Preview &preview, quint64 turn, int pixels) { + if (!file.seek(qint64(preview.offset))) + return {}; + QByteArray jpeg = file.read(qint64(preview.length)); + if (quint64(jpeg.size()) != preview.length) + return {}; + if (turn >= kFirstTurn && turn <= kLastTurn && !ownOrientation(jpeg.left(kJpegPrefix))) { + QByteArray segment(reinterpret_cast(kExifOrientation), sizeof kExifOrientation); + qToLittleEndian(quint16(turn), segment.data() + kExifOrientationAt); + jpeg.insert(2, segment); + } + QBuffer buffer(&jpeg); + QImageReader reader(&buffer, "jpeg"); + reader.setAutoTransform(true); + const QSize full = reader.size(); + if (full.width() > pixels || full.height() > pixels) + reader.setScaledSize(full.scaled(pixels, pixels, Qt::KeepAspectRatio)); + return reader.read(); +} + +} // namespace + +bool RawEmbed::handles(const QString &mime) { + // shared-mime-info's names for .cr2 .nef .nrw .arw .sr2 .dng .pef. + static const QStringList types = {"image/x-canon-cr2", "image/x-nikon-nef", "image/x-nikon-nrw", "image/x-sony-arw", + "image/x-sony-sr2", "image/x-adobe-dng", "image/x-pentax-pef"}; + return types.contains(mime); +} + +QImage RawEmbed::thumbnail(const QString &path, int pixels) { + // Unbuffered, so a small directory read costs its own bytes and no read-ahead. + QFile file(path); + if (!file.open(QIODevice::ReadOnly | QIODevice::Unbuffered)) + return {}; + Source src{&file, {}, quint64(file.size()), kHeaderBudget}; + Walker walker(src); + bool le = true; + quint64 ifd = 0; + if (!tiffHeader(src, 0, le, ifd)) + return {}; + walker.walk(le, 0, ifd); + + // A preview that doesn't decode is passed over for the next largest that fits what is left of one limit of bytes. + quint64 left = kMaxJpeg; + for (const Preview &preview : ranked(file, walker.candidates)) { + if (preview.length > left) + continue; + left -= preview.length; + const QImage image = decoded(file, preview, walker.orientation.value_or(0), pixels); + if (!image.isNull()) + return image; + } + return {}; +} diff --git a/src/rawembed.h b/src/rawembed.h new file mode 100644 index 00000000..cbdec325 --- /dev/null +++ b/src/rawembed.h @@ -0,0 +1,14 @@ +#pragma once + +#include +#include + +// Camera RAW thumbnails from the embedded JPEG, found in the TIFF directories of CR2 NEF NRW ARW SR2 DNG PEF. +namespace RawEmbed { + +bool handles(const QString &mime); + +// The largest valid embedded JPEG, upright and at most `pixels` on its longer side; null when the file holds none. +QImage thumbnail(const QString &path, int pixels); + +} // namespace RawEmbed diff --git a/src/thumbnails.cpp b/src/thumbnails.cpp index 3d51199e..69de6c69 100644 --- a/src/thumbnails.cpp +++ b/src/thumbnails.cpp @@ -1,4 +1,5 @@ #include "thumbnails.h" +#include "rawembed.h" #include #include @@ -194,6 +195,12 @@ QImage Thumbnails::load(const QString &path, const QString &mime, int size) cons } QImage Thumbnails::generate(const QString &path, const QString &mime, int pixels, bool &attempted) const { + // A camera RAW with no installed thumbnailer, the desktop's own answer, shows the JPEG the camera embedded. + if (RawEmbed::handles(mime) && !m_thumbnailers.contains(mime)) { + attempted = true; + if (const QImage image = RawEmbed::thumbnail(path, pixels); !image.isNull()) + return image; + } // Common photo formats decode fastest in-process; anything Qt can't read // (HEIC, JPEG XL) falls through to the installed thumbnailers. if (mime.startsWith("image/")) { diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index b1e3e3a3..50db3f0b 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -5258,6 +5258,116 @@ private slots: QVERIFY(thumbnails.thumbnailerFor("").isEmpty()); } + void thumbnailsShowACameraRawsEmbeddedJpeg() + { + // IFD0's quarter turn applies to the stored 400 by 300 picture unless the JPEG names its own, which then rules. + QCOMPARE(rawThumbnail(rawWithJpeg(jpeg(400, 300), kQuarterTurn)), QSize(300, 400)); + QCOMPARE(rawThumbnail(rawWithJpeg(jpeg(400, 300, kUpright), kQuarterTurn)), QSize(400, 300)); + QCOMPARE(rawThumbnail(rawWithJpeg(jpeg(400, 300, kQuarterTurn), kUpright)), QSize(300, 400)); + } + + void thumbnailsPickTheLargestEmbeddedJpeg() + { + // Big-endian like a NEF: IFD0 names a small JPEG, its SubIFDs lossless raw data (SOF3) and the preview. + const QByteArray small = jpeg(160, 120), big = jpeg(640, 480); + const QByteArray sof3("\xFF\xD8\xFF\xC3\x00\x0B\x08\x0B\xB8\x0F\xA0\x01\x01\x22\x00\xFF\xD9\0\0\0\0\0\0\0\0", 25); + Raw nef(false); + const quint32 smallAt = nef.add(small), bigAt = nef.add(big), sof3At = nef.add(sof3); + const quint32 raw = nef.ifd(strip(sof3At, sof3.size())), preview = nef.ifd(pair(bigAt, big.size())); + const quint32 subs = nef.add(nef.word(raw, 4) + nef.word(preview, 4)); + nef.setFirst(nef.ifd(pair(smallAt, small.size()) + Entries{{kSubIfds, kLong, 2, subs}})); + QCOMPARE(rawThumbnail(nef.bytes), QSize(640, 480)); + // Like a CR2: the preview is IFD0's JPEG-compressed strip and IFD1 holds a thumbnail. + Raw cr2(true); + const quint32 stripAt = cr2.add(big), thumbAt = cr2.add(small); + cr2.setFirst(cr2.ifd(strip(stripAt, big.size()), cr2.ifd(pair(thumbAt, small.size())))); + QCOMPARE(rawThumbnail(cr2.bytes), QSize(640, 480)); + } + + void thumbnailsFallBackToASmallerEmbeddedJpeg() + { + // The larger preview's frame header reads but its picture data is gone, so the thumbnail IFD0 names is shown. + const QByteArray small = jpeg(160, 120), big = jpeg(640, 480); + const qsizetype scan = big.indexOf(QByteArray("\xFF\xDA", 2)); + QVERIFY(scan > 0); + const QByteArray damaged = big.left(scan) + QByteArray(big.size() - scan, '\0'); + Raw raw(true); + const quint32 smallAt = raw.add(small), damagedAt = raw.add(damaged); + raw.setFirst(raw.ifd(pair(smallAt, small.size()), raw.ifd(pair(damagedAt, damaged.size())))); + QCOMPARE(rawThumbnail(raw.bytes), QSize(160, 120)); + // A preview one byte over what the reader holds in memory is never picked ahead of one it can show. + Raw huge(true); + const quint32 thumbAt = huge.add(small), hugeAt = huge.add(big); + huge.setFirst(huge.ifd(pair(thumbAt, small.size()), huge.ifd(pair(hugeAt, kMostJpegBytes + 1)))); + QCOMPARE(rawThumbnail(huge.bytes, hugeAt + kMostJpegBytes + 1), QSize(160, 120)); + // All the previews tried share that limit: a damaged one that leaves a byte too few for the next sends it past that one. + const QByteArray wide = jpeg(800, 600), gone = wide.left(wide.indexOf(QByteArray("\xFF\xDA", 2))); + const auto after = [&](qint64 damagedLength) { + Raw spent(true); + const quint32 firstAt = spent.add(small), nextAt = spent.add(big), goneAt = spent.add(gone); + spent.setFirst(spent.ifd(pair(firstAt, small.size()), + spent.ifd(pair(nextAt, big.size()), spent.ifd(pair(goneAt, damagedLength))))); + return rawThumbnail(spent.bytes, goneAt + damagedLength); + }; + QCOMPARE(after(kMostJpegBytes - big.size()), QSize(640, 480)); + QCOMPARE(after(kMostJpegBytes - big.size() + 1), QSize(160, 120)); + } + + void thumbnailsRefuseABrokenCameraRaw() + { + QCOMPARE(rawThumbnail(rawWithJpeg(jpeg(400, 300), kUpright)), QSize(400, 300)); + // A preview that runs past the end of the file is passed over for the whole thumbnail IFD0 names. + const QByteArray small = jpeg(160, 120), big = jpeg(640, 480); + Raw cut(true); + const quint32 smallAt = cut.add(small), bigAt = quint32(cut.bytes.size()) + 2 * kTwoEntryIfd; + cut.setFirst(cut.ifd(pair(smallAt, small.size()), cut.ifd(pair(bigAt, big.size())))); + cut.add(big); + QCOMPARE(rawThumbnail(cut.bytes), QSize(640, 480)); + QCOMPARE(rawThumbnail(cut.bytes.left(cut.bytes.size() - 16)), QSize(160, 120)); + // A SubIFD that names itself as its next directory is walked once, so the preview in IFD1 is still reached. + Raw loop(true); + const quint32 previewAt = loop.add(big), self = quint32(loop.bytes.size()); + loop.ifd({}, self); + loop.setFirst(loop.ifd({{kSubIfds, kLong, 1, self}}, loop.ifd(pair(previewAt, big.size())))); + QCOMPARE(rawThumbnail(loop.bytes), QSize(640, 480)); + // The picture the 64th directory of a chain names is reached; with one more directory ahead of it, it is not. + Raw chain(true); + const quint32 lastAt = chain.add(small); + quint32 next = chain.ifd(pair(lastAt, small.size())); + for (int i = 1; i < kDirectoryLimit; ++i) + next = chain.ifd({}, next); + chain.setFirst(next); + QCOMPARE(rawThumbnail(chain.bytes), QSize(160, 120)); + chain.setFirst(chain.ifd({}, next)); + QVERIFY(rawThumbnail(chain.bytes).isEmpty()); + // An IFD0 whose first two entries name the JPEG and whose other entries, all held by the file, name nothing. + const auto wide = [&small](int entries) { + Raw raw(true); + const quint32 at = raw.add(small); + raw.setFirst(raw.ifd(pair(at, small.size()) + Entries(entries - 2, std::array{}))); + return rawThumbnail(raw.bytes); + }; + QCOMPARE(wide(kEntryLimit), QSize(160, 120)); + QVERIFY(wide(kEntryLimit + 1).isEmpty()); + QVERIFY(wide(kMostEntries).isEmpty()); + } + + void thumbnailsKeepOtherPathsForCameraRaws() + { + QTemporaryDir dir; + const QByteArray raw = rawWithJpeg(jpeg(400, 300), kUpright); + QCOMPARE(rawThumbnail(raw), QSize(400, 300)); + // An installed thumbnailer stays the desktop's answer, and a plain TIFF never takes the RAW reader. + QVERIFY(QImage(10, 20, QImage::Format_RGB32).save(dir.filePath("mark.png"))); + write(dir.filePath("raw.thumbnailer"), "[Thumbnailer Entry]\nTryExec=/bin/cp\nExec=/bin/cp \"" + + dir.filePath("mark.png").toUtf8() + "\" %o\nMimeType=image/x-adobe-dng;\n"); + Thumbnails thumbnails(dir.filePath("cache"), {dir.path()}); + write(dir.filePath("photo.dng"), raw); + QCOMPARE(thumbnails.load(dir.filePath("photo.dng"), "image/x-adobe-dng", 1024).size(), QSize(10, 20)); + write(dir.filePath("photo.tif"), raw); + QVERIFY(thumbnails.load(dir.filePath("photo.tif"), "image/tiff", 1024).isNull()); + } + void previewerPicksAViewerForEachType() { QCOMPARE(Previewer::viewerFor("image/png", false), QString("image")); @@ -5797,6 +5907,89 @@ private slots: return file.open(QIODevice::ReadOnly) ? file.readAll() : QByteArray(); } + using Entries = QList>; + // The TIFF 6.0 tags, field types and values a camera RAW names its embedded JPEG and its turn with. + static constexpr quint32 kCompression = 0x0103, kStripOffsets = 0x0111, kOrientation = 0x0112, kStripByteCounts = 0x0117; + static constexpr quint32 kSubIfds = 0x014A, kJpegOffset = 0x0201, kJpegLength = 0x0202; + static constexpr quint32 kShort = 3, kLong = 4, kJpegCompression = 7, kUpright = 1, kQuarterTurn = 6; + // An IFD of two entries: count(2), two 12-byte entries, next(4). + static constexpr quint32 kTwoEntryIfd = 2 + 2 * 12 + 4; + // The reader walks this many directories of one file and no more. + static constexpr int kDirectoryLimit = 64; + // The reader takes a directory of this many entries and none longer; a 16-bit count can claim the second. + static constexpr int kEntryLimit = 1024, kMostEntries = 0xFFFF; + // The reader holds an embedded JPEG of this many bytes in memory and none longer. + static constexpr qint64 kMebibyte = 1048576, kMostJpegBytes = 64 * kMebibyte; + // A camera RAW in either byte order; blobs and IFDs are appended, so every offset is known before it is named. + struct Raw { + explicit Raw(bool littleEndian) + : le(littleEndian), bytes(QByteArray(le ? "II*\0" : "MM\0*", 4) + QByteArray(4, '\0')) {} + QByteArray word(quint32 value, int size) const + { + QByteArray out(4, '\0'); + le ? qToLittleEndian(value, out.data()) : qToBigEndian(value, out.data()); + return le ? out.left(size) : out.right(size); + } + // Entries are (tag, type, count, value) and a SHORT value fills its field's first two bytes. + QByteArray dir(const Entries &entries, quint32 next = 0) const + { + QByteArray out = word(entries.size(), 2); + for (const auto &[tag, type, count, value] : entries) + out += word(tag, 2) + word(type, 2) + word(count, 4) + + (type == kShort ? word(value, 2) + QByteArray(2, '\0') : word(value, 4)); + return out + word(next, 4); + } + quint32 add(const QByteArray &data) { bytes += data; return quint32(bytes.size() - data.size()); } + quint32 ifd(const Entries &entries, quint32 next = 0) { return add(dir(entries, next)); } + void setFirst(quint32 ifd) { bytes.replace(4, 4, word(ifd, 4)); } + bool le; + QByteArray bytes; + }; + // JPEGInterchangeFormat and its length, or one JPEG-compressed strip as DNG and CR2 store a preview. + static Entries pair(quint32 at, qsizetype length) + { + return {{kJpegOffset, kLong, 1, at}, {kJpegLength, kLong, 1, quint32(length)}}; + } + static Entries strip(quint32 at, qsizetype length) + { + return {{kStripOffsets, kLong, 1, at}, {kStripByteCounts, kLong, 1, quint32(length)}, + {kCompression, kShort, 1, kJpegCompression}}; + } + // A green JPEG; `own`, when not 0, is the Orientation its own Exif segment (FF E1, length, "Exif\0\0", a TIFF) names. + static QByteArray jpeg(int width, int height, quint32 own = 0) + { + QImage picture(width, height, QImage::Format_RGB32); + picture.fill(Qt::green); + QByteArray data; + QBuffer buffer(&data); + if (!buffer.open(QIODevice::WriteOnly) || !picture.save(&buffer, "JPEG")) + return {}; + Raw tiff(true); + tiff.setFirst(tiff.ifd({{kOrientation, kShort, 1, own}})); + const QByteArray exif = QByteArray("Exif\0\0", 6) + tiff.bytes; + return own ? data.insert(2, QByteArray("\xFF\xE1", 2) + Raw(false).word(2 + exif.size(), 2) + exif) : data; + } + + // A little-endian RAW whose IFD0 names one JPEG and an orientation. + static QByteArray rawWithJpeg(const QByteArray &picture, quint32 orientation) + { + Raw raw(true); + const quint32 at = raw.add(picture); + raw.setFirst(raw.ifd(pair(at, picture.size()) + Entries{{kOrientation, kShort, 1, orientation}})); + return raw.bytes; + } + + // What Folio shows for these bytes as a DNG with no thumbnailer installed, unscaled; `grownTo` lengthens the file with a hole. + static QSize rawThumbnail(const QByteArray &raw, qint64 grownTo = 0) + { + QTemporaryDir dir; + write(dir.filePath("photo.dng"), raw); + if (grownTo > 0 && !QFile::resize(dir.filePath("photo.dng"), grownTo)) + return {}; + const Thumbnails thumbnails(dir.filePath("cache"), {dir.filePath("none")}); + return thumbnails.load(dir.filePath("photo.dng"), "image/x-adobe-dng", 1024).size(); + } + static void write(const QString &path, const QByteArray &contents) { QFile file(path); From 4c940534ee08da35bc1902fbbe3593483e75aca0 Mon Sep 17 00:00:00 2001 From: GM Date: Wed, 11 Nov 2026 00:45:58 -0400 Subject: [PATCH 05/12] Name the package that would draw a missing thumbnail A type with no installed thumbnailer showed a blank tile and nothing else. The details pane now says which optional package draws it, from the types ffmpegthumbnailer, papers, glycin and libgsf declare. --- src/PreviewPane.qml | 10 ++++++ src/i18n/en.json | 1 + src/main.cpp | 1 + src/thumbnails.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++ src/thumbnails.h | 7 ++++- tests/folio_tests.cpp | 15 +++++++++ 6 files changed, 106 insertions(+), 1 deletion(-) diff --git a/src/PreviewPane.qml b/src/PreviewPane.qml index bc93b1b6..3d7fe2bd 100644 --- a/src/PreviewPane.qml +++ b/src/PreviewPane.qml @@ -129,6 +129,16 @@ Rectangle { font.pixelSize: 12 elide: Text.ElideRight } + // A type nothing installed can draw: which package would. + Text { + readonly property string missing: big.status === Image.Error ? thumbnails.missingPackage(pane.item.mime) : "" + visible: missing.length > 0 + Layout.fillWidth: true + text: qsTr("Thumbnails of this type need the %1 package.").arg(missing) + color: theme.secondaryForeground + font.pixelSize: 12 + wrapMode: Text.Wrap + } } GridLayout { diff --git a/src/i18n/en.json b/src/i18n/en.json index 369dbda5..fa546b3e 100644 --- a/src/i18n/en.json +++ b/src/i18n/en.json @@ -393,6 +393,7 @@ "This computer can't make %1 drives.": "This computer can't make %1 drives.", "This folder is empty": "This folder is empty", "This folder no longer exists.": "This folder no longer exists.", + "Thumbnails of this type need the %1 package.": "Thumbnails of this type need the %1 package.", "Trash": "Trash", "Try again once the work under way is done.": "Try again once the work under way is done.", "Type a path, or a server's address like sftp://host/ (Tab completes)": "Type a path, or a server's address like sftp://host/ (Tab completes)", diff --git a/src/main.cpp b/src/main.cpp index 9228dca5..813d21c1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -148,6 +148,7 @@ int main(int argc, char *argv[]) { engine.rootContext()->setContextProperty("theme", &theme); engine.rootContext()->setContextProperty("places", &places); engine.rootContext()->setContextProperty("plugins", &plugins); + engine.rootContext()->setContextProperty("thumbnails", &thumbnails); Windows windows(&engine, &plugins); if (!windows.open(start, select)) diff --git a/src/thumbnails.cpp b/src/thumbnails.cpp index 69de6c69..46d7b92e 100644 --- a/src/thumbnails.cpp +++ b/src/thumbnails.cpp @@ -80,6 +80,71 @@ QStringList expandCommand(const QString &command, const QString &input, const QS return args; } +// Each optional package's thumbnailers and the MimeType= lines they ship, as "video/mp4;video/webm;". +struct PackageTypes { + const char *package; + const char *types; +}; +const PackageTypes packageTypes[] = { + {"ffmpegthumbnailer", + "video/3gpp;video/3gpp2;video/annodex;video/dv;video/isivideo;video/matroska;video/matroska-3d;" + "video/mj2;video/mp2t;video/mp4;video/mpeg;video/ogg;video/quicktime;video/vnd.avi;" + "video/vnd.mpegurl;video/vnd.radgamettools.bink;video/vnd.radgamettools.smacker;" + "video/vnd.rn-realvideo;video/vnd.vivo;video/vnd.youtube.yt;video/wavelet;video/webm;" + "video/x-anim;video/x-flic;video/x-flv;video/x-javafx;video/x-matroska;video/x-matroska-3d;" + "video/x-mjpeg;video/x-mng;video/x-ms-wmv;video/x-nsv;video/x-ogm+ogg;video/x-sgi-movie;" + "video/x-theora+ogg;application/mxf;application/vnd.ms-asf;application/vnd.rn-realmedia;" + "application/matroska;application/x-matroska;application/ogg;"}, + {"glycin", + "image/avif;image/heif;image/apng;image/bmp;image/gif;image/jp2;image/jpeg;image/png;image/qoi;" + "image/tiff;image/vnd.microsoft.icon;image/webp;image/x-dds;image/x-exr;image/x-jp2-codestream;" + "image/x-portable-anymap;image/x-portable-bitmap;image/x-portable-graymap;" + "image/x-portable-pixmap;image/x-tga;image/x-win-bitmap;image/x-xbitmap;image/x-xpixmap;" + "image/jxl;image/svg+xml;image/svg+xml-compressed;"}, + {"papers", + "application/vnd.comicbook-rar;application/vnd.comicbook+zip;application/x-cb7;application/x-cbr;" + "application/x-cbt;application/x-cbz;application/x-ext-cb7;application/x-ext-cbr;" + "application/x-ext-cbt;application/x-ext-cbz;application/x-ext-djv;application/x-ext-djvu;" + "image/vnd.djvu;image/vnd.djvu+multipage;application/pdf;application/x-bzpdf;" + "application/x-ext-pdf;application/x-gzpdf;application/x-xzpdf;application/illustrator;" + "image/tiff;"}, + {"libgsf", + "application/msword;application/vnd.ms-excel;application/vnd.ms-powerpoint;" + "application/vnd.oasis.opendocument.chart;application/vnd.oasis.opendocument.formula;" + "application/vnd.oasis.opendocument.graphics;" + "application/vnd.oasis.opendocument.graphics-template;application/vnd.oasis.opendocument.image;" + "application/vnd.oasis.opendocument.presentation;" + "application/vnd.oasis.opendocument.presentation-template;" + "application/vnd.oasis.opendocument.spreadsheet;" + "application/vnd.oasis.opendocument.spreadsheet-template;application/vnd.oasis.opendocument.text;" + "application/vnd.oasis.opendocument.text-master;application/vnd.oasis.opendocument.text-template;" + "application/vnd.openxmlformats-officedocument.presentationml.presentation;" + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;" + "application/vnd.openxmlformats-officedocument.wordprocessingml.document;" + "application/vnd.sun.xml.calc;application/vnd.sun.xml.calc.template;application/vnd.sun.xml.draw;" + "application/vnd.sun.xml.draw.template;application/vnd.sun.xml.impress;" + "application/vnd.sun.xml.impress.template;application/vnd.sun.xml.math;" + "application/vnd.sun.xml.writer;application/vnd.sun.xml.writer.global;" + "application/vnd.sun.xml.writer.template;"}, +}; + +// The package for each type by its own name, read once; the first package listed wins, so glycin draws TIFF before papers. +const QHash &packageFor() { + static const QHash table = [] { + QMimeDatabase mimes; + QHash byType; + for (const PackageTypes &entry : packageTypes) { + for (const QString &declared : QString::fromLatin1(entry.types).split(';', Qt::SkipEmptyParts)) { + const QString type = mimes.mimeTypeForName(declared).name(); + if (const QString key = type.isEmpty() ? declared : type; !byType.contains(key)) + byType.insert(key, QString::fromLatin1(entry.package)); + } + } + return byType; + }(); + return table; +} + } // namespace Thumbnails::Thumbnails(const QString &cacheDirectory, const QStringList &thumbnailerDirectories) @@ -132,6 +197,14 @@ Thumbnails::Thumbnails(const QString &cacheDirectory, const QStringList &thumbna } } +QString Thumbnails::missingPackage(const QString &mime) const { + // Qt draws its own pictures in-process, and an installed thumbnailer needs nothing more. + if ((mime.startsWith("image/") && QImageReader::supportedMimeTypes().contains(mime.toLatin1())) + || m_thumbnailers.contains(mime)) + return {}; + return packageFor().value(mime); +} + QString Thumbnails::cacheKey(const QString &path) { return QCryptographicHash::hash(fileUri(path).toUtf8(), QCryptographicHash::Md5).toHex() + ".png"; } diff --git a/src/thumbnails.h b/src/thumbnails.h index 819cb585..bedf77f1 100644 --- a/src/thumbnails.h +++ b/src/thumbnails.h @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -15,7 +16,9 @@ // /usr/share/thumbnailers (videos via ffmpegthumbnailer, and so on), with // Poppler's pdftoppm drawing PDFs when no thumbnailer does. Nothing is generated until a // tile asks for it: there is no background crawl. -class Thumbnails { +class Thumbnails : public QObject { + Q_OBJECT + public: explicit Thumbnails(const QString &cacheDirectory = {}, const QStringList &thumbnailerDirectories = {}); @@ -31,6 +34,8 @@ class Thumbnails { // fresh each time rather than cached, since the colors follow the theme. static QImage textPage(const QString &path, int pixels, const QColor &paper, const QColor &ink); QString thumbnailerFor(const QString &mime) const { return m_thumbnailers.value(mime); } + // The package that would draw `mime` when neither Qt nor an installed thumbnailer does, or nothing. + Q_INVOKABLE QString missingPackage(const QString &mime) const; private: QString cachePath(const QString &path, int size) const; diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 50db3f0b..45b6320f 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -5368,6 +5368,21 @@ private slots: QVERIFY(thumbnails.load(dir.filePath("photo.tif"), "image/tiff", 1024).isNull()); } + void thumbnailsNameThePackageThatWouldDrawAType() + { + QTemporaryDir dir; + write(dir.filePath("webm.thumbnailer"), + "[Thumbnailer Entry]\nTryExec=/bin/sh\nExec=/bin/sh -c \"exit 1\" %i %o\nMimeType=video/webm;\n"); + const Thumbnails thumbnails(dir.filePath("cache"), {dir.path()}); + QCOMPARE(thumbnails.missingPackage("video/mp4"), QString("ffmpegthumbnailer")); + QCOMPARE(thumbnails.missingPackage("application/pdf"), QString("papers")); + QCOMPARE(thumbnails.missingPackage("application/vnd.oasis.opendocument.text"), QString("libgsf")); + // Installed already, drawn by Qt itself, or no package known: nothing to name. + QVERIFY(thumbnails.missingPackage("video/webm").isEmpty()); + QVERIFY(thumbnails.missingPackage("image/png").isEmpty()); + QVERIFY(thumbnails.missingPackage("model/step").isEmpty()); + } + void previewerPicksAViewerForEachType() { QCOMPARE(Previewer::viewerFor("image/png", false), QString("image")); From 9621d88b0f1e084ddd6106872ddf0e74563d97ab Mon Sep 17 00:00:00 2001 From: GM Date: Wed, 11 Nov 2026 03:38:19 -0400 Subject: [PATCH 06/12] Let a thumbnailer installed later draw what failed A file nothing installed could draw kept its failure marker, so a thumbnailer installed afterwards never ran for it. The marker now records the thumbnailer the type had, none included, and holds the file back only while that is unchanged. --- src/thumbnails.cpp | 18 ++++++++++++------ src/thumbnails.h | 5 +++-- tests/folio_tests.cpp | 16 ++++++++++++++++ 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/thumbnails.cpp b/src/thumbnails.cpp index 46d7b92e..46fc461a 100644 --- a/src/thumbnails.cpp +++ b/src/thumbnails.cpp @@ -231,6 +231,9 @@ QString Thumbnails::failPath(const QString &path) const { return m_cache + "/" + failures + "/" + cacheKey(path); } +// The marker's own key for the thumbnailer command the type had when it failed, empty for none. +static const QString kFailedWith = QStringLiteral("X-Folio::Thumbnailer"); + QImage Thumbnails::load(const QString &path, const QString &mime, int size) const { const QFileInfo info(path); if (!info.isFile()) @@ -243,14 +246,15 @@ QImage Thumbnails::load(const QString &path, const QString &mime, int size) cons const QDateTime modified = info.lastModified(); if (const QImage image(cachePath(path, size)); madeFor(image, path, modified)) return image; - if (failedBefore(path, modified)) - return {}; // A folder's peek asks for any picture ("image/*"); named by extension, // as the grid names it, it goes to the same thumbnailer as its tile. const QString type = mime.endsWith("/*") ? QMimeDatabase().mimeTypeForFile(path, QMimeDatabase::MatchExtension).name() : mime; + const QString thumbnailer = m_thumbnailers.value(type); + if (failedBefore(path, modified, thumbnailer)) + return {}; const int pixels = bucketPixels(bucketFor(size)); bool attempted = false; QImage image = generate(path, type, pixels, attempted); @@ -258,7 +262,7 @@ QImage Thumbnails::load(const QString &path, const QString &mime, int size) cons // Only a reader or thumbnailer that ran and failed is remembered, so // one installed later still gets its turn. if (attempted) - rememberFailure(path, modified); + rememberFailure(path, modified, thumbnailer); return {}; } if (image.width() > pixels || image.height() > pixels) @@ -400,14 +404,16 @@ void Thumbnails::remember(QImage image, const QString &path, int size, const QDa save(std::move(image), cachePath(path, size), path, modified); } -void Thumbnails::rememberFailure(const QString &path, const QDateTime &modified) const { +void Thumbnails::rememberFailure(const QString &path, const QDateTime &modified, const QString &thumbnailer) const { QImage marker(1, 1, QImage::Format_ARGB32); marker.fill(Qt::transparent); + marker.setText(kFailedWith, thumbnailer); save(std::move(marker), failPath(path), path, modified); } -bool Thumbnails::failedBefore(const QString &path, const QDateTime &modified) const { - return madeFor(QImage(failPath(path)), path, modified); +bool Thumbnails::failedBefore(const QString &path, const QDateTime &modified, const QString &thumbnailer) const { + const QImage marker(failPath(path)); + return madeFor(marker, path, modified) && marker.text(kFailedWith) == thumbnailer; } // Whether a cached thumbnail (or failure marker) was made from the file as it diff --git a/src/thumbnails.h b/src/thumbnails.h index bedf77f1..cad3ba10 100644 --- a/src/thumbnails.h +++ b/src/thumbnails.h @@ -45,8 +45,9 @@ class Thumbnails : public QObject { QImage runThumbnailer(const QString &command, const QString &path, int pixels) const; static QImage pdfPage(const QString &path, int pixels); void remember(QImage image, const QString &path, int size, const QDateTime &modified) const; - void rememberFailure(const QString &path, const QDateTime &modified) const; - bool failedBefore(const QString &path, const QDateTime &modified) const; + // A failure goes with the type's thumbnailer as it was then, none included: one installed since gets its turn. + void rememberFailure(const QString &path, const QDateTime &modified, const QString &thumbnailer) const; + bool failedBefore(const QString &path, const QDateTime &modified, const QString &thumbnailer) const; void save(QImage image, const QString &target, const QString &path, const QDateTime &modified) const; static bool madeFor(const QImage &image, const QString &path, const QDateTime &modified); diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 45b6320f..9ae61ccf 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -5162,6 +5162,22 @@ private slots: QCOMPARE(QFileInfo(runs).size(), 8); } + void thumbnailsLetAThumbnailerInstalledLaterDraw() + { + QTemporaryDir dir; + // A camera RAW with no picture inside and nothing installed to draw it fails, and that is remembered. + const QString raw = dir.filePath("bare.cr2"); + write(raw, "no picture inside"); + QVERIFY(Thumbnails(dir.filePath("cache"), {dir.filePath("none")}).load(raw, "image/x-canon-cr2", 128).isNull()); + QVERIFY(QFile::exists(dir.filePath("cache/" + Thumbnails::failures + "/" + Thumbnails::cacheKey(raw)))); + + // The failure was the built-in reader's, so a thumbnailer installed since still draws the file. + QVERIFY(QImage(10, 20, QImage::Format_RGB32).save(dir.filePath("mark.png"))); + write(dir.filePath("raw.thumbnailer"), "[Thumbnailer Entry]\nTryExec=/bin/cp\nExec=/bin/cp \"" + + dir.filePath("mark.png").toUtf8() + "\" %o\nMimeType=image/x-canon-cr2;\n"); + QCOMPARE(Thumbnails(dir.filePath("cache"), {dir.path()}).load(raw, "image/x-canon-cr2", 128).size(), QSize(10, 20)); + } + void thumbnailsRememberOnlyWhatFailed() { QTemporaryDir dir; From 29ee8c266e4867bdd0dfea672c5db9ea5ecb5a67 Mon Sep 17 00:00:00 2001 From: GM Date: Wed, 11 Nov 2026 10:52:35 -0400 Subject: [PATCH 07/12] Turn a RAW preview by its own directory's orientation One orientation was kept for the whole file, so a preview whose directory names a turn of its own was turned by IFD0's instead. Each embedded JPEG now carries its directory's orientation, and takes the file's only when its directory names none. --- src/rawembed.cpp | 30 ++++++++++++++++++++---------- tests/folio_tests.cpp | 21 +++++++++++++++++++++ 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/rawembed.cpp b/src/rawembed.cpp index 5ba6d702..8edc7c98 100644 --- a/src/rawembed.cpp +++ b/src/rawembed.cpp @@ -98,10 +98,16 @@ bool tiffHeader(Source &src, quint64 at, bool &le, quint64 &ifd) { return rd16(b.constData() + 2, le) == kTiffMagic; } +// A JPEG a directory names, with the turn that directory gives its picture, if any. +struct Candidate { + quint64 offset = 0, length = 0; + std::optional turn; +}; + struct Walker { explicit Walker(Source &source) : src(source) {} Source &src; - QVector> candidates; + QVector candidates; // Each directory is read once, so a cycle neither loops nor uses up the limit. QVector seen; std::optional orientation; @@ -134,13 +140,13 @@ struct Walker { if (count > kMaxEntries || !src.get(at + 2, count * kEntryLength, entries)) return; seen.append(at); - std::optional jpegOffset, jpegLength, stripOffset, stripLength, compression; + std::optional jpegOffset, jpegLength, stripOffset, stripLength, compression, turn; QVector children; for (int i = 0; i < count; ++i) { const char *e = entries.constData() + i * kEntryLength; const quint16 tag = rd16(e, le), type = rd16(e + 2, le); const quint32 n = rd32(e + 4, le); - if (tag == kOrientation && !orientation) orientation = one(le, type, n, e + 8); + if (tag == kOrientation) turn = one(le, type, n, e + 8); else if (tag == kJpegOffset) jpegOffset = one(le, type, n, e + 8); else if (tag == kJpegLength) jpegLength = one(le, type, n, e + 8); // Many strips are raw sensor data, never one preview. @@ -152,12 +158,14 @@ struct Walker { for (quint32 k = 0; k < qMin(n, kMaxPointers); ++k) children.append(type == kShort ? rd16(b.constData() + k * 2, le) : rd32(b.constData() + k * 4, le)); } + // The first directory to name a turn speaks for the file, as IFD0 does in a camera's own. + if (!orientation) orientation = turn; if (jpegOffset && jpegLength && *jpegLength > 0) - candidates.append({*jpegOffset, *jpegLength}); + candidates.append({*jpegOffset, *jpegLength, turn}); // Any lone JPEG-compressed strip is a preview (a CR2 keeps its big one in IFD0); the frame rule keeps raw data out. if (stripOffset && stripLength && compression && (*compression == kOldJpeg || *compression == kJpeg) && *stripLength > 0) - candidates.append({*stripOffset, *stripLength}); + candidates.append({*stripOffset, *stripLength, turn}); for (const quint64 child : children) walk(le, depth + 1, child); // A next pointer that cannot be read ends the chain and keeps what it collected. @@ -238,14 +246,15 @@ std::optional ownOrientation(const QByteArray &jpeg) { struct Preview { quint64 offset = 0, length = 0, pixels = 0; + std::optional turn; }; // Most pixels first, the longer of two equals ahead; only Huffman frames (SOF0 to SOF2) count, SOF3 is raw data. -QVector ranked(QFile &file, const QVector> &candidates) { +QVector ranked(QFile &file, const QVector &candidates) { qint64 sofLeft = kSofBudget; QVector previews; for (int i = 0; i < qMin(int(candidates.size()), kMaxCandidates); ++i) { - const quint64 offset = candidates[i].first, length = candidates[i].second; + const quint64 offset = candidates[i].offset, length = candidates[i].length; // A span running past the end of the file is a cut file, not a preview. if (length < quint64(kMinJpeg) || sofLeft == 0 || offset > quint64(file.size()) || length > quint64(file.size()) - offset) @@ -257,7 +266,7 @@ QVector ranked(QFile &file, const QVector> &can const bool found = span >= kMinJpeg && file.seek(qint64(offset)) && firstFrame(file, left, marker, width, height); sofLeft -= span - left; if (found && marker <= kLastHuffmanFrame) - previews.append({offset, length, quint64(width) * height}); + previews.append({offset, length, quint64(width) * height, candidates[i].turn}); } std::stable_sort(previews.begin(), previews.end(), [](const Preview &a, const Preview &b) { return a.pixels != b.pixels ? a.pixels > b.pixels : a.length > b.length; @@ -265,7 +274,7 @@ QVector ranked(QFile &file, const QVector> &can return previews; } -// One preview read and scaled to fit `pixels`, turned by the directory's `turn` when it names none of its own. +// One preview read and scaled to fit `pixels`, turned by `turn` when it names no orientation of its own. QImage decoded(QFile &file, const Preview &preview, quint64 turn, int pixels) { if (!file.seek(qint64(preview.offset))) return {}; @@ -314,7 +323,8 @@ QImage RawEmbed::thumbnail(const QString &path, int pixels) { if (preview.length > left) continue; left -= preview.length; - const QImage image = decoded(file, preview, walker.orientation.value_or(0), pixels); + // A directory's own turn rules its picture; one that names none takes the file's. + const QImage image = decoded(file, preview, preview.turn.value_or(walker.orientation.value_or(0)), pixels); if (!image.isNull()) return image; } diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 9ae61ccf..6bfaf54c 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -5280,6 +5280,27 @@ private slots: QCOMPARE(rawThumbnail(rawWithJpeg(jpeg(400, 300), kQuarterTurn)), QSize(300, 400)); QCOMPARE(rawThumbnail(rawWithJpeg(jpeg(400, 300, kUpright), kQuarterTurn)), QSize(400, 300)); QCOMPARE(rawThumbnail(rawWithJpeg(jpeg(400, 300, kQuarterTurn), kUpright)), QSize(300, 400)); + + // A later directory that names a turn of its own rules its picture; one that names none takes IFD0's. + const QByteArray picture = jpeg(400, 300); + const auto turned = [&](quint32 first, const Entries &own) { + Raw raw(true); + const quint32 at = raw.add(picture); + raw.setFirst(raw.ifd({{kOrientation, kShort, 1, first}}, raw.ifd(pair(at, picture.size()) + own))); + return rawThumbnail(raw.bytes); + }; + QCOMPARE(turned(kUpright, {{kOrientation, kShort, 1, kQuarterTurn}}), QSize(300, 400)); + QCOMPARE(turned(kQuarterTurn, {{kOrientation, kShort, 1, kUpright}}), QSize(400, 300)); + QCOMPARE(turned(kQuarterTurn, {}), QSize(300, 400)); + + // Of two directories that name a turn the first speaks for the file, so a third that names none takes that one. + Raw three(true); + const QByteArray small = jpeg(160, 120); + const quint32 bigAt = three.add(picture), smallAt = three.add(small); + three.setFirst(three.ifd({{kOrientation, kShort, 1, kQuarterTurn}}, + three.ifd(pair(smallAt, small.size()) + Entries{{kOrientation, kShort, 1, kUpright}}, + three.ifd(pair(bigAt, picture.size()))))); + QCOMPARE(rawThumbnail(three.bytes), QSize(300, 400)); } void thumbnailsPickTheLargestEmbeddedJpeg() From c19cd88a3e4c0bdd180c0ab33b260b8528dd7fa3 Mon Sep 17 00:00:00 2001 From: GM Date: Wed, 11 Nov 2026 10:52:35 -0400 Subject: [PATCH 08/12] Name no package for a PDF that pdftoppm draws With Poppler installed and no PDF thumbnailer, a PDF that failed to draw (damaged, or locked with a password) had the details pane name papers as missing. A PDF's package is now named only when pdftoppm is not there either. --- src/thumbnails.cpp | 5 +++-- tests/folio_tests.cpp | 13 ++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/thumbnails.cpp b/src/thumbnails.cpp index 46fc461a..a2294686 100644 --- a/src/thumbnails.cpp +++ b/src/thumbnails.cpp @@ -198,9 +198,10 @@ Thumbnails::Thumbnails(const QString &cacheDirectory, const QStringList &thumbna } QString Thumbnails::missingPackage(const QString &mime) const { - // Qt draws its own pictures in-process, and an installed thumbnailer needs nothing more. + // Qt draws its own pictures in-process, an installed thumbnailer needs nothing more, and pdftoppm draws a PDF without one. if ((mime.startsWith("image/") && QImageReader::supportedMimeTypes().contains(mime.toLatin1())) - || m_thumbnailers.contains(mime)) + || m_thumbnailers.contains(mime) + || (mime == "application/pdf" && !QStandardPaths::findExecutable("pdftoppm").isEmpty())) return {}; return packageFor().value(mime); } diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 6bfaf54c..7cfab3ea 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -5412,12 +5412,23 @@ private slots: "[Thumbnailer Entry]\nTryExec=/bin/sh\nExec=/bin/sh -c \"exit 1\" %i %o\nMimeType=video/webm;\n"); const Thumbnails thumbnails(dir.filePath("cache"), {dir.path()}); QCOMPARE(thumbnails.missingPackage("video/mp4"), QString("ffmpegthumbnailer")); - QCOMPARE(thumbnails.missingPackage("application/pdf"), QString("papers")); QCOMPARE(thumbnails.missingPackage("application/vnd.oasis.opendocument.text"), QString("libgsf")); // Installed already, drawn by Qt itself, or no package known: nothing to name. QVERIFY(thumbnails.missingPackage("video/webm").isEmpty()); QVERIFY(thumbnails.missingPackage("image/png").isEmpty()); QVERIFY(thumbnails.missingPackage("model/step").isEmpty()); + + // A PDF has its package named only where pdftoppm is not there to draw it. + const QByteArray programs = qgetenv("PATH"); + QVERIFY(QDir(dir.path()).mkpath("bin")); + qputenv("PATH", dir.filePath("bin").toUtf8()); + const QString without = thumbnails.missingPackage("application/pdf"); + write(dir.filePath("bin/pdftoppm"), "#!/bin/sh\n"); + QFile::setPermissions(dir.filePath("bin/pdftoppm"), QFile::ReadOwner | QFile::ExeOwner); + const QString with = thumbnails.missingPackage("application/pdf"); + qputenv("PATH", programs); + QCOMPARE(without, QString("papers")); + QVERIFY(with.isEmpty()); } void previewerPicksAViewerForEachType() From c9ccf3ce40284b4db9cd129ce6d6f5530f7b23e9 Mon Sep 17 00:00:00 2001 From: GM Date: Wed, 11 Nov 2026 05:52:23 -0400 Subject: [PATCH 09/12] Show in a row the picture its tile shows The list and the columns asked for a thumbnail only for a photo or a video, so a PDF, a song with a cover or a document had its picture in the grid and an icon in a row. A row now asks for every picture a tile shows but a text file's page, which says nothing at a row's size, and a page or a cover is fitted whole in the row where a photo is cropped. --- src/FileRow.qml | 14 ++++-- src/FileTile.qml | 2 +- src/Format.js | 10 ++++ tests/folio_tests.cpp | 110 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 6 deletions(-) diff --git a/src/FileRow.qml b/src/FileRow.qml index a8dab284..b4779bb9 100644 --- a/src/FileRow.qml +++ b/src/FileRow.qml @@ -80,7 +80,11 @@ Item { implicitHeight: 20 Image { id: thumb - anchors.fill: parent + // A fitted picture is laid out at its own shape, as on a tile, so its corners can round. + readonly property real fit: Format.filled(row.kind) || implicitWidth <= 0 ? 0 : Math.min(parent.width / implicitWidth, parent.height / implicitHeight) + width: fit > 0 ? implicitWidth * fit : parent.width + height: fit > 0 ? implicitHeight * fit : parent.height + anchors.centerIn: parent layer.enabled: theme.radius > 0 && status === Image.Ready layer.effect: MultiEffect { maskEnabled: true @@ -89,14 +93,14 @@ Item { maskSpreadAtMin: 1 } asynchronous: true - fillMode: Image.PreserveAspectCrop - source: row.kind === "image" || row.kind === "video" ? Format.thumb(row.path, row.mime, 128, row.modified) : "" + fillMode: Format.filled(row.kind) ? Image.PreserveAspectCrop : Image.Stretch + source: Format.pictured(row.isDir, row.kind) ? Format.thumb(row.path, row.mime, 128, row.modified) : "" visible: status === Image.Ready } Rectangle { id: rowMask - width: 20 - height: 20 + width: thumb.width + height: thumb.height radius: Math.min(4, theme.radius) visible: false layer.enabled: true diff --git a/src/FileTile.qml b/src/FileTile.qml index 4c85c7a5..51fbf21d 100644 --- a/src/FileTile.qml +++ b/src/FileTile.qml @@ -192,7 +192,7 @@ Item { Item { // The picture is laid out at its own shape (cropped to fill for // photos and video, fitted for pages), so its corners can round. - readonly property bool crop: tile.kind === "image" || tile.kind === "video" + readonly property bool crop: Format.filled(tile.kind) readonly property size natural: thumb.implicitWidth > 0 ? Qt.size(thumb.implicitWidth, thumb.implicitHeight) : Qt.size(1, 1) readonly property real fit: Math.min(width / natural.width, height / natural.height) Image { diff --git a/src/Format.js b/src/Format.js index a359f60e..33eea6a6 100644 --- a/src/Format.js +++ b/src/Format.js @@ -69,3 +69,13 @@ function page(path, px, modified, paper, ink) { function keys(text) { return text.replace(/(\S)\+(?=\S)/g, "$1 + ") } + +// Whether a row asks for an item's thumbnail, as its tile does: every file but text, which a tile draws as a page and a row leaves to its icon. +function pictured(isDir, kind) { + return !isDir && kind !== "text" && kind !== "item" +} + +// Photos and video fill their frame; a page or a cover is fitted whole. +function filled(kind) { + return kind === "image" || kind === "video" +} diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 7cfab3ea..cdb86eea 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -15,11 +15,13 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include #include @@ -474,6 +476,34 @@ static Plugins::Run runBundled(const Plugins::Manifest &plugin, const QStringLis return Plugins::execute(plugin, command, env, 5000); } +// Answers every picture asked of it and keeps the pixels and path of each, for a test of what a view asks for. +class AskedPictures : public QQuickImageProvider { +public: + AskedPictures() : QQuickImageProvider(QQuickImageProvider::Image) {} + QImage requestImage(const QString &id, QSize *size, const QSize &) override + { + const QMutexLocker locker(&m_mutex); + // "128/1700000000000/image%2Fpng/%2Ftmp%2Fa%20b%2Fphoto.png", or for a text file's page "page/256/1700000000000/1a1b26/a9b1d6/%2Ftmp%2Fnotes.txt": the path last and percent-encoded. + const bool page = id.startsWith("page/"); + m_asked << id.section('/', 0, 0) + ' ' + QUrl::fromPercentEncoding(id.section('/', page ? 5 : 3).toUtf8()); + // Twice as tall as wide, so a test can tell a fitted picture from a cropped one by its shape. + QImage image(8, 16, QImage::Format_RGB32); + image.fill(Qt::gray); + if (size) + *size = image.size(); + return image; + } + QStringList asked() + { + const QMutexLocker locker(&m_mutex); + return m_asked; + } + +private: + QMutex m_mutex; + QStringList m_asked; +}; + class FolioTests : public QObject { Q_OBJECT @@ -5121,6 +5151,86 @@ private slots: QCOMPARE(shownWidth(second.addMSecs(600)), 128); } + void rowsAskForThePicturesTilesAskFor() + { + // A row of the list and the columns asks for the thumbnail its tile asks for, and only photos and video are cropped. Text is the one difference: a tile draws its page, a row keeps its icon. + QTemporaryDir dir; + Theme theme(dir.path()); + Translator translator(dir.path() + "/language", dir.path()); + QQmlEngine engine; + auto *pictures = new AskedPictures; + engine.addImageProvider("thumb", pictures); + engine.rootContext()->setContextProperty("theme", &theme); + engine.rootContext()->setContextProperty("translator", &translator); + QQmlComponent row(&engine, QUrl::fromLocalFile(QFINDTESTDATA("../src/FileRow.qml"))); + QQmlComponent tile(&engine, QUrl::fromLocalFile(QFINDTESTDATA("../src/FileTile.qml"))); + // The thumbnail pictures an item holds, as " ". + const auto asks = [](QObject *item) { + QStringList found; + for (QObject *child : item->findChildren()) { + const QString source = child->inherits("QQuickImage") ? child->property("source").toUrl().toString() : QString(); + if (source.startsWith("image://thumb/")) + found << source.section('/', 3, 3) + ' ' + QString::number(child->property("fillMode").toInt()); + } + return found; + }; + // The first thumbnail picture an item holds. + const auto picture = [](QObject *item) { + for (QObject *child : item->findChildren()) + if (child->inherits("QQuickImage") && child->property("source").toUrl().toString().startsWith("image://thumb/")) + return child; + return static_cast(nullptr); + }; + constexpr int stretch = 0, crop = 2, ready = 1; + constexpr qreal frame = 20; + int pictured = 0; + struct Case { + const char *name, *mime; + bool pictured, filled; + }; + // The three that are not pictured go first, so the waits of the others cover an ask of theirs that came late. + for (const Case &c : {Case{"notes.txt", "text/plain", false, false}, Case{"Folder", "inode/directory", false, false}, + Case{"studio", "", false, false}, + Case{"photo.png", "image/png", true, true}, Case{"clip.mp4", "video/mp4", true, true}, + Case{"song.mp3", "audio/mpeg", true, false}, Case{"paper.pdf", "application/pdf", true, false}, + Case{"backup.zip", "application/zip", true, false}, Case{"model.stl", "model/stl", true, false}}) { + // A plugin's item has no type: its kind is set where the items are listed. + const QString kind = *c.mime ? FolderModel::kindForMime(c.mime) : QStringLiteral("item"); + const QVariantMap item{{"index", 0}, {"name", c.name}, {"path", dir.filePath(c.name)}, {"isDir", kind == "folder"}, + {"kind", kind}, {"mime", c.mime}, {"extension", QFileInfo(c.name).suffix()}, {"size", 1}, + {"modified", QDateTime::currentDateTime()}, {"itemCount", 0}, {"icon", kind == "item" ? "peer" : ""}, + {"selected", false}}; + std::unique_ptr aRow(row.createWithInitialProperties(item)); + QVERIFY2(aRow, qPrintable(row.errorString())); + std::unique_ptr aTile(tile.createWithInitialProperties(item)); + QVERIFY2(aTile, qPrintable(tile.errorString())); + const QString rowFill = QString::number(c.filled ? crop : stretch); + QVERIFY2(asks(aRow.get()) == (c.pictured ? QStringList{"128 " + rowFill} : QStringList{}), c.name); + // A tile lays a fitted picture out at its own shape, so its fill mode is its own; a folder's and a plugin item's tile are drawn, not asked, and text asks for its page. + const bool text = kind == "text"; + QVERIFY2(asks(aTile.get()).size() == (c.pictured || text ? 1 : 0), c.name); + if (c.pictured || text) + QVERIFY2(asks(aTile.get()).first().startsWith(text ? "page " : "256 "), c.name); + // What they hold is what reaches the cache: one ask of each size for this file. + const QString path = dir.filePath(c.name); + if (c.pictured) { + QTRY_VERIFY2(pictures->asked().count("128 " + path) == 1 && pictures->asked().count("256 " + path) == 1, c.name); + // Once drawn, a photo fills the row's frame; a page or a cover takes its own shape inside it, where its corners can round. + const QObject *shown = picture(aRow.get()); + QVERIFY2(shown, c.name); + QTRY_VERIFY2(shown->property("status").toInt() == ready, c.name); + QVERIFY2(shown->property("width").toReal() == (c.filled ? frame : frame / 2), c.name); + QVERIFY2(shown->property("height").toReal() == frame, c.name); + } + // The page is waited for while its tile lives: an ask still queued when its picture goes is dropped. + if (text) + QTRY_VERIFY2(pictures->asked().count("page " + path) == 1, c.name); + pictured += c.pictured; + } + // Nothing else was asked but the one page: a file that is not pictured never reaches the cache. + QCOMPARE(pictures->asked().size(), 2 * pictured + 1); + } + void thumbnailsReadInstalledThumbnailers() { QTemporaryDir dir; From 66d1d4636a2376e73f12eeee2b3b1dcb5efc9824 Mon Sep 17 00:00:00 2001 From: GM Date: Wed, 11 Nov 2026 09:47:40 -0400 Subject: [PATCH 10/12] Hand a thumbnailer a name that holds a code as it is A thumbnailer's command was filled in one code after another, so a file called 50%off.mp4 had the %o in its own name replaced by the output path. The thumbnailer was sent a file that is not there, and the failure was remembered. Each code is now read once. --- src/thumbnails.cpp | 25 ++++++++++++++++++++++--- tests/folio_tests.cpp | 20 ++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/thumbnails.cpp b/src/thumbnails.cpp index a2294686..87e1cb12 100644 --- a/src/thumbnails.cpp +++ b/src/thumbnails.cpp @@ -70,12 +70,31 @@ QByteArray withPlainText(const QByteArray &png) { return out; } -// The spec's Exec field: %i input path, %u input URI, %o output, %s size. +// The spec's Exec field, such as "ffmpegthumbnailer -i %i -o %o -s %s -f": %i input path, %u input URI, %o output, %s size, %% a percent sign. +// Each code is read once, so a name that holds one arrives as it is. QStringList expandCommand(const QString &command, const QString &input, const QString &output, int size) { QStringList args = QProcess::splitCommand(command); for (QString &arg : args) { - arg.replace("%i", input).replace("%u", fileUri(input)).replace("%o", output) - .replace("%s", QString::number(size)).replace("%%", "%"); + QString expanded; + for (qsizetype at = 0; at < arg.size(); ++at) { + const QChar code = arg.at(at) == '%' && at + 1 < arg.size() ? arg.at(at + 1) : QChar(); + if (code == 'i') + expanded += input; + else if (code == 'u') + expanded += fileUri(input); + else if (code == 'o') + expanded += output; + else if (code == 's') + expanded += QString::number(size); + else if (code == '%') + expanded += '%'; + else { + expanded += arg.at(at); + continue; + } + ++at; + } + arg = expanded; } return args; } diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index cdb86eea..6d5f8d11 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -5246,6 +5246,26 @@ private slots: QVERIFY(QFile::exists(dir.filePath("cache/" + Thumbnails::failures + "/" + Thumbnails::cacheKey(dir.filePath("clip.mp4"))))); } + void thumbnailsHandAThumbnailerANameThatHoldsACode() + { + QTemporaryDir dir; + // The thumbnailer copies its input out and notes the third argument it was given beside itself. + const QString script = dir.filePath("copy.sh"); + write(script, "cp \"$1\" \"$2\" && printf %s \"$3\" > \"$0.said\"\n"); + write(dir.filePath("demo.thumbnailer"), "[Thumbnailer Entry]\nExec=/bin/sh \"" + script.toUtf8() + + "\" %i %o \"--size=%s,%s 100%% %u\"\nMimeType=video/mp4;\n"); + Thumbnails thumbnails(dir.filePath("cache"), {dir.path()}); + + // A name with every code in it is still one file to the thumbnailer, by path and by URI. + const QString clip = dir.filePath("50%off %sale %up %in 100%%.mp4"); + QVERIFY(QImage(10, 20, QImage::Format_RGB32).save(clip, "PNG")); + QCOMPARE(thumbnails.load(clip, "video/mp4", 128).size(), QSize(10, 20)); + QFile said(script + ".said"); + QVERIFY(said.open(QIODevice::ReadOnly)); + QCOMPARE(QString::fromUtf8(said.readAll()), + "--size=128,128 100% " + QUrl::fromLocalFile(clip).toString(QUrl::FullyEncoded)); + } + void thumbnailsTryAgainWhatAnOlderVersionCouldnt() { QTemporaryDir dir; From 6d536ec447b8609030e4214a20005581aa1cf701 Mon Sep 17 00:00:00 2001 From: GM Date: Wed, 11 Nov 2026 09:53:56 -0400 Subject: [PATCH 11/12] Remember a failed thumbnail only when its file was judged A failure marker keeps every app from trying a file again, and one was written for a thumbnailer that never started, for a spec whose program is not installed, for a file that could not be read and for one moved away or written to during the run. None of those says the file is broken, so none is remembered now, and a PDF whose thumbnailer cannot start is left to pdftoppm. --- src/thumbnails.cpp | 75 +++++++++++++++++++------------ src/thumbnails.h | 5 ++- tests/folio_tests.cpp | 102 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+), 30 deletions(-) diff --git a/src/thumbnails.cpp b/src/thumbnails.cpp index 87e1cb12..8370d526 100644 --- a/src/thumbnails.cpp +++ b/src/thumbnails.cpp @@ -99,6 +99,21 @@ QStringList expandCommand(const QString &command, const QString &input, const QS return args; } +// How a started helper ended. One that never started has said nothing about the file it was given. +enum class Ran { NotStarted, Failed, Finished }; + +Ran finish(QProcess &process) { + if (!process.waitForStarted(15000)) + return Ran::NotStarted; + // A broken file must never hang a tile forever. + if (!process.waitForFinished(15000)) { + process.kill(); + process.waitForFinished(1000); + return Ran::Failed; + } + return process.exitStatus() == QProcess::NormalExit && process.exitCode() == 0 ? Ran::Finished : Ran::Failed; +} + // Each optional package's thumbnailers and the MimeType= lines they ship, as "video/mp4;video/webm;". struct PackageTypes { const char *package; @@ -201,7 +216,10 @@ Thumbnails::Thumbnails(const QString &cacheDirectory, const QStringList &thumbna } const QString exec = fields.value("Exec"); const QString tryExec = fields.value("TryExec"); - if (exec.isEmpty() || (!tryExec.isEmpty() && QStandardPaths::findExecutable(tryExec).isEmpty())) + // The program Exec names, read as it will run (%% is one percent sign), has to be there: a spec a removed program left behind draws nothing. + const QStringList command = expandCommand(exec, {}, {}, 0); + const auto missing = [](const QString &program) { return QStandardPaths::findExecutable(program).isEmpty(); }; + if (command.isEmpty() || missing(command.first()) || (!tryExec.isEmpty() && missing(tryExec))) continue; for (const QString &declared : fields.value("MimeType").split(';', Qt::SkipEmptyParts)) { const QString mime = declared.trimmed(); @@ -275,13 +293,19 @@ QImage Thumbnails::load(const QString &path, const QString &mime, int size) cons const QString thumbnailer = m_thumbnailers.value(type); if (failedBefore(path, modified, thumbnailer)) return {}; + // A file that cannot be read is not a broken one: nothing is tried, so nothing is remembered. + if (!info.isReadable()) + return {}; const int pixels = bucketPixels(bucketFor(size)); bool attempted = false; QImage image = generate(path, type, pixels, attempted); if (image.isNull()) { // Only a reader or thumbnailer that ran and failed is remembered, so // one installed later still gets its turn. - if (attempted) + if (!attempted) + return {}; + // A file moved away or written to meanwhile was never judged as it is now. + if (QFileInfo(path).lastModified() == modified) rememberFailure(path, modified, thumbnailer); return {}; } @@ -315,14 +339,18 @@ QImage Thumbnails::generate(const QString &path, const QString &mime, int pixels } const QString command = m_thumbnailers.value(mime); if (!command.isEmpty()) { - attempted = true; - return runThumbnailer(command, path, pixels); + // A thumbnailer that could not start has not judged the file, whatever a reader made of it first; a PDF is then left to pdftoppm. + QImage image; + attempted = runThumbnailer(command, path, pixels, image); + if (attempted) + return image; } // Without a PDF thumbnailer installed, Poppler's pdftoppm (there with // CUPS) draws the first page. if (mime == "application/pdf" && !QStandardPaths::findExecutable("pdftoppm").isEmpty()) { - attempted = true; - return pdfPage(path, pixels); + QImage image; + attempted = pdfPage(path, pixels, image); + return image; } return {}; } @@ -374,31 +402,27 @@ QImage Thumbnails::textPage(const QString &path, int pixels, const QColor &paper return page; } -QImage Thumbnails::pdfPage(const QString &path, int pixels) { +bool Thumbnails::pdfPage(const QString &path, int pixels, QImage &image) { QTemporaryDir scratch; if (!scratch.isValid()) - return {}; + return false; QProcess process; process.start("pdftoppm", {"-f", "1", "-l", "1", "-singlefile", "-png", "-scale-to", QString::number(pixels), path, scratch.filePath("page")}); - if (!process.waitForFinished(15000)) { - process.kill(); - process.waitForFinished(1000); - return {}; - } - if (process.exitStatus() != QProcess::NormalExit || process.exitCode() != 0) - return {}; - return QImage(scratch.filePath("page.png")); + const Ran ran = finish(process); + if (ran == Ran::Finished) + image = QImage(scratch.filePath("page.png")); + return ran != Ran::NotStarted; } -QImage Thumbnails::runThumbnailer(const QString &command, const QString &path, int pixels) const { +bool Thumbnails::runThumbnailer(const QString &command, const QString &path, int pixels, QImage &image) const { QTemporaryDir scratch; if (!scratch.isValid()) - return {}; + return false; const QString output = scratch.filePath("thumbnail.png"); QStringList args = expandCommand(command, path, output, pixels); if (args.isEmpty()) - return {}; + return false; const QString program = args.takeFirst(); // ffmpegthumbnailer's -f frames the picture as a film strip; the tile // marks a video with its own play badge instead. @@ -408,15 +432,10 @@ QImage Thumbnails::runThumbnailer(const QString &command, const QString &path, i QProcess process; process.setProcessChannelMode(QProcess::ForwardedErrorChannel); process.start(program, args); - // A broken file must never hang a tile forever. - if (!process.waitForFinished(15000)) { - process.kill(); - process.waitForFinished(1000); - return {}; - } - if (process.exitStatus() != QProcess::NormalExit || process.exitCode() != 0) - return {}; - return QImage(output); + const Ran ran = finish(process); + if (ran == Ran::Finished) + image = QImage(output); + return ran != Ran::NotStarted; } void Thumbnails::remember(QImage image, const QString &path, int size, const QDateTime &modified) const { diff --git a/src/thumbnails.h b/src/thumbnails.h index cad3ba10..c668e460 100644 --- a/src/thumbnails.h +++ b/src/thumbnails.h @@ -42,8 +42,9 @@ class Thumbnails : public QObject { QString failPath(const QString &path) const; // Sets `attempted` when Qt's reader or a thumbnailer could take the file. QImage generate(const QString &path, const QString &mime, int pixels, bool &attempted) const; - QImage runThumbnailer(const QString &command, const QString &path, int pixels) const; - static QImage pdfPage(const QString &path, int pixels); + // Both answer whether the program ran at all: one that could not start has not judged the file. + bool runThumbnailer(const QString &command, const QString &path, int pixels, QImage &image) const; + static bool pdfPage(const QString &path, int pixels, QImage &image); void remember(QImage image, const QString &path, int size, const QDateTime &modified) const; // A failure goes with the type's thumbnailer as it was then, none included: one installed since gets its turn. void rememberFailure(const QString &path, const QDateTime &modified, const QString &thumbnailer) const; diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 6d5f8d11..bbd55cfa 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -5266,6 +5266,108 @@ private slots: "--size=128,128 100% " + QUrl::fromLocalFile(clip).toString(QUrl::FullyEncoded)); } + void thumbnailsRememberOnlyAVerdictOnTheFile() + { + QTemporaryDir dir; + const auto marked = [&](const QString &path) { + return QFile::exists(dir.filePath("cache/" + Thumbnails::failures + "/" + Thumbnails::cacheKey(path))); + }; + const auto spec = [&](const QString &name, const QString &exec, const QString &type) { + write(dir.filePath(name + ".thumbnailer"), ("[Thumbnailer Entry]\nExec=" + exec + "\nMimeType=" + type + ";\n").toUtf8()); + }; + const auto script = [&](const QString &name, const QByteArray &body) { + write(dir.filePath(name), body); + return "/bin/sh \"" + dir.filePath(name) + "\" %i %o"; + }; + const auto clip = [&](const QString &name) { + write(dir.filePath(name), "not a video"); + return dir.filePath(name); + }; + const QString frame = dir.filePath("frame.png"), copier = dir.filePath("copier"), unstartable = dir.filePath("unstartable"); + const auto owner = QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner; + QVERIFY(QImage(10, 20, QImage::Format_RGB32).save(frame)); + QVERIFY(QFile::copy("/bin/cp", copier) && QFile::setPermissions(copier, owner)); + write(unstartable, "#!/no/such/interpreter\n"); + QVERIFY(QFile::setPermissions(unstartable, owner)); + + spec("gone", "\"" + dir.filePath("no-such-program") + "\" %i %o", "video/mp4"); + spec("copier", "\"" + copier + "\" \"" + frame + "\" %o", "video/webm"); + spec("taker", script("take.sh", "rm \"$1\"\nexit 1\n"), "video/ogg"); + spec("writer", script("write.sh", "echo more >> \"$1\"\nexit 1\n"), "video/quicktime"); + spec("failer", script("fail.sh", "echo run >> \"$0.runs\"\nexit 1\n"), "video/mpeg"); + spec("unstartable", "\"" + unstartable + "\" %i %o", "application/pdf"); + // A program in a folder with a percent sign in its name is spelled with %% in Exec. + const QString percent = dir.filePath("100%tools/copier"); + QVERIFY(QDir(dir.path()).mkpath("100%tools") && QFile::copy("/bin/cp", percent) && QFile::setPermissions(percent, owner)); + spec("percent", "\"" + QString(percent).replace("%", "%%") + "\" \"" + frame + "\" %o", "video/x-matroska"); + const Thumbnails thumbnails(dir.filePath("cache"), {dir.path()}); + + // A spec whose program is not installed is no thumbnailer, and its type's package is still the one to name. + QVERIFY(thumbnails.thumbnailerFor("video/mp4").isEmpty()); + QCOMPARE(thumbnails.missingPackage("video/mp4"), QString("ffmpegthumbnailer")); + QVERIFY(thumbnails.load(clip("gone.mp4"), "video/mp4", 128).isNull()); + QVERIFY(!marked(dir.filePath("gone.mp4"))); + // One that is there under a name with a percent sign is found and draws. + QCOMPARE(thumbnails.load(clip("percent.mkv"), "video/x-matroska", 128).size(), QSize(10, 20)); + + // A thumbnailer that could not start has said nothing about the file: once it can, it draws it. + const QString later = clip("later.webm"); + QVERIFY(QFile::remove(copier)); + QVERIFY(thumbnails.load(later, "video/webm", 128).isNull()); + QVERIFY(!marked(later)); + QVERIFY(QFile::copy("/bin/cp", copier) && QFile::setPermissions(copier, owner)); + QCOMPARE(thumbnails.load(later, "video/webm", 128).size(), QSize(10, 20)); + + // A PDF's thumbnailer that cannot start leaves the page to pdftoppm, whose verdict counts only when it ran too. + const QString pdftoppm = dir.filePath("bin/pdftoppm"); + const QByteArray programs = qgetenv("PATH"); + QVERIFY(QDir(dir.path()).mkpath("bin")); + const auto pageWith = [&](const QByteArray &program, const QString &pdf) { + write(pdftoppm, program); + QFile::setPermissions(pdftoppm, owner); + qputenv("PATH", dir.filePath("bin").toUtf8()); + const QImage page = thumbnails.load(pdf, "application/pdf", 128); + qputenv("PATH", programs); + return page; + }; + const QString torn = clip("torn.pdf"), paper = clip("paper.pdf"); + QVERIFY(pageWith("#!/bin/sh\nexit 1\n", torn).isNull()); + QVERIFY(marked(torn)); + QVERIFY(pageWith("#!/no/such/interpreter\n", paper).isNull()); + QVERIFY(!marked(paper)); + // Its last argument is the page's path without the ending. + const QByteArray drawing = "#!/bin/sh\nfor last; do :; done\n/bin/cp \"" + frame.toUtf8() + "\" \"$last.png\"\n"; + QCOMPARE(pageWith(drawing, clip("drawn.pdf")).size(), QSize(10, 20)); + + // A file taken away during the run, or written to, was not judged as it is now. + const QString taken = clip("taken.ogv"), written = clip("written.mov"); + QVERIFY(thumbnails.load(taken, "video/ogg", 128).isNull()); + QVERIFY(!QFile::exists(taken)); + QVERIFY(!marked(taken)); + { + QFile old(written); + QVERIFY(old.open(QIODevice::ReadWrite)); + QVERIFY(old.setFileTime(QDateTime::currentDateTime().addSecs(-60), QFileDevice::FileModificationTime)); + } + QVERIFY(thumbnails.load(written, "video/quicktime", 128).isNull()); + QCOMPARE(QFileInfo(written).size(), 16); + QVERIFY(!marked(written)); + + // A file that cannot be read is not tried, so it is not held back once it can be; one that fails is. + if (geteuid() == 0) + QSKIP("root reads every file"); + const QString locked = clip("locked.mpg"), runs = dir.filePath("fail.sh.runs"); + QVERIFY(QFile::setPermissions(locked, QFile::WriteOwner)); + QVERIFY(thumbnails.load(locked, "video/mpeg", 128).isNull()); + QVERIFY(!QFile::exists(runs)); + QVERIFY(!marked(locked)); + QVERIFY(QFile::setPermissions(locked, QFile::ReadOwner | QFile::WriteOwner)); + QVERIFY(thumbnails.load(locked, "video/mpeg", 128).isNull()); + QVERIFY(thumbnails.load(locked, "video/mpeg", 128).isNull()); + QVERIFY(marked(locked)); + QCOMPARE(QFileInfo(runs).size(), 4); + } + void thumbnailsTryAgainWhatAnOlderVersionCouldnt() { QTemporaryDir dir; From b10ad6b8546d826407ab07d8bcbb272c1092810c Mon Sep 17 00:00:00 2001 From: GM Date: Wed, 11 Nov 2026 10:04:45 -0400 Subject: [PATCH 12/12] Stop a thumbnailer past its time with what it started A thumbnailer still running after 15 seconds was killed alone, so the programs it had started (glycin and the office thumbnailer start their own) ran on. Each now runs in a session of its own, under a keeper that stops the whole session past its time, and when Folio itself is gone. --- src/thumbnails.cpp | 77 ++++++++++++++++++++++++++++++++++--------- src/thumbnails.h | 7 ++-- tests/folio_tests.cpp | 62 ++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 17 deletions(-) diff --git a/src/thumbnails.cpp b/src/thumbnails.cpp index 8370d526..71aa9b65 100644 --- a/src/thumbnails.cpp +++ b/src/thumbnails.cpp @@ -23,6 +23,12 @@ #include #include #include +#include +#include +#include +#include +#include +#include namespace { @@ -99,16 +105,58 @@ QStringList expandCommand(const QString &command, const QString &input, const QS return args; } -// How a started helper ended. One that never started has said nothing about the file it was given. +// How long a stopped helper has to be collected, in milliseconds. +constexpr int stoppedPatience = 1000; + +// How a helper ended. One that never started has said nothing about the file it was given. enum class Ran { NotStarted, Failed, Finished }; -Ran finish(QProcess &process) { - if (!process.waitForStarted(15000)) +// What a keeper is sent when Folio is gone: it stops its whole session, itself included. +void stopSession(int) { ::kill(0, SIGKILL); } + +// Runs in the child Qt forks, before the helper's program: a lock another thread held may still be held there, so it calls only what a signal handler may. +void keepInASession(pid_t folio) { + // A session of its own, so whatever the helper starts can be stopped with it; a terminal's signals no longer reach it there. + ::setsid(); + const pid_t helper = ::_Fork(); + // Back to Qt in the helper, and in a child that could fork none and so becomes the helper itself. + if (helper <= 0) + return; + // The child stays as the helper's keeper; one of Folio's files held open would keep Qt waiting for the helper's start. + ::close_range(0, ~0U, 0); + struct sigaction gone = {}; + gone.sa_handler = stopSession; + ::sigaction(SIGTERM, &gone, nullptr); + ::prctl(PR_SET_PDEATHSIG, SIGTERM); + // Folio may have gone before the line above asked to be told. + if (::getppid() != folio) + stopSession(SIGTERM); + int status = 0; + pid_t ended = 0; + do ended = ::waitpid(helper, &status, 0); while (ended < 0 && errno == EINTR); + // An ending that could not be read is a failure, never a thumbnail. + ::_exit(ended == helper && WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE); +} + +// Stops a helper's whole session: the keeper's number is its process group, and one that has gone has no number left to signal. +void stop(QProcess &process) { + if (const qint64 leader = process.processId(); leader > 0) + ::kill(-pid_t(leader), SIGKILL); + process.kill(); + process.waitForFinished(stoppedPatience); +} + +Ran run(QProcess &process, const QString &program, const QStringList &args, int patience) { + process.setChildProcessModifier([folio = ::getpid()] { keepInASession(folio); }); + process.start(program, args); + if (!process.waitForStarted(patience)) { + // One still on its way to its program after that long is stopped too, or its keeper would be killed alone. + stop(process); return Ran::NotStarted; + } // A broken file must never hang a tile forever. - if (!process.waitForFinished(15000)) { - process.kill(); - process.waitForFinished(1000); + if (!process.waitForFinished(patience)) { + stop(process); return Ran::Failed; } return process.exitStatus() == QProcess::NormalExit && process.exitCode() == 0 ? Ran::Finished : Ran::Failed; @@ -181,10 +229,11 @@ const QHash &packageFor() { } // namespace -Thumbnails::Thumbnails(const QString &cacheDirectory, const QStringList &thumbnailerDirectories) +Thumbnails::Thumbnails(const QString &cacheDirectory, const QStringList &thumbnailerDirectories, int patience) : m_cache(cacheDirectory.isEmpty() ? QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/thumbnails" - : cacheDirectory) { + : cacheDirectory), + m_patience(patience) { QStringList directories = thumbnailerDirectories; if (directories.isEmpty()) { // Later directories win, so the user's own thumbnailers override the @@ -349,7 +398,7 @@ QImage Thumbnails::generate(const QString &path, const QString &mime, int pixels // CUPS) draws the first page. if (mime == "application/pdf" && !QStandardPaths::findExecutable("pdftoppm").isEmpty()) { QImage image; - attempted = pdfPage(path, pixels, image); + attempted = pdfPage(path, pixels, image, m_patience); return image; } return {}; @@ -402,14 +451,13 @@ QImage Thumbnails::textPage(const QString &path, int pixels, const QColor &paper return page; } -bool Thumbnails::pdfPage(const QString &path, int pixels, QImage &image) { +bool Thumbnails::pdfPage(const QString &path, int pixels, QImage &image, int patience) { QTemporaryDir scratch; if (!scratch.isValid()) return false; QProcess process; - process.start("pdftoppm", {"-f", "1", "-l", "1", "-singlefile", "-png", "-scale-to", QString::number(pixels), - path, scratch.filePath("page")}); - const Ran ran = finish(process); + const Ran ran = run(process, "pdftoppm", {"-f", "1", "-l", "1", "-singlefile", "-png", "-scale-to", QString::number(pixels), + path, scratch.filePath("page")}, patience); if (ran == Ran::Finished) image = QImage(scratch.filePath("page.png")); return ran != Ran::NotStarted; @@ -431,8 +479,7 @@ bool Thumbnails::runThumbnailer(const QString &command, const QString &path, int QProcess process; process.setProcessChannelMode(QProcess::ForwardedErrorChannel); - process.start(program, args); - const Ran ran = finish(process); + const Ran ran = run(process, program, args, m_patience); if (ran == Ran::Finished) image = QImage(output); return ran != Ran::NotStarted; diff --git a/src/thumbnails.h b/src/thumbnails.h index c668e460..7866da6f 100644 --- a/src/thumbnails.h +++ b/src/thumbnails.h @@ -20,7 +20,9 @@ class Thumbnails : public QObject { Q_OBJECT public: - explicit Thumbnails(const QString &cacheDirectory = {}, const QStringList &thumbnailerDirectories = {}); + // `patience` is how long a thumbnailer may take, in milliseconds; tests shorten it. + explicit Thumbnails(const QString &cacheDirectory = {}, const QStringList &thumbnailerDirectories = {}, + int patience = 15000); // 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. @@ -44,7 +46,7 @@ class Thumbnails : public QObject { QImage generate(const QString &path, const QString &mime, int pixels, bool &attempted) const; // Both answer whether the program ran at all: one that could not start has not judged the file. bool runThumbnailer(const QString &command, const QString &path, int pixels, QImage &image) const; - static bool pdfPage(const QString &path, int pixels, QImage &image); + static bool pdfPage(const QString &path, int pixels, QImage &image, int patience); void remember(QImage image, const QString &path, int size, const QDateTime &modified) const; // A failure goes with the type's thumbnailer as it was then, none included: one installed since gets its turn. void rememberFailure(const QString &path, const QDateTime &modified, const QString &thumbnailer) const; @@ -53,6 +55,7 @@ class Thumbnails : public QObject { static bool madeFor(const QImage &image, const QString &path, const QDateTime &modified); QString m_cache; + int m_patience; QHash m_thumbnailers; }; diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index bbd55cfa..6ff66304 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -5368,6 +5368,61 @@ private slots: QCOMPARE(QFileInfo(runs).size(), 4); } + void thumbnailsStopAThumbnailerWithWhatItStarted() + { + QTemporaryDir dir; + // The thumbnailer starts a helper of its own, notes the helper's number beside itself, and never ends. + const QString script = dir.filePath("hang.sh"), clip = dir.filePath("clip.mp4"); + write(script, "sleep 300 &\necho $! > \"$0.helper\"\nwait\n"); + write(dir.filePath("demo.thumbnailer"), + "[Thumbnailer Entry]\nExec=/bin/sh \"" + script.toUtf8() + "\" %i %o\nMimeType=video/mp4;\n"); + write(clip, "not a video"); + const Thumbnails thumbnails(dir.filePath("cache"), {dir.path()}, 1000); + QVERIFY(thumbnails.load(clip, "video/mp4", 128).isNull()); + + // Past its time the helper is stopped as well. + QFile noted(script + ".helper"); + QVERIFY(noted.open(QIODevice::ReadOnly)); + const QString helper = QString::fromLatin1(noted.readAll()).trimmed(); + QVERIFY(helper.toInt() > 0); + QTRY_VERIFY(!running(helper)); + // A file its thumbnailer hangs on is held back like any other it fails on. + QVERIFY(QFile::exists(dir.filePath("cache/" + Thumbnails::failures + "/" + Thumbnails::cacheKey(clip)))); + } + + void thumbnailsStopAThumbnailerWhenFolioGoes() + { + // Started again by itself with a folder to work in, this test only loads the clip, so the first run can end it mid-load. + const QString given = qEnvironmentVariable("FOLIO_TEST_LOADS_IN"); + QTemporaryDir made; + const QDir dir(given.isEmpty() ? made.path() : given); + const QString script = dir.filePath("hang.sh"), clip = dir.filePath("clip.mp4"); + if (!given.isEmpty()) { + Thumbnails(dir.filePath("cache"), {dir.path()}).load(clip, "video/mp4", 128); + return; + } + // The thumbnailer starts a helper of its own, notes the helper's number and its own beside itself, and never ends. + write(script, "sleep 300 &\necho $! > \"$0.helper\"\necho $$ > \"$0.number\"\nwait\n"); + write(dir.filePath("demo.thumbnailer"), + "[Thumbnailer Entry]\nExec=/bin/sh \"" + script.toUtf8() + "\" %i %o\nMimeType=video/mp4;\n"); + write(clip, "not a video"); + QProcessEnvironment environment = QProcessEnvironment::systemEnvironment(); + environment.insert("FOLIO_TEST_LOADS_IN", dir.path()); + QProcess loading; + loading.setProcessEnvironment(environment); + loading.start(QCoreApplication::applicationFilePath(), {"thumbnailsStopAThumbnailerWhenFolioGoes"}); + QTRY_VERIFY(QFileInfo(script + ".number").size() > 1); + const auto number = [&](const QString &ending) { + QFile noted(script + ending); + return noted.open(QIODevice::ReadOnly) ? QString::fromLatin1(noted.readAll()).trimmed() : QString(); + }; + const QString thumbnailer = number(".number"), helper = number(".helper"); + QVERIFY(thumbnailer.toInt() > 0 && helper.toInt() > 0); + loading.kill(); + QVERIFY(loading.waitForFinished()); + QTRY_VERIFY(!running(thumbnailer) && !running(helper)); + } + void thumbnailsTryAgainWhatAnOlderVersionCouldnt() { QTemporaryDir dir; @@ -6285,6 +6340,13 @@ private slots: return thumbnails.load(dir.filePath("photo.dng"), "image/x-adobe-dng", 1024).size(); } + // Whether the process of that number still runs; one ended but not collected yet keeps its line in /proc, as a zombie. + static bool running(const QString &number) + { + QFile stat("/proc/" + number + "/stat"); + return stat.open(QIODevice::ReadOnly) && !stat.readAll().contains(") Z "); + } + static void write(const QString &path, const QByteArray &contents) { QFile file(path);