tdesktop/Telegram/SourceFiles/storage/file_download.h

185 lines
4.3 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/observer.h"
#include "base/timer.h"
#include "base/binary_guard.h"
#include "base/weak_ptr.h"
2019-09-04 07:19:15 +00:00
#include <QtNetwork/QNetworkReply>
namespace Data {
struct FileOrigin;
} // namespace Data
2019-07-24 11:45:24 +00:00
namespace Main {
class Session;
} // namespace Main
namespace Storage {
namespace Cache {
struct Key;
} // namespace Cache
// 10 MB max file could be hold in memory
2019-06-27 16:57:32 +00:00
// This value is used in local cache database settings!
constexpr auto kMaxFileInMemory = 10 * 1024 * 1024;
2019-06-27 16:57:32 +00:00
// 2 MB stickers hold in memory, auto loaded and displayed inline
constexpr auto kMaxStickerBytesSize = 2 * 1024 * 1024;
// 10 MB GIF and mp4 animations held in memory while playing
constexpr auto kMaxWallPaperInMemory = kMaxFileInMemory;
constexpr auto kMaxAnimationInMemory = kMaxFileInMemory;
// 4096x4096 is max area.
constexpr auto kMaxWallPaperDimension = 4096;
} // namespace Storage
2015-09-29 18:44:31 +00:00
struct StorageImageSaved {
StorageImageSaved() = default;
explicit StorageImageSaved(const QByteArray &data) : data(data) {
2015-09-29 18:44:31 +00:00
}
2015-09-29 18:44:31 +00:00
QByteArray data;
2015-09-29 18:44:31 +00:00
};
class FileLoader : public base::has_weak_ptr {
public:
FileLoader(
const QString &toFile,
int32 size,
LocationType locationType,
LoadToCacheSetting toCache,
LoadFromCloudSetting fromCloud,
bool autoLoading,
uint8 cacheTag);
virtual ~FileLoader();
[[nodiscard]] Main::Session &session() const;
2020-05-25 14:16:04 +00:00
[[nodiscard]] bool finished() const {
return _finished;
2015-09-29 18:44:31 +00:00
}
void finishWithBytes(const QByteArray &data);
2020-05-25 14:16:04 +00:00
[[nodiscard]] bool cancelled() const {
return _cancelled;
2015-09-29 18:44:31 +00:00
}
2020-05-25 14:16:04 +00:00
[[nodiscard]] const QByteArray &bytes() const {
return _data;
2015-09-29 18:44:31 +00:00
}
2020-05-25 14:16:04 +00:00
[[nodiscard]] virtual uint64 objId() const {
return 0;
}
2020-05-25 14:16:04 +00:00
[[nodiscard]] QByteArray imageFormat(
const QSize &shrinkBox = QSize()) const;
[[nodiscard]] QImage imageData(const QSize &shrinkBox = QSize()) const;
[[nodiscard]] QString fileName() const {
return _filename;
2015-09-29 18:44:31 +00:00
}
// Used in MainWidget::documentLoadFailed.
[[nodiscard]] virtual Data::FileOrigin fileOrigin() const;
2020-05-25 14:16:04 +00:00
[[nodiscard]] float64 currentProgress() const;
[[nodiscard]] virtual int currentOffset() const;
[[nodiscard]] int fullSize() const;
bool setFileName(const QString &filename); // set filename for loaders to cache
void permitLoadFromCloud();
2014-09-04 07:33:44 +00:00
void start();
void cancel();
2015-12-23 19:23:14 +00:00
2020-05-25 14:16:04 +00:00
[[nodiscard]] bool loadingLocal() const {
return (_localStatus == LocalStatus::Loading);
2015-12-23 19:23:14 +00:00
}
2020-05-25 14:16:04 +00:00
[[nodiscard]] bool autoLoading() const {
return _autoLoading;
}
void localLoaded(
const StorageImageSaved &result,
const QByteArray &imageFormat,
const QImage &imageData);
2015-09-29 18:44:31 +00:00
[[nodiscard]] rpl::producer<rpl::empty_value, bool> updates() const {
return _updates.events();
}
2020-05-25 14:16:04 +00:00
[[nodiscard]] rpl::lifetime &lifetime() {
2020-05-21 12:49:47 +00:00
return _lifetime;
}
protected:
enum class LocalStatus {
NotTried,
NotFound,
Loading,
Loaded,
};
void readImage(const QSize &shrinkBox) const;
bool tryLoadLocal();
void loadLocal(const Storage::Cache::Key &key);
2019-04-12 07:34:39 +00:00
virtual Storage::Cache::Key cacheKey() const = 0;
virtual std::optional<MediaKey> fileLocationKey() const = 0;
virtual void cancelHook() = 0;
virtual void startLoading() = 0;
void cancel(bool failed);
2018-11-23 14:39:14 +00:00
void notifyAboutProgress();
bool writeResultPart(int offset, bytes::const_span buffer);
bool finalizeResult();
[[nodiscard]] QByteArray readLoadedPartBack(int offset, int size);
const not_null<Main::Session*> _session;
bool _autoLoading = false;
uint8 _cacheTag = 0;
bool _finished = false;
bool _cancelled = false;
mutable LocalStatus _localStatus = LocalStatus::NotTried;
QString _filename;
QFile _file;
bool _fileIsOpen = false;
LoadToCacheSetting _toCache;
LoadFromCloudSetting _fromCloud;
QByteArray _data;
int _size = 0;
int _skippedBytes = 0;
LocationType _locationType = LocationType();
base::binary_guard _localLoading;
2015-09-29 18:44:31 +00:00
mutable QByteArray _imageFormat;
mutable QImage _imageData;
2015-09-29 18:44:31 +00:00
2020-05-21 12:49:47 +00:00
rpl::lifetime _lifetime;
rpl::event_stream<rpl::empty_value, bool> _updates;
};
[[nodiscard]] std::unique_ptr<FileLoader> CreateFileLoader(
const DownloadLocation &location,
Data::FileOrigin origin,
const QString &toFile,
int size,
LocationType locationType,
LoadToCacheSetting toCache,
LoadFromCloudSetting fromCloud,
bool autoLoading,
uint8 cacheTag);