diff --git a/src/FileRow.qml b/src/FileRow.qml index a8dab284..b674ac15 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.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..138b2f62 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(kind) { + return kind !== "folder" && 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/src/main.cpp b/src/main.cpp index 813d21c1..9121700d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -95,6 +95,7 @@ static int preview(QGuiApplication &app) { app.installTranslator(&translator); Theme theme; Thumbnails thumbnails; + QObject::connect(&app, &QCoreApplication::aboutToQuit, &thumbnails, &Thumbnails::quit); Previewer previewer; previewer.start(path, showHidden, sort); if (follow) @@ -131,6 +132,7 @@ int main(int argc, char *argv[]) { Theme theme; Places places; Thumbnails thumbnails; + QObject::connect(&app, &QCoreApplication::aboutToQuit, &thumbnails, &Thumbnails::quit); // Plugins' text follows the language too. Plugins plugins(translator.language()); QObject::connect(&translator, &Translator::changed, &plugins, [&] { plugins.setLanguage(translator.language()); }); diff --git a/src/thumbnails.cpp b/src/thumbnails.cpp index 97552113..3613a6d9 100644 --- a/src/thumbnails.cpp +++ b/src/thumbnails.cpp @@ -2,6 +2,7 @@ #include "rawembed.h" #include +#include #include #include #include @@ -23,6 +24,12 @@ #include #include #include +#include +#include +#include +#include +#include +#include namespace { @@ -70,16 +77,110 @@ 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; } +// How long a stopped helper has to be collected, in milliseconds. +constexpr int stoppedPatience = 1000; + +// How long a helper is waited for at a time, in milliseconds, before Folio checks whether it is quitting. +constexpr int waitSlice = 100; + +// How a helper ended. One that never started, or was stopped as Folio quit, has said nothing about the file it was given. +enum class Ran { Unjudged, Failed, Finished }; + +// 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, which its keeper's number names (none for a helper that never had one). The session +// outlives the keeper while anything is left in it, and the kernel hands a freed number out again only after going +// round all the others. +void stop(QProcess &process, qint64 session) { + if (session > 0) + ::kill(-pid_t(session), SIGKILL); + process.kill(); + process.waitForFinished(stoppedPatience); +} + +Ran run(QProcess &process, const QString &program, const QStringList &args, int patience, const std::atomic_bool &quitting) { + // A thumbnail asked for as Folio quits is never started. + if (quitting) + return Ran::Unjudged; + // One deadline for the start and the finish together. + const QDeadlineTimer deadline(patience); + process.setChildProcessModifier([folio = ::getpid()] { keepInASession(folio); }); + process.start(program, args); + // In slices, so quitting doesn't wait on a program slow to start (one on a stalled network mount, say). + while (process.state() == QProcess::Starting && !deadline.hasExpired() && !quitting) + process.waitForStarted(int(qMin(waitSlice, deadline.remainingTime()))); + if (process.state() != QProcess::Running) { + // One still on its way to its program after that long is stopped too, or its keeper would be killed alone. + stop(process, process.processId()); + return Ran::Unjudged; + } + // Read now: a keeper that has ended has no number left. + const qint64 session = process.processId(); + // A broken file must never hang a tile forever, nor Folio as it quits. + bool finished = false; + while (!finished && !deadline.hasExpired() && !quitting) + finished = process.waitForFinished(int(qMin(waitSlice, deadline.remainingTime()))); + // What a helper started in the background is stopped with it even when it finished. + stop(process, session); + if (!finished) + return quitting ? Ran::Unjudged : 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;". Copied from the // .thumbnailer files of Arch's ffmpegthumbnailer 2.3.1 (the video one only: the audio one draws just cover art, which // most files lack), glycin 2.2.6, papers 51.1 and libgsf's gsf-office; refresh them from there when a package adds types. @@ -149,10 +250,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 @@ -184,7 +286,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 .thumbnailer file 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(); @@ -263,8 +368,12 @@ 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, path, modified)) - return image; + const int pixels = bucketPixels(bucketFor(size)); + // One made for a larger size serves this one too, so a folder seen as tiles and then as rows is not drawn twice. + for (int larger = pixels; larger <= 1024; larger *= 2) { + if (const QImage image(cachePath(path, larger)); madeFor(image, path, modified)) + return larger == pixels ? image : image.scaled(pixels, pixels, Qt::KeepAspectRatio, Qt::SmoothTransformation); + } // 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. @@ -274,13 +383,16 @@ 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 {}; - const int pixels = bucketPixels(bucketFor(size)); + // A file that cannot be read is not a broken one: nothing is tried, so nothing is remembered. + if (!info.isReadable()) + return {}; 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) + // one installed later still gets its turn. A file moved away or + // written to meanwhile was never judged as it is now. + if (attempted && QFileInfo(path).lastModified() == modified) rememberFailure(path, modified, thumbnailer); return {}; } @@ -314,14 +426,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 {}; } @@ -373,31 +489,26 @@ 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) const { 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 = run(process, "pdftoppm", {"-f", "1", "-l", "1", "-singlefile", "-png", "-scale-to", QString::number(pixels), + path, scratch.filePath("page")}, m_patience, m_quitting); + if (ran == Ran::Finished) + image = QImage(scratch.filePath("page.png")); + return ran != Ran::Unjudged; } -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. @@ -406,16 +517,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 = run(process, program, args, m_patience, m_quitting); + if (ran == Ran::Finished) + image = QImage(output); + return ran != Ran::Unjudged; } 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..07435888 100644 --- a/src/thumbnails.h +++ b/src/thumbnails.h @@ -7,6 +7,7 @@ #include #include #include +#include // Thumbnails per the freedesktop thumbnail spec: one shared cache under // ~/.cache/thumbnails, keyed by the MD5 of the file's URI and invalidated by @@ -20,15 +21,21 @@ 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. QImage load(const QString &path, const QString &mime, int size) const; + // Stops the thumbnailers still running and starts no more, as Folio quits: it waits for every thumbnail being made, + // and a hung thumbnailer would keep it waiting. + void quit() { m_quitting = true; } 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"); + // The spec's fail/-: the number is raised when Folio learns to draw more, so old failures are tried + // again. 3: names with a thumbnailer's codes in them, and PDFs whose thumbnailer couldn't start, are drawn now. + static inline const QString failures = QStringLiteral("fail/folio-3"); 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. @@ -42,8 +49,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 judged the file: one that could not start, or was stopped as Folio quit, has not. + bool runThumbnailer(const QString &command, const QString &path, int pixels, QImage &image) const; + bool pdfPage(const QString &path, int pixels, QImage &image) const; 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; @@ -52,6 +60,8 @@ class Thumbnails : public QObject { static bool madeFor(const QImage &image, const QString &path, const QDateTime &modified); QString m_cache; + int m_patience; + std::atomic_bool m_quitting{false}; QHash m_thumbnailers; }; diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index e769a7f2..51803520 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 @@ -5441,6 +5471,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; @@ -5456,6 +5566,242 @@ 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 thumbnailsRememberOnlyAVerdictOnTheFile() + { + QTemporaryDir dir; + const auto marked = [&](const QString &path) { + return QFile::exists(dir.filePath("cache/" + Thumbnails::failures + "/" + Thumbnails::cacheKey(path))); + }; + const auto thumbnailer = [&](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)); + + thumbnailer("gone", "\"" + dir.filePath("no-such-program") + "\" %i %o", "video/mp4"); + thumbnailer("copier", "\"" + copier + "\" \"" + frame + "\" %o", "video/webm"); + thumbnailer("taker", script("take.sh", "rm \"$1\"\nexit 1\n"), "video/ogg"); + thumbnailer("writer", script("write.sh", "echo more >> \"$1\"\nexit 1\n"), "video/quicktime"); + thumbnailer("failer", script("fail.sh", "echo run >> \"$0.runs\"\nexit 1\n"), "video/mpeg"); + thumbnailer("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)); + thumbnailer("percent", "\"" + QString(percent).replace("%", "%%") + "\" \"" + frame + "\" %o", "video/x-matroska"); + const Thumbnails thumbnails(dir.filePath("cache"), {dir.path()}); + + // A .thumbnailer file 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 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(ended(helper.toInt())); + // 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(ended(thumbnailer.toInt()) && ended(helper.toInt())); + } + + void thumbnailsStopWhatAFinishedThumbnailerLeft() + { + QTemporaryDir dir; + // The thumbnailer starts a helper of its own, notes the helper's number beside itself, and ends at once. + const QString script = dir.filePath("leave.sh"), clip = dir.filePath("clip.mp4"); + write(script, "sleep 300 &\necho $! > \"$0.helper\"\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()}); + QVERIFY(thumbnails.load(clip, "video/mp4", 128).isNull()); + + QFile noted(script + ".helper"); + QVERIFY(noted.open(QIODevice::ReadOnly)); + const QString helper = QString::fromLatin1(noted.readAll()).trimmed(); + QVERIFY(helper.toInt() > 0); + QTRY_VERIFY(ended(helper.toInt())); + } + + void thumbnailsStopAThumbnailerWhenFolioQuits() + { + QTemporaryDir dir; + // The thumbnailer notes its number beside itself and never ends; Folio would wait a minute for it. + const QString script = dir.filePath("hang.sh"), clip = dir.filePath("clip.mp4"); + write(script, "echo $$ > \"$0.number\"\nexec sleep 300\n"); + write(dir.filePath("demo.thumbnailer"), + "[Thumbnailer Entry]\nExec=/bin/sh \"" + script.toUtf8() + "\" %i %o\nMimeType=video/mp4;\n"); + write(clip, "not a video"); + Thumbnails thumbnails(dir.filePath("cache"), {dir.path()}, 60000); + QFuture loaded = QtConcurrent::run([&] { return thumbnails.load(clip, "video/mp4", 128); }); + QTRY_VERIFY(QFileInfo(script + ".number").size() > 1); + + // Quitting stops it at once, and a file it was stopped on is not held back. + thumbnails.quit(); + QTRY_VERIFY_WITH_TIMEOUT(loaded.isFinished(), 5000); + QVERIFY(loaded.result().isNull()); + QFile noted(script + ".number"); + QVERIFY(noted.open(QIODevice::ReadOnly)); + QTRY_VERIFY(ended(QString::fromLatin1(noted.readAll()).trimmed().toInt())); + QVERIFY(!QFile::exists(dir.filePath("cache/" + Thumbnails::failures + "/" + Thumbnails::cacheKey(clip)))); + } + + void thumbnailsServeASmallerSizeFromALargerOne() + { + QTemporaryDir dir; + // The thumbnailer copies its input out and notes each run beside itself. + const QString script = dir.filePath("copy.sh"), clip = dir.filePath("clip.mp4"); + write(script, "cp \"$1\" \"$2\" && echo run >> \"$0.runs\"\n"); + write(dir.filePath("demo.thumbnailer"), + "[Thumbnailer Entry]\nExec=/bin/sh \"" + script.toUtf8() + "\" %i %o\nMimeType=video/mp4;\n"); + QVERIFY(QImage(512, 256, QImage::Format_RGB32).save(clip, "PNG")); + const Thumbnails thumbnails(dir.filePath("cache"), {dir.path()}); + + // A tile's thumbnail, scaled down, is a row's: the thumbnailer runs once, and each run notes four bytes. + QCOMPARE(thumbnails.load(clip, "video/mp4", 512).size(), QSize(512, 256)); + QCOMPARE(thumbnails.load(clip, "video/mp4", 128).size(), QSize(128, 64)); + QCOMPARE(QFileInfo(script + ".runs").size(), 4); + } + void thumbnailsTryAgainWhatAnOlderVersionCouldnt() { QTemporaryDir dir;