diff --git a/td/telegram/Requests.cpp b/td/telegram/Requests.cpp index 18a15879b..c7360d828 100644 --- a/td/telegram/Requests.cpp +++ b/td/telegram/Requests.cpp @@ -1920,32 +1920,8 @@ class Requests::DownloadFileCallback final : public FileManager::DownloadCallbac } }; -class Requests::UploadFileCallback final : public FileManager::UploadCallback { - public: - void on_upload_ok(FileId file_id, tl_object_ptr input_file) final { - // cancel file upload of the file to allow next upload with the same file to succeed - send_closure(G()->file_manager(), &FileManager::cancel_upload, file_id); - } - - void on_upload_encrypted_ok(FileId file_id, tl_object_ptr input_file) final { - // cancel file upload of the file to allow next upload with the same file to succeed - send_closure(G()->file_manager(), &FileManager::cancel_upload, file_id); - } - - void on_upload_secure_ok(FileId file_id, tl_object_ptr input_file) final { - // cancel file upload of the file to allow next upload with the same file to succeed - send_closure(G()->file_manager(), &FileManager::cancel_upload, file_id); - } - - void on_upload_error(FileId file_id, Status error) final { - } -}; - Requests::Requests(Td *td) - : td_(td) - , td_actor_(td->actor_id(td)) - , download_file_callback_(std::make_shared()) - , upload_file_callback_(std::make_shared()) { + : td_(td), td_actor_(td->actor_id(td)), download_file_callback_(std::make_shared()) { } void Requests::run_request(uint64 id, td_api::object_ptr &&function) { @@ -5501,25 +5477,9 @@ void Requests::on_request(uint64 id, const td_api::getSuggestedFileName &request } void Requests::on_request(uint64 id, const td_api::preliminaryUploadFile &request) { - auto priority = request.priority_; - if (!(1 <= priority && priority <= 32)) { - return send_error_raw(id, 400, "Upload priority must be between 1 and 32"); - } - + CREATE_REQUEST_PROMISE(); auto file_type = request.file_type_ == nullptr ? FileType::Temp : get_file_type(*request.file_type_); - bool is_secret = file_type == FileType::Encrypted || file_type == FileType::EncryptedThumbnail; - bool is_secure = file_type == FileType::SecureEncrypted; - auto r_file_id = td_->file_manager_->get_input_file_id(file_type, request.file_, DialogId(), false, is_secret, - !is_secure && !is_secret, is_secure); - if (r_file_id.is_error()) { - return send_error_raw(id, r_file_id.error().code(), r_file_id.error().message()); - } - auto file_id = r_file_id.ok(); - auto upload_file_id = td_->file_manager_->dup_file_id(file_id, "preliminaryUploadFile"); - - td_->file_manager_->upload(upload_file_id, upload_file_callback_, priority, 0); - - send_closure(td_actor_, &Td::send_result, id, td_->file_manager_->get_file_object(upload_file_id, false)); + td_->file_manager_->preliminary_upload_file(request.file_, file_type, request.priority_, std::move(promise)); } void Requests::on_request(uint64 id, const td_api::cancelPreliminaryUploadFile &request) { diff --git a/td/telegram/Requests.h b/td/telegram/Requests.h index 1c223ff71..5260e6ce8 100644 --- a/td/telegram/Requests.h +++ b/td/telegram/Requests.h @@ -49,10 +49,6 @@ class Requests { std::shared_ptr download_file_callback_; - class UploadFileCallback; - - std::shared_ptr upload_file_callback_; - template Promise create_request_promise(uint64 id) { return PromiseCreator::lambda([actor_id = td_actor_, id](Result r_state) { diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index 541482e06..f1fbfb069 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -4361,6 +4361,48 @@ FullRemoteFileLocation *FileManager::get_remote(int32 key) { return &remote_location_info_.get(key).remote_; } +class FileManager::PreliminaryUploadFileCallback final : public UploadCallback { + public: + void on_upload_ok(FileId file_id, tl_object_ptr input_file) final { + // cancel file upload of the file to allow next upload with the same file to succeed + send_closure(G()->file_manager(), &FileManager::cancel_upload, file_id); + } + + void on_upload_encrypted_ok(FileId file_id, tl_object_ptr input_file) final { + // cancel file upload of the file to allow next upload with the same file to succeed + send_closure(G()->file_manager(), &FileManager::cancel_upload, file_id); + } + + void on_upload_secure_ok(FileId file_id, tl_object_ptr input_file) final { + // cancel file upload of the file to allow next upload with the same file to succeed + send_closure(G()->file_manager(), &FileManager::cancel_upload, file_id); + } + + void on_upload_error(FileId file_id, Status error) final { + } +}; + +void FileManager::preliminary_upload_file(const td_api::object_ptr &input_file, FileType file_type, + int32 priority, Promise> &&promise) { + if (!(1 <= priority && priority <= 32)) { + return promise.set_error(Status::Error(400, "Upload priority must be between 1 and 32")); + } + + bool is_secret = file_type == FileType::Encrypted || file_type == FileType::EncryptedThumbnail; + bool is_secure = file_type == FileType::SecureEncrypted; + auto r_file_id = + get_input_file_id(file_type, input_file, DialogId(), false, is_secret, !is_secure && !is_secret, is_secure); + if (r_file_id.is_error()) { + return promise.set_error(Status::Error(r_file_id.error().code(), r_file_id.error().message())); + } + auto file_id = r_file_id.ok(); + auto upload_file_id = dup_file_id(file_id, "preliminary_upload_file"); + + upload(upload_file_id, std::make_shared(), priority, 0); + + promise.set_value(get_file_object(upload_file_id, false)); +} + Result FileManager::get_suggested_file_name(FileId file_id, const string &directory) { if (!file_id.is_valid()) { return Status::Error(400, "Invalid file identifier"); diff --git a/td/telegram/files/FileManager.h b/td/telegram/files/FileManager.h index 24be50daa..4de084701 100644 --- a/td/telegram/files/FileManager.h +++ b/td/telegram/files/FileManager.h @@ -503,6 +503,9 @@ class FileManager final : public Actor { void delete_file_reference(FileId file_id, Slice file_reference); void get_content(FileId file_id, Promise promise); + void preliminary_upload_file(const td_api::object_ptr &input_file, FileType file_type, + int32 priority, Promise> &&promise); + Result get_suggested_file_name(FileId file_id, const string &directory); void read_file_part(FileId file_id, int64 offset, int64 count, int left_tries, @@ -618,6 +621,8 @@ class FileManager final : public Actor { } }; + class PreliminaryUploadFileCallback; + Result check_input_file_id(FileType type, Result result, bool is_encrypted, bool allow_zero, bool is_secure) TD_WARN_UNUSED_RESULT;