1 // 2 // Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 3 // 4 // Distributed under the Boost Software License, Version 1.0. (See accompanying 5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 // 7 #pragma once 8 9 #include "td/telegram/files/FileLoaderActor.h" 10 #include "td/telegram/files/FileLocation.h" 11 #include "td/telegram/files/ResourceManager.h" 12 13 #include "td/actor/actor.h" 14 15 #include "td/utils/BufferedFd.h" 16 #include "td/utils/crypto.h" 17 #include "td/utils/port/FileFd.h" 18 #include "td/utils/Status.h" 19 20 namespace td { 21 22 class FileHashUploader final : public FileLoaderActor { 23 public: 24 class Callback { 25 public: 26 Callback() = default; 27 Callback(const Callback &) = delete; 28 Callback &operator=(const Callback &) = delete; 29 virtual ~Callback() = default; 30 31 virtual void on_ok(FullRemoteFileLocation location) = 0; 32 virtual void on_error(Status status) = 0; 33 }; 34 FileHashUploader(const FullLocalFileLocation & local,int64 size,unique_ptr<Callback> callback)35 FileHashUploader(const FullLocalFileLocation &local, int64 size, unique_ptr<Callback> callback) 36 : local_(local), size_(size), size_left_(size), callback_(std::move(callback)) { 37 } 38 set_resource_manager(ActorShared<ResourceManager> resource_manager)39 void set_resource_manager(ActorShared<ResourceManager> resource_manager) final { 40 resource_manager_ = std::move(resource_manager); 41 send_closure(resource_manager_, &ResourceManager::update_resources, resource_state_); 42 } 43 update_priority(int8 priority)44 void update_priority(int8 priority) final { 45 send_closure(resource_manager_, &ResourceManager::update_priority, priority); 46 } update_resources(const ResourceState & other)47 void update_resources(const ResourceState &other) final { 48 if (stop_flag_) { 49 return; 50 } 51 resource_state_.update_slave(other); 52 loop(); 53 } 54 55 private: 56 ResourceState resource_state_; 57 BufferedFd<FileFd> fd_; 58 59 FullLocalFileLocation local_; 60 int64 size_; 61 int64 size_left_; 62 unique_ptr<Callback> callback_; 63 64 ActorShared<ResourceManager> resource_manager_; 65 66 enum class State : int32 { CalcSha, NetRequest, WaitNetResult } state_ = State::CalcSha; 67 bool stop_flag_ = false; 68 Sha256State sha256_state_; 69 70 void start_up() final; 71 Status init(); 72 73 void loop() final; 74 75 Status loop_impl(); 76 77 Status loop_sha(); 78 79 void on_result(NetQueryPtr net_query) final; 80 81 Status on_result_impl(NetQueryPtr net_query); 82 }; 83 84 } // namespace td 85