From 3a275827e9aea7dd21b5f4605d9e9118b409c5a2 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 28 Jul 2022 15:59:57 +0300 Subject: [PATCH] Add FileManager::is_remotely_generated_file and improve some names. --- td/telegram/files/FileGenerateManager.cpp | 23 ++++++++++++----------- td/telegram/files/FileManager.cpp | 21 +++++++++++++-------- td/telegram/files/FileManager.h | 6 ++++-- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/td/telegram/files/FileGenerateManager.cpp b/td/telegram/files/FileGenerateManager.cpp index 57e18a866..6b37f8334 100644 --- a/td/telegram/files/FileGenerateManager.cpp +++ b/td/telegram/files/FileGenerateManager.cpp @@ -113,9 +113,9 @@ class FileDownloadGenerateActor final : public FileGenerateActor { } }; -class MapDownloadGenerateActor final : public FileGenerateActor { +class WebFileDownloadGenerateActor final : public FileGenerateActor { public: - MapDownloadGenerateActor(string conversion, unique_ptr callback, ActorShared<> parent) + WebFileDownloadGenerateActor(string conversion, unique_ptr callback, ActorShared<> parent) : conversion_(std::move(conversion)), callback_(std::move(callback)), parent_(std::move(parent)) { } void file_generate_progress(int64 expected_size, int64 local_prefix_size, Promise<> promise) final { @@ -132,18 +132,18 @@ class MapDownloadGenerateActor final : public FileGenerateActor { string file_name_; class Callback final : public NetQueryCallback { - ActorId parent_; + ActorId parent_; public: - explicit Callback(ActorId parent) : parent_(parent) { + explicit Callback(ActorId parent) : parent_(parent) { } void on_result(NetQueryPtr query) final { - send_closure(parent_, &MapDownloadGenerateActor::on_result, std::move(query)); + send_closure(parent_, &WebFileDownloadGenerateActor::on_result, std::move(query)); } void hangup_shared() final { - send_closure(parent_, &MapDownloadGenerateActor::hangup_shared); + send_closure(parent_, &WebFileDownloadGenerateActor::hangup_shared); } }; ActorOwn net_callback_; @@ -197,7 +197,7 @@ class MapDownloadGenerateActor final : public FileGenerateActor { return on_error(r_input_web_file.move_as_error()); } - net_callback_ = create_actor("MapDownloadGenerateCallback", actor_id(this)); + net_callback_ = create_actor("WebFileDownloadGenerateCallback", actor_id(this)); LOG(INFO) << "Download " << conversion_; auto query = @@ -220,7 +220,7 @@ class MapDownloadGenerateActor final : public FileGenerateActor { TRY_RESULT(web_file, fetch_result(std::move(query))); if (static_cast(web_file->size_) != web_file->bytes_.size()) { - LOG(ERROR) << "Failed to download map of size " << web_file->size_; + LOG(ERROR) << "Failed to download web file of size " << web_file->size_; return Status::Error("File is too big"); } @@ -418,9 +418,10 @@ void FileGenerateManager::generate_file(uint64 query_id, FullGenerateFileLocatio auto file_id = FileId(to_integer(conversion.substr(file_id_query.size())), 0); query.worker_ = create_actor("FileDownloadGenerateActor", generate_location.file_type_, file_id, std::move(callback), std::move(parent)); - } else if (begins_with(conversion, "#map#") && generate_location.original_path_.empty()) { - query.worker_ = create_actor( - "MapDownloadGenerateActor", std::move(generate_location.conversion_), std::move(callback), std::move(parent)); + } else if (FileManager::is_remotely_generated_file(conversion) && generate_location.original_path_.empty()) { + query.worker_ = create_actor("WebFileDownloadGenerateActor", + std::move(generate_location.conversion_), + std::move(callback), std::move(parent)); } else { query.worker_ = create_actor("FileExternalGenerationActor", query_id, generate_location, local_location, std::move(name), std::move(callback), diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index b47d2777b..a4ba39d22 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -771,7 +771,7 @@ string FileView::get_persistent_id(const FullGenerateFileLocation &location) { auto binary = serialize(location); binary = zero_encode(binary); - binary.push_back(FileNode::PERSISTENT_ID_VERSION_MAP); + binary.push_back(FileNode::PERSISTENT_ID_VERSION_GENERATED); return base64url_encode(binary); } @@ -790,7 +790,7 @@ string FileView::get_persistent_file_id() const { return get_persistent_id(remote_location()); } else if (has_url()) { return url(); - } else if (has_generate_location() && begins_with(generate_location().conversion_, "#map#")) { + } else if (has_generate_location() && FileManager::is_remotely_generated_file(generate_location().conversion_)) { return get_persistent_id(generate_location()); } } @@ -803,7 +803,7 @@ string FileView::get_unique_file_id() const { if (!remote_location().is_web()) { return get_unique_id(remote_location()); } - } else if (has_generate_location() && begins_with(generate_location().conversion_, "#map#")) { + } else if (has_generate_location() && FileManager::is_remotely_generated_file(generate_location().conversion_)) { return get_unique_id(generate_location()); } } @@ -931,6 +931,10 @@ bool FileManager::are_modification_times_equal(int64 old_mtime, int64 new_mtime) return false; } +bool FileManager::is_remotely_generated_file(Slice conversion) { + return begins_with(conversion, "#map#"); +} + Status FileManager::check_local_location(FullLocalFileLocation &location, int64 &size, bool skip_file_size_checks) { constexpr int64 MAX_THUMBNAIL_SIZE = 200 * (1 << 10) - 1 /* 200 KB - 1 B */; constexpr int64 MAX_PHOTO_SIZE = 10 * (1 << 20) /* 10 MB */; @@ -2917,13 +2921,13 @@ Result FileManager::from_persistent_id(CSlice persistent_id, FileType fi if (binary.back() == FileNode::PERSISTENT_ID_VERSION) { return from_persistent_id_v3(binary, file_type); } - if (binary.back() == FileNode::PERSISTENT_ID_VERSION_MAP) { - return from_persistent_id_map(binary, file_type); + if (binary.back() == FileNode::PERSISTENT_ID_VERSION_GENERATED) { + return from_persistent_id_generated(binary, file_type); } return Status::Error(400, "Wrong remote file identifier specified: can't unserialize it. Wrong last symbol"); } -Result FileManager::from_persistent_id_map(Slice binary, FileType file_type) { +Result FileManager::from_persistent_id_generated(Slice binary, FileType file_type) { binary.remove_suffix(1); auto decoded_binary = zero_decode(binary); FullGenerateFileLocation generate_location; @@ -2936,12 +2940,13 @@ Result FileManager::from_persistent_id_map(Slice binary, FileType file_t (real_file_type != FileType::Thumbnail && real_file_type != FileType::EncryptedThumbnail)) { return Status::Error(400, "Type of file mismatch"); } - if (!begins_with(generate_location.conversion_, "#map#")) { + if (!is_remotely_generated_file(generate_location.conversion_)) { return Status::Error(400, "Unexpected conversion type"); } FileData data; data.generate_ = make_unique(std::move(generate_location)); - return register_file(std::move(data), FileLocationSource::FromUser, "from_persistent_id_map", false).move_as_ok(); + return register_file(std::move(data), FileLocationSource::FromUser, "from_persistent_id_generated", false) + .move_as_ok(); } Result FileManager::from_persistent_id_v23(Slice binary, FileType file_type, int32 version) { diff --git a/td/telegram/files/FileManager.h b/td/telegram/files/FileManager.h index eb2f00a38..0190539f9 100644 --- a/td/telegram/files/FileManager.h +++ b/td/telegram/files/FileManager.h @@ -126,7 +126,7 @@ class FileNode { friend class FileManager; static constexpr char PERSISTENT_ID_VERSION_OLD = 2; - static constexpr char PERSISTENT_ID_VERSION_MAP = 3; + static constexpr char PERSISTENT_ID_VERSION_GENERATED = 3; static constexpr char PERSISTENT_ID_VERSION = 4; LocalFileLocation local_; @@ -417,6 +417,8 @@ class FileManager final : public FileLoadManager::Callback { static bool are_modification_times_equal(int64 old_mtime, int64 new_mtime); + static bool is_remotely_generated_file(Slice conversion); + void init_actor(); FileId dup_file_id(FileId file_id); @@ -620,7 +622,7 @@ class FileManager final : public FileLoadManager::Callback { void flush_to_pmc(FileNodePtr node, bool new_remote, bool new_local, bool new_generate, const char *source); void load_from_pmc(FileNodePtr node, bool new_remote, bool new_local, bool new_generate); - Result from_persistent_id_map(Slice binary, FileType file_type); + Result from_persistent_id_generated(Slice binary, FileType file_type); Result from_persistent_id_v2(Slice binary, FileType file_type); Result from_persistent_id_v3(Slice binary, FileType file_type); Result from_persistent_id_v23(Slice binary, FileType file_type, int32 version);