// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // KOutNet - Reassembles chunked file transfers received over the session transport #pragma once #include #include #include #include #include #include namespace koutnet { class FileTransferHandler : public QObject { Q_OBJECT public: // Built-in ceiling on one incoming transfer; larger meta is refused outright, // with no entry created so its chunks are dropped too. The default instance // uses it, and main.cpp replaces it from the maxTransferMb AppSettings entry. static constexpr qint64 kMaxTransferBytes = 200LL * 1024 * 1024; // 200 MB // Cap on concurrent in-flight transfers, so peers spamming file_meta without // ever sending chunks cannot grow m_pending unboundedly. static constexpr int kMaxPendingTransfers = 50; // the per-transfer cap is not enough: many incomplete transfers could otherwise // consume one cap each before their TTL expires. static constexpr qint64 kMaxPendingBytes = 256LL * 1024 * 1024; static constexpr qint64 kPendingTransferTtlMs = 10 * 60 * 1000; // 10 min // Filename length cap in UTF-8 bytes, the unit the filesystem counts in, with // room left for the "(2)" a name collision appends. static constexpr int kMaxFilenameBytes = 200; explicit FileTransferHandler(QObject *parent = nullptr); // what a single announced size may be, in bytes. Starts at kMaxTransferBytes; // main.cpp lowers or raises it from AppSettings. void setMaxTransferBytes(qint64 bytes); // A peer announced a transfer. Nothing is created yet and no chunk is kept: // the offer waits here until the user says yes, and host is the address the // frame arrived on, which is the only sender identity this class trusts. void onMetaOffered(const QString &host, const QJsonObject &meta); // Consent, by transfer id. QML calls these two once the incoming-file dialog // is answered; they take an id rather than a meta so the object that ends up // being written is the one the authenticated frame carried. Q_INVOKABLE void acceptOffer(const QString &tid); Q_INVOKABLE void declineOffer(const QString &tid); // Chunks are only ever taken from the host that offered the transfer. Any // other peer that knows the tid has its chunk dropped, so it cannot add // bytes to the file or walk an index twice to get the transfer killed. void onChunkMessage(const QString &host, const QString &tid, int idx, int total, const QByteArray &chunk); // how many offers are waiting for an answer. A refused transfer has to bring // this back down too. int offeredCount() const { return m_offers.size(); } // Turns a peer-supplied name into a bare filename that cannot leave the // destination folder. Public because it is the entire security boundary of this // class and deserves checking on its own. static QString sanitizeFilename(const QString &rawName); // what the peers are currently making us hold. A refused transfer has to bring // both back down. int pendingTransferCount() const { return m_pending.size(); } qint64 pendingBufferedBytes() const; Q_SIGNALS: void fileReceived(QJsonObject meta, QByteArray data); // Fired once a completed transfer is on disk. QML listens to this one - a // file:// path is far cheaper across the QML/C++ boundary than a byte blob. void fileSaved(QJsonObject meta, QString localPath); void transferRejected(QString tid, QString reason); private: struct PendingTransfer { QJsonObject meta; QMap chunks; // idx -> raw chunk bytes int total = -1; qint64 announcedBytes = -1; qint64 receivedBytes = 0; qint64 startedAtMs = 0; QString sender; // the host whose meta created this, and the only one whose chunks count }; // an announced transfer nobody has answered yet struct Offer { QJsonObject meta; QString host; qint64 offeredAtMs = 0; }; bool startTransfer(const QString &tid); QString saveToDisk(const QJsonObject &meta, const QByteArray &data) const; void pruneStaleTransfers(); QHash m_pending; // tid -> transfer state QHash m_offers; // tid -> announced, waiting for consent QTimer m_pruneTimer; qint64 m_maxTransferBytes = kMaxTransferBytes; }; } // namespace koutnet