2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "base/variant.h"
|
2019-08-12 12:11:34 +00:00
|
|
|
#include "api/api_common.h"
|
2022-11-10 12:18:42 +00:00
|
|
|
|
|
|
|
namespace Ui {
|
|
|
|
struct PreparedFileInformation;
|
|
|
|
} // namespace Ui
|
2017-03-10 14:14:10 +00:00
|
|
|
|
2021-08-31 11:50:49 +00:00
|
|
|
namespace Main {
|
|
|
|
class Session;
|
|
|
|
} // namespace Main
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
// Load files up to 2'000 MB.
|
|
|
|
constexpr auto kFileSizeLimit = 2'000 * int64(1024 * 1024);
|
|
|
|
|
|
|
|
// Load files up to 4'000 MB.
|
|
|
|
constexpr auto kFileSizePremiumLimit = 4'000 * int64(1024 * 1024);
|
2020-07-06 08:18:11 +00:00
|
|
|
|
2016-11-28 15:45:07 +00:00
|
|
|
enum class SendMediaType {
|
|
|
|
Photo,
|
|
|
|
Audio,
|
|
|
|
File,
|
2019-09-03 08:25:19 +00:00
|
|
|
ThemeFile,
|
2018-03-25 11:37:57 +00:00
|
|
|
Secure,
|
2016-11-28 15:45:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SendMediaPrepare {
|
2021-01-23 22:34:40 +00:00
|
|
|
SendMediaPrepare(
|
|
|
|
const QString &file,
|
|
|
|
const PeerId &peer,
|
|
|
|
SendMediaType type,
|
2021-09-15 10:21:45 +00:00
|
|
|
MsgId replyTo);
|
2021-01-23 22:34:40 +00:00
|
|
|
SendMediaPrepare(
|
|
|
|
const QImage &img,
|
|
|
|
const PeerId &peer,
|
|
|
|
SendMediaType type,
|
2021-09-15 10:21:45 +00:00
|
|
|
MsgId replyTo);
|
2021-01-23 22:34:40 +00:00
|
|
|
SendMediaPrepare(
|
|
|
|
const QByteArray &data,
|
|
|
|
const PeerId &peer,
|
|
|
|
SendMediaType type,
|
2021-09-15 10:21:45 +00:00
|
|
|
MsgId replyTo);
|
2021-01-23 22:34:40 +00:00
|
|
|
SendMediaPrepare(
|
|
|
|
const QByteArray &data,
|
|
|
|
int duration,
|
|
|
|
const PeerId &peer,
|
|
|
|
SendMediaType type,
|
2021-09-15 10:21:45 +00:00
|
|
|
MsgId replyTo);
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
PhotoId id;
|
|
|
|
QString file;
|
|
|
|
QImage img;
|
|
|
|
QByteArray data;
|
|
|
|
PeerId peer;
|
2016-11-28 15:45:07 +00:00
|
|
|
SendMediaType type;
|
|
|
|
int duration = 0;
|
2015-03-19 09:18:19 +00:00
|
|
|
MsgId replyTo;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-28 15:45:07 +00:00
|
|
|
};
|
|
|
|
using SendMediaPrepareList = QList<SendMediaPrepare>;
|
|
|
|
|
|
|
|
using UploadFileParts = QMap<int, QByteArray>;
|
|
|
|
struct SendMediaReady {
|
|
|
|
SendMediaReady() = default; // temp
|
2019-01-18 11:26:43 +00:00
|
|
|
SendMediaReady(
|
|
|
|
SendMediaType type,
|
|
|
|
const QString &file,
|
|
|
|
const QString &filename,
|
2022-05-10 14:22:28 +00:00
|
|
|
int64 filesize,
|
2019-01-18 11:26:43 +00:00
|
|
|
const QByteArray &data,
|
|
|
|
const uint64 &id,
|
|
|
|
const uint64 &thumbId,
|
|
|
|
const QString &thumbExt,
|
|
|
|
const PeerId &peer,
|
|
|
|
const MTPPhoto &photo,
|
|
|
|
const PreparedPhotoThumbs &photoThumbs,
|
|
|
|
const MTPDocument &document,
|
|
|
|
const QByteArray &jpeg,
|
|
|
|
MsgId replyTo);
|
|
|
|
|
2015-03-19 09:18:19 +00:00
|
|
|
MsgId replyTo;
|
2016-11-28 15:45:07 +00:00
|
|
|
SendMediaType type;
|
2014-05-30 08:53:19 +00:00
|
|
|
QString file, filename;
|
2022-05-10 14:22:28 +00:00
|
|
|
int64 filesize = 0;
|
2014-05-30 08:53:19 +00:00
|
|
|
QByteArray data;
|
2015-01-05 20:17:33 +00:00
|
|
|
QString thumbExt;
|
|
|
|
uint64 id, thumbId; // id always file-id of media, thumbId is file-id of thumb ( == id for photos)
|
2014-05-30 08:53:19 +00:00
|
|
|
PeerId peer;
|
|
|
|
|
|
|
|
MTPPhoto photo;
|
|
|
|
MTPDocument document;
|
|
|
|
PreparedPhotoThumbs photoThumbs;
|
2015-10-27 02:39:02 +00:00
|
|
|
UploadFileParts parts;
|
2014-05-30 08:53:19 +00:00
|
|
|
QByteArray jpeg_md5;
|
2014-10-17 19:14:42 +00:00
|
|
|
|
2015-10-11 08:37:24 +00:00
|
|
|
QString caption;
|
2015-10-27 02:39:02 +00:00
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
};
|
2015-09-29 18:44:31 +00:00
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
using TaskId = void*; // no interface, just id
|
|
|
|
|
2015-09-29 18:44:31 +00:00
|
|
|
class Task {
|
|
|
|
public:
|
|
|
|
virtual void process() = 0; // is executed in a separate thread
|
|
|
|
virtual void finish() = 0; // is executed in the same as TaskQueue thread
|
2016-11-28 15:45:07 +00:00
|
|
|
virtual ~Task() = default;
|
2015-09-29 18:59:26 +00:00
|
|
|
|
2015-09-29 18:44:31 +00:00
|
|
|
TaskId id() const {
|
2016-11-28 15:45:07 +00:00
|
|
|
return static_cast<TaskId>(const_cast<Task*>(this));
|
2015-09-29 18:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-09-29 18:59:26 +00:00
|
|
|
class TaskQueueWorker;
|
2015-09-29 18:44:31 +00:00
|
|
|
class TaskQueue : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2019-02-19 06:57:53 +00:00
|
|
|
explicit TaskQueue(crl::time stopTimeoutMs = 0); // <= 0 - never stop worker
|
2015-09-29 18:44:31 +00:00
|
|
|
|
2017-12-18 10:38:14 +00:00
|
|
|
TaskId addTask(std::unique_ptr<Task> &&task);
|
|
|
|
void addTasks(std::vector<std::unique_ptr<Task>> &&tasks);
|
2015-09-29 18:44:31 +00:00
|
|
|
void cancelTask(TaskId id); // this task finish() won't be called
|
2016-02-12 16:35:06 +00:00
|
|
|
|
2015-09-29 18:44:31 +00:00
|
|
|
~TaskQueue();
|
|
|
|
|
2021-03-03 18:22:42 +00:00
|
|
|
Q_SIGNALS:
|
2015-09-29 18:44:31 +00:00
|
|
|
void taskAdded();
|
|
|
|
|
2021-03-03 18:22:42 +00:00
|
|
|
public Q_SLOTS:
|
2015-09-29 18:44:31 +00:00
|
|
|
void onTaskProcessed();
|
|
|
|
void stop();
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class TaskQueueWorker;
|
|
|
|
|
2015-10-28 02:41:13 +00:00
|
|
|
void wakeThread();
|
|
|
|
|
2017-12-18 10:38:14 +00:00
|
|
|
std::deque<std::unique_ptr<Task>> _tasksToProcess;
|
|
|
|
std::deque<std::unique_ptr<Task>> _tasksToFinish;
|
|
|
|
TaskId _taskInProcessId = TaskId();
|
2015-09-29 18:44:31 +00:00
|
|
|
QMutex _tasksToProcessMutex, _tasksToFinishMutex;
|
2017-12-19 16:57:42 +00:00
|
|
|
QThread *_thread = nullptr;
|
|
|
|
TaskQueueWorker *_worker = nullptr;
|
|
|
|
QTimer *_stopTimer = nullptr;
|
2015-09-29 18:44:31 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class TaskQueueWorker : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2016-11-28 15:45:07 +00:00
|
|
|
TaskQueueWorker(TaskQueue *queue) : _queue(queue) {
|
2015-09-29 18:44:31 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 18:22:42 +00:00
|
|
|
Q_SIGNALS:
|
2015-09-29 18:44:31 +00:00
|
|
|
void taskProcessed();
|
|
|
|
|
2021-03-03 18:22:42 +00:00
|
|
|
public Q_SLOTS:
|
2015-09-29 18:44:31 +00:00
|
|
|
void onTaskAdded();
|
|
|
|
|
|
|
|
private:
|
|
|
|
TaskQueue *_queue;
|
2016-11-28 15:45:07 +00:00
|
|
|
bool _inTaskAdded = false;
|
2015-09-29 18:44:31 +00:00
|
|
|
|
|
|
|
};
|
2015-10-27 02:39:02 +00:00
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
struct SendingAlbum {
|
|
|
|
struct Item {
|
2021-11-17 16:00:00 +00:00
|
|
|
explicit Item(TaskId taskId);
|
2018-12-26 07:22:36 +00:00
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
TaskId taskId;
|
2019-09-09 08:13:41 +00:00
|
|
|
uint64 randomId = 0;
|
2017-12-19 16:57:42 +00:00
|
|
|
FullMsgId msgId;
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<MTPInputSingleMedia> media;
|
2017-12-19 16:57:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SendingAlbum();
|
|
|
|
|
2018-12-26 07:22:36 +00:00
|
|
|
void fillMedia(
|
|
|
|
not_null<HistoryItem*> item,
|
|
|
|
const MTPInputMedia &media,
|
|
|
|
uint64 randomId);
|
|
|
|
void refreshMediaCaption(not_null<HistoryItem*> item);
|
|
|
|
void removeItem(not_null<HistoryItem*> item);
|
|
|
|
|
2017-12-25 14:17:00 +00:00
|
|
|
uint64 groupId = 0;
|
2017-12-19 16:57:42 +00:00
|
|
|
std::vector<Item> items;
|
2019-08-12 12:11:34 +00:00
|
|
|
Api::SendOptions options;
|
2017-12-19 16:57:42 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-10-27 02:39:02 +00:00
|
|
|
struct FileLoadTo {
|
2021-03-09 09:11:40 +00:00
|
|
|
FileLoadTo(
|
2021-11-09 15:24:13 +00:00
|
|
|
PeerId peer,
|
2021-03-09 09:11:40 +00:00
|
|
|
Api::SendOptions options,
|
|
|
|
MsgId replyTo,
|
2022-10-17 16:29:48 +00:00
|
|
|
MsgId topicRootId,
|
2021-03-09 09:11:40 +00:00
|
|
|
MsgId replaceMediaOf)
|
2019-08-12 12:11:34 +00:00
|
|
|
: peer(peer)
|
|
|
|
, options(options)
|
2021-03-09 09:11:40 +00:00
|
|
|
, replyTo(replyTo)
|
2022-10-17 16:29:48 +00:00
|
|
|
, topicRootId(topicRootId)
|
2021-03-09 09:11:40 +00:00
|
|
|
, replaceMediaOf(replaceMediaOf) {
|
2015-10-27 02:39:02 +00:00
|
|
|
}
|
|
|
|
PeerId peer;
|
2019-08-12 12:11:34 +00:00
|
|
|
Api::SendOptions options;
|
2015-10-27 02:39:02 +00:00
|
|
|
MsgId replyTo;
|
2022-10-17 16:29:48 +00:00
|
|
|
MsgId topicRootId;
|
2021-03-09 09:11:40 +00:00
|
|
|
MsgId replaceMediaOf;
|
2015-10-27 02:39:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct FileLoadResult {
|
2017-12-19 16:57:42 +00:00
|
|
|
FileLoadResult(
|
|
|
|
TaskId taskId,
|
|
|
|
uint64 id,
|
|
|
|
const FileLoadTo &to,
|
2018-05-24 13:03:21 +00:00
|
|
|
const TextWithTags &caption,
|
2017-12-19 16:57:42 +00:00
|
|
|
std::shared_ptr<SendingAlbum> album);
|
|
|
|
|
|
|
|
TaskId taskId;
|
2015-10-27 02:39:02 +00:00
|
|
|
uint64 id;
|
|
|
|
FileLoadTo to;
|
2017-12-19 16:57:42 +00:00
|
|
|
std::shared_ptr<SendingAlbum> album;
|
2017-09-17 11:53:38 +00:00
|
|
|
SendMediaType type = SendMediaType::File;
|
2015-10-27 02:39:02 +00:00
|
|
|
QString filepath;
|
|
|
|
QByteArray content;
|
|
|
|
|
|
|
|
QString filename;
|
2015-12-19 14:37:28 +00:00
|
|
|
QString filemime;
|
2022-05-10 14:22:28 +00:00
|
|
|
int64 filesize = 0;
|
2015-10-27 02:39:02 +00:00
|
|
|
UploadFileParts fileparts;
|
|
|
|
QByteArray filemd5;
|
2022-05-10 14:22:28 +00:00
|
|
|
int64 partssize = 0;
|
2015-10-27 02:39:02 +00:00
|
|
|
|
2016-11-28 15:45:07 +00:00
|
|
|
uint64 thumbId = 0; // id is always file-id of media, thumbId is file-id of thumb ( == id for photos)
|
2015-10-27 02:39:02 +00:00
|
|
|
QString thumbname;
|
|
|
|
UploadFileParts thumbparts;
|
2020-08-26 13:28:56 +00:00
|
|
|
QByteArray thumbbytes;
|
2015-10-27 02:39:02 +00:00
|
|
|
QByteArray thumbmd5;
|
2018-10-12 16:41:51 +00:00
|
|
|
QImage thumb;
|
2015-10-27 02:39:02 +00:00
|
|
|
|
2018-10-25 08:47:06 +00:00
|
|
|
QImage goodThumbnail;
|
|
|
|
QByteArray goodThumbnailBytes;
|
|
|
|
|
2015-10-27 02:39:02 +00:00
|
|
|
MTPPhoto photo;
|
|
|
|
MTPDocument document;
|
|
|
|
|
|
|
|
PreparedPhotoThumbs photoThumbs;
|
2018-05-24 13:03:21 +00:00
|
|
|
TextWithTags caption;
|
2015-10-27 02:39:02 +00:00
|
|
|
|
2021-03-14 09:46:17 +00:00
|
|
|
std::vector<MTPInputDocument> attachedStickers;
|
|
|
|
|
2019-01-18 11:26:43 +00:00
|
|
|
void setFileData(const QByteArray &filedata);
|
|
|
|
void setThumbData(const QByteArray &thumbdata);
|
|
|
|
|
2015-10-27 02:39:02 +00:00
|
|
|
};
|
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
class FileLoadTask final : public Task {
|
|
|
|
public:
|
2020-10-13 12:07:53 +00:00
|
|
|
static std::unique_ptr<Ui::PreparedFileInformation> ReadMediaInformation(
|
2017-12-19 16:57:42 +00:00
|
|
|
const QString &filepath,
|
|
|
|
const QByteArray &content,
|
|
|
|
const QString &filemime);
|
2017-12-21 16:15:37 +00:00
|
|
|
static bool FillImageInformation(
|
|
|
|
QImage &&image,
|
|
|
|
bool animated,
|
2022-03-09 13:37:48 +00:00
|
|
|
std::unique_ptr<Ui::PreparedFileInformation> &result,
|
|
|
|
QByteArray content = {},
|
|
|
|
QByteArray format = {});
|
2017-12-19 16:57:42 +00:00
|
|
|
|
|
|
|
FileLoadTask(
|
2020-06-12 14:09:04 +00:00
|
|
|
not_null<Main::Session*> session,
|
2017-12-19 16:57:42 +00:00
|
|
|
const QString &filepath,
|
|
|
|
const QByteArray &content,
|
2020-10-13 12:07:53 +00:00
|
|
|
std::unique_ptr<Ui::PreparedFileInformation> information,
|
2017-12-19 16:57:42 +00:00
|
|
|
SendMediaType type,
|
|
|
|
const FileLoadTo &to,
|
2018-05-24 13:03:21 +00:00
|
|
|
const TextWithTags &caption,
|
2021-03-09 09:11:40 +00:00
|
|
|
std::shared_ptr<SendingAlbum> album = nullptr);
|
2017-12-19 16:57:42 +00:00
|
|
|
FileLoadTask(
|
2020-06-12 14:09:04 +00:00
|
|
|
not_null<Main::Session*> session,
|
2017-12-19 16:57:42 +00:00
|
|
|
const QByteArray &voice,
|
|
|
|
int32 duration,
|
|
|
|
const VoiceWaveform &waveform,
|
|
|
|
const FileLoadTo &to,
|
2018-05-24 13:03:21 +00:00
|
|
|
const TextWithTags &caption);
|
2020-10-13 12:07:53 +00:00
|
|
|
~FileLoadTask();
|
2015-10-27 02:39:02 +00:00
|
|
|
|
|
|
|
uint64 fileid() const {
|
|
|
|
return _id;
|
|
|
|
}
|
|
|
|
|
2020-10-14 15:12:24 +00:00
|
|
|
struct Args {
|
|
|
|
bool generateGoodThumbnail = true;
|
|
|
|
};
|
|
|
|
void process(Args &&args);
|
|
|
|
|
|
|
|
void process() override {
|
|
|
|
process({});
|
|
|
|
}
|
|
|
|
void finish() override;
|
2015-10-27 02:39:02 +00:00
|
|
|
|
2020-03-04 09:21:19 +00:00
|
|
|
FileLoadResult *peekResult() const;
|
|
|
|
|
2017-03-10 14:14:10 +00:00
|
|
|
private:
|
2017-12-21 16:15:37 +00:00
|
|
|
static bool CheckForSong(
|
|
|
|
const QString &filepath,
|
|
|
|
const QByteArray &content,
|
2020-10-13 12:07:53 +00:00
|
|
|
std::unique_ptr<Ui::PreparedFileInformation> &result);
|
2017-12-21 16:15:37 +00:00
|
|
|
static bool CheckForVideo(
|
|
|
|
const QString &filepath,
|
|
|
|
const QByteArray &content,
|
2020-10-13 12:07:53 +00:00
|
|
|
std::unique_ptr<Ui::PreparedFileInformation> &result);
|
2017-12-21 16:15:37 +00:00
|
|
|
static bool CheckForImage(
|
|
|
|
const QString &filepath,
|
|
|
|
const QByteArray &content,
|
2020-10-13 12:07:53 +00:00
|
|
|
std::unique_ptr<Ui::PreparedFileInformation> &result);
|
2017-03-10 14:14:10 +00:00
|
|
|
|
|
|
|
template <typename Mimes, typename Extensions>
|
|
|
|
static bool CheckMimeOrExtensions(const QString &filepath, const QString &filemime, Mimes &mimes, Extensions &extensions);
|
|
|
|
|
2020-10-13 12:07:53 +00:00
|
|
|
std::unique_ptr<Ui::PreparedFileInformation> readMediaInformation(const QString &filemime) const;
|
2017-12-19 16:57:42 +00:00
|
|
|
void removeFromAlbum();
|
2017-03-10 14:14:10 +00:00
|
|
|
|
2020-06-11 16:09:46 +00:00
|
|
|
uint64 _id = 0;
|
2020-06-12 14:09:04 +00:00
|
|
|
base::weak_ptr<Main::Session> _session;
|
2020-06-11 16:09:46 +00:00
|
|
|
MTP::DcId _dcId = 0;
|
2015-10-27 02:39:02 +00:00
|
|
|
FileLoadTo _to;
|
2017-12-19 16:57:42 +00:00
|
|
|
const std::shared_ptr<SendingAlbum> _album;
|
2015-10-27 02:39:02 +00:00
|
|
|
QString _filepath;
|
|
|
|
QByteArray _content;
|
2020-10-13 12:07:53 +00:00
|
|
|
std::unique_ptr<Ui::PreparedFileInformation> _information;
|
2016-10-12 19:34:25 +00:00
|
|
|
int32 _duration = 0;
|
2016-02-12 16:35:06 +00:00
|
|
|
VoiceWaveform _waveform;
|
2016-11-28 15:45:07 +00:00
|
|
|
SendMediaType _type;
|
2018-05-24 13:03:21 +00:00
|
|
|
TextWithTags _caption;
|
2015-10-27 02:39:02 +00:00
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
std::shared_ptr<FileLoadResult> _result;
|
2015-10-27 02:39:02 +00:00
|
|
|
|
|
|
|
};
|