Use photo as video thumbnail in WebPageData.

This commit is contained in:
John Preston 2018-10-25 13:24:45 +04:00
parent 550b67236e
commit 4837117719
9 changed files with 87 additions and 7 deletions

View File

@ -552,11 +552,15 @@ void DocumentData::validateGoodThumbnail() {
void DocumentData::refreshGoodThumbnail() {
if (_goodThumbnail && hasRemoteLocation()) {
_goodThumbnail->replaceSource(
std::make_unique<Data::GoodThumbSource>(this));
replaceGoodThumbnail(std::make_unique<Data::GoodThumbSource>(this));
}
}
void DocumentData::replaceGoodThumbnail(
std::unique_ptr<Images::Source> &&source) {
_goodThumbnail->replaceSource(std::move(source));
}
void DocumentData::setGoodThumbnail(QImage &&image, QByteArray &&bytes) {
Expects(uploadingData != nullptr);

View File

@ -9,6 +9,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/data_types.h"
namespace Images {
class Source;
} // namespace Images
namespace Storage {
namespace Cache {
struct Key;
@ -158,6 +162,7 @@ public:
Storage::Cache::Key goodThumbnailCacheKey() const;
void setGoodThumbnail(QImage &&image, QByteArray &&bytes);
void refreshGoodThumbnail();
void replaceGoodThumbnail(std::unique_ptr<Images::Source> &&source);
void setRemoteLocation(
int32 dc,

View File

@ -52,9 +52,11 @@ void GoodThumbSource::generate(base::binary_guard &&guard) {
if (!filepath.isEmpty()) {
location->accessDisable();
}
const auto bytesSize = bytes.size();
ready(
std::move(guard),
std::move(result.thumbnail),
bytesSize,
std::move(bytes));
});
}
@ -63,12 +65,13 @@ void GoodThumbSource::generate(base::binary_guard &&guard) {
void GoodThumbSource::ready(
base::binary_guard &&guard,
QImage &&image,
QByteArray &&bytes) {
int bytesSize,
QByteArray &&bytesForCache) {
crl::on_main([
=,
guard = std::move(guard),
image = std::move(image),
bytes = std::move(bytes)
bytes = std::move(bytesForCache)
]() mutable {
if (!guard.alive()) {
return;
@ -80,6 +83,7 @@ void GoodThumbSource::ready(
_loaded = std::move(image);
_width = _loaded.width();
_height = _loaded.height();
_bytesSize = bytesSize;
if (!bytes.isEmpty()) {
Auth().data().cache().put(
_document->goodThumbnailCacheKey(),
@ -114,7 +118,7 @@ void GoodThumbSource::load(
guard = std::move(guard),
value = std::move(value)
]() mutable {
ready(std::move(guard), App::readImage(value));
ready(std::move(guard), App::readImage(value), value.size());
});
};
@ -194,6 +198,9 @@ void GoodThumbSource::setImageBytes(const QByteArray &bytes) {
if (!bytes.isEmpty()) {
cancel();
_loaded = App::readImage(bytes);
_width = _loaded.width();
_height = _loaded.height();
_bytesSize = bytes.size();
}
}
@ -205,9 +212,14 @@ int GoodThumbSource::height() {
return _height;
}
int GoodThumbSource::bytesSize() {
return _bytesSize;
}
void GoodThumbSource::setInformation(int size, int width, int height) {
_width = width;
_height = height;
_bytesSize = size;
}
QByteArray GoodThumbSource::bytesForCache() {

View File

@ -50,6 +50,7 @@ public:
int width() override;
int height() override;
int bytesSize() override;
void setInformation(int size, int width, int height) override;
QByteArray bytesForCache() override;
@ -61,13 +62,15 @@ private:
void ready(
base::binary_guard &&guard,
QImage &&image,
QByteArray &&bytes = {});
int bytesSize,
QByteArray &&bytesForCache = {});
not_null<DocumentData*> _document;
QImage _loaded;
base::binary_guard _loading;
int _width = 0;
int _height = 0;
int _bytesSize = 0;
bool _empty = false;
};

View File

@ -14,6 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/data_photo.h"
#include "data/data_document.h"
#include "ui/image/image.h"
#include "ui/image/image_source.h"
#include "ui/text/text_entity.h"
namespace {
@ -196,5 +197,22 @@ bool WebPageData::applyChanges(
author = resultAuthor;
pendingTill = newPendingTill;
++version;
replaceDocumentGoodThumbnail();
return true;
}
}
void WebPageData::replaceDocumentGoodThumbnail() {
if (!document || !photo || !document->goodThumbnail()) {
return;
}
const auto &location = photo->full->location();
if (!location.isNull()) {
document->replaceGoodThumbnail(
std::make_unique<Images::StorageSource>(
location,
photo->full->bytesSize()));
}
}

View File

@ -95,4 +95,7 @@ struct WebPageData {
int pendingTill = 0;
int version = 0;
private:
void replaceDocumentGoodThumbnail();
};

View File

@ -78,6 +78,7 @@ public:
virtual int width() = 0;
virtual int height() = 0;
virtual int bytesSize() = 0;
virtual void setInformation(int size, int width, int height) = 0;
virtual QByteArray bytesForCache() = 0;
@ -194,6 +195,9 @@ public:
int height() const {
return _source->height();
}
int bytesSize() const {
return _source->bytesSize();
}
void setInformation(int size, int width, int height) {
_source->setInformation(size, width, height);
}

View File

@ -113,6 +113,10 @@ int ImageSource::height() {
return _data.height();
}
int ImageSource::bytesSize() {
return _bytes.size();
}
void ImageSource::setInformation(int size, int width, int height) {
}
@ -242,6 +246,11 @@ int LocalFileSource::height() {
return _height;
}
int LocalFileSource::bytesSize() {
ensureDimensionsKnown();
return _bytes.size();
}
void LocalFileSource::setInformation(int size, int width, int height) {
ensureDimensionsKnown(); // First load _bytes.
if (width && height) {
@ -456,6 +465,10 @@ int StorageSource::height() {
return _location.height();
}
int StorageSource::bytesSize() {
return _size;
}
void StorageSource::setInformation(int size, int width, int height) {
if (size) {
_size = size;
@ -519,6 +532,10 @@ int WebCachedSource::height() {
return _height;
}
int WebCachedSource::bytesSize() {
return _size;
}
void WebCachedSource::setInformation(int size, int width, int height) {
if (size) {
_size = size;
@ -563,6 +580,10 @@ int GeoPointSource::height() {
return _location.height * _location.scale;
}
int GeoPointSource::bytesSize() {
return _size;
}
void GeoPointSource::setInformation(int size, int width, int height) {
Expects(_location.scale != 0);
@ -705,6 +726,10 @@ int WebUrlSource::height() {
return _height;
}
int WebUrlSource::bytesSize() {
return _size;
}
void WebUrlSource::setInformation(int size, int width, int height) {
if (size) {
_size = size;

View File

@ -48,6 +48,7 @@ public:
int width() override;
int height() override;
int bytesSize() override;
void setInformation(int size, int width, int height) override;
QByteArray bytesForCache() override;
@ -100,6 +101,7 @@ public:
int width() override;
int height() override;
int bytesSize() override;
void setInformation(int size, int width, int height) override;
QByteArray bytesForCache() override;
@ -184,6 +186,7 @@ public:
int width() override;
int height() override;
int bytesSize() override;
void setInformation(int size, int width, int height) override;
protected:
@ -211,6 +214,7 @@ public:
int width() override;
int height() override;
int bytesSize() override;
void setInformation(int size, int width, int height) override;
protected:
@ -236,6 +240,7 @@ public:
int width() override;
int height() override;
int bytesSize() override;
void setInformation(int size, int width, int height) override;
protected:
@ -298,6 +303,7 @@ public:
int width() override;
int height() override;
int bytesSize() override;
void setInformation(int size, int width, int height) override;
protected: