2020-05-26 11:59:45 +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
|
|
|
|
*/
|
|
|
|
#include "data/data_cloud_file.h"
|
|
|
|
|
|
|
|
#include "data/data_file_origin.h"
|
2020-05-27 14:31:39 +00:00
|
|
|
#include "data/data_session.h"
|
2020-05-26 11:59:45 +00:00
|
|
|
#include "storage/cache/storage_cache_database.h"
|
|
|
|
#include "storage/file_download.h"
|
2020-05-29 15:10:25 +00:00
|
|
|
#include "ui/image/image.h"
|
2020-05-27 14:31:39 +00:00
|
|
|
#include "main/main_session.h"
|
|
|
|
|
2020-05-26 11:59:45 +00:00
|
|
|
namespace Data {
|
|
|
|
|
2020-06-03 09:48:11 +00:00
|
|
|
CloudFile::~CloudFile() {
|
|
|
|
// Destroy loader with still alive CloudFile with already zero '.loader'.
|
|
|
|
// Otherwise in ~FileLoader it tries to clear file.loader and crashes.
|
|
|
|
base::take(loader);
|
|
|
|
}
|
|
|
|
|
2024-04-15 07:18:57 +00:00
|
|
|
void CloudFile::clear() {
|
|
|
|
location = {};
|
|
|
|
base::take(loader);
|
|
|
|
byteSize = 0;
|
|
|
|
progressivePartSize = 0;
|
|
|
|
flags = {};
|
|
|
|
}
|
|
|
|
|
2020-05-28 13:51:18 +00:00
|
|
|
CloudImage::CloudImage() = default;
|
|
|
|
|
|
|
|
CloudImage::CloudImage(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
const ImageWithLocation &data) {
|
|
|
|
update(session, data);
|
2020-05-27 14:31:39 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 14:32:10 +00:00
|
|
|
void CloudImage::set(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
const ImageWithLocation &data) {
|
|
|
|
const auto &was = _file.location.file().data;
|
|
|
|
const auto &now = data.location.file().data;
|
|
|
|
if (!data.location.valid()) {
|
|
|
|
_file.flags |= CloudFile::Flag::Cancelled;
|
|
|
|
_file.loader = nullptr;
|
|
|
|
_file.location = ImageLocation();
|
|
|
|
_file.byteSize = 0;
|
|
|
|
_file.flags = CloudFile::Flag();
|
2022-12-05 12:18:10 +00:00
|
|
|
_view = std::weak_ptr<QImage>();
|
2020-05-28 14:32:10 +00:00
|
|
|
} else if (was != now
|
2020-08-31 08:14:53 +00:00
|
|
|
&& (!v::is<InMemoryLocation>(was) || v::is<InMemoryLocation>(now))) {
|
2020-05-28 14:32:10 +00:00
|
|
|
_file.location = ImageLocation();
|
2022-12-05 12:18:10 +00:00
|
|
|
_view = std::weak_ptr<QImage>();
|
2020-05-28 14:32:10 +00:00
|
|
|
}
|
|
|
|
UpdateCloudFile(
|
|
|
|
_file,
|
|
|
|
data,
|
|
|
|
session->data().cache(),
|
|
|
|
kImageCacheTag,
|
|
|
|
[=](FileOrigin origin) { load(session, origin); },
|
2022-02-21 14:23:49 +00:00
|
|
|
[=](QImage preloaded, QByteArray) {
|
2022-12-05 12:18:10 +00:00
|
|
|
setToActive(session, std::move(preloaded));
|
2020-05-28 14:32:10 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-28 13:51:18 +00:00
|
|
|
void CloudImage::update(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
const ImageWithLocation &data) {
|
2020-05-27 14:31:39 +00:00
|
|
|
UpdateCloudFile(
|
|
|
|
_file,
|
|
|
|
data,
|
2020-05-28 13:51:18 +00:00
|
|
|
session->data().cache(),
|
2020-05-27 14:31:39 +00:00
|
|
|
kImageCacheTag,
|
2020-05-28 13:51:18 +00:00
|
|
|
[=](FileOrigin origin) { load(session, origin); },
|
2022-02-21 14:23:49 +00:00
|
|
|
[=](QImage preloaded, QByteArray) {
|
2022-12-05 12:18:10 +00:00
|
|
|
setToActive(session, std::move(preloaded));
|
2020-05-27 14:31:39 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CloudImage::empty() const {
|
|
|
|
return !_file.location.valid();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CloudImage::loading() const {
|
|
|
|
return (_file.loader != nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CloudImage::failed() const {
|
|
|
|
return (_file.flags & CloudFile::Flag::Failed);
|
|
|
|
}
|
|
|
|
|
2021-12-06 11:00:17 +00:00
|
|
|
bool CloudImage::loadedOnce() const {
|
|
|
|
return (_file.flags & CloudFile::Flag::Loaded);
|
|
|
|
}
|
|
|
|
|
2020-05-28 13:51:18 +00:00
|
|
|
void CloudImage::load(not_null<Main::Session*> session, FileOrigin origin) {
|
2020-05-27 14:31:39 +00:00
|
|
|
const auto autoLoading = false;
|
2020-06-08 15:17:33 +00:00
|
|
|
const auto finalCheck = [=] {
|
2020-05-27 14:31:39 +00:00
|
|
|
if (const auto active = activeView()) {
|
2022-12-05 12:18:10 +00:00
|
|
|
return active->isNull();
|
2021-12-06 11:00:17 +00:00
|
|
|
} else if (_file.flags & CloudFile::Flag::Loaded) {
|
|
|
|
return false;
|
2020-05-27 14:31:39 +00:00
|
|
|
}
|
2021-12-06 11:00:17 +00:00
|
|
|
return !(_file.flags & CloudFile::Flag::Loaded);
|
2020-06-08 15:17:33 +00:00
|
|
|
};
|
2022-02-21 14:23:49 +00:00
|
|
|
const auto done = [=](QImage result, QByteArray) {
|
2022-12-05 12:18:10 +00:00
|
|
|
setToActive(session, std::move(result));
|
2020-06-08 15:17:33 +00:00
|
|
|
};
|
|
|
|
LoadCloudFile(
|
|
|
|
session,
|
|
|
|
_file,
|
|
|
|
origin,
|
|
|
|
LoadFromCloudOrLocal,
|
|
|
|
autoLoading,
|
|
|
|
kImageCacheTag,
|
|
|
|
finalCheck,
|
|
|
|
done);
|
2020-05-27 14:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ImageLocation &CloudImage::location() const {
|
|
|
|
return _file.location;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CloudImage::byteSize() const {
|
|
|
|
return _file.byteSize;
|
|
|
|
}
|
|
|
|
|
2022-12-05 12:18:10 +00:00
|
|
|
std::shared_ptr<QImage> CloudImage::createView() {
|
2020-05-27 14:31:39 +00:00
|
|
|
if (auto active = activeView()) {
|
|
|
|
return active;
|
|
|
|
}
|
2022-12-05 12:18:10 +00:00
|
|
|
auto view = std::make_shared<QImage>();
|
2020-05-27 14:31:39 +00:00
|
|
|
_view = view;
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2022-12-05 12:18:10 +00:00
|
|
|
std::shared_ptr<QImage> CloudImage::activeView() const {
|
2020-05-27 14:31:39 +00:00
|
|
|
return _view.lock();
|
|
|
|
}
|
|
|
|
|
2022-12-05 12:18:10 +00:00
|
|
|
bool CloudImage::isCurrentView(const std::shared_ptr<QImage> &view) const {
|
2020-05-28 14:32:10 +00:00
|
|
|
if (!view) {
|
|
|
|
return empty();
|
|
|
|
}
|
|
|
|
return !view.owner_before(_view) && !_view.owner_before(view);
|
|
|
|
}
|
|
|
|
|
2022-12-05 12:18:10 +00:00
|
|
|
void CloudImage::setToActive(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
QImage image) {
|
|
|
|
if (const auto view = activeView()) {
|
|
|
|
*view = image.isNull()
|
|
|
|
? Image::Empty()->original()
|
|
|
|
: std::move(image);
|
|
|
|
session->notifyDownloaderTaskFinished();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 11:59:45 +00:00
|
|
|
void UpdateCloudFile(
|
|
|
|
CloudFile &file,
|
|
|
|
const ImageWithLocation &data,
|
|
|
|
Storage::Cache::Database &cache,
|
2020-05-27 09:01:25 +00:00
|
|
|
uint8 cacheTag,
|
2020-05-26 11:59:45 +00:00
|
|
|
Fn<void(FileOrigin)> restartLoader,
|
2022-02-21 14:23:49 +00:00
|
|
|
Fn<void(QImage, QByteArray)> usePreloaded) {
|
2020-05-26 11:59:45 +00:00
|
|
|
if (!data.location.valid()) {
|
2020-08-25 18:03:41 +00:00
|
|
|
if (data.progressivePartSize && !file.location.valid()) {
|
|
|
|
file.progressivePartSize = data.progressivePartSize;
|
|
|
|
}
|
2020-05-26 11:59:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-31 13:40:01 +00:00
|
|
|
const auto needStickerThumbnailUpdate = [&] {
|
|
|
|
const auto was = std::get_if<StorageFileLocation>(
|
|
|
|
&file.location.file().data);
|
|
|
|
const auto now = std::get_if<StorageFileLocation>(
|
|
|
|
&data.location.file().data);
|
|
|
|
using Type = StorageFileLocation::Type;
|
|
|
|
if (!was || !now || was->type() != Type::StickerSetThumb) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return now->valid()
|
|
|
|
&& (now->type() != Type::StickerSetThumb
|
|
|
|
|| now->cacheKey() != was->cacheKey());
|
|
|
|
};
|
2020-05-26 11:59:45 +00:00
|
|
|
const auto update = !file.location.valid()
|
|
|
|
|| (data.location.file().cacheKey()
|
|
|
|
&& (!file.location.file().cacheKey()
|
|
|
|
|| (file.location.width() < data.location.width())
|
2021-12-31 13:40:01 +00:00
|
|
|
|| (file.location.height() < data.location.height())
|
|
|
|
|| needStickerThumbnailUpdate()));
|
2020-05-26 11:59:45 +00:00
|
|
|
if (!update) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-26 17:07:42 +00:00
|
|
|
auto cacheBytes = !data.bytes.isEmpty()
|
2020-05-26 11:59:45 +00:00
|
|
|
? data.bytes
|
2020-08-31 08:14:53 +00:00
|
|
|
: v::is<InMemoryLocation>(file.location.file().data)
|
2020-09-01 19:56:25 +00:00
|
|
|
? v::get<InMemoryLocation>(file.location.file().data).bytes
|
2020-05-26 11:59:45 +00:00
|
|
|
: QByteArray();
|
|
|
|
if (!cacheBytes.isEmpty()) {
|
|
|
|
if (const auto cacheKey = data.location.file().cacheKey()) {
|
|
|
|
cache.putIfEmpty(
|
|
|
|
cacheKey,
|
|
|
|
Storage::Cache::Database::TaggedValue(
|
2020-05-26 17:07:42 +00:00
|
|
|
std::move(cacheBytes),
|
2020-05-27 09:01:25 +00:00
|
|
|
cacheTag));
|
2020-05-26 11:59:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
file.location = data.location;
|
|
|
|
file.byteSize = data.bytesCount;
|
|
|
|
if (!data.preloaded.isNull()) {
|
|
|
|
file.loader = nullptr;
|
|
|
|
if (usePreloaded) {
|
2022-02-21 14:23:49 +00:00
|
|
|
usePreloaded(data.preloaded, data.bytes);
|
2020-05-26 11:59:45 +00:00
|
|
|
}
|
|
|
|
} else if (file.loader) {
|
|
|
|
const auto origin = base::take(file.loader)->fileOrigin();
|
|
|
|
restartLoader(origin);
|
2021-12-31 13:40:01 +00:00
|
|
|
} else if (file.flags & CloudFile::Flag::Failed) {
|
|
|
|
file.flags &= ~CloudFile::Flag::Failed;
|
2020-05-26 11:59:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 09:01:25 +00:00
|
|
|
void LoadCloudFile(
|
2020-06-08 15:17:33 +00:00
|
|
|
not_null<Main::Session*> session,
|
2020-05-27 09:01:25 +00:00
|
|
|
CloudFile &file,
|
|
|
|
FileOrigin origin,
|
|
|
|
LoadFromCloudSetting fromCloud,
|
|
|
|
bool autoLoading,
|
|
|
|
uint8 cacheTag,
|
|
|
|
Fn<bool()> finalCheck,
|
|
|
|
Fn<void(CloudFile&)> done,
|
|
|
|
Fn<void(bool)> fail,
|
2020-08-25 18:03:41 +00:00
|
|
|
Fn<void()> progress,
|
|
|
|
int downloadFrontPartSize = 0) {
|
|
|
|
const auto loadSize = downloadFrontPartSize
|
2020-08-26 13:28:56 +00:00
|
|
|
? std::min(downloadFrontPartSize, file.byteSize)
|
2020-08-25 18:03:41 +00:00
|
|
|
: file.byteSize;
|
2020-05-27 09:01:25 +00:00
|
|
|
if (file.loader) {
|
|
|
|
if (fromCloud == LoadFromCloudOrLocal) {
|
|
|
|
file.loader->permitLoadFromCloud();
|
|
|
|
}
|
2020-08-26 13:28:56 +00:00
|
|
|
if (file.loader->loadSize() < loadSize) {
|
|
|
|
file.loader->increaseLoadSize(loadSize, autoLoading);
|
2020-08-25 18:03:41 +00:00
|
|
|
}
|
2020-05-27 09:01:25 +00:00
|
|
|
return;
|
|
|
|
} else if ((file.flags & CloudFile::Flag::Failed)
|
|
|
|
|| !file.location.valid()
|
|
|
|
|| (finalCheck && !finalCheck())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
file.flags &= ~CloudFile::Flag::Cancelled;
|
|
|
|
file.loader = CreateFileLoader(
|
2020-06-08 15:17:33 +00:00
|
|
|
session,
|
2020-05-27 09:01:25 +00:00
|
|
|
file.location.file(),
|
|
|
|
origin,
|
|
|
|
QString(),
|
2020-08-25 18:03:41 +00:00
|
|
|
loadSize,
|
2020-08-26 13:28:56 +00:00
|
|
|
file.byteSize,
|
2020-05-27 09:01:25 +00:00
|
|
|
UnknownFileLocation,
|
|
|
|
LoadToCacheAsWell,
|
|
|
|
fromCloud,
|
|
|
|
autoLoading,
|
|
|
|
cacheTag);
|
|
|
|
|
|
|
|
const auto finish = [done](CloudFile &file) {
|
|
|
|
if (!file.loader || file.loader->cancelled()) {
|
|
|
|
file.flags |= CloudFile::Flag::Cancelled;
|
|
|
|
} else {
|
2021-12-06 11:00:17 +00:00
|
|
|
file.flags |= CloudFile::Flag::Loaded;
|
2020-05-27 09:01:25 +00:00
|
|
|
done(file);
|
|
|
|
}
|
|
|
|
// NB! file.loader may be in ~FileLoader() already.
|
|
|
|
if (const auto loader = base::take(file.loader)) {
|
2020-06-03 09:48:11 +00:00
|
|
|
if ((file.flags & CloudFile::Flag::Cancelled)
|
|
|
|
&& !loader->cancelled()) {
|
2020-05-27 09:01:25 +00:00
|
|
|
loader->cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
file.loader->updates(
|
|
|
|
) | rpl::start_with_next_error_done([=] {
|
|
|
|
if (const auto onstack = progress) {
|
|
|
|
onstack();
|
|
|
|
}
|
2023-01-05 13:32:51 +00:00
|
|
|
}, [=, &file](FileLoader::Error error) {
|
2020-05-27 09:01:25 +00:00
|
|
|
finish(file);
|
|
|
|
file.flags |= CloudFile::Flag::Failed;
|
|
|
|
if (const auto onstack = fail) {
|
2023-01-05 13:32:51 +00:00
|
|
|
onstack(error.started);
|
2020-05-27 09:01:25 +00:00
|
|
|
}
|
|
|
|
}, [=, &file] {
|
|
|
|
finish(file);
|
|
|
|
}, file.loader->lifetime());
|
|
|
|
|
|
|
|
file.loader->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LoadCloudFile(
|
2020-06-08 15:17:33 +00:00
|
|
|
not_null<Main::Session*> session,
|
2020-05-27 09:01:25 +00:00
|
|
|
CloudFile &file,
|
|
|
|
FileOrigin origin,
|
|
|
|
LoadFromCloudSetting fromCloud,
|
|
|
|
bool autoLoading,
|
|
|
|
uint8 cacheTag,
|
|
|
|
Fn<bool()> finalCheck,
|
2022-02-21 14:23:49 +00:00
|
|
|
Fn<void(QImage, QByteArray)> done,
|
2020-05-27 09:01:25 +00:00
|
|
|
Fn<void(bool)> fail,
|
2020-08-25 18:03:41 +00:00
|
|
|
Fn<void()> progress,
|
|
|
|
int downloadFrontPartSize) {
|
2020-05-27 09:01:25 +00:00
|
|
|
const auto callback = [=](CloudFile &file) {
|
|
|
|
if (auto read = file.loader->imageData(); read.isNull()) {
|
|
|
|
file.flags |= CloudFile::Flag::Failed;
|
|
|
|
if (const auto onstack = fail) {
|
|
|
|
onstack(true);
|
|
|
|
}
|
|
|
|
} else if (const auto onstack = done) {
|
2022-02-21 14:23:49 +00:00
|
|
|
onstack(std::move(read), file.loader->bytes());
|
2020-05-27 09:01:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
LoadCloudFile(
|
2020-06-08 15:17:33 +00:00
|
|
|
session,
|
2020-05-27 09:01:25 +00:00
|
|
|
file,
|
|
|
|
origin,
|
|
|
|
fromCloud,
|
|
|
|
autoLoading,
|
|
|
|
cacheTag,
|
|
|
|
finalCheck,
|
|
|
|
callback,
|
|
|
|
std::move(fail),
|
2020-08-25 18:03:41 +00:00
|
|
|
std::move(progress),
|
|
|
|
downloadFrontPartSize);
|
2020-05-27 09:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LoadCloudFile(
|
2020-06-08 15:17:33 +00:00
|
|
|
not_null<Main::Session*> session,
|
2020-05-27 09:01:25 +00:00
|
|
|
CloudFile &file,
|
|
|
|
FileOrigin origin,
|
|
|
|
LoadFromCloudSetting fromCloud,
|
|
|
|
bool autoLoading,
|
|
|
|
uint8 cacheTag,
|
|
|
|
Fn<bool()> finalCheck,
|
|
|
|
Fn<void(QByteArray)> done,
|
|
|
|
Fn<void(bool)> fail,
|
|
|
|
Fn<void()> progress) {
|
|
|
|
const auto callback = [=](CloudFile &file) {
|
|
|
|
if (auto bytes = file.loader->bytes(); bytes.isEmpty()) {
|
2020-05-27 14:31:39 +00:00
|
|
|
file.flags |= CloudFile::Flag::Failed;
|
2020-05-27 09:01:25 +00:00
|
|
|
if (const auto onstack = fail) {
|
|
|
|
onstack(true);
|
|
|
|
}
|
|
|
|
} else if (const auto onstack = done) {
|
|
|
|
onstack(std::move(bytes));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
LoadCloudFile(
|
2020-06-08 15:17:33 +00:00
|
|
|
session,
|
2020-05-27 09:01:25 +00:00
|
|
|
file,
|
|
|
|
origin,
|
|
|
|
fromCloud,
|
|
|
|
autoLoading,
|
|
|
|
cacheTag,
|
|
|
|
finalCheck,
|
|
|
|
callback,
|
|
|
|
std::move(fail),
|
|
|
|
std::move(progress));
|
|
|
|
}
|
|
|
|
|
2020-05-29 18:06:40 +00:00
|
|
|
} // namespace Data
|