Add FileManager::preliminary_upload_file.

This commit is contained in:
levlam 2024-08-15 19:57:58 +03:00
parent 866f9f463a
commit f23959070b
4 changed files with 50 additions and 47 deletions

View File

@ -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<telegram_api::InputFile> 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<telegram_api::InputEncryptedFile> 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<telegram_api::InputSecureFile> 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) Requests::Requests(Td *td)
: td_(td) : td_(td), td_actor_(td->actor_id(td)), download_file_callback_(std::make_shared<DownloadFileCallback>()) {
, td_actor_(td->actor_id(td))
, download_file_callback_(std::make_shared<DownloadFileCallback>())
, upload_file_callback_(std::make_shared<UploadFileCallback>()) {
} }
void Requests::run_request(uint64 id, td_api::object_ptr<td_api::Function> &&function) { void Requests::run_request(uint64 id, td_api::object_ptr<td_api::Function> &&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) { void Requests::on_request(uint64 id, const td_api::preliminaryUploadFile &request) {
auto priority = request.priority_; CREATE_REQUEST_PROMISE();
if (!(1 <= priority && priority <= 32)) {
return send_error_raw(id, 400, "Upload priority must be between 1 and 32");
}
auto file_type = request.file_type_ == nullptr ? FileType::Temp : get_file_type(*request.file_type_); 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; td_->file_manager_->preliminary_upload_file(request.file_, file_type, request.priority_, std::move(promise));
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));
} }
void Requests::on_request(uint64 id, const td_api::cancelPreliminaryUploadFile &request) { void Requests::on_request(uint64 id, const td_api::cancelPreliminaryUploadFile &request) {

View File

@ -49,10 +49,6 @@ class Requests {
std::shared_ptr<DownloadFileCallback> download_file_callback_; std::shared_ptr<DownloadFileCallback> download_file_callback_;
class UploadFileCallback;
std::shared_ptr<UploadFileCallback> upload_file_callback_;
template <class T> template <class T>
Promise<T> create_request_promise(uint64 id) { Promise<T> create_request_promise(uint64 id) {
return PromiseCreator::lambda([actor_id = td_actor_, id](Result<T> r_state) { return PromiseCreator::lambda([actor_id = td_actor_, id](Result<T> r_state) {

View File

@ -4361,6 +4361,48 @@ FullRemoteFileLocation *FileManager::get_remote(int32 key) {
return &remote_location_info_.get(key).remote_; return &remote_location_info_.get(key).remote_;
} }
class FileManager::PreliminaryUploadFileCallback final : public UploadCallback {
public:
void on_upload_ok(FileId file_id, tl_object_ptr<telegram_api::InputFile> 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<telegram_api::InputEncryptedFile> 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<telegram_api::InputSecureFile> 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<td_api::InputFile> &input_file, FileType file_type,
int32 priority, Promise<td_api::object_ptr<td_api::file>> &&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<PreliminaryUploadFileCallback>(), priority, 0);
promise.set_value(get_file_object(upload_file_id, false));
}
Result<string> FileManager::get_suggested_file_name(FileId file_id, const string &directory) { Result<string> FileManager::get_suggested_file_name(FileId file_id, const string &directory) {
if (!file_id.is_valid()) { if (!file_id.is_valid()) {
return Status::Error(400, "Invalid file identifier"); return Status::Error(400, "Invalid file identifier");

View File

@ -503,6 +503,9 @@ class FileManager final : public Actor {
void delete_file_reference(FileId file_id, Slice file_reference); void delete_file_reference(FileId file_id, Slice file_reference);
void get_content(FileId file_id, Promise<BufferSlice> promise); void get_content(FileId file_id, Promise<BufferSlice> promise);
void preliminary_upload_file(const td_api::object_ptr<td_api::InputFile> &input_file, FileType file_type,
int32 priority, Promise<td_api::object_ptr<td_api::file>> &&promise);
Result<string> get_suggested_file_name(FileId file_id, const string &directory); Result<string> get_suggested_file_name(FileId file_id, const string &directory);
void read_file_part(FileId file_id, int64 offset, int64 count, int left_tries, 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<FileId> check_input_file_id(FileType type, Result<FileId> result, bool is_encrypted, bool allow_zero, Result<FileId> check_input_file_id(FileType type, Result<FileId> result, bool is_encrypted, bool allow_zero,
bool is_secure) TD_WARN_UNUSED_RESULT; bool is_secure) TD_WARN_UNUSED_RESULT;