Move file_type inside PhotoSizeSource::Thumbnail.

GitOrigin-RevId: 0cf016d083bbd39d2a7abf7e2e1c267b4af29e90
This commit is contained in:
levlam 2019-06-18 19:45:26 +03:00
parent b92223a61c
commit c38180f014
4 changed files with 67 additions and 39 deletions

View File

@ -54,11 +54,29 @@ tl_object_ptr<telegram_api::InputPeer> PhotoSizeSource::DialogPhoto::get_input_p
}
}
FileType get_photo_size_source_file_type(const PhotoSizeSource &source) {
switch (source.type) {
case PhotoSizeSource::Type::Thumbnail:
return source.thumbnail().file_type;
case PhotoSizeSource::Type::DialogPhoto:
return FileType::ProfilePhoto;
case PhotoSizeSource::Type::StickerSetThumbnail:
return FileType::Thumbnail;
case PhotoSizeSource::Type::Empty:
default:
UNREACHABLE();
return FileType::Thumbnail;
}
}
bool operator==(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs) {
if (lhs.type != rhs.type || lhs.file_type != rhs.file_type) {
if (lhs.type != rhs.type) {
return false;
}
switch (lhs.type) {
case PhotoSizeSource::Type::Thumbnail:
return lhs.thumbnail().file_type == rhs.thumbnail().file_type &&
lhs.thumbnail().thumbnail_type == rhs.thumbnail().thumbnail_type;
case PhotoSizeSource::Type::DialogPhoto:
return lhs.dialog_photo().dialog_id == rhs.dialog_photo().dialog_id &&
lhs.dialog_photo().dialog_access_hash == rhs.dialog_photo().dialog_access_hash &&
@ -66,8 +84,6 @@ bool operator==(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs) {
case PhotoSizeSource::Type::StickerSetThumbnail:
return lhs.sticker_set_thumbnail().sticker_set_id == rhs.sticker_set_thumbnail().sticker_set_id &&
lhs.sticker_set_thumbnail().sticker_set_access_hash == rhs.sticker_set_thumbnail().sticker_set_access_hash;
case PhotoSizeSource::Type::Thumbnail:
return lhs.thumbnail().thumbnail_type == rhs.thumbnail().thumbnail_type;
case PhotoSizeSource::Type::Empty:
default:
return true;
@ -153,9 +169,9 @@ static FileId register_photo(FileManager *file_manager, const PhotoSizeSource &s
bool is_png = false) {
int32 local_id = location->local_id_;
int64 volume_id = location->volume_id_;
LOG(DEBUG) << "Receive " << (is_webp ? "webp" : (is_png ? "png" : "jpeg")) << " photo of type " << source.file_type
<< " in [" << dc_id << "," << volume_id << "," << local_id << "]. Id: (" << id << ", " << access_hash
<< ")";
LOG(DEBUG) << "Receive " << (is_webp ? "webp" : (is_png ? "png" : "jpeg")) << " photo of type "
<< get_photo_size_source_file_type(source) << " in [" << dc_id << "," << volume_id << "," << local_id
<< "]. Id: (" << id << ", " << access_hash << ")";
auto suggested_name = PSTRING() << static_cast<uint64>(volume_id) << "_" << static_cast<uint64>(local_id)
<< (is_webp ? ".webp" : (is_png ? ".png" : ".jpg"));
auto file_location_source = owner_dialog_id.get_type() == DialogType::SecretChat ? FileLocationSource::FromUser

View File

@ -50,14 +50,20 @@ struct PhotoSize {
struct PhotoSizeSource {
enum class Type : int32 { Empty, Thumbnail, DialogPhoto, StickerSetThumbnail };
Type type;
FileType file_type;
// for photos, document thumbnails, encrypted thumbnails
struct Thumbnail {
Thumbnail() = default;
explicit Thumbnail(int32 thumbnail_type) : thumbnail_type(thumbnail_type) {
Thumbnail(FileType file_type, int32 thumbnail_type) : thumbnail_type(thumbnail_type) {
}
FileType file_type;
int32 thumbnail_type = 0;
template <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
void parse(ParserT &parser);
};
// for dialog photos
@ -100,20 +106,16 @@ struct PhotoSizeSource {
};
Variant<Thumbnail, DialogPhoto, StickerSetThumbnail> variant;
PhotoSizeSource() : type(Type::Empty), file_type(FileType::None) {
PhotoSizeSource() : type(Type::Empty) {
}
PhotoSizeSource(FileType file_type, int32 thumbnail_type)
: type(Type::Thumbnail), file_type(file_type), variant(Thumbnail(thumbnail_type)) {
: type(Type::Thumbnail), variant(Thumbnail(file_type, thumbnail_type)) {
}
PhotoSizeSource(DialogId dialog_id, int64 dialog_access_hash, bool is_big)
: type(Type::DialogPhoto)
, file_type(FileType::ProfilePhoto)
, variant(DialogPhoto(dialog_id, dialog_access_hash, is_big)) {
: type(Type::DialogPhoto), variant(DialogPhoto(dialog_id, dialog_access_hash, is_big)) {
}
PhotoSizeSource(int64 sticker_set_id, int64 sticker_set_access_hash)
: type(Type::StickerSetThumbnail)
, file_type(FileType::Thumbnail)
, variant(StickerSetThumbnail(sticker_set_id, sticker_set_access_hash)) {
: type(Type::StickerSetThumbnail), variant(StickerSetThumbnail(sticker_set_id, sticker_set_access_hash)) {
}
Thumbnail &thumbnail() {
@ -135,9 +137,6 @@ struct PhotoSizeSource {
void parse(ParserT &parser);
};
bool operator==(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs);
bool operator!=(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs);
struct Photo {
int64 id = 0;
int32 date = 0;
@ -148,6 +147,11 @@ struct Photo {
vector<FileId> sticker_file_ids;
};
FileType get_photo_size_source_file_type(const PhotoSizeSource &source);
bool operator==(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs);
bool operator!=(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs);
Dimensions get_dimensions(int32 width, int32 height);
bool operator==(const Dimensions &lhs, const Dimensions &rhs);

View File

@ -13,10 +13,25 @@
#include "td/utils/logging.h"
#include "td/utils/tl_helpers.h"
#include <limits>
namespace td {
template <class StorerT>
void PhotoSizeSource::Thumbnail::store(StorerT &storer) const {
using td::store;
store(file_type, storer);
store(thumbnail_type, storer);
}
template <class ParserT>
void PhotoSizeSource::Thumbnail::parse(ParserT &parser) {
using td::parse;
parse(file_type, parser);
parse(thumbnail_type, parser);
if (thumbnail_type < 0 || thumbnail_type > 255) {
parser.set_error("Wrong thumbnail type");
}
}
template <class StorerT>
void PhotoSizeSource::StickerSetThumbnail::store(StorerT &storer) const {
using td::store;
@ -59,20 +74,17 @@ void PhotoSizeSource::DialogPhoto::parse(ParserT &parser) {
template <class StorerT>
void PhotoSizeSource::store(StorerT &storer) const {
using td::store;
store(file_type, storer);
store(type, storer);
switch (type) {
case Type::Thumbnail:
store(thumbnail(), storer);
break;
case Type::DialogPhoto:
store(dialog_photo(), storer);
break;
case Type::StickerSetThumbnail:
store(sticker_set_thumbnail(), storer);
break;
case Type::Thumbnail: {
auto &thumbnail = this->thumbnail();
store(thumbnail.thumbnail_type, storer);
break;
}
case Type::Empty:
break;
}
@ -81,9 +93,14 @@ void PhotoSizeSource::store(StorerT &storer) const {
template <class ParserT>
void PhotoSizeSource::parse(ParserT &parser) {
using td::parse;
parse(file_type, parser);
parse(type, parser);
switch (type) {
case Type::Thumbnail: {
Thumbnail thumbnail;
parse(thumbnail, parser);
variant = thumbnail;
break;
}
case Type::DialogPhoto: {
DialogPhoto dialog_photo;
parse(dialog_photo, parser);
@ -96,15 +113,6 @@ void PhotoSizeSource::parse(ParserT &parser) {
variant = sticker_set_thumbnail;
break;
}
case Type::Thumbnail: {
Thumbnail thumbnail;
parse(thumbnail.thumbnail_type, parser);
if (thumbnail.thumbnail_type < 0 || thumbnail.thumbnail_type > std::numeric_limits<uint8>::max()) {
parser.set_error("Wrong thumbnail type");
}
variant = thumbnail;
break;
}
case Type::Empty:
break;
}

View File

@ -401,7 +401,7 @@ class FullRemoteFileLocation {
photo().secret_, BufferSlice(file_reference_));
case PhotoSizeSource::Type::Thumbnail: {
auto &thumbnail = photo().source_.thumbnail();
switch (file_type_) {
switch (thumbnail.file_type) {
case FileType::Photo:
return make_tl_object<telegram_api::inputPhotoFileLocation>(
photo().id_, photo().access_hash_, BufferSlice(file_reference_),
@ -475,7 +475,7 @@ class FullRemoteFileLocation {
// photo
FullRemoteFileLocation(const PhotoSizeSource &source, int64 id, int64 access_hash, int32 local_id, int64 volume_id,
DcId dc_id, std::string file_reference)
: file_type_(source.file_type)
: file_type_(get_photo_size_source_file_type(source))
, dc_id_(dc_id)
, file_reference_(std::move(file_reference))
, variant_(PhotoRemoteFileLocation{id, access_hash, volume_id, 0, local_id, source}) {