From 8750c7a41ac75a6970dd74a854c6bb0cf2ed2a67 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 1 Jun 2018 01:23:07 +0300 Subject: [PATCH] Better suggested name for sticker thumbnails. GitOrigin-RevId: 5372a13825f558e47ee84bfaa705af6a9b587298 --- td/telegram/DocumentsManager.cpp | 4 +++- td/telegram/Photo.cpp | 18 ++++++++++-------- td/telegram/Photo.h | 2 +- td/telegram/StickersManager.cpp | 29 +++++++++++++++++++++++++++-- td/telegram/StickersManager.h | 4 ++++ td/telegram/Td.cpp | 4 ++-- 6 files changed, 47 insertions(+), 14 deletions(-) diff --git a/td/telegram/DocumentsManager.cpp b/td/telegram/DocumentsManager.cpp index 6004ed65..ff329856 100644 --- a/td/telegram/DocumentsManager.cpp +++ b/td/telegram/DocumentsManager.cpp @@ -128,6 +128,7 @@ std::pair DocumentsManager::on_get_docum FileType file_type = FileType::Document; Slice default_extension; bool supports_streaming = false; + bool has_webp_thumbnail = false; if (type_attributes == 1 || default_document_type != DocumentType::General) { // not a general document if (animated != nullptr || default_document_type == DocumentType::Animation) { document_type = DocumentType::Animation; @@ -155,6 +156,7 @@ std::pair DocumentsManager::on_get_docum default_extension = "webp"; owner_dialog_id = DialogId(); file_name.clear(); + has_webp_thumbnail = td_->stickers_manager_->has_webp_thumbnail(sticker); } else if (video != nullptr || default_document_type == DocumentType::Video || default_document_type == DocumentType::VideoNote) { bool is_video_note = default_document_type == DocumentType::VideoNote; @@ -202,7 +204,7 @@ std::pair DocumentsManager::on_get_docum if (document_type != DocumentType::VoiceNote) { thumbnail = get_photo_size(td_->file_manager_.get(), FileType::Thumbnail, 0, 0, owner_dialog_id, - std::move(document->thumb_)); + std::move(document->thumb_), has_webp_thumbnail); } } else if (remote_document.secret_file != nullptr) { CHECK(remote_document.secret_document != nullptr); diff --git a/td/telegram/Photo.cpp b/td/telegram/Photo.cpp index 099603c0..0bff8fa0 100644 --- a/td/telegram/Photo.cpp +++ b/td/telegram/Photo.cpp @@ -61,7 +61,7 @@ StringBuilder &operator<<(StringBuilder &string_builder, const Dimensions &dimen static FileId register_photo(FileManager *file_manager, FileType file_type, int64 id, int64 access_hash, tl_object_ptr &&location_ptr, DialogId owner_dialog_id, - int32 file_size) { + int32 file_size, bool is_webp = false) { int32 location_id = location_ptr->get_id(); DcId dc_id; int32 local_id; @@ -93,9 +93,11 @@ static FileId register_photo(FileManager *file_manager, FileType file_type, int6 break; } - LOG(DEBUG) << "Receive photo of type " << static_cast(file_type) << " in [" << dc_id << "," << volume_id << "," - << local_id << "]. Id: (" << id << ", " << access_hash << ")"; - auto suggested_name = PSTRING() << static_cast(volume_id) << "_" << static_cast(local_id) << ".jpg"; + LOG(DEBUG) << "Receive " << (is_webp ? "webp" : "jpeg") << " photo of type " << static_cast(file_type) + << " in [" << dc_id << "," << volume_id << "," << local_id << "]. Id: (" << id << ", " << access_hash + << ")"; + auto suggested_name = PSTRING() << static_cast(volume_id) << "_" << static_cast(local_id) + << (is_webp ? ".webp" : ".jpg"); return file_manager->register_remote( FullRemoteFileLocation(file_type, id, access_hash, local_id, volume_id, secret, dc_id), FileLocationSource::FromServer, owner_dialog_id, file_size, 0, std::move(suggested_name)); @@ -235,7 +237,7 @@ PhotoSize get_thumbnail_photo_size(FileManager *file_manager, BufferSlice bytes, } PhotoSize get_photo_size(FileManager *file_manager, FileType file_type, int64 id, int64 access_hash, - DialogId owner_dialog_id, tl_object_ptr &&size_ptr) { + DialogId owner_dialog_id, tl_object_ptr &&size_ptr, bool is_webp) { tl_object_ptr location_ptr; string type; @@ -275,8 +277,8 @@ PhotoSize get_photo_size(FileManager *file_manager, FileType file_type, int64 id break; } - res.file_id = - register_photo(file_manager, file_type, id, access_hash, std::move(location_ptr), owner_dialog_id, res.size); + res.file_id = register_photo(file_manager, file_type, id, access_hash, std::move(location_ptr), owner_dialog_id, + res.size, is_webp); if (!content.empty()) { file_manager->set_content(res.file_id, std::move(content)); @@ -468,7 +470,7 @@ Photo get_photo(FileManager *file_manager, tl_object_ptr && for (auto &size_ptr : photo->sizes_) { res.photos.push_back(get_photo_size(file_manager, FileType::Photo, photo->id_, photo->access_hash_, owner_dialog_id, - std::move(size_ptr))); + std::move(size_ptr), false)); } return res; diff --git a/td/telegram/Photo.h b/td/telegram/Photo.h index 6678ac45..638bee90 100644 --- a/td/telegram/Photo.h +++ b/td/telegram/Photo.h @@ -81,7 +81,7 @@ StringBuilder &operator<<(StringBuilder &string_builder, const DialogPhoto &dial PhotoSize get_thumbnail_photo_size(FileManager *file_manager, BufferSlice bytes, DialogId owner_dialog_id, int32 width, int32 height); PhotoSize get_photo_size(FileManager *file_manager, FileType file_type, int64 id, int64 access_hash, - DialogId owner_dialog_id, tl_object_ptr &&size_ptr); + DialogId owner_dialog_id, tl_object_ptr &&size_ptr, bool is_webp); PhotoSize get_web_document_photo_size(FileManager *file_manager, FileType file_type, DialogId owner_dialog_id, tl_object_ptr web_document_ptr); tl_object_ptr get_photo_size_object(FileManager *file_manager, const PhotoSize *photo_size); diff --git a/td/telegram/StickersManager.cpp b/td/telegram/StickersManager.cpp index 309d3ef8..de3cbff7 100644 --- a/td/telegram/StickersManager.cpp +++ b/td/telegram/StickersManager.cpp @@ -973,6 +973,14 @@ FileId StickersManager::on_get_sticker(std::unique_ptr new_sticker, boo return file_id; } +bool StickersManager::has_webp_thumbnail(const tl_object_ptr &sticker) { + if (sticker == nullptr) { + return false; + } + + return get_sticker_set_id(sticker->stickerset_) != 0; +} + std::pair StickersManager::on_get_sticker_document(tl_object_ptr &&document_ptr, bool from_message) { int32 document_constructor_id = document_ptr->get_id(); @@ -1014,8 +1022,8 @@ std::pair StickersManager::on_get_sticker_document(tl_object_ptr< FullRemoteFileLocation(FileType::Sticker, document_id, document->access_hash_, DcId::internal(document->dc_id_)), FileLocationSource::FromServer, DialogId(), document->size_, 0, to_string(document_id) + ".webp"); - PhotoSize thumbnail = - get_photo_size(td_->file_manager_.get(), FileType::Thumbnail, 0, 0, DialogId(), std::move(document->thumb_)); + PhotoSize thumbnail = get_photo_size(td_->file_manager_.get(), FileType::Thumbnail, 0, 0, DialogId(), + std::move(document->thumb_), has_webp_thumbnail(sticker)); create_sticker(sticker_id, std::move(thumbnail), dimensions, from_message, std::move(sticker), nullptr); return {document_id, sticker_id}; @@ -1059,6 +1067,23 @@ const StickersManager::StickerSet *StickersManager::get_sticker_set(int64 sticke return sticker_set->second.get(); } +int64 StickersManager::get_sticker_set_id(const tl_object_ptr &set_ptr) { + CHECK(set_ptr != nullptr); + switch (set_ptr->get_id()) { + case telegram_api::inputStickerSetEmpty::ID: + return 0; + case telegram_api::inputStickerSetID::ID: + return static_cast(set_ptr.get())->id_; + case telegram_api::inputStickerSetShortName::ID: + LOG(ERROR) << "Receive sticker set by its short name"; + return search_sticker_set(static_cast(set_ptr.get())->short_name_, + Auto()); + default: + UNREACHABLE(); + return 0; + } +} + int64 StickersManager::add_sticker_set(tl_object_ptr &&set_ptr) { CHECK(set_ptr != nullptr); switch (set_ptr->get_id()) { diff --git a/td/telegram/StickersManager.h b/td/telegram/StickersManager.h index 367c8c59..0451eefa 100644 --- a/td/telegram/StickersManager.h +++ b/td/telegram/StickersManager.h @@ -71,6 +71,10 @@ class StickersManager : public Actor { vector get_installed_sticker_sets(bool is_masks, Promise &&promise); + bool has_webp_thumbnail(const tl_object_ptr &sticker); + + int64 get_sticker_set_id(const tl_object_ptr &set_ptr); + int64 add_sticker_set(tl_object_ptr &&set_ptr); int64 get_sticker_set(int64 set_id, Promise &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index c4655287..1625f41f 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -178,8 +178,8 @@ class GetWallpapersQuery : public Td::ResultHandler { vector> sizes; sizes.reserve(wallpaper->sizes_.size()); for (auto &size_ptr : wallpaper->sizes_) { - auto photo_size = - get_photo_size(td->file_manager_.get(), FileType::Wallpaper, 0, 0, DialogId(), std::move(size_ptr)); + auto photo_size = get_photo_size(td->file_manager_.get(), FileType::Wallpaper, 0, 0, DialogId(), + std::move(size_ptr), false); sizes.push_back(get_photo_size_object(td->file_manager_.get(), &photo_size)); } sort_photo_sizes(sizes);