// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL #include "OutgoingFile.h" #include #include #include #include #include #include #include #include #include #include #include QString OutgoingFile::preparedPath(const QString &localFilePath) { return prepare(localFilePath).path; } OutgoingFile::Prepared OutgoingFile::prepare(const QString &localFilePath) { Prepared out; out.path = localFilePath; out.name = QFileInfo(localFilePath).fileName(); const QFileInfo file(localFilePath); if (!file.isFile() || !file.isReadable()) return out; out.before = file.size(); out.after = file.size(); // Only pictures, and only ones worth the trouble. A recording goes as it // was: this process has no encoder, and there is no version of "smaller" // that is worth handing over to somebody else's player. const QMimeType mime = QMimeDatabase().mimeTypeForFile(file); if (!mime.name().startsWith(QLatin1String("image/")) || out.before < kMinBytesToShrink) return out; // Decoded straight to the size it is going to be sent at, rather than decoded // whole and then made smaller: a twelve-megapixel photograph is 48 megabytes // of pixels, and the whole point of the exercise is to never hold that. // setScaledSize() asks the decoder for the smaller of the two directly. QImageReader reader(localFilePath); const QSize full = reader.size(); if (!full.isValid() || full.isEmpty()) return out; // a header that will not say how big the picture is is not one to re-encode if (std::max(full.width(), full.height()) > kMaxImageEdge) { const QSize target = full.scaled(kMaxImageEdge, kMaxImageEdge, Qt::KeepAspectRatio); reader.setScaledSize(target.isValid() && !target.isEmpty() ? target : QSize(kMaxImageEdge, kMaxImageEdge)); } const QImage scaled = reader.read(); if (scaled.isNull()) return out; // it could not be decoded, so the original is the only copy there is // Encoded once into memory rather than into a file that might turn out to be // larger than what it replaced: a file is only written once there is a // smaller one to write. QByteArray webp; QBuffer webpBuffer(&webp); webpBuffer.open(QIODevice::WriteOnly); if (!scaled.save(&webpBuffer, "WEBP", kQuality)) { webp.clear(); } // The name the receiver sees. WebP is what is sent when it could be encoded, // and a jpeg only where it could not - a picture announced as a webp and // delivered as a jpeg is a picture some client opens by its name. const bool useWebp = !webp.isEmpty(); if (!useWebp) { QBuffer jpegBuffer(&webp); jpegBuffer.open(QIODevice::WriteOnly); if (!scaled.save(&jpegBuffer, "JPEG", kQuality)) return out; } if (webp.isEmpty() || webp.size() >= out.before) return out; // larger, or nothing: the original is the better file const QString extension = useWebp ? QStringLiteral("webp") : QStringLiteral("jpg"); // The name the receiver sees, which is the name the person gave the file and // not a hash: it is a caption on a picture, and a chat full of photographs // called 4f2a1c9e.webp is a chat nobody can read. Letters in any script // survive, spaces and all - only the characters that would break a path, a // url or a line of text are replaced, and there is a name to fall back on if // what is left of it is nothing. QString baseName = file.completeBaseName().trimmed(); baseName.replace(QRegularExpression(QStringLiteral("[/\\\\:*?\"<>|\\x00-\\x1f]")), QStringLiteral("_")); if (baseName.size() > 60) baseName.truncate(60); if (baseName.isEmpty()) baseName = QStringLiteral("image"); out.name = baseName + QLatin1Char('.') + extension; // Where the bytes go. Content-addressed, so attaching the same photograph // into three rooms writes it once - and a row of SHA prefixes in a cache // directory is right, because nothing is ever shown from there. const QString digest = QString::fromLatin1(QCryptographicHash::hash(webp, QCryptographicHash::Sha1).toHex().left(16)); const QString dir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QStringLiteral("/outgoing"); if (!QDir().mkpath(dir)) return out; const QString path = dir + QLatin1Char('/') + digest + QLatin1Char('.') + extension; // Already written by an earlier send of the same picture: the bytes are // addressed by what they are, so the file on disk is the file to send. if (!QFileInfo::exists(path)) { QSaveFile file2(path); if (!file2.open(QIODevice::WriteOnly) || file2.write(webp) < 0 || !file2.commit()) return out; } out.path = path; out.after = webp.size(); out.compressed = true; return out; }