2018-12-26 17:05:06 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2019-11-13 08:31:12 +00:00
|
|
|
#include "mtproto/mtp_instance.h"
|
2018-12-26 17:05:06 +00:00
|
|
|
|
2020-06-10 13:24:41 +00:00
|
|
|
namespace Main {
|
|
|
|
class Session;
|
|
|
|
} // namespace Main
|
|
|
|
|
2019-11-13 08:31:12 +00:00
|
|
|
namespace MTP {
|
2018-12-26 17:05:06 +00:00
|
|
|
|
2021-07-15 11:12:38 +00:00
|
|
|
class WeakInstance final : private QObject {
|
2018-12-26 17:05:06 +00:00
|
|
|
public:
|
2020-06-10 13:24:41 +00:00
|
|
|
explicit WeakInstance(base::weak_ptr<Main::Session> session);
|
2018-12-26 17:05:06 +00:00
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
template <typename Request>
|
2018-12-26 17:05:06 +00:00
|
|
|
void send(
|
2021-03-12 10:45:13 +00:00
|
|
|
const Request &request,
|
|
|
|
Fn<void(const typename Request::ResponseType &result)> done,
|
2021-03-12 12:48:00 +00:00
|
|
|
Fn<void(const Error &error)> fail,
|
2018-12-26 17:05:06 +00:00
|
|
|
ShiftedDcId dcId = 0);
|
|
|
|
|
2020-06-10 13:24:41 +00:00
|
|
|
[[nodiscard]] base::weak_ptr<Main::Session> session() const;
|
|
|
|
[[nodiscard]] bool valid() const;
|
|
|
|
[[nodiscard]] Instance *instance() const;
|
2018-12-26 17:05:06 +00:00
|
|
|
|
|
|
|
~WeakInstance();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void die();
|
|
|
|
bool removeRequest(mtpRequestId requestId);
|
2021-03-12 12:48:00 +00:00
|
|
|
void reportUnavailable(Fn<void(const Error &error)> callback);
|
2018-12-26 17:05:06 +00:00
|
|
|
|
2020-06-10 13:24:41 +00:00
|
|
|
base::weak_ptr<Main::Session> _session;
|
|
|
|
Instance *_instance = nullptr;
|
2021-03-12 12:48:00 +00:00
|
|
|
std::map<mtpRequestId, Fn<void(const Error &)>> _requests;
|
2019-06-06 09:37:12 +00:00
|
|
|
rpl::lifetime _lifetime;
|
2018-12-26 17:05:06 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class AbstractDedicatedLoader : public base::has_weak_ptr {
|
|
|
|
public:
|
|
|
|
AbstractDedicatedLoader(const QString &filepath, int chunkSize);
|
|
|
|
|
|
|
|
static constexpr auto kChunkSize = 128 * 1024;
|
|
|
|
static constexpr auto kMaxFileSize = 256 * 1024 * 1024;
|
|
|
|
|
|
|
|
struct Progress {
|
|
|
|
int64 already;
|
|
|
|
int64 size;
|
|
|
|
|
|
|
|
inline bool operator<(const Progress &other) const {
|
|
|
|
return (already < other.already)
|
|
|
|
|| (already == other.already && size < other.size);
|
|
|
|
}
|
|
|
|
inline bool operator==(const Progress &other) const {
|
|
|
|
return (already == other.already) && (size == other.size);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void start();
|
|
|
|
void wipeFolder();
|
|
|
|
void wipeOutput();
|
|
|
|
|
|
|
|
int alreadySize() const;
|
|
|
|
int totalSize() const;
|
|
|
|
|
|
|
|
rpl::producer<Progress> progress() const;
|
|
|
|
rpl::producer<QString> ready() const;
|
|
|
|
rpl::producer<> failed() const;
|
|
|
|
|
|
|
|
rpl::lifetime &lifetime();
|
|
|
|
|
|
|
|
virtual ~AbstractDedicatedLoader() = default;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void threadSafeFailed();
|
|
|
|
|
|
|
|
// Single threaded.
|
|
|
|
void writeChunk(bytes::const_span data, int totalSize);
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual void startLoading() = 0;
|
|
|
|
|
|
|
|
bool validateOutput();
|
|
|
|
void threadSafeProgress(Progress progress);
|
|
|
|
void threadSafeReady();
|
|
|
|
|
|
|
|
QString _filepath;
|
|
|
|
int _chunkSize = 0;
|
|
|
|
|
|
|
|
QFile _output;
|
|
|
|
int _alreadySize = 0;
|
|
|
|
int _totalSize = 0;
|
|
|
|
mutable QMutex _sizesMutex;
|
|
|
|
rpl::event_stream<Progress> _progress;
|
|
|
|
rpl::event_stream<QString> _ready;
|
|
|
|
rpl::event_stream<> _failed;
|
|
|
|
|
|
|
|
rpl::lifetime _lifetime;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class DedicatedLoader : public AbstractDedicatedLoader {
|
|
|
|
public:
|
|
|
|
struct Location {
|
|
|
|
QString username;
|
|
|
|
int32 postId = 0;
|
|
|
|
};
|
|
|
|
struct File {
|
|
|
|
QString name;
|
|
|
|
int32 size = 0;
|
|
|
|
DcId dcId = 0;
|
|
|
|
MTPInputFileLocation location;
|
|
|
|
};
|
|
|
|
|
|
|
|
DedicatedLoader(
|
2020-06-10 13:24:41 +00:00
|
|
|
base::weak_ptr<Main::Session> session,
|
2018-12-26 17:05:06 +00:00
|
|
|
const QString &folder,
|
|
|
|
const File &file);
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Request {
|
|
|
|
int offset = 0;
|
|
|
|
QByteArray bytes;
|
|
|
|
};
|
|
|
|
void startLoading() override;
|
|
|
|
void sendRequest();
|
|
|
|
void gotPart(int offset, const MTPupload_File &result);
|
2021-03-12 12:48:00 +00:00
|
|
|
Fn<void(const Error &)> failHandler();
|
2018-12-26 17:05:06 +00:00
|
|
|
|
|
|
|
static constexpr auto kRequestsCount = 2;
|
2019-02-19 06:57:53 +00:00
|
|
|
static constexpr auto kNextRequestDelay = crl::time(20);
|
2018-12-26 17:05:06 +00:00
|
|
|
|
|
|
|
std::deque<Request> _requests;
|
|
|
|
int32 _size = 0;
|
|
|
|
int _offset = 0;
|
|
|
|
DcId _dcId = 0;
|
|
|
|
MTPInputFileLocation _location;
|
|
|
|
WeakInstance _mtp;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
void ResolveChannel(
|
|
|
|
not_null<MTP::WeakInstance*> mtp,
|
|
|
|
const QString &username,
|
|
|
|
Fn<void(const MTPInputChannel &channel)> done,
|
|
|
|
Fn<void()> fail);
|
|
|
|
|
|
|
|
std::optional<MTPMessage> GetMessagesElement(
|
|
|
|
const MTPmessages_Messages &list);
|
|
|
|
|
|
|
|
void StartDedicatedLoader(
|
|
|
|
not_null<MTP::WeakInstance*> mtp,
|
|
|
|
const DedicatedLoader::Location &location,
|
|
|
|
const QString &folder,
|
|
|
|
Fn<void(std::unique_ptr<DedicatedLoader>)> ready);
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
template <typename Request>
|
2018-12-26 17:05:06 +00:00
|
|
|
void WeakInstance::send(
|
2021-03-12 10:45:13 +00:00
|
|
|
const Request &request,
|
|
|
|
Fn<void(const typename Request::ResponseType &result)> done,
|
2021-03-12 12:48:00 +00:00
|
|
|
Fn<void(const Error &error)> fail,
|
2018-12-26 17:05:06 +00:00
|
|
|
MTP::ShiftedDcId dcId) {
|
2021-03-12 10:45:13 +00:00
|
|
|
using Result = typename Request::ResponseType;
|
2018-12-26 17:05:06 +00:00
|
|
|
if (!valid()) {
|
|
|
|
reportUnavailable(fail);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto onDone = crl::guard((QObject*)this, [=](
|
2021-03-12 10:45:13 +00:00
|
|
|
const Response &response) {
|
|
|
|
auto result = Result();
|
|
|
|
auto from = response.reply.constData();
|
|
|
|
if (!result.read(from, from + response.reply.size())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (removeRequest(response.requestId)) {
|
2018-12-26 17:05:06 +00:00
|
|
|
done(result);
|
|
|
|
}
|
2021-03-12 10:45:13 +00:00
|
|
|
return true;
|
2018-12-26 17:05:06 +00:00
|
|
|
});
|
|
|
|
const auto onFail = crl::guard((QObject*)this, [=](
|
2021-03-12 12:48:00 +00:00
|
|
|
const Error &error,
|
2021-03-12 10:45:13 +00:00
|
|
|
const Response &response) {
|
2021-03-12 12:48:00 +00:00
|
|
|
if (MTP::IsDefaultHandledError(error)) {
|
2018-12-26 17:05:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-12 10:45:13 +00:00
|
|
|
if (removeRequest(response.requestId)) {
|
2018-12-26 17:05:06 +00:00
|
|
|
fail(error);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
const auto requestId = _instance->send(
|
|
|
|
request,
|
2021-03-12 10:45:13 +00:00
|
|
|
std::move(onDone),
|
|
|
|
std::move(onFail),
|
2018-12-26 17:05:06 +00:00
|
|
|
dcId);
|
|
|
|
_requests.emplace(requestId, fail);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace MTP
|