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/FileEncryptionKey.h"
10 #include "td/telegram/files/FileLoader.h"
11 #include "td/telegram/files/FileLocation.h"
12 #include "td/telegram/files/FileType.h"
13 
14 #include "td/utils/port/FileFd.h"
15 #include "td/utils/Status.h"
16 #include "td/utils/UInt.h"
17 
18 #include <utility>
19 
20 namespace td {
21 
22 class FileUploader final : public FileLoader {
23  public:
24   class Callback : public FileLoader::Callback {
25    public:
26     virtual void on_hash(string hash) = 0;
27     virtual void on_partial_upload(PartialRemoteFileLocation partial_remote, int64 ready_size) = 0;
28     virtual void on_ok(FileType file_type, PartialRemoteFileLocation partial_remote, int64 size) = 0;
29     virtual void on_error(Status status) = 0;
30   };
31 
32   FileUploader(const LocalFileLocation &local, const RemoteFileLocation &remote, int64 expected_size,
33                const FileEncryptionKey &encryption_key, std::vector<int> bad_parts, unique_ptr<Callback> callback);
34 
35   // Should just implement all parent pure virtual methods.
36   // Must not call any of them...
37  private:
38   ResourceState resource_state_;
39   LocalFileLocation local_;
40   RemoteFileLocation remote_;
41   int64 expected_size_;
42   FileEncryptionKey encryption_key_;
43   std::vector<int> bad_parts_;
44   unique_ptr<Callback> callback_;
45   int64 local_size_ = 0;
46   bool local_is_ready_ = false;
47   FileType file_type_ = FileType::Temp;
48 
49   std::vector<UInt256> iv_map_;
50   UInt256 iv_;
51   string generate_iv_;
52   int64 generate_offset_ = 0;
53   int64 next_offset_ = 0;
54 
55   FileFd fd_;
56   string fd_path_;
57   bool is_temp_ = false;
58   int64 file_id_ = 0;
59   bool big_flag_ = false;
60 
61   Result<FileInfo> init() final TD_WARN_UNUSED_RESULT;
62   Status on_ok(int64 size) final TD_WARN_UNUSED_RESULT;
63   void on_error(Status status) final;
64   Status before_start_parts() final;
65   void after_start_parts() final;
66   Result<std::pair<NetQueryPtr, bool>> start_part(Part part, int32 part_count,
67                                                   int64 streaming_offset) final TD_WARN_UNUSED_RESULT;
68   Result<size_t> process_part(Part part, NetQueryPtr net_query) final TD_WARN_UNUSED_RESULT;
69   void on_progress(Progress progress) final;
70   FileLoader::Callback *get_callback() final;
71   Result<PrefixInfo> on_update_local_location(const LocalFileLocation &location,
72                                               int64 file_size) final TD_WARN_UNUSED_RESULT;
73 
74   Status generate_iv_map();
75 
76   bool keep_fd_ = false;
77   void keep_fd_flag(bool keep_fd) final;
78   void try_release_fd();
79   Status acquire_fd() TD_WARN_UNUSED_RESULT;
80 };
81 
82 }  // namespace td
83