// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL #pragma once #include #include // What is actually put on the wire when a file is attached. // // A camera produces twelve megapixels and a phone records video at four // gigabytes an hour, and both go into a message unexamined, which means every // photograph in a conversation costs every reader the whole original. A // messenger that does not shrink a file is not really sending the picture, it // is sending the negative. // // So a picture is scaled to a size a screen can show and re-encoded, a recording // is left alone (there is no re-encoder in this process, and a file that cannot // be shrunk is better than one shrunk badly), and anything under the threshold // goes as it is - re-encoding a screenshot to save a kilobyte makes it worse. // // The original is never touched: what comes back is a file in the cache // directory, and it is the caller's to keep or drop. Nothing here is asked // about a file that cannot be read, and the answer is the file itself, so a // failure to compress is a file sent unshrunken rather than a file not sent. class OutgoingFile { public: // The same arguments a backend's sendFile() takes, and the file to hand it. // Empty when there is nothing to compress or nothing could be read, in which // case the caller sends what it was given. static QString preparedPath(const QString &localFilePath); // What the file came to, and what it was: for a line under the attachment // saying it went from four megabytes to three hundred, and for a sticker or // a voice clip the name it goes out with. struct Prepared { QString path; QString name; qint64 before = 0; qint64 after = 0; bool compressed = false; }; static Prepared prepare(const QString &localFilePath); // A picture is scaled down to this on its long side. Big enough for a // retina screen at arm's length and for a zoom to be worth doing, small // enough that the whole conversation of photographs fits on a phone. static constexpr int kMaxImageEdge = 2560; // Below this a file is sent as it is: re-encoding it costs bytes and // quality to save a saving nobody notices. static constexpr qint64 kMinBytesToShrink = 96 * 1024; // WebP at this quality is indistinguishable from the original to look at and // a fraction of its size, and every client in use decodes it. It is not the // highest setting that still looks like a photograph: a camera's own jpeg is // written near its maximum, and anything sent to other people should be // well under the size the file they picked was. JPEG is the fallback for a // build with no WebP writer, and is a little larger. static constexpr int kQuality = 80; };