diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 885df67b6..ef361283f 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -15334,15 +15334,9 @@ Result MessagesManager::process_input_mess file_id = r_file_id.ok(); CHECK(file_id.is_valid()); file_view = td_->file_manager_->get_file_view(file_id); - if (file_view.has_local_location()) { - const PathView path_view(file_view.local_location().path_); - file_name = path_view.file_name().str(); - mime_type = MimeType::from_extension(path_view.extension()); - } else if (file_view.has_generate_location()) { - const PathView path_view(file_view.generate_location().original_path_); - file_name = path_view.file_name().str(); - mime_type = MimeType::from_extension(path_view.extension()); - } + const PathView path_view(file_view.suggested_name()); + file_name = path_view.file_name().str(); + mime_type = MimeType::from_extension(path_view.extension()); } FileId thumbnail_file_id; diff --git a/td/telegram/files/FileLocation.h b/td/telegram/files/FileLocation.h index 2cb69f8d6..7733b65d2 100644 --- a/td/telegram/files/FileLocation.h +++ b/td/telegram/files/FileLocation.h @@ -903,6 +903,18 @@ class LocalFileLocation { return variant_.get<2>(); } + CSlice file_name() const { + switch (type()) { + case Type::Partial: + return partial().path_; + case Type::Full: + return full().path_; + case Type::Empty: + default: + return CSlice(); + } + } + template void store(StorerT &storer) const { using td::store; diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index 70454e602..f0be80f81 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -35,21 +35,26 @@ namespace td { static int VERBOSITY_NAME(update_file) = VERBOSITY_NAME(INFO); + FileNode *FileNodePtr::operator->() const { return get(); } + FileNode &FileNodePtr::operator*() const { return *get(); } + FileNode *FileNodePtr::get() const { auto res = get_unsafe(); CHECK(res); return res; } + FileNode *FileNodePtr::get_unsafe() const { CHECK(file_manager_ != nullptr); return file_manager_->get_file_node_raw(file_id_); } + FileNodePtr::operator bool() const { return file_manager_ != nullptr && get_unsafe() != nullptr; } @@ -227,6 +232,26 @@ void FileNode::on_info_flushed() { info_changed_flag_ = false; } +CSlice FileNode::suggested_name() const { + if (!remote_name_.empty()) { + return remote_name_; + } + CSlice local_name(local_.file_name()); + if (!local_name.empty()) { + return local_name; + } + if (!url_.empty()) { + return url_; + } + if (generate_ != nullptr) { + CSlice generate_name(generate_->original_path_); + if (!generate_name.empty()) { + return generate_name; + } + } + return CSlice(); +} + /*** FileView ***/ bool FileView::has_local_location() const { return node_->local_.type() == LocalFileLocation::Type::Full; @@ -335,6 +360,10 @@ const string &FileView::remote_name() const { return node_->remote_name_; } +CSlice FileView::suggested_name() const { + return node_->suggested_name(); +} + DialogId FileView::owner_dialog_id() const { return node_->owner_dialog_id_; } @@ -1273,7 +1302,7 @@ bool FileManager::set_content(FileId file_id, BufferSlice bytes) { node->download_id_ = id; node->is_download_started_ = true; send_closure(file_load_manager_, &FileLoadManager::from_bytes, id, node->remote_.full().file_type_, std::move(bytes), - node->remote_name_); + node->suggested_name().str()); return true; } @@ -1423,7 +1452,7 @@ void FileManager::run_download(FileNodePtr node) { node->download_id_ = id; node->is_download_started_ = false; send_closure(file_load_manager_, &FileLoadManager::download, id, node->remote_.full(), node->local_, node->size_, - node->remote_name_, node->encryption_key_, priority); + node->suggested_name().str(), node->encryption_key_, priority); } void FileManager::resume_upload(FileId file_id, std::vector bad_parts, std::shared_ptr callback, @@ -1566,7 +1595,7 @@ void FileManager::run_generate(FileNodePtr node) { QueryId id = queries_container_.create(Query{file_id, Query::Generate}); node->generate_id_ = id; send_closure(file_generate_manager_, &FileGenerateManager::generate_file, id, *node->generate_, node->local_, - node->remote_name_, [file_manager = this, id] { + node->suggested_name().str(), [file_manager = this, id] { class Callback : public FileGenerateCallback { ActorId actor_; uint64 query_id_; diff --git a/td/telegram/files/FileManager.h b/td/telegram/files/FileManager.h index c46ecfb19..3466d9183 100644 --- a/td/telegram/files/FileManager.h +++ b/td/telegram/files/FileManager.h @@ -73,6 +73,8 @@ class FileNode { void on_pmc_flushed(); void on_info_flushed(); + CSlice suggested_name() const; + private: friend class FileView; friend class FileManager; @@ -181,6 +183,8 @@ class FileView { const string &remote_name() const; + CSlice suggested_name() const; + DialogId owner_dialog_id() const; bool get_by_hash() const; diff --git a/td/telegram/files/FileManager.hpp b/td/telegram/files/FileManager.hpp index 4d0c6923a..69c820eac 100644 --- a/td/telegram/files/FileManager.hpp +++ b/td/telegram/files/FileManager.hpp @@ -28,7 +28,7 @@ void FileManager::store_file(FileId file_id, StorerT &storer, int32 ttl) const { file_store_type = FileStoreType::Remote; } else if (file_view.has_local_location()) { file_store_type = FileStoreType::Local; - } else if (!file_view.url().empty()) { + } else if (file_view.has_url()) { file_store_type = FileStoreType::Url; } else if (file_view.has_generate_location()) { file_store_type = FileStoreType::Generate;