Add FileManager::cancel_upload.

GitOrigin-RevId: d6db4e90fbe361ed2c90e509fe3aa609df5988e6
This commit is contained in:
levlam 2019-01-25 04:52:38 +03:00
parent 44ed807417
commit c958bd5b7c
7 changed files with 43 additions and 38 deletions

View File

@ -2110,10 +2110,10 @@ tl_object_ptr<telegram_api::InputMedia> get_input_media(const MessageContent *co
// if we had InputFile, but has failed to use it, then we need to immediately cancel file upload // if we had InputFile, but has failed to use it, then we need to immediately cancel file upload
// so the next upload with the same file can succeed // so the next upload with the same file can succeed
CHECK(file_id.is_valid()); CHECK(file_id.is_valid());
td->file_manager_->upload(file_id, nullptr, 0, 0); td->file_manager_->cancel_upload(file_id);
if (had_input_thumbnail) { if (had_input_thumbnail) {
CHECK(thumbnail_file_id.is_valid()); CHECK(thumbnail_file_id.is_valid());
td->file_manager_->upload(thumbnail_file_id, nullptr, 0, 0); td->file_manager_->cancel_upload(thumbnail_file_id);
} }
} }
} else { } else {

View File

@ -6214,7 +6214,7 @@ void MessagesManager::on_load_secret_thumbnail(FileId thumbnail_file_id, BufferS
if (m == nullptr) { if (m == nullptr) {
// message has already been deleted by the user, do not need to send it // message has already been deleted by the user, do not need to send it
// cancel file upload of the main file to allow next upload with the same file to succeed // cancel file upload of the main file to allow next upload with the same file to succeed
td_->file_manager_->upload(file_id, nullptr, 0, 0); td_->file_manager_->cancel_upload(file_id);
LOG(INFO) << "Message with a media has already been deleted"; LOG(INFO) << "Message with a media has already been deleted";
return; return;
} }
@ -14932,12 +14932,12 @@ void MessagesManager::cancel_upload_message_content_files(const MessageContent *
// always cancel file upload, it is a no-op in the worst case // always cancel file upload, it is a no-op in the worst case
if (being_uploaded_files_.erase(file_id) || file_id.is_valid()) { if (being_uploaded_files_.erase(file_id) || file_id.is_valid()) {
LOG(INFO) << "Cancel upload file " << file_id; LOG(INFO) << "Cancel upload file " << file_id;
td_->file_manager_->upload(file_id, nullptr, 0, 0); td_->file_manager_->cancel_upload(file_id);
} }
file_id = get_message_content_thumbnail_file_id(content, td_); file_id = get_message_content_thumbnail_file_id(content, td_);
if (being_uploaded_thumbnails_.erase(file_id) || file_id.is_valid()) { if (being_uploaded_thumbnails_.erase(file_id) || file_id.is_valid()) {
LOG(INFO) << "Cancel upload thumbnail file " << file_id; LOG(INFO) << "Cancel upload thumbnail file " << file_id;
td_->file_manager_->upload(file_id, nullptr, 0, 0); td_->file_manager_->cancel_upload(file_id);
} }
} }
@ -22142,7 +22142,7 @@ bool MessagesManager::update_message_content(DialogId dialog_id, Message *old_me
if (need_finish_upload) { if (need_finish_upload) {
// the file is likely to be already merged with a server file, but if not we need to // the file is likely to be already merged with a server file, but if not we need to
// cancel file upload of the main file to allow next upload with the same file to succeed // cancel file upload of the main file to allow next upload with the same file to succeed
td_->file_manager_->upload(old_file_id, nullptr, 0, 0); td_->file_manager_->cancel_upload(old_file_id);
} }
if (is_content_changed || need_update) { if (is_content_changed || need_update) {

View File

@ -527,19 +527,19 @@ void SetSecureValue::tear_down() {
return; return;
} }
for (auto &file_info : files_to_upload_) { for (auto &file_info : files_to_upload_) {
file_manager->upload(file_info.file_id, nullptr, 0, 0); file_manager->cancel_upload(file_info.file_id);
} }
for (auto &file_info : translations_to_upload_) { for (auto &file_info : translations_to_upload_) {
file_manager->upload(file_info.file_id, nullptr, 0, 0); file_manager->cancel_upload(file_info.file_id);
} }
if (front_side_) { if (front_side_) {
file_manager->upload(front_side_.value().file_id, nullptr, 0, 0); file_manager->cancel_upload(front_side_.value().file_id);
} }
if (reverse_side_) { if (reverse_side_) {
file_manager->upload(reverse_side_.value().file_id, nullptr, 0, 0); file_manager->cancel_upload(reverse_side_.value().file_id);
} }
if (selfie_) { if (selfie_) {
file_manager->upload(selfie_.value().file_id, nullptr, 0, 0); file_manager->cancel_upload(selfie_.value().file_id);
} }
} }

View File

@ -614,7 +614,7 @@ class UploadStickerFileQuery : public Td::ResultHandler {
CHECK(file_id_.is_valid()); CHECK(file_id_.is_valid());
if (begins_with(status.message(), "FILE_PART_") && ends_with(status.message(), "_MISSING")) { if (begins_with(status.message(), "FILE_PART_") && ends_with(status.message(), "_MISSING")) {
// TODO td->stickers_manager_->on_upload_sticker_file_part_missing(file_id_, to_integer<int32>(status.message().substr(10))); // TODO td->stickers_manager_->on_upload_sticker_file_part_missing(file_id_, to_integer<int32>(status.message().substr(10)));
return; // return;
} else { } else {
if (status.code() != 429 && status.code() < 500 && !G()->close_flag()) { if (status.code() != 429 && status.code() < 500 && !G()->close_flag()) {
td->file_manager_->delete_partial_remote_location(file_id_); td->file_manager_->delete_partial_remote_location(file_id_);
@ -3310,7 +3310,7 @@ void StickersManager::do_upload_sticker_file(UserId user_id, FileId file_id,
if (had_input_file && !FileManager::extract_was_uploaded(input_media)) { if (had_input_file && !FileManager::extract_was_uploaded(input_media)) {
// if we had InputFile, but has failed to use it, then we need to immediately cancel file upload // if we had InputFile, but has failed to use it, then we need to immediately cancel file upload
// so the next upload with the same file can succeed // so the next upload with the same file can succeed
td_->file_manager_->upload(file_id, nullptr, 0, 0); td_->file_manager_->cancel_upload(file_id);
} }
td_->create_handler<UploadStickerFileQuery>(std::move(promise)) td_->create_handler<UploadStickerFileQuery>(std::move(promise))

View File

@ -3886,17 +3886,17 @@ class Td::UploadFileCallback : public FileManager::UploadCallback {
void on_upload_ok(FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file) override { void on_upload_ok(FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file) override {
// cancel file upload of the file to allow next upload with the same file to succeed // cancel file upload of the file to allow next upload with the same file to succeed
send_closure(G()->file_manager(), &FileManager::upload, file_id, nullptr, 0, 0); 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) override { void on_upload_encrypted_ok(FileId file_id, tl_object_ptr<telegram_api::InputEncryptedFile> input_file) override {
// cancel file upload of the file to allow next upload with the same file to succeed // cancel file upload of the file to allow next upload with the same file to succeed
send_closure(G()->file_manager(), &FileManager::upload, file_id, nullptr, 0, 0); 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) override { void on_upload_secure_ok(FileId file_id, tl_object_ptr<telegram_api::InputSecureFile> input_file) override {
// cancel file upload of the file to allow next upload with the same file to succeed // cancel file upload of the file to allow next upload with the same file to succeed
send_closure(G()->file_manager(), &FileManager::upload, file_id, nullptr, 0, 0); send_closure(G()->file_manager(), &FileManager::cancel_upload, file_id);
} }
void on_upload_error(FileId file_id, Status error) override { void on_upload_error(FileId file_id, Status error) override {
@ -5700,7 +5700,7 @@ void Td::on_request(uint64 id, td_api::uploadFile &request) {
} }
void Td::on_request(uint64 id, const td_api::cancelUploadFile &request) { void Td::on_request(uint64 id, const td_api::cancelUploadFile &request) {
file_manager_->upload(FileId(request.file_id_, 0), nullptr, 0, 0); file_manager_->cancel_upload(FileId(request.file_id_, 0));
send_closure(actor_id(this), &Td::send_result, id, make_tl_object<td_api::ok>()); send_closure(actor_id(this), &Td::send_result, id, make_tl_object<td_api::ok>());
} }

View File

@ -1078,7 +1078,7 @@ static int merge_choose_encryption_key(const FileEncryptionKey &a, const FileEnc
return 2; return 2;
} }
void FileManager::cancel_download(FileNodePtr node) { void FileManager::do_cancel_download(FileNodePtr node) {
if (node->download_id_ == 0) { if (node->download_id_ == 0) {
return; return;
} }
@ -1089,7 +1089,7 @@ void FileManager::cancel_download(FileNodePtr node) {
node->set_download_priority(0); node->set_download_priority(0);
} }
void FileManager::cancel_upload(FileNodePtr node) { void FileManager::do_cancel_upload(FileNodePtr node) {
if (node->upload_id_ == 0) { if (node->upload_id_ == 0) {
return; return;
} }
@ -1099,7 +1099,7 @@ void FileManager::cancel_upload(FileNodePtr node) {
node->set_upload_priority(0); node->set_upload_priority(0);
} }
void FileManager::cancel_generate(FileNodePtr node) { void FileManager::do_cancel_generate(FileNodePtr node) {
if (node->generate_id_ == 0) { if (node->generate_id_ == 0) {
return; return;
} }
@ -1205,7 +1205,7 @@ Result<FileId> FileManager::merge(FileId x_file_id, FileId y_file_id, bool no_sy
<< ", url_i = " << url_i << ", owner_i = " << owner_i << ", encryption_key_i = " << encryption_key_i << ", url_i = " << url_i << ", owner_i = " << owner_i << ", encryption_key_i = " << encryption_key_i
<< ", main_file_id_i = " << main_file_id_i; << ", main_file_id_i = " << main_file_id_i;
if (local_i == other_node_i) { if (local_i == other_node_i) {
cancel_download(node); do_cancel_download(node);
node->set_download_offset(other_node->download_offset_); node->set_download_offset(other_node->download_offset_);
node->set_local_location(other_node->local_, other_node->local_ready_size_, other_node->download_offset_, node->set_local_location(other_node->local_, other_node->local_ready_size_, other_node->download_offset_,
other_node->local_ready_prefix_size_); other_node->local_ready_prefix_size_);
@ -1220,7 +1220,7 @@ Result<FileId> FileManager::merge(FileId x_file_id, FileId y_file_id, bool no_sy
other_node->download_offset_ = 0; other_node->download_offset_ = 0;
other_node->local_ready_prefix_size_ = 0; other_node->local_ready_prefix_size_ = 0;
//cancel_generate(node); //do_cancel_generate(node);
//node->set_generate_location(std::move(other_node->generate_)); //node->set_generate_location(std::move(other_node->generate_));
//node->generate_id_ = other_node->generate_id_; //node->generate_id_ = other_node->generate_id_;
//node->set_generate_priority(other_node->generate_download_priority_, other_node->generate_upload_priority_); //node->set_generate_priority(other_node->generate_download_priority_, other_node->generate_upload_priority_);
@ -1230,12 +1230,12 @@ Result<FileId> FileManager::merge(FileId x_file_id, FileId y_file_id, bool no_sy
//other_node->generate_download_priority_ = 0; //other_node->generate_download_priority_ = 0;
//other_node->generate_upload_priority_ = 0; //other_node->generate_upload_priority_ = 0;
} else { } else {
cancel_download(other_node); do_cancel_download(other_node);
//cancel_generate(other_node); //do_cancel_generate(other_node);
} }
if (remote_i == other_node_i) { if (remote_i == other_node_i) {
cancel_upload(node); do_cancel_upload(node);
node->set_remote_location(other_node->remote_, other_node->remote_source_, other_node->remote_ready_size_); node->set_remote_location(other_node->remote_, other_node->remote_source_, other_node->remote_ready_size_);
node->upload_id_ = other_node->upload_id_; node->upload_id_ = other_node->upload_id_;
node->upload_was_update_file_reference_ = other_node->upload_was_update_file_reference_; node->upload_was_update_file_reference_ = other_node->upload_was_update_file_reference_;
@ -1246,11 +1246,11 @@ Result<FileId> FileManager::merge(FileId x_file_id, FileId y_file_id, bool no_sy
other_node->upload_priority_ = 0; other_node->upload_priority_ = 0;
other_node->set_upload_pause(FileId()); other_node->set_upload_pause(FileId());
} else { } else {
cancel_upload(other_node); do_cancel_upload(other_node);
} }
if (generate_i == other_node_i) { if (generate_i == other_node_i) {
cancel_generate(node); do_cancel_generate(node);
node->set_generate_location(std::move(other_node->generate_)); node->set_generate_location(std::move(other_node->generate_));
node->generate_id_ = other_node->generate_id_; node->generate_id_ = other_node->generate_id_;
node->set_generate_priority(other_node->generate_download_priority_, other_node->generate_upload_priority_); node->set_generate_priority(other_node->generate_download_priority_, other_node->generate_upload_priority_);
@ -1259,7 +1259,7 @@ Result<FileId> FileManager::merge(FileId x_file_id, FileId y_file_id, bool no_sy
other_node->generate_download_priority_ = 0; other_node->generate_download_priority_ = 0;
other_node->generate_upload_priority_ = 0; other_node->generate_upload_priority_ = 0;
} else { } else {
cancel_generate(other_node); do_cancel_generate(other_node);
} }
if (size_i == other_node_i) { if (size_i == other_node_i) {
@ -1621,7 +1621,7 @@ bool FileManager::set_content(FileId file_id, BufferSlice bytes) {
return true; return true;
} }
cancel_download(node); do_cancel_download(node);
auto *file_info = get_file_id_info(file_id); auto *file_info = get_file_id_info(file_id);
file_info->download_priority_ = FROM_BYTES_PRIORITY; file_info->download_priority_ = FROM_BYTES_PRIORITY;
@ -1772,7 +1772,7 @@ void FileManager::run_download(FileNodePtr node) {
if (priority == 0) { if (priority == 0) {
if (old_priority != 0) { if (old_priority != 0) {
cancel_download(node); do_cancel_download(node);
} }
return; return;
} }
@ -1973,7 +1973,7 @@ void FileManager::run_generate(FileNodePtr node) {
if (node->generate_priority_ == 0) { if (node->generate_priority_ == 0) {
if (old_priority != 0) { if (old_priority != 0) {
LOG(INFO) << "Cancel file " << file_id << " generation"; LOG(INFO) << "Cancel file " << file_id << " generation";
cancel_generate(node); do_cancel_generate(node);
} }
return; return;
} }
@ -2049,7 +2049,7 @@ void FileManager::run_upload(FileNodePtr node, std::vector<int> bad_parts) {
if (priority == 0) { if (priority == 0) {
if (old_priority != 0) { if (old_priority != 0) {
LOG(INFO) << "Cancel file " << file_id << " uploading"; LOG(INFO) << "Cancel file " << file_id << " uploading";
cancel_upload(node); do_cancel_upload(node);
} }
return; return;
} }
@ -2118,6 +2118,10 @@ void FileManager::upload(FileId file_id, std::shared_ptr<UploadCallback> callbac
return resume_upload(file_id, std::vector<int>(), std::move(callback), new_priority, upload_order); return resume_upload(file_id, std::vector<int>(), std::move(callback), new_priority, upload_order);
} }
void FileManager::cancel_upload(FileId file_id) {
return resume_upload(file_id, std::vector<int>(), nullptr, 0, 0);
}
static bool is_document_type(FileType type) { static bool is_document_type(FileType type) {
return type == FileType::Document || type == FileType::Sticker || type == FileType::Audio || return type == FileType::Document || type == FileType::Sticker || type == FileType::Audio ||
type == FileType::Animation; type == FileType::Animation;
@ -2885,9 +2889,9 @@ void FileManager::on_error_impl(FileNodePtr node, FileManager::Query::Type type,
} }
// Stop everything on error // Stop everything on error
cancel_generate(node); do_cancel_generate(node);
cancel_download(node); do_cancel_download(node);
cancel_upload(node); do_cancel_upload(node);
for (auto file_id : vector<FileId>(node->file_ids_)) { for (auto file_id : vector<FileId>(node->file_ids_)) {
auto *info = get_file_id_info(file_id); auto *info = get_file_id_info(file_id);

View File

@ -365,6 +365,7 @@ class FileManager : public FileLoadManager::Callback {
void upload(FileId file_id, std::shared_ptr<UploadCallback> callback, int32 new_priority, uint64 upload_order); void upload(FileId file_id, std::shared_ptr<UploadCallback> callback, int32 new_priority, uint64 upload_order);
void resume_upload(FileId file_id, std::vector<int> bad_parts, std::shared_ptr<UploadCallback> callback, void resume_upload(FileId file_id, std::vector<int> bad_parts, std::shared_ptr<UploadCallback> callback,
int32 new_priority, uint64 upload_order); int32 new_priority, uint64 upload_order);
void cancel_upload(FileId file_id);
bool delete_partial_remote_location(FileId file_id); bool delete_partial_remote_location(FileId file_id);
void delete_file_reference(FileId file_id, std::string file_reference); void delete_file_reference(FileId file_id, std::string file_reference);
void get_content(FileId file_id, Promise<BufferSlice> promise); void get_content(FileId file_id, Promise<BufferSlice> promise);
@ -521,9 +522,9 @@ class FileManager : public FileLoadManager::Callback {
FileNodePtr get_sync_file_node(FileId file_id); FileNodePtr get_sync_file_node(FileId file_id);
// void release_file_node(FileNodeId id); // void release_file_node(FileNodeId id);
void cancel_download(FileNodePtr node); void do_cancel_download(FileNodePtr node);
void cancel_upload(FileNodePtr node); void do_cancel_upload(FileNodePtr node);
void cancel_generate(FileNodePtr node); void do_cancel_generate(FileNodePtr node);
void run_upload(FileNodePtr node, std::vector<int> bad_parts); void run_upload(FileNodePtr node, std::vector<int> bad_parts);
void run_download(FileNodePtr node); void run_download(FileNodePtr node);
void run_generate(FileNodePtr node); void run_generate(FileNodePtr node);