From 2be350b8fe875e7e0680adcf65435b9c2ff59dc9 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 5 Aug 2022 22:43:53 +0300 Subject: [PATCH] Support multiple sizes for audio album covers. --- td/generate/scheme/td_api.tl | 4 ++-- td/generate/scheme/telegram_api.tl | 2 +- td/telegram/AudiosManager.cpp | 20 ++++++++++++++------ td/telegram/InlineQueriesManager.cpp | 6 +++++- td/telegram/files/FileGenerateManager.cpp | 18 +++++++++++++----- td/telegram/files/FileManager.cpp | 5 +++-- td/telegram/files/FileManager.h | 2 +- 7 files changed, 39 insertions(+), 18 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index b53a204b2..12384f7ac 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -293,8 +293,8 @@ animation duration:int32 width:int32 height:int32 file_name:string mime_type:str //@description Describes an audio file. Audio is usually in MP3 or M4A format @duration Duration of the audio, in seconds; as defined by the sender @title Title of the audio; as defined by the sender @performer Performer of the audio; as defined by the sender //@file_name Original name of the file; as defined by the sender @mime_type The MIME type of the file; as defined by the sender @album_cover_minithumbnail The minithumbnail of the album cover; may be null //@album_cover_thumbnail The thumbnail of the album cover in JPEG format; as defined by the sender. The full size thumbnail is supposed to be extracted from the downloaded audio file; may be null -//@album_cover Album cover to use if the downloaded audio file have no album cover; may be null. Downloading of this file is expected to fail if album cover is unknown @audio File containing the audio -audio duration:int32 title:string performer:string file_name:string mime_type:string album_cover_minithumbnail:minithumbnail album_cover_thumbnail:thumbnail album_cover:file audio:file = Audio; +//@external_album_covers Album cover variants to use if the downloaded audio file contains no album cover. Provided thumbnail dimensions are approximate @audio File containing the audio +audio duration:int32 title:string performer:string file_name:string mime_type:string album_cover_minithumbnail:minithumbnail album_cover_thumbnail:thumbnail external_album_covers:vector audio:file = Audio; //@description Describes a document of any type @file_name Original name of the file; as defined by the sender @mime_type MIME type of the file; as defined by the sender //@minithumbnail Document minithumbnail; may be null @thumbnail Document thumbnail in JPEG or PNG format (PNG will be used only for background patterns); as defined by the sender; may be null @document File containing the document diff --git a/td/generate/scheme/telegram_api.tl b/td/generate/scheme/telegram_api.tl index be109e015..68ad20578 100644 --- a/td/generate/scheme/telegram_api.tl +++ b/td/generate/scheme/telegram_api.tl @@ -841,7 +841,7 @@ inputWebDocument#9bed434d url:string size:int mime_type:string attributes:Vector inputWebFileLocation#c239d686 url:string access_hash:long = InputWebFileLocation; inputWebFileGeoPointLocation#9f2221c9 geo_point:InputGeoPoint access_hash:long w:int h:int zoom:int scale:int = InputWebFileLocation; -inputWebFileAudioAlbumThumbLocation#f46fe924 flags:# document:flags.0?InputDocument title:flags.1?string performer:flags.1?string = InputWebFileLocation; +inputWebFileAudioAlbumThumbLocation#f46fe924 flags:# small:flags.2?true document:flags.0?InputDocument title:flags.1?string performer:flags.1?string = InputWebFileLocation; upload.webFile#21e753bc size:int mime_type:string file_type:storage.FileType mtime:int bytes:bytes = upload.WebFile; diff --git a/td/telegram/AudiosManager.cpp b/td/telegram/AudiosManager.cpp index 7a3fb6de5..fab4ac187 100644 --- a/td/telegram/AudiosManager.cpp +++ b/td/telegram/AudiosManager.cpp @@ -44,17 +44,25 @@ tl_object_ptr AudiosManager::get_audio_object(FileId file_id) con auto audio = get_audio(file_id); CHECK(audio != nullptr); - td_api::object_ptr album_cover_file; + vector> album_covers; if (!td_->auth_manager_->is_bot()) { - auto r_file_id = td_->file_manager_->get_audio_thumbnail_file_id(audio->title, audio->performer, DialogId()); - if (r_file_id.is_ok()) { - album_cover_file = td_->file_manager_->get_file_object(r_file_id.move_as_ok()); - } + auto add_album_cover = [&](bool is_small, int32 width, int32 height) { + auto r_file_id = + td_->file_manager_->get_audio_thumbnail_file_id(audio->title, audio->performer, is_small, DialogId()); + if (r_file_id.is_ok()) { + album_covers.emplace_back( + td_api::make_object(td_api::make_object(), width, height, + td_->file_manager_->get_file_object(r_file_id.move_as_ok()))); + } + }; + + add_album_cover(true, 100, 100); + add_album_cover(false, 600, 600); } return make_tl_object( audio->duration, audio->title, audio->performer, audio->file_name, audio->mime_type, get_minithumbnail_object(audio->minithumbnail), - get_thumbnail_object(td_->file_manager_.get(), audio->thumbnail, PhotoFormat::Jpeg), std::move(album_cover_file), + get_thumbnail_object(td_->file_manager_.get(), audio->thumbnail, PhotoFormat::Jpeg), std::move(album_covers), td_->file_manager_->get_file_object(file_id)); } diff --git a/td/telegram/InlineQueriesManager.cpp b/td/telegram/InlineQueriesManager.cpp index 4c12dca60..474b7215f 100644 --- a/td/telegram/InlineQueriesManager.cpp +++ b/td/telegram/InlineQueriesManager.cpp @@ -1124,6 +1124,10 @@ tl_object_ptr copy(const td_api::thumbnail &obj) { return td_api::make_object(std::move(format), obj.width_, obj.height_, copy(obj.file_)); } +static tl_object_ptr copy_thumbnail(const tl_object_ptr &obj) { + return copy(obj); +} + template <> tl_object_ptr copy(const td_api::StickerFormat &obj) { switch (obj.get_id()) { @@ -1245,7 +1249,7 @@ template <> tl_object_ptr copy(const td_api::audio &obj) { return td_api::make_object(obj.duration_, obj.title_, obj.performer_, obj.file_name_, obj.mime_type_, copy(obj.album_cover_minithumbnail_), copy(obj.album_cover_thumbnail_), - copy(obj.album_cover_), copy(obj.audio_)); + transform(obj.external_album_covers_, copy_thumbnail), copy(obj.audio_)); } template <> diff --git a/td/telegram/files/FileGenerateManager.cpp b/td/telegram/files/FileGenerateManager.cpp index 6b8edba10..00f7e1507 100644 --- a/td/telegram/files/FileGenerateManager.cpp +++ b/td/telegram/files/FileGenerateManager.cpp @@ -154,17 +154,25 @@ class WebFileDownloadGenerateActor final : public FileGenerateActor { return Status::Error("Wrong conversion"); } - if (parts.size() == 5 && parts[1] == "audio_t") { + if (parts.size() == 6 && parts[1] == "audio_t") { // music thumbnail if (parts[2].empty() && parts[3].empty()) { return Status::Error("Title or performer must be non-empty"); } + if (parts[4] != "0" && parts[4] != "1") { + return Status::Error("Invalid conversion"); + } - file_name_ = PSTRING() << "Album cover for " << parts[3] << " - " << parts[2] << ".jpg"; + bool is_small = parts[4][0] == '1'; + file_name_ = PSTRING() << "Album cover " << (is_small ? "thumbnail " : "") << "for " << parts[3] << " - " + << parts[2] << ".jpg"; - auto flags = telegram_api::inputWebFileAudioAlbumThumbLocation::TITLE_MASK; - return make_tl_object(flags, nullptr, parts[2].str(), - parts[3].str()); + int32 flags = telegram_api::inputWebFileAudioAlbumThumbLocation::TITLE_MASK; + if (is_small) { + flags |= telegram_api::inputWebFileAudioAlbumThumbLocation::SMALL_MASK; + } + return make_tl_object(flags, false /*ignored*/, nullptr, + parts[2].str(), parts[3].str()); } if (parts.size() != 9 || parts[1] != "map") { diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index ec8e1a5c7..319ce2c9e 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -3251,7 +3251,8 @@ Result FileManager::get_map_thumbnail_file_id(Location location, int32 z FileLocationSource::FromUser, string(), std::move(conversion), owner_dialog_id, 0); } -Result FileManager::get_audio_thumbnail_file_id(string title, string performer, DialogId owner_dialog_id) { +Result FileManager::get_audio_thumbnail_file_id(string title, string performer, bool is_small, + DialogId owner_dialog_id) { if (!clean_input_string(title)) { return Status::Error(400, "Title must be encoded in UTF-8"); } @@ -3274,7 +3275,7 @@ Result FileManager::get_audio_thumbnail_file_id(string title, string per return Status::Error(400, "Title or performer must be non-empty"); } - string conversion = PSTRING() << "#audio_t#" << title << '#' << performer << '#'; + string conversion = PSTRING() << "#audio_t#" << title << '#' << performer << '#' << (is_small ? '1' : '0') << '#'; return register_generate( owner_dialog_id.get_type() == DialogType::SecretChat ? FileType::EncryptedThumbnail : FileType::Thumbnail, FileLocationSource::FromUser, string(), std::move(conversion), owner_dialog_id, 0); diff --git a/td/telegram/files/FileManager.h b/td/telegram/files/FileManager.h index 5fc285b64..3dab218e5 100644 --- a/td/telegram/files/FileManager.h +++ b/td/telegram/files/FileManager.h @@ -491,7 +491,7 @@ class FileManager final : public FileLoadManager::Callback { Result get_map_thumbnail_file_id(Location location, int32 zoom, int32 width, int32 height, int32 scale, DialogId owner_dialog_id) TD_WARN_UNUSED_RESULT; - Result get_audio_thumbnail_file_id(string title, string performer, + Result get_audio_thumbnail_file_id(string title, string performer, bool is_small, DialogId owner_dialog_id) TD_WARN_UNUSED_RESULT; FileType guess_file_type(const tl_object_ptr &file);