From d768a194f6e2fe2a74b9c10cab572e859e62bd96 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 9 Jun 2021 20:58:39 +0300 Subject: [PATCH] Add BackgroundType::get_mime_type. --- td/telegram/BackgroundManager.cpp | 6 ++---- td/telegram/BackgroundType.cpp | 32 ++++++++++++++++++------------- td/telegram/BackgroundType.h | 2 ++ 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/td/telegram/BackgroundManager.cpp b/td/telegram/BackgroundManager.cpp index 1ecc8be1f..0b1c5c732 100644 --- a/td/telegram/BackgroundManager.cpp +++ b/td/telegram/BackgroundManager.cpp @@ -144,9 +144,8 @@ class UploadBackgroundQuery : public Td::ResultHandler { file_id_ = file_id; type_ = type; for_dark_theme_ = for_dark_theme; - string mime_type = type.type == BackgroundType::Type::Pattern ? "image/png" : "image/jpeg"; - send_query(G()->net_query_creator().create( - telegram_api::account_uploadWallPaper(std::move(input_file), mime_type, get_input_wallpaper_settings(type)))); + send_query(G()->net_query_creator().create(telegram_api::account_uploadWallPaper( + std::move(input_file), type_.get_mime_type(), get_input_wallpaper_settings(type)))); } void on_result(uint64 id, BufferSlice packet) override { @@ -552,7 +551,6 @@ BackgroundId BackgroundManager::set_background(const td_api::InputBackground *in return BackgroundId(); } - CHECK(type.type == BackgroundType::Type::Fill); auto background_id = add_fill_background(type.fill); set_background_id(background_id, type, for_dark_theme); promise.set_value(Unit()); diff --git a/td/telegram/BackgroundType.cpp b/td/telegram/BackgroundType.cpp index e9d471ca5..f54b94a7c 100644 --- a/td/telegram/BackgroundType.cpp +++ b/td/telegram/BackgroundType.cpp @@ -258,6 +258,11 @@ bool operator==(const BackgroundFill &lhs, const BackgroundFill &rhs) { lhs.fourth_color == rhs.fourth_color; } +string BackgroundType::get_mime_type() const { + CHECK(has_file()); + return type == Type::Pattern ? "image/png" : "image/jpeg"; +} + void BackgroundType::apply_parameters_from_link(Slice name) { const auto query = parse_url_query(name); @@ -307,13 +312,13 @@ string BackgroundType::get_link() const { } switch (type) { - case BackgroundType::Type::Wallpaper: { + case Type::Wallpaper: { if (!mode.empty()) { return PSTRING() << "mode=" << mode; } return string(); } - case BackgroundType::Type::Pattern: { + case Type::Pattern: { string link = PSTRING() << "intensity=" << intensity << "&bg_color=" << get_background_fill_color_hex_string(fill, false); if (!mode.empty()) { @@ -322,7 +327,7 @@ string BackgroundType::get_link() const { } return link; } - case BackgroundType::Type::Fill: + case Type::Fill: return get_background_fill_color_hex_string(fill, true); default: UNREACHABLE(); @@ -335,22 +340,23 @@ bool operator==(const BackgroundType &lhs, const BackgroundType &rhs) { lhs.intensity == rhs.intensity && lhs.fill == rhs.fill; } -static StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundType::Type &type) { - switch (type) { +StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundType &type) { + string_builder << "type "; + switch (type.type) { case BackgroundType::Type::Wallpaper: - return string_builder << "Wallpaper"; + string_builder << "Wallpaper"; + break; case BackgroundType::Type::Pattern: - return string_builder << "Pattern"; + string_builder << "Pattern"; + break; case BackgroundType::Type::Fill: - return string_builder << "Fill"; + string_builder << "Fill"; + break; default: UNREACHABLE(); - return string_builder; + break; } -} - -StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundType &type) { - return string_builder << "type " << type.type << '[' << type.get_link() << ']'; + return string_builder << '[' << type.get_link() << ']'; } Result get_background_type(const td_api::BackgroundType *type) { diff --git a/td/telegram/BackgroundType.h b/td/telegram/BackgroundType.h index c470cde3e..702335afc 100644 --- a/td/telegram/BackgroundType.h +++ b/td/telegram/BackgroundType.h @@ -79,6 +79,8 @@ struct BackgroundType { return type == Type::Wallpaper || type == Type::Pattern; } + string get_mime_type() const; + void apply_parameters_from_link(Slice name); string get_link() const;