From d26cf8f8a3b38bbecf116a399fa7937302fdccf6 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 24 Apr 2020 00:50:14 +0300 Subject: [PATCH] Add temporary "upload by hash" for photos. GitOrigin-RevId: f2fca54bce80d3eb9a84a7f4f6326e98633417a5 --- td/telegram/files/FileManager.cpp | 21 ++++++++++++++++++++- td/telegram/files/FileManager.h | 2 ++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index 777f7f240..a0ed6213a 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -25,6 +25,7 @@ #include "td/actor/SleepActor.h" #include "td/utils/base64.h" +#include "td/utils/filesystem.h" #include "td/utils/format.h" #include "td/utils/HttpUrl.h" #include "td/utils/logging.h" @@ -3025,7 +3026,25 @@ Result FileManager::get_input_file_id(FileType type, const tl_object_ptr if (allow_zero && path.empty()) { return FileId(); } - return register_local(FullLocalFileLocation(new_type, path, 0), owner_dialog_id, 0, get_by_hash); + string hash; + if (false && new_type == FileType::Photo) { + auto r_stat = stat(path); + if (r_stat.is_ok() && r_stat.ok().size_ > 0 && r_stat.ok().size_ < 1000000) { + auto r_file_content = read_file_str(path, r_stat.ok().size_); + if (r_file_content.is_ok()) { + hash = sha256(r_file_content.ok()); + auto it = file_hash_to_file_id_.find(hash); + if (it != file_hash_to_file_id_.end()) { + return it->second; + } + } + } + } + TRY_RESULT(file_id, register_local(FullLocalFileLocation(new_type, path, 0), owner_dialog_id, 0, get_by_hash)); + if (!hash.empty()) { + file_hash_to_file_id_[hash] = file_id; + } + return file_id; } case td_api::inputFileId::ID: { FileId file_id(static_cast(file.get())->id_, 0); diff --git a/td/telegram/files/FileManager.h b/td/telegram/files/FileManager.h index d6da57540..fe9a859db 100644 --- a/td/telegram/files/FileManager.h +++ b/td/telegram/files/FileManager.h @@ -546,6 +546,8 @@ class FileManager : public FileLoadManager::Callback { }; Enumerator remote_location_info_; + std::unordered_map file_hash_to_file_id_; + std::map local_location_to_file_id_; std::map generate_location_to_file_id_; std::map pmc_id_to_file_node_id_;