From 8a80571903a78debedda10ab6c94579dca8f1e13 Mon Sep 17 00:00:00 2001 From: Arseny Smirnov Date: Thu, 31 Jan 2019 12:33:35 +0400 Subject: [PATCH] Files: make all file references encoded with FileReferenceView GitOrigin-RevId: c876f0d115035cb0e9030b6a4124c31c53a191b2 --- td/telegram/MessageContent.cpp | 4 ++-- td/telegram/files/FileLocation.h | 29 +++++++++++++++++------------ td/telegram/files/FileManager.cpp | 15 ++++++++------- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index cf775c70..dd903088 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -2122,7 +2122,7 @@ tl_object_ptr get_input_media(const MessageContent *co } if (!was_uploaded) { auto file_reference = FileManager::extract_file_reference(input_media); - if (!FileReferenceView(file_reference).has_upload()) { + if (file_reference == FileReferenceView::invalid_file_reference()) { return nullptr; } } @@ -2132,7 +2132,7 @@ tl_object_ptr get_input_media(const MessageContent *co tl_object_ptr get_input_media(const MessageContent *content, Td *td, int32 ttl) { auto input_media = get_input_media(content, td, nullptr, nullptr, ttl); auto file_reference = FileManager::extract_file_reference(input_media); - if (!FileReferenceView(file_reference).has_upload()) { + if (file_reference == FileReferenceView::invalid_file_reference()) { return nullptr; } return input_media; diff --git a/td/telegram/files/FileLocation.h b/td/telegram/files/FileLocation.h index 51beb26c..3b489a3b 100644 --- a/td/telegram/files/FileLocation.h +++ b/td/telegram/files/FileLocation.h @@ -29,14 +29,23 @@ namespace td { class FileReferenceView { public: + static Slice invalid_file_reference() { + return Slice("#"); + } static std::string create_invalid() { return create_one(invalid_file_reference()); } static std::string create_one(Slice first) { + if (first.empty()) { + return {}; + } unsigned char second_length = 255; return PSTRING() << static_cast(second_length) << first; } static std::string create_two(Slice first, Slice second) { + if (first.empty() && second.empty()) { + return {}; + } if (second.size() >= 255) { LOG(ERROR) << "File reference is too big " << base64_encode(second); second = invalid_file_reference(); @@ -99,10 +108,6 @@ class FileReferenceView { } private: - static Slice invalid_file_reference() { - return Slice("#"); - } - std::string create(Slice first, Slice second) const { if (size_ == 1) { return create_one(first); @@ -112,7 +117,7 @@ class FileReferenceView { Slice first_; Slice second_; - int size_ = 1; + int size_{1}; }; struct EmptyRemoteFileLocation { @@ -495,8 +500,6 @@ class FullRemoteFileLocation { auto res = FileReferenceView(file_reference_).delete_file_reference(bad_file_reference); if (res.second) { file_reference_ = res.first; - } else if (file_reference_ == bad_file_reference) { - file_reference_ = FileReferenceView::create_invalid(); } return res.second; } @@ -570,8 +573,8 @@ class FullRemoteFileLocation { } else if (is_secure()) { return make_tl_object(common().id_, common().access_hash_); } else { - return make_tl_object(common().id_, common().access_hash_, - BufferSlice(file_reference_)); + return make_tl_object( + common().id_, common().access_hash_, BufferSlice(FileReferenceView(file_reference_).download())); } case LocationType::Web: case LocationType::None: @@ -586,7 +589,7 @@ class FullRemoteFileLocation { CHECK(is_common()) << file << ' ' << line; CHECK(is_document()) << file << ' ' << line; return make_tl_object(common().id_, common().access_hash_, - BufferSlice(file_reference_)); + BufferSlice(FileReferenceView(file_reference_).upload())); } #define as_input_photo() as_input_photo_impl(__FILE__, __LINE__) @@ -625,7 +628,7 @@ class FullRemoteFileLocation { FullRemoteFileLocation(FileType file_type, int64 id, int64 access_hash, DcId dc_id, std::string file_reference) : file_type_(file_type) , dc_id_(dc_id) - , file_reference_(std::move(file_reference)) + , file_reference_(FileReferenceView::create_one(file_reference)) , variant_(CommonRemoteFileLocation{id, access_hash}) { CHECK(is_common()); FileReferenceView view(file_reference_); @@ -694,7 +697,9 @@ inline StringBuilder &operator<<(StringBuilder &string_builder, string_builder << ", " << full_remote_file_location.get_dc_id(); } if (!full_remote_file_location.file_reference_.empty()) { - string_builder << ", " << tag("file_reference", base64_encode(full_remote_file_location.file_reference_)); + FileReferenceView view(full_remote_file_location.file_reference_); + string_builder << ", " << tag("file_reference_upload", base64_encode(view.upload())) + << tag("file_reference_download", base64_encode(view.download())); } string_builder << ", location = "; diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index 9c83366c..4144a10a 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -167,14 +167,15 @@ bool FileNode::delete_file_reference(Slice file_reference) { return false; } - if (remote_.full().delete_file_reference(file_reference)) { - VLOG(file_references) << "Do delete file reference of main file " << main_file_id_; - upload_was_update_file_reference_ = false; - download_was_update_file_reference_ = false; - on_pmc_changed(); - return true; + if (!remote_.full().delete_file_reference(file_reference)) { + return false; } - return false; + + VLOG(file_references) << "Do delete file reference of main file " << main_file_id_; + upload_was_update_file_reference_ = false; + download_was_update_file_reference_ = false; + on_pmc_changed(); + return true; } void FileNode::set_generate_location(unique_ptr &&generate) {