2018-10-25 08:12:56 +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_document_good_thumbnail.h"
|
|
|
|
|
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "data/data_document.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "data/data_file_origin.h"
|
2019-09-09 11:56:05 +00:00
|
|
|
#include "data/data_cloud_themes.h"
|
2019-02-13 12:36:59 +00:00
|
|
|
#include "media/clip/media_clip_reader.h"
|
2019-07-04 08:20:36 +00:00
|
|
|
#include "lottie/lottie_animation.h"
|
2019-09-09 11:56:05 +00:00
|
|
|
#include "window/themes/window_theme_preview.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "app.h"
|
2018-10-25 08:12:56 +00:00
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtCore/QBuffer>
|
|
|
|
#include <QtGui/QImageReader>
|
|
|
|
|
2018-10-25 08:12:56 +00:00
|
|
|
namespace Data {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kGoodThumbQuality = 87;
|
2019-01-29 16:26:19 +00:00
|
|
|
constexpr auto kWallPaperSize = 960;
|
|
|
|
|
2019-07-04 08:20:36 +00:00
|
|
|
enum class FileType {
|
|
|
|
Video,
|
|
|
|
AnimatedSticker,
|
|
|
|
WallPaper,
|
2019-09-09 11:56:05 +00:00
|
|
|
Theme,
|
2019-07-04 08:20:36 +00:00
|
|
|
};
|
|
|
|
|
2019-01-29 16:26:19 +00:00
|
|
|
QImage Prepare(
|
|
|
|
const QString &path,
|
|
|
|
QByteArray data,
|
2019-07-04 08:20:36 +00:00
|
|
|
FileType type) {
|
|
|
|
if (type == FileType::Video) {
|
2019-08-08 14:13:26 +00:00
|
|
|
return ::Media::Clip::PrepareForSending(path, data).thumbnail;
|
2019-07-04 08:20:36 +00:00
|
|
|
} else if (type == FileType::AnimatedSticker) {
|
|
|
|
return Lottie::ReadThumbnail(Lottie::ReadContent(data, path));
|
2019-09-09 11:56:05 +00:00
|
|
|
} else if (type == FileType::Theme) {
|
|
|
|
return Window::Theme::GeneratePreview(data, path);
|
2019-01-29 16:26:19 +00:00
|
|
|
}
|
|
|
|
const auto validateSize = [](QSize size) {
|
|
|
|
return (size.width() + size.height()) < 10'000;
|
|
|
|
};
|
|
|
|
auto buffer = QBuffer(&data);
|
|
|
|
auto file = QFile(path);
|
|
|
|
auto device = data.isEmpty() ? static_cast<QIODevice*>(&file) : &buffer;
|
|
|
|
auto reader = QImageReader(device);
|
|
|
|
#ifndef OS_MAC_OLD
|
|
|
|
reader.setAutoTransform(true);
|
|
|
|
#endif // OS_MAC_OLD
|
|
|
|
if (!reader.canRead() || !validateSize(reader.size())) {
|
|
|
|
return QImage();
|
|
|
|
}
|
|
|
|
auto result = reader.read();
|
|
|
|
if (!result.width() || !result.height()) {
|
|
|
|
return QImage();
|
|
|
|
}
|
|
|
|
return (result.width() > kWallPaperSize
|
|
|
|
|| result.height() > kWallPaperSize)
|
|
|
|
? result.scaled(
|
|
|
|
kWallPaperSize,
|
|
|
|
kWallPaperSize,
|
|
|
|
Qt::KeepAspectRatio,
|
|
|
|
Qt::SmoothTransformation)
|
|
|
|
: result;
|
|
|
|
}
|
2018-10-25 08:12:56 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
GoodThumbSource::GoodThumbSource(not_null<DocumentData*> document)
|
|
|
|
: _document(document) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void GoodThumbSource::generate(base::binary_guard &&guard) {
|
2019-01-17 08:18:23 +00:00
|
|
|
if (!guard) {
|
2018-10-25 08:12:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto data = _document->data();
|
2019-07-04 08:20:36 +00:00
|
|
|
const auto type = _document->isWallPaper()
|
|
|
|
? FileType::WallPaper
|
2019-09-09 11:56:05 +00:00
|
|
|
: _document->isTheme()
|
|
|
|
? FileType::Theme
|
2019-07-04 08:20:36 +00:00
|
|
|
: _document->sticker()
|
|
|
|
? FileType::AnimatedSticker
|
|
|
|
: FileType::Video;
|
2018-10-25 08:12:56 +00:00
|
|
|
auto location = _document->location().isEmpty()
|
|
|
|
? nullptr
|
|
|
|
: std::make_unique<FileLocation>(_document->location());
|
|
|
|
if (data.isEmpty() && !location) {
|
|
|
|
_empty = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
crl::async([
|
|
|
|
=,
|
|
|
|
guard = std::move(guard),
|
|
|
|
location = std::move(location)
|
|
|
|
]() mutable {
|
|
|
|
const auto filepath = (location && location->accessEnable())
|
|
|
|
? location->name()
|
|
|
|
: QString();
|
2019-07-04 08:20:36 +00:00
|
|
|
auto result = Prepare(filepath, data, type);
|
2018-10-25 08:12:56 +00:00
|
|
|
auto bytes = QByteArray();
|
2019-01-29 16:26:19 +00:00
|
|
|
if (!result.isNull()) {
|
|
|
|
auto buffer = QBuffer(&bytes);
|
2019-07-04 08:20:36 +00:00
|
|
|
const auto format = (type == FileType::AnimatedSticker)
|
|
|
|
? "WEBP"
|
|
|
|
: (type == FileType::WallPaper && result.hasAlphaChannel())
|
2019-01-29 16:26:19 +00:00
|
|
|
? "PNG"
|
|
|
|
: "JPG";
|
|
|
|
result.save(&buffer, format, kGoodThumbQuality);
|
2018-10-25 08:12:56 +00:00
|
|
|
}
|
|
|
|
if (!filepath.isEmpty()) {
|
|
|
|
location->accessDisable();
|
|
|
|
}
|
2018-10-25 09:24:45 +00:00
|
|
|
const auto bytesSize = bytes.size();
|
2018-10-25 08:12:56 +00:00
|
|
|
ready(
|
|
|
|
std::move(guard),
|
2019-01-29 16:26:19 +00:00
|
|
|
std::move(result),
|
2018-10-25 09:24:45 +00:00
|
|
|
bytesSize,
|
2018-10-25 08:12:56 +00:00
|
|
|
std::move(bytes));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: This method is called from crl::async(), 'this' is unreliable.
|
|
|
|
void GoodThumbSource::ready(
|
|
|
|
base::binary_guard &&guard,
|
|
|
|
QImage &&image,
|
2018-10-25 09:24:45 +00:00
|
|
|
int bytesSize,
|
|
|
|
QByteArray &&bytesForCache) {
|
2019-02-17 11:52:57 +00:00
|
|
|
crl::on_main(std::move(guard), [
|
2018-10-25 08:12:56 +00:00
|
|
|
=,
|
|
|
|
image = std::move(image),
|
2018-10-25 09:24:45 +00:00
|
|
|
bytes = std::move(bytesForCache)
|
2018-10-25 08:12:56 +00:00
|
|
|
]() mutable {
|
|
|
|
if (image.isNull()) {
|
|
|
|
_empty = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_loaded = std::move(image);
|
|
|
|
_width = _loaded.width();
|
|
|
|
_height = _loaded.height();
|
2018-10-25 09:24:45 +00:00
|
|
|
_bytesSize = bytesSize;
|
2018-10-25 08:12:56 +00:00
|
|
|
if (!bytes.isEmpty()) {
|
|
|
|
Auth().data().cache().put(
|
|
|
|
_document->goodThumbnailCacheKey(),
|
|
|
|
Storage::Cache::Database::TaggedValue{
|
|
|
|
std::move(bytes),
|
2019-09-09 11:56:05 +00:00
|
|
|
kImageCacheTag });
|
2018-10-25 08:12:56 +00:00
|
|
|
}
|
|
|
|
Auth().downloaderTaskFinished().notify();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-09 11:56:05 +00:00
|
|
|
void GoodThumbSource::load(FileOrigin origin) {
|
2018-10-25 08:12:56 +00:00
|
|
|
if (loading() || _empty) {
|
|
|
|
return;
|
|
|
|
}
|
2019-03-27 12:11:38 +00:00
|
|
|
auto callback = [=, guard = _loading.make_guard()](
|
2018-10-25 08:12:56 +00:00
|
|
|
QByteArray &&value) mutable {
|
|
|
|
if (value.isEmpty()) {
|
|
|
|
crl::on_main([=, guard = std::move(guard)]() mutable {
|
|
|
|
generate(std::move(guard));
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
crl::async([
|
|
|
|
=,
|
|
|
|
guard = std::move(guard),
|
|
|
|
value = std::move(value)
|
|
|
|
]() mutable {
|
2019-01-29 16:26:19 +00:00
|
|
|
ready(
|
|
|
|
std::move(guard),
|
|
|
|
App::readImage(value, nullptr, false),
|
|
|
|
value.size());
|
2018-10-25 08:12:56 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Auth().data().cache().get(
|
|
|
|
_document->goodThumbnailCacheKey(),
|
|
|
|
std::move(callback));
|
|
|
|
}
|
|
|
|
|
2019-09-09 11:56:05 +00:00
|
|
|
void GoodThumbSource::loadEvenCancelled(FileOrigin origin) {
|
2018-10-25 08:12:56 +00:00
|
|
|
_empty = false;
|
2019-04-12 11:52:39 +00:00
|
|
|
load(origin);
|
2018-10-25 08:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QImage GoodThumbSource::takeLoaded() {
|
|
|
|
return std::move(_loaded);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GoodThumbSource::unload() {
|
|
|
|
_loaded = QImage();
|
|
|
|
cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GoodThumbSource::automaticLoad(
|
2019-09-09 11:56:05 +00:00
|
|
|
FileOrigin origin,
|
2018-10-25 08:12:56 +00:00
|
|
|
const HistoryItem *item) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void GoodThumbSource::automaticLoadSettingsChanged() {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GoodThumbSource::loading() {
|
|
|
|
return _loading.alive();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GoodThumbSource::displayLoading() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GoodThumbSource::cancel() {
|
2019-01-17 08:18:23 +00:00
|
|
|
_loading = nullptr;
|
2018-10-25 08:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float64 GoodThumbSource::progress() {
|
|
|
|
return 1.;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GoodThumbSource::loadOffset() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const StorageImageLocation &GoodThumbSource::location() {
|
2019-03-22 14:19:43 +00:00
|
|
|
return StorageImageLocation::Invalid();
|
2018-10-25 08:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GoodThumbSource::refreshFileReference(const QByteArray &data) {
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<Storage::Cache::Key> GoodThumbSource::cacheKey() {
|
|
|
|
return _document->goodThumbnailCacheKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GoodThumbSource::setDelayedStorageLocation(
|
|
|
|
const StorageImageLocation &location) {
|
|
|
|
}
|
|
|
|
|
2019-09-09 11:56:05 +00:00
|
|
|
void GoodThumbSource::performDelayedLoad(FileOrigin origin) {
|
2018-10-25 08:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GoodThumbSource::isDelayedStorageImage() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GoodThumbSource::setImageBytes(const QByteArray &bytes) {
|
|
|
|
if (!bytes.isEmpty()) {
|
|
|
|
cancel();
|
|
|
|
_loaded = App::readImage(bytes);
|
2018-10-25 09:24:45 +00:00
|
|
|
_width = _loaded.width();
|
|
|
|
_height = _loaded.height();
|
|
|
|
_bytesSize = bytes.size();
|
2018-10-25 08:12:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int GoodThumbSource::width() {
|
|
|
|
return _width;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GoodThumbSource::height() {
|
|
|
|
return _height;
|
|
|
|
}
|
|
|
|
|
2018-10-25 09:24:45 +00:00
|
|
|
int GoodThumbSource::bytesSize() {
|
|
|
|
return _bytesSize;
|
|
|
|
}
|
|
|
|
|
2018-10-25 08:12:56 +00:00
|
|
|
void GoodThumbSource::setInformation(int size, int width, int height) {
|
|
|
|
_width = width;
|
|
|
|
_height = height;
|
2018-10-25 09:24:45 +00:00
|
|
|
_bytesSize = size;
|
2018-10-25 08:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray GoodThumbSource::bytesForCache() {
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Data
|