2020-04-16 12:37:26 +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 "ui/image/image_location_factory.h"
|
|
|
|
|
|
|
|
#include "ui/image/image.h"
|
|
|
|
#include "main/main_session.h"
|
|
|
|
|
|
|
|
#include <QtCore/QBuffer>
|
|
|
|
|
|
|
|
namespace Images {
|
|
|
|
|
2020-05-25 14:16:04 +00:00
|
|
|
ImageWithLocation FromPhotoSize(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
const MTPDphoto &photo,
|
|
|
|
const MTPPhotoSize &size) {
|
2020-05-26 11:59:45 +00:00
|
|
|
if (!photo.vaccess_hash().v && photo.vfile_reference().v.isEmpty()) {
|
|
|
|
// Locally created fake photo.
|
|
|
|
return ImageWithLocation();
|
|
|
|
}
|
2020-05-25 14:16:04 +00:00
|
|
|
return size.match([&](const MTPDphotoSize &data) {
|
|
|
|
return ImageWithLocation{
|
|
|
|
.location = ImageLocation(
|
|
|
|
DownloadLocation{ StorageFileLocation(
|
|
|
|
photo.vdc_id().v,
|
|
|
|
session->userId(),
|
|
|
|
MTP_inputPhotoFileLocation(
|
|
|
|
photo.vid(),
|
|
|
|
photo.vaccess_hash(),
|
|
|
|
photo.vfile_reference(),
|
|
|
|
data.vtype())) },
|
|
|
|
data.vw().v,
|
|
|
|
data.vh().v),
|
|
|
|
.bytesCount = data.vsize().v
|
|
|
|
};
|
|
|
|
}, [&](const MTPDphotoCachedSize &data) {
|
|
|
|
const auto bytes = qba(data.vbytes());
|
|
|
|
return ImageWithLocation{
|
|
|
|
.location = ImageLocation(
|
|
|
|
DownloadLocation{ StorageFileLocation(
|
|
|
|
photo.vdc_id().v,
|
|
|
|
session->userId(),
|
|
|
|
MTP_inputPhotoFileLocation(
|
|
|
|
photo.vid(),
|
|
|
|
photo.vaccess_hash(),
|
|
|
|
photo.vfile_reference(),
|
|
|
|
data.vtype())) },
|
|
|
|
data.vw().v,
|
|
|
|
data.vh().v),
|
2020-07-03 10:48:55 +00:00
|
|
|
.bytes = bytes,
|
2020-05-25 14:16:04 +00:00
|
|
|
.bytesCount = bytes.size(),
|
|
|
|
};
|
2020-09-01 06:44:18 +00:00
|
|
|
}, [&](const MTPDphotoSizeProgressive &data) {
|
|
|
|
// #TODO layer118
|
|
|
|
if (data.vsizes().v.isEmpty()) {
|
|
|
|
return ImageWithLocation();
|
|
|
|
}
|
|
|
|
return ImageWithLocation{
|
|
|
|
.location = ImageLocation(
|
|
|
|
DownloadLocation{ StorageFileLocation(
|
|
|
|
photo.vdc_id().v,
|
|
|
|
session->userId(),
|
|
|
|
MTP_inputPhotoFileLocation(
|
|
|
|
photo.vid(),
|
|
|
|
photo.vaccess_hash(),
|
|
|
|
photo.vfile_reference(),
|
|
|
|
data.vtype())) },
|
|
|
|
data.vw().v,
|
|
|
|
data.vh().v),
|
|
|
|
.bytesCount = data.vsizes().v.back().v
|
|
|
|
};
|
2020-05-25 14:16:04 +00:00
|
|
|
}, [&](const MTPDphotoStrippedSize &data) {
|
|
|
|
return ImageWithLocation();
|
|
|
|
//const auto bytes = ExpandInlineBytes(qba(data.vbytes()));
|
|
|
|
//return ImageWithLocation{
|
|
|
|
// .location = ImageLocation(
|
|
|
|
// DownloadLocation{ StorageFileLocation(
|
|
|
|
// photo.vdc_id().v,
|
|
|
|
// session->userId(),
|
|
|
|
// MTP_inputPhotoFileLocation(
|
|
|
|
// photo.vid(),
|
|
|
|
// photo.vaccess_hash(),
|
|
|
|
// photo.vfile_reference(),
|
|
|
|
// data.vtype())) },
|
|
|
|
// width, // ???
|
|
|
|
// height), // ???
|
2020-07-03 10:48:55 +00:00
|
|
|
// .bytes = bytes,
|
2020-05-25 14:16:04 +00:00
|
|
|
// .bytesCount = bytes.size(),
|
|
|
|
//};
|
2020-11-20 12:57:17 +00:00
|
|
|
}, [&](const MTPDphotoPathSize &) {
|
|
|
|
return ImageWithLocation();
|
2020-05-25 14:16:04 +00:00
|
|
|
}, [&](const MTPDphotoSizeEmpty &) {
|
|
|
|
return ImageWithLocation();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-25 18:03:41 +00:00
|
|
|
ImageWithLocation FromProgressiveSize(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
const MTPPhotoSize &size,
|
|
|
|
int index) {
|
|
|
|
Expects(size.type() == mtpc_photoSizeProgressive);
|
|
|
|
|
|
|
|
const auto &data = size.c_photoSizeProgressive();
|
|
|
|
if (data.vsizes().v.size() <= index) {
|
|
|
|
return ImageWithLocation();
|
|
|
|
}
|
|
|
|
return ImageWithLocation{
|
|
|
|
.progressivePartSize = data.vsizes().v[index].v,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-16 12:37:26 +00:00
|
|
|
ImageWithLocation FromPhotoSize(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
const MTPDdocument &document,
|
|
|
|
const MTPPhotoSize &size) {
|
|
|
|
return size.match([&](const MTPDphotoSize &data) {
|
|
|
|
return ImageWithLocation{
|
|
|
|
.location = ImageLocation(
|
|
|
|
DownloadLocation{ StorageFileLocation(
|
|
|
|
document.vdc_id().v,
|
|
|
|
session->userId(),
|
|
|
|
MTP_inputDocumentFileLocation(
|
|
|
|
document.vid(),
|
|
|
|
document.vaccess_hash(),
|
|
|
|
document.vfile_reference(),
|
|
|
|
data.vtype())) },
|
|
|
|
data.vw().v,
|
|
|
|
data.vh().v),
|
|
|
|
.bytesCount = data.vsize().v
|
|
|
|
};
|
|
|
|
}, [&](const MTPDphotoCachedSize &data) {
|
|
|
|
const auto bytes = qba(data.vbytes());
|
|
|
|
return ImageWithLocation{
|
|
|
|
.location = ImageLocation(
|
|
|
|
DownloadLocation{ StorageFileLocation(
|
|
|
|
document.vdc_id().v,
|
|
|
|
session->userId(),
|
|
|
|
MTP_inputDocumentFileLocation(
|
|
|
|
document.vid(),
|
|
|
|
document.vaccess_hash(),
|
|
|
|
document.vfile_reference(),
|
|
|
|
data.vtype())) },
|
|
|
|
data.vw().v,
|
|
|
|
data.vh().v),
|
2020-07-03 10:48:55 +00:00
|
|
|
.bytes = bytes,
|
2020-04-16 12:37:26 +00:00
|
|
|
.bytesCount = bytes.size(),
|
|
|
|
};
|
2020-09-01 06:44:18 +00:00
|
|
|
}, [&](const MTPDphotoSizeProgressive &data) {
|
|
|
|
if (data.vsizes().v.isEmpty()) {
|
|
|
|
return ImageWithLocation();
|
|
|
|
}
|
|
|
|
return ImageWithLocation{
|
|
|
|
.location = ImageLocation(
|
|
|
|
DownloadLocation{ StorageFileLocation(
|
|
|
|
document.vdc_id().v,
|
|
|
|
session->userId(),
|
|
|
|
MTP_inputDocumentFileLocation(
|
|
|
|
document.vid(),
|
|
|
|
document.vaccess_hash(),
|
|
|
|
document.vfile_reference(),
|
|
|
|
data.vtype())) },
|
|
|
|
data.vw().v,
|
|
|
|
data.vh().v),
|
|
|
|
.bytesCount = data.vsizes().v.back().v
|
|
|
|
};
|
2020-04-16 12:37:26 +00:00
|
|
|
}, [&](const MTPDphotoStrippedSize &data) {
|
|
|
|
return ImageWithLocation();
|
|
|
|
//const auto bytes = ExpandInlineBytes(qba(data.vbytes()));
|
|
|
|
//return ImageWithLocation{
|
|
|
|
// .location = ImageLocation(
|
|
|
|
// DownloadLocation{ StorageFileLocation(
|
|
|
|
// document.vdc_id().v,
|
|
|
|
// session->userId(),
|
|
|
|
// MTP_inputDocumentFileLocation(
|
|
|
|
// document.vid(),
|
|
|
|
// document.vaccess_hash(),
|
|
|
|
// document.vfile_reference(),
|
|
|
|
// data.vtype())) },
|
|
|
|
// width, // ???
|
|
|
|
// height), // ???
|
2020-07-03 10:48:55 +00:00
|
|
|
// .bytes = bytes,
|
2020-04-16 12:37:26 +00:00
|
|
|
// .bytesCount = bytes.size(),
|
|
|
|
//};
|
2020-11-20 12:57:17 +00:00
|
|
|
}, [&](const MTPDphotoPathSize &data) {
|
|
|
|
return ImageWithLocation();
|
2020-04-16 12:37:26 +00:00
|
|
|
}, [&](const MTPDphotoSizeEmpty &) {
|
|
|
|
return ImageWithLocation();
|
2020-05-28 10:00:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageWithLocation FromPhotoSize(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
const MTPDstickerSet &set,
|
|
|
|
const MTPPhotoSize &size) {
|
2021-03-30 08:16:05 +00:00
|
|
|
if (!set.vthumb_dc_id() || !set.vthumb_version()) {
|
2020-05-28 10:00:51 +00:00
|
|
|
return ImageWithLocation();
|
|
|
|
}
|
|
|
|
return size.match([&](const MTPDphotoSize &data) {
|
|
|
|
return ImageWithLocation{
|
|
|
|
.location = ImageLocation(
|
|
|
|
DownloadLocation{ StorageFileLocation(
|
|
|
|
set.vthumb_dc_id()->v,
|
|
|
|
session->userId(),
|
|
|
|
MTP_inputStickerSetThumb(
|
|
|
|
MTP_inputStickerSetID(set.vid(), set.vaccess_hash()),
|
2021-03-30 08:16:05 +00:00
|
|
|
MTP_int(set.vthumb_version()->v))) },
|
2020-05-28 10:00:51 +00:00
|
|
|
data.vw().v,
|
|
|
|
data.vh().v),
|
|
|
|
.bytesCount = data.vsize().v
|
|
|
|
};
|
|
|
|
}, [&](const MTPDphotoCachedSize &data) {
|
|
|
|
const auto bytes = qba(data.vbytes());
|
|
|
|
return ImageWithLocation{
|
|
|
|
.location = ImageLocation(
|
|
|
|
DownloadLocation{ StorageFileLocation(
|
|
|
|
set.vthumb_dc_id()->v,
|
|
|
|
session->userId(),
|
|
|
|
MTP_inputStickerSetThumb(
|
|
|
|
MTP_inputStickerSetID(set.vid(), set.vaccess_hash()),
|
2021-03-30 08:16:05 +00:00
|
|
|
MTP_int(set.vthumb_version()->v))) },
|
2020-05-28 10:00:51 +00:00
|
|
|
data.vw().v,
|
|
|
|
data.vh().v),
|
2020-07-03 10:48:55 +00:00
|
|
|
.bytes = bytes,
|
2020-05-28 10:00:51 +00:00
|
|
|
.bytesCount = bytes.size(),
|
|
|
|
};
|
2020-09-01 06:44:18 +00:00
|
|
|
}, [&](const MTPDphotoSizeProgressive &data) {
|
|
|
|
if (data.vsizes().v.isEmpty()) {
|
|
|
|
return ImageWithLocation();
|
|
|
|
}
|
|
|
|
return ImageWithLocation{
|
|
|
|
.location = ImageLocation(
|
|
|
|
DownloadLocation{ StorageFileLocation(
|
|
|
|
set.vthumb_dc_id()->v,
|
|
|
|
session->userId(),
|
|
|
|
MTP_inputStickerSetThumb(
|
|
|
|
MTP_inputStickerSetID(set.vid(), set.vaccess_hash()),
|
2021-03-30 08:16:05 +00:00
|
|
|
MTP_int(set.vthumb_version()->v))) },
|
2020-09-01 06:44:18 +00:00
|
|
|
data.vw().v,
|
|
|
|
data.vh().v),
|
|
|
|
.bytesCount = data.vsizes().v.back().v
|
|
|
|
};
|
2020-05-28 10:00:51 +00:00
|
|
|
}, [&](const MTPDphotoStrippedSize &data) {
|
|
|
|
return ImageWithLocation();
|
|
|
|
//const auto bytes = ExpandInlineBytes(qba(data.vbytes()));
|
|
|
|
//return ImageWithLocation{
|
|
|
|
// .location = ImageLocation(
|
|
|
|
// DownloadLocation{ StorageFileLocation(
|
|
|
|
// document.vdc_id().v,
|
|
|
|
// session->userId(),
|
|
|
|
// MTP_inputDocumentFileLocation(
|
|
|
|
// document.vid(),
|
|
|
|
// document.vaccess_hash(),
|
|
|
|
// document.vfile_reference(),
|
|
|
|
// data.vtype())) },
|
|
|
|
// width, // ???
|
|
|
|
// height), // ???
|
2020-07-03 10:48:55 +00:00
|
|
|
// .bytes = bytes,
|
2020-05-28 10:00:51 +00:00
|
|
|
// .bytesCount = bytes.size(),
|
|
|
|
//};
|
2020-11-20 12:57:17 +00:00
|
|
|
}, [&](const MTPDphotoPathSize &data) {
|
|
|
|
return ImageWithLocation();
|
2020-05-28 10:00:51 +00:00
|
|
|
}, [&](const MTPDphotoSizeEmpty &) {
|
|
|
|
return ImageWithLocation();
|
2020-04-16 12:37:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageWithLocation FromImageInMemory(
|
|
|
|
const QImage &image,
|
2020-08-26 13:28:56 +00:00
|
|
|
const char *format,
|
|
|
|
QByteArray bytes) {
|
2020-04-16 12:37:26 +00:00
|
|
|
if (image.isNull()) {
|
|
|
|
return ImageWithLocation();
|
|
|
|
}
|
2020-08-26 13:28:56 +00:00
|
|
|
if (bytes.isEmpty()) {
|
|
|
|
auto buffer = QBuffer(&bytes);
|
|
|
|
image.save(&buffer, format);
|
|
|
|
}
|
2020-04-16 12:37:26 +00:00
|
|
|
return ImageWithLocation{
|
|
|
|
.location = ImageLocation(
|
|
|
|
DownloadLocation{ InMemoryLocation{ bytes } },
|
|
|
|
image.width(),
|
|
|
|
image.height()),
|
|
|
|
.bytes = bytes,
|
2020-07-03 10:48:55 +00:00
|
|
|
.preloaded = image,
|
|
|
|
.bytesCount = bytes.size(),
|
2020-04-16 12:37:26 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageLocation FromWebDocument(const MTPWebDocument &document) {
|
|
|
|
return document.match([](const MTPDwebDocument &data) {
|
|
|
|
const auto size = GetSizeForDocument(data.vattributes().v);
|
|
|
|
|
|
|
|
// We don't use size from WebDocument, because it is not reliable.
|
|
|
|
// It can be > 0 and different from the real size
|
|
|
|
// that we get in upload.WebFile result.
|
|
|
|
//auto filesize = 0; // data.vsize().v;
|
|
|
|
return ImageLocation(
|
|
|
|
DownloadLocation{ WebFileLocation(
|
|
|
|
data.vurl().v,
|
|
|
|
data.vaccess_hash().v) },
|
|
|
|
size.width(),
|
|
|
|
size.height());
|
|
|
|
}, [](const MTPDwebDocumentNoProxy &data) {
|
|
|
|
const auto size = GetSizeForDocument(data.vattributes().v);
|
|
|
|
|
|
|
|
// We don't use size from WebDocument, because it is not reliable.
|
|
|
|
// It can be > 0 and different from the real size
|
|
|
|
// that we get in upload.WebFile result.
|
|
|
|
//auto filesize = 0; // data.vsize().v;
|
|
|
|
return ImageLocation(
|
|
|
|
DownloadLocation{ PlainUrlLocation{ qs(data.vurl()) } },
|
|
|
|
size.width(),
|
|
|
|
size.height());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-22 11:15:09 +00:00
|
|
|
ImageWithLocation FromVideoSize(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
const MTPDdocument &document,
|
|
|
|
const MTPVideoSize &size) {
|
|
|
|
return size.match([&](const MTPDvideoSize &data) {
|
|
|
|
return ImageWithLocation{
|
|
|
|
.location = ImageLocation(
|
|
|
|
DownloadLocation{ StorageFileLocation(
|
|
|
|
document.vdc_id().v,
|
|
|
|
session->userId(),
|
|
|
|
MTP_inputDocumentFileLocation(
|
|
|
|
document.vid(),
|
|
|
|
document.vaccess_hash(),
|
|
|
|
document.vfile_reference(),
|
|
|
|
data.vtype())) },
|
|
|
|
data.vw().v,
|
|
|
|
data.vh().v),
|
2020-07-03 10:48:55 +00:00
|
|
|
.bytesCount = data.vsize().v,
|
2020-05-22 11:15:09 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-03 08:42:09 +00:00
|
|
|
ImageWithLocation FromVideoSize(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
const MTPDphoto &photo,
|
|
|
|
const MTPVideoSize &size) {
|
|
|
|
return size.match([&](const MTPDvideoSize &data) {
|
|
|
|
return ImageWithLocation{
|
|
|
|
.location = ImageLocation(
|
|
|
|
DownloadLocation{ StorageFileLocation(
|
|
|
|
photo.vdc_id().v,
|
|
|
|
session->userId(),
|
|
|
|
MTP_inputPhotoFileLocation(
|
|
|
|
photo.vid(),
|
|
|
|
photo.vaccess_hash(),
|
|
|
|
photo.vfile_reference(),
|
|
|
|
data.vtype())) },
|
|
|
|
data.vw().v,
|
|
|
|
data.vh().v),
|
2020-07-03 10:48:55 +00:00
|
|
|
.bytesCount = data.vsize().v,
|
2020-07-03 08:42:09 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-16 12:37:26 +00:00
|
|
|
} // namespace Images
|