2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-09-01 07:05:13 +00:00

Update API and use WebDocument for inline bots.

This commit is contained in:
John Preston
2018-03-04 23:04:13 +03:00
parent 09aba596ac
commit 0f901b3728
18 changed files with 536 additions and 238 deletions

View File

@@ -403,7 +403,7 @@ uint64 SinglePixKey(Images::Options options) {
} // namespace
StorageImageLocation StorageImageLocation::Null;
WebFileImageLocation WebFileImageLocation::Null;
WebFileLocation WebFileLocation::Null;
bool Image::isNull() const {
return (this == blank());
@@ -1040,25 +1040,45 @@ FileLoader *StorageImage::createLoader(LoadFromCloudSetting fromCloud, bool auto
return new mtpFileLoader(&_location, _size, fromCloud, autoLoading);
}
WebFileImage::WebFileImage(const WebFileImageLocation &location, int32 size)
WebFileImage::WebFileImage(
const WebFileLocation &location,
QSize box,
int size)
: _location(location)
, _box(box)
, _width(0)
, _height(0)
, _size(size) {
}
int32 WebFileImage::countWidth() const {
return _location.width();
WebFileImage::WebFileImage(
const WebFileLocation &location,
int width,
int height,
int size)
: _location(location)
, _width(width)
, _height(height)
, _size(size) {
}
int32 WebFileImage::countHeight() const {
return _location.height();
int WebFileImage::countWidth() const {
return _width;
}
void WebFileImage::setInformation(int32 size, int32 width, int32 height) {
int WebFileImage::countHeight() const {
return _height;
}
void WebFileImage::setInformation(int size, int width, int height) {
_size = size;
_location.setSize(width, height);
_width = width;
_height = height;
}
FileLoader *WebFileImage::createLoader(LoadFromCloudSetting fromCloud, bool autoLoading) {
FileLoader *WebFileImage::createLoader(
LoadFromCloudSetting fromCloud,
bool autoLoading) {
if (_location.isNull()) return 0;
return new mtpFileLoader(&_location, _size, fromCloud, autoLoading);
}
@@ -1146,10 +1166,19 @@ void DelayedStorageImage::cancel() {
StorageImage::cancel();
}
WebImage::WebImage(const QString &url, QSize box) : _url(url), _box(box), _size(0), _width(0), _height(0) {
WebImage::WebImage(const QString &url, QSize box)
: _url(url)
, _box(box)
, _size(0)
, _width(0)
, _height(0) {
}
WebImage::WebImage(const QString &url, int width, int height) : _url(url), _size(0), _width(width), _height(height) {
WebImage::WebImage(const QString &url, int width, int height)
: _url(url)
, _size(0)
, _width(width)
, _height(height) {
}
void WebImage::setSize(int width, int height) {
@@ -1256,11 +1285,116 @@ StorageImage *getImage(const StorageImageLocation &location, const QByteArray &b
return i.value();
}
WebFileImage *getImage(const WebFileImageLocation &location, int32 size) {
QSize getImageSize(const QVector<MTPDocumentAttribute> &attributes) {
for (const auto &attribute : attributes) {
if (attribute.type() == mtpc_documentAttributeImageSize) {
auto &size = attribute.c_documentAttributeImageSize();
return QSize(size.vw.v, size.vh.v);
}
}
return QSize();
}
Image *getImage(const MTPDwebDocument &document) {
const auto size = getImageSize(document.vattributes.v);
if (size.isEmpty()) {
return blank();
}
// 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; // document.vsize.v;
return getImage(
WebFileLocation(
document.vdc_id.v,
document.vurl.v,
document.vaccess_hash.v),
size.width(),
size.height(),
filesize);
}
Image *getImage(const MTPDwebDocumentNoProxy &document) {
const auto size = getImageSize(document.vattributes.v);
if (size.isEmpty()) {
return blank();
}
return getImage(qs(document.vurl), size.width(), size.height());
}
Image *getImage(const MTPDwebDocument &document, QSize box) {
const auto size = getImageSize(document.vattributes.v);
if (size.isEmpty()) {
return blank();
}
// 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; // document.vsize.v;
return getImage(
WebFileLocation(
document.vdc_id.v,
document.vurl.v,
document.vaccess_hash.v),
box,
filesize);
}
Image *getImage(const MTPDwebDocumentNoProxy &document, QSize box) {
const auto size = getImageSize(document.vattributes.v);
if (size.isEmpty()) {
return blank();
}
return getImage(qs(document.vurl), box);
}
Image *getImage(const MTPWebDocument &document) {
switch (document.type()) {
case mtpc_webDocument:
return getImage(document.c_webDocument());
case mtpc_webDocumentNoProxy:
return getImage(document.c_webDocumentNoProxy());
}
Unexpected("Type in getImage(MTPWebDocument).");
}
Image *getImage(const MTPWebDocument &document, QSize box) {
switch (document.type()) {
case mtpc_webDocument:
return getImage(document.c_webDocument(), box);
case mtpc_webDocumentNoProxy:
return getImage(document.c_webDocumentNoProxy(), box);
}
Unexpected("Type in getImage(MTPWebDocument).");
}
WebFileImage *getImage(
const WebFileLocation &location,
QSize box,
int size) {
auto key = storageKey(location);
auto i = webFileImages.constFind(key);
if (i == webFileImages.cend()) {
i = webFileImages.insert(key, new WebFileImage(location, size));
i = webFileImages.insert(
key,
new WebFileImage(location, box, size));
}
return i.value();
}
WebFileImage *getImage(
const WebFileLocation &location,
int width,
int height,
int size) {
auto key = storageKey(location);
auto i = webFileImages.constFind(key);
if (i == webFileImages.cend()) {
i = webFileImages.insert(
key,
new WebFileImage(location, width, height, size));
}
return i.value();
}

View File

@@ -183,23 +183,17 @@ inline bool operator!=(const StorageImageLocation &a, const StorageImageLocation
return !(a == b);
}
class WebFileImageLocation {
class WebFileLocation {
public:
WebFileImageLocation() = default;
WebFileImageLocation(int32 width, int32 height, int32 dc, const QByteArray &url, uint64 accessHash) : _widthheight(packIntInt(width, height)), _accessHash(accessHash), _url(url), _dc(dc) {
WebFileLocation() = default;
WebFileLocation(int32 dc, const QByteArray &url, uint64 accessHash)
: _accessHash(accessHash)
, _url(url)
, _dc(dc) {
}
bool isNull() const {
return !_dc;
}
int32 width() const {
return unpackIntFirst(_widthheight);
}
int32 height() const {
return unpackIntSecond(_widthheight);
}
void setSize(int32 width, int32 height) {
_widthheight = packIntInt(width, height);
}
int32 dc() const {
return _dc;
}
@@ -210,21 +204,20 @@ public:
return _url;
}
static WebFileImageLocation Null;
static WebFileLocation Null;
private:
uint64 _widthheight = 0;
uint64 _accessHash = 0;
QByteArray _url;
int32 _dc = 0;
friend inline bool operator==(const WebFileImageLocation &a, const WebFileImageLocation &b) {
friend inline bool operator==(const WebFileLocation &a, const WebFileLocation &b) {
return (a._dc == b._dc) && (a._accessHash == b._accessHash) && (a._url == b._url);
}
};
inline bool operator!=(const WebFileImageLocation &a, const WebFileImageLocation &b) {
inline bool operator!=(const WebFileLocation &a, const WebFileLocation &b) {
return !(a == b);
}
@@ -354,10 +347,13 @@ inline StorageKey storageKey(const MTPDfileLocation &location) {
inline StorageKey storageKey(const StorageImageLocation &location) {
return storageKey(location.dc(), location.volume(), location.local());
}
inline StorageKey storageKey(const WebFileImageLocation &location) {
inline StorageKey storageKey(const WebFileLocation &location) {
auto url = location.url();
auto sha = hashSha1(url.data(), url.size());
return storageKey(location.dc(), *reinterpret_cast<const uint64*>(sha.data()), *reinterpret_cast<const int32*>(sha.data() + sizeof(uint64)));
return storageKey(
location.dc(),
*reinterpret_cast<const uint64*>(sha.data()),
*reinterpret_cast<const int32*>(sha.data() + sizeof(uint64)));
}
class RemoteImage : public Image {
@@ -427,17 +423,25 @@ protected:
class WebFileImage : public RemoteImage {
public:
WebFileImage(const WebFileImageLocation &location, int32 size = 0);
WebFileImage(const WebFileLocation &location, QSize box, int size = 0);
WebFileImage(const WebFileLocation &location, int width, int height, int size = 0);
protected:
void setInformation(int32 size, int32 width, int32 height) override;
void setInformation(int size, int width, int height) override;
FileLoader *createLoader(LoadFromCloudSetting fromCloud, bool autoLoading) override;
WebFileImageLocation _location;
int32 _size;
QSize shrinkBox() const override {
return _box;
}
int32 countWidth() const override;
int32 countHeight() const override;
WebFileLocation _location;
QSize _box;
int _width = 0;
int _height = 0;
int _size = 0;
int countWidth() const override;
int countHeight() const override;
};
@@ -476,7 +480,6 @@ private:
class WebImage : public RemoteImage {
public:
// If !box.isEmpty() then resize the image to fit in this box.
WebImage(const QString &url, QSize box = QSize());
WebImage(const QString &url, int width, int height);
@@ -484,7 +487,6 @@ public:
void setSize(int width, int height);
protected:
QSize shrinkBox() const override {
return _box;
}
@@ -502,16 +504,33 @@ private:
};
namespace internal {
Image *getImage(const QString &file, QByteArray format);
Image *getImage(const QString &url, QSize box);
Image *getImage(const QString &url, int width, int height);
Image *getImage(const QByteArray &filecontent, QByteArray format);
Image *getImage(const QPixmap &pixmap, QByteArray format);
Image *getImage(const QByteArray &filecontent, QByteArray format, const QPixmap &pixmap);
Image *getImage(int32 width, int32 height);
StorageImage *getImage(const StorageImageLocation &location, int32 size = 0);
StorageImage *getImage(const StorageImageLocation &location, const QByteArray &bytes);
WebFileImage *getImage(const WebFileImageLocation &location, int32 size = 0);
Image *getImage(const QString &file, QByteArray format);
Image *getImage(const QString &url, QSize box);
Image *getImage(const QString &url, int width, int height);
Image *getImage(const QByteArray &filecontent, QByteArray format);
Image *getImage(const QPixmap &pixmap, QByteArray format);
Image *getImage(
const QByteArray &filecontent,
QByteArray format,
const QPixmap &pixmap);
Image *getImage(int32 width, int32 height);
StorageImage *getImage(const StorageImageLocation &location, int size = 0);
StorageImage *getImage(
const StorageImageLocation &location,
const QByteArray &bytes);
Image *getImage(const MTPWebDocument &location);
Image *getImage(const MTPWebDocument &location, QSize box);
WebFileImage *getImage(
const WebFileLocation &location,
int width,
int height,
int size = 0);
WebFileImage *getImage(
const WebFileLocation &location,
QSize box,
int size = 0);
} // namespace internal
class ImagePtr : public ManagedPtr<Image> {
@@ -533,7 +552,15 @@ public:
}
ImagePtr(const StorageImageLocation &location, const QByteArray &bytes) : Parent(internal::getImage(location, bytes)) {
}
ImagePtr(const WebFileImageLocation &location, int32 size = 0) : Parent(internal::getImage(location, size)) {
ImagePtr(const MTPWebDocument &location) : Parent(internal::getImage(location)) {
}
ImagePtr(const MTPWebDocument &location, QSize box) : Parent(internal::getImage(location, box)) {
}
ImagePtr(const WebFileLocation &location, int width, int height, int size = 0)
: Parent(internal::getImage(location, width, height, size)) {
}
ImagePtr(const WebFileLocation &location, QSize box, int size = 0)
: Parent(internal::getImage(location, box, size)) {
}
ImagePtr(int32 width, int32 height, const MTPFileLocation &location, ImagePtr def = ImagePtr());
ImagePtr(int32 width, int32 height) : Parent(internal::getImage(width, height)) {