tdesktop/Telegram/SourceFiles/storage/localimageloader.h

350 lines
7.4 KiB
C
Raw Normal View History

/*
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
2017-04-06 14:38:10 +00:00
#include "base/variant.h"
#include "api/api_common.h"
namespace Ui {
struct PreparedFileInformation;
} // namespace Ui
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
enum class SendMediaType {
Photo,
Audio,
File,
2019-09-03 08:25:19 +00:00
ThemeFile,
Secure,
};
struct SendMediaPrepare {
SendMediaPrepare(
const QString &file,
const PeerId &peer,
SendMediaType type,
MsgId replyTo);
SendMediaPrepare(
const QImage &img,
const PeerId &peer,
SendMediaType type,
MsgId replyTo);
SendMediaPrepare(
const QByteArray &data,
const PeerId &peer,
SendMediaType type,
MsgId replyTo);
SendMediaPrepare(
const QByteArray &data,
int duration,
const PeerId &peer,
SendMediaType type,
MsgId replyTo);
PhotoId id;
QString file;
QImage img;
QByteArray data;
PeerId peer;
SendMediaType type;
int duration = 0;
MsgId replyTo;
};
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);
MsgId replyTo;
SendMediaType type;
QString file, filename;
2022-05-10 14:22:28 +00:00
int64 filesize = 0;
QByteArray data;
QString thumbExt;
uint64 id, thumbId; // id always file-id of media, thumbId is file-id of thumb ( == id for photos)
PeerId peer;
MTPPhoto photo;
MTPDocument document;
PreparedPhotoThumbs photoThumbs;
UploadFileParts parts;
QByteArray jpeg_md5;
2014-10-17 19:14:42 +00:00
2015-10-11 08:37:24 +00:00
QString caption;
};
2015-09-29 18:44:31 +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
virtual ~Task() = default;
2015-09-29 18:59:26 +00:00
2015-09-29 18:44:31 +00:00
TaskId id() const {
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:
explicit TaskQueue(crl::time stopTimeoutMs = 0); // <= 0 - never stop worker
2015-09-29 18:44:31 +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
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;
void wakeThread();
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:
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;
bool _inTaskAdded = false;
2015-09-29 18:44:31 +00:00
};
2017-12-19 16:57:42 +00:00
struct SendingAlbum {
struct Item {
explicit Item(TaskId taskId);
2017-12-19 16:57:42 +00:00
TaskId taskId;
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();
void fillMedia(
not_null<HistoryItem*> item,
const MTPInputMedia &media,
uint64 randomId);
void refreshMediaCaption(not_null<HistoryItem*> item);
void removeItem(not_null<HistoryItem*> item);
uint64 groupId = 0;
2017-12-19 16:57:42 +00:00
std::vector<Item> items;
Api::SendOptions options;
2017-12-19 16:57:42 +00:00
};
struct FileLoadTo {
FileLoadTo(
2021-11-09 15:24:13 +00:00
PeerId peer,
Api::SendOptions options,
MsgId replyTo,
MsgId topicRootId,
MsgId replaceMediaOf)
: peer(peer)
, options(options)
, replyTo(replyTo)
, topicRootId(topicRootId)
, replaceMediaOf(replaceMediaOf) {
}
PeerId peer;
Api::SendOptions options;
MsgId replyTo;
MsgId topicRootId;
MsgId replaceMediaOf;
};
struct FileLoadResult {
2017-12-19 16:57:42 +00:00
FileLoadResult(
TaskId taskId,
uint64 id,
const FileLoadTo &to,
const TextWithTags &caption,
2017-12-19 16:57:42 +00:00
std::shared_ptr<SendingAlbum> album);
TaskId taskId;
uint64 id;
FileLoadTo to;
2017-12-19 16:57:42 +00:00
std::shared_ptr<SendingAlbum> album;
SendMediaType type = SendMediaType::File;
QString filepath;
QByteArray content;
QString filename;
QString filemime;
2022-05-10 14:22:28 +00:00
int64 filesize = 0;
UploadFileParts fileparts;
QByteArray filemd5;
2022-05-10 14:22:28 +00:00
int64 partssize = 0;
uint64 thumbId = 0; // id is always file-id of media, thumbId is file-id of thumb ( == id for photos)
QString thumbname;
UploadFileParts thumbparts;
QByteArray thumbbytes;
QByteArray thumbmd5;
QImage thumb;
QImage goodThumbnail;
QByteArray goodThumbnailBytes;
MTPPhoto photo;
MTPDocument document;
PreparedPhotoThumbs photoThumbs;
TextWithTags caption;
std::vector<MTPInputDocument> attachedStickers;
2019-01-18 11:26:43 +00:00
void setFileData(const QByteArray &filedata);
void setThumbData(const QByteArray &thumbdata);
};
2017-12-19 16:57:42 +00:00
class FileLoadTask final : public Task {
public:
static std::unique_ptr<Ui::PreparedFileInformation> ReadMediaInformation(
2017-12-19 16:57:42 +00:00
const QString &filepath,
const QByteArray &content,
const QString &filemime);
static bool FillImageInformation(
QImage &&image,
bool animated,
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,
std::unique_ptr<Ui::PreparedFileInformation> information,
2017-12-19 16:57:42 +00:00
SendMediaType type,
const FileLoadTo &to,
const TextWithTags &caption,
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,
const TextWithTags &caption);
~FileLoadTask();
uint64 fileid() const {
return _id;
}
struct Args {
bool generateGoodThumbnail = true;
};
void process(Args &&args);
void process() override {
process({});
}
void finish() override;
2020-03-04 09:21:19 +00:00
FileLoadResult *peekResult() const;
private:
static bool CheckForSong(
const QString &filepath,
const QByteArray &content,
std::unique_ptr<Ui::PreparedFileInformation> &result);
static bool CheckForVideo(
const QString &filepath,
const QByteArray &content,
std::unique_ptr<Ui::PreparedFileInformation> &result);
static bool CheckForImage(
const QString &filepath,
const QByteArray &content,
std::unique_ptr<Ui::PreparedFileInformation> &result);
template <typename Mimes, typename Extensions>
static bool CheckMimeOrExtensions(const QString &filepath, const QString &filemime, Mimes &mimes, Extensions &extensions);
std::unique_ptr<Ui::PreparedFileInformation> readMediaInformation(const QString &filemime) const;
2017-12-19 16:57:42 +00:00
void removeFromAlbum();
uint64 _id = 0;
2020-06-12 14:09:04 +00:00
base::weak_ptr<Main::Session> _session;
MTP::DcId _dcId = 0;
FileLoadTo _to;
2017-12-19 16:57:42 +00:00
const std::shared_ptr<SendingAlbum> _album;
QString _filepath;
QByteArray _content;
std::unique_ptr<Ui::PreparedFileInformation> _information;
int32 _duration = 0;
VoiceWaveform _waveform;
SendMediaType _type;
TextWithTags _caption;
2017-12-19 16:57:42 +00:00
std::shared_ptr<FileLoadResult> _result;
};