Replace image source after photo sending.

This commit is contained in:
John Preston 2018-10-23 12:33:01 +04:00
parent 4b5b79e415
commit 591fbf0ec6
4 changed files with 72 additions and 45 deletions

View File

@ -347,63 +347,84 @@ bool MediaPhoto::updateSentMedia(const MTPMessageMedia &media) {
if (photo.type() != mtpc_photo) { if (photo.type() != mtpc_photo) {
return false; return false;
} }
struct SizeData {
char letter = 0;
int width = 0;
int height = 0;
const MTPFileLocation *location = nullptr;
QByteArray bytes;
};
const auto saveImageToCache = []( const auto saveImageToCache = [](
const MTPDfileLocation &location, const ImagePtr &image,
const ImagePtr &image) { SizeData size) {
const auto key = StorageImageLocation(0, 0, location); Expects(size.location != nullptr);
const auto key = StorageImageLocation(
size.width,
size.height,
size.location->c_fileLocation());
if (key.isNull() || image->isNull() || !image->loaded()) { if (key.isNull() || image->isNull() || !image->loaded()) {
return; return;
} }
auto bytes = image->bytesForCache(); if (size.bytes.isEmpty()) {
if (bytes.isEmpty() || bytes.size() > Storage::kMaxFileInMemory) { size.bytes = image->bytesForCache();
}
const auto length = size.bytes.size();
if (!length || length > Storage::kMaxFileInMemory) {
LOG(("App Error: Bad photo data for saving to cache."));
return; return;
} }
Auth().data().cache().putIfEmpty( Auth().data().cache().putIfEmpty(
Data::StorageCacheKey(key), Data::StorageCacheKey(key),
Storage::Cache::Database::TaggedValue( Storage::Cache::Database::TaggedValue(
std::move(bytes), std::move(size.bytes),
Data::kImageCacheTag)); Data::kImageCacheTag));
image->replaceSource(
std::make_unique<Images::StorageSource>(key, length));
}; };
auto &sizes = photo.c_photo().vsizes.v; auto &sizes = photo.c_photo().vsizes.v;
auto max = 0; auto max = 0;
const MTPDfileLocation *maxLocation = 0; auto maxSize = SizeData();
for (const auto &data : sizes) { for (const auto &data : sizes) {
char size = 0; const auto size = data.match([](const MTPDphotoSize &data) {
const MTPFileLocation *loc = 0; return SizeData{
switch (data.type()) { data.vtype.v.isEmpty() ? char(0) : data.vtype.v[0],
case mtpc_photoSize: { data.vw.v,
const auto &s = data.c_photoSize().vtype.v; data.vh.v,
loc = &data.c_photoSize().vlocation; &data.vlocation,
if (s.size()) size = s[0]; QByteArray()
} break; };
}, [](const MTPDphotoCachedSize &data) {
case mtpc_photoCachedSize: { return SizeData{
const auto &s = data.c_photoCachedSize().vtype.v; data.vtype.v.isEmpty() ? char(0) : data.vtype.v[0],
loc = &data.c_photoCachedSize().vlocation; data.vw.v,
if (s.size()) size = s[0]; data.vh.v,
} break; &data.vlocation,
} qba(data.vbytes)
if (!loc || loc->type() != mtpc_fileLocation) { };
}, [](const MTPDphotoSizeEmpty &) {
return SizeData();
});
if (!size.location || size.location->type() != mtpc_fileLocation) {
continue; continue;
} }
const auto &location = loc->c_fileLocation(); if (size.letter == 's') {
if (size == 's') { saveImageToCache(_photo->thumb, size);
saveImageToCache(location, _photo->thumb); } else if (size.letter == 'm') {
} else if (size == 'm') { saveImageToCache(_photo->medium, size);
saveImageToCache(location, _photo->medium); } else if (size.letter == 'x' && max < 1) {
} else if (size == 'x' && max < 1) {
max = 1; max = 1;
maxLocation = &location; maxSize = size;
} else if (size == 'y' && max < 2) { } else if (size.letter == 'y' && max < 2) {
max = 2; max = 2;
maxLocation = &location; maxSize = size;
//} else if (size == 'w' && max < 3) { //} else if (size.letter == 'w' && max < 3) {
// max = 3; // max = 3;
// maxLocation = &loc->c_fileLocation(); // maxSize = size;
} }
} }
if (maxLocation) { if (maxSize.location) {
saveImageToCache(*maxLocation, _photo->full); saveImageToCache(_photo->full, maxSize);
} }
return true; return true;
} }

View File

@ -880,15 +880,15 @@ PhotoData *Session::photoFromWeb(
if (full->isNull()) { if (full->isNull()) {
return nullptr; return nullptr;
} }
const auto width = full->width(); //const auto width = full->width();
const auto height = full->height(); //const auto height = full->height();
if (thumb->isNull()) { //if (thumb->isNull()) {
auto thumbsize = shrinkToKeepAspect(width, height, 100, 100); // auto thumbsize = shrinkToKeepAspect(width, height, 100, 100);
thumb = ImagePtr(thumbsize.width(), thumbsize.height()); // thumb = ImagePtr(thumbsize.width(), thumbsize.height());
} //}
auto mediumsize = shrinkToKeepAspect(width, height, 320, 320); //auto mediumsize = shrinkToKeepAspect(width, height, 320, 320);
auto medium = ImagePtr(mediumsize.width(), mediumsize.height()); //auto medium = ImagePtr(mediumsize.width(), mediumsize.height());
return photo( return photo(
rand_value<PhotoId>(), rand_value<PhotoId>(),
@ -896,7 +896,7 @@ PhotoData *Session::photoFromWeb(
QByteArray(), QByteArray(),
unixtime(), unixtime(),
thumb, thumb,
medium, ImagePtr(),
full); full);
} }

View File

@ -778,6 +778,10 @@ Image::Image(std::unique_ptr<Images::Source> &&source)
: _source(std::move(source)) { : _source(std::move(source)) {
} }
void Image::replaceSource(std::unique_ptr<Images::Source> &&source) {
_source = std::move(source);
}
Image *Image::Blank() { Image *Image::Blank() {
static const auto blankImage = [] { static const auto blankImage = [] {
const auto factor = cIntRetinaFactor(); const auto factor = cIntRetinaFactor();

View File

@ -368,6 +368,8 @@ class Image final {
public: public:
explicit Image(std::unique_ptr<Images::Source> &&source); explicit Image(std::unique_ptr<Images::Source> &&source);
void replaceSource(std::unique_ptr<Images::Source> &&source);
static Image *Blank(); static Image *Blank();
const QPixmap &pix( const QPixmap &pix(