2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/files/FileManager.h"
|
2019-01-06 21:47:40 +01:00
|
|
|
|
2023-05-09 00:10:14 +02:00
|
|
|
#include "td/telegram/ConfigManager.h"
|
2022-03-03 16:58:51 +01:00
|
|
|
#include "td/telegram/DownloadManager.h"
|
2019-01-31 03:05:40 +01:00
|
|
|
#include "td/telegram/FileReferenceManager.h"
|
2019-01-19 22:26:23 +01:00
|
|
|
#include "td/telegram/files/FileData.h"
|
2019-01-06 21:47:40 +01:00
|
|
|
#include "td/telegram/files/FileDb.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/files/FileLoaderUtils.h"
|
|
|
|
#include "td/telegram/files/FileLocation.h"
|
2019-02-12 18:48:13 +01:00
|
|
|
#include "td/telegram/files/FileLocation.hpp"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/Global.h"
|
2019-06-12 13:42:06 +02:00
|
|
|
#include "td/telegram/logevent/LogEvent.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/misc.h"
|
2018-12-27 19:38:43 +01:00
|
|
|
#include "td/telegram/SecureStorage.h"
|
2019-01-06 20:59:17 +01:00
|
|
|
#include "td/telegram/TdDb.h"
|
2019-06-12 15:05:22 +02:00
|
|
|
#include "td/telegram/Version.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-04-26 00:03:31 +02:00
|
|
|
#include "td/actor/SleepActor.h"
|
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/base64.h"
|
2020-04-30 21:21:42 +02:00
|
|
|
#include "td/utils/crypto.h"
|
2020-04-23 23:50:14 +02:00
|
|
|
#include "td/utils/filesystem.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/HttpUrl.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/PathView.h"
|
|
|
|
#include "td/utils/port/Stat.h"
|
|
|
|
#include "td/utils/ScopeGuard.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2019-02-15 21:09:18 +01:00
|
|
|
#include "td/utils/StringBuilder.h"
|
2019-11-26 19:57:48 +01:00
|
|
|
#include "td/utils/Time.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/tl_helpers.h"
|
2019-06-12 15:05:22 +02:00
|
|
|
#include "td/utils/tl_parsers.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <algorithm>
|
2018-08-13 22:18:27 +02:00
|
|
|
#include <cmath>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <limits>
|
2019-12-24 16:51:41 +01:00
|
|
|
#include <numeric>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <tuple>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
2018-11-11 12:38:04 +01:00
|
|
|
namespace {
|
2022-06-06 19:36:15 +02:00
|
|
|
constexpr int64 MAX_FILE_SIZE = static_cast<int64>(4000) << 20; // 4000MB
|
2018-12-27 20:24:44 +01:00
|
|
|
} // namespace
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-12-23 15:32:03 +01:00
|
|
|
int VERBOSITY_NAME(update_file) = VERBOSITY_NAME(INFO);
|
|
|
|
|
2019-02-15 21:09:18 +01:00
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, FileLocationSource source) {
|
|
|
|
switch (source) {
|
|
|
|
case FileLocationSource::None:
|
|
|
|
return string_builder << "None";
|
|
|
|
case FileLocationSource::FromUser:
|
|
|
|
return string_builder << "User";
|
2019-02-15 21:22:40 +01:00
|
|
|
case FileLocationSource::FromBinlog:
|
|
|
|
return string_builder << "Binlog";
|
2019-02-15 21:09:18 +01:00
|
|
|
case FileLocationSource::FromDatabase:
|
|
|
|
return string_builder << "Database";
|
|
|
|
case FileLocationSource::FromServer:
|
|
|
|
return string_builder << "Server";
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return string_builder << "Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-28 22:39:07 +01:00
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const NewRemoteFileLocation &location) {
|
|
|
|
if (location.is_full_alive) {
|
|
|
|
string_builder << "alive ";
|
|
|
|
}
|
|
|
|
if (location.full) {
|
|
|
|
string_builder << location.full.value();
|
|
|
|
} else {
|
|
|
|
string_builder << "[no location]";
|
|
|
|
}
|
|
|
|
return string_builder << " from " << location.full_source;
|
|
|
|
}
|
|
|
|
|
2019-12-23 15:32:03 +01:00
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, FileManager::Query::Type type) {
|
|
|
|
switch (type) {
|
|
|
|
case FileManager::Query::Type::UploadByHash:
|
|
|
|
return string_builder << "UploadByHash";
|
|
|
|
case FileManager::Query::Type::UploadWaitFileReference:
|
|
|
|
return string_builder << "UploadWaitFileReference";
|
|
|
|
case FileManager::Query::Type::Upload:
|
|
|
|
return string_builder << "Upload";
|
2020-01-06 16:02:42 +01:00
|
|
|
case FileManager::Query::Type::DownloadWaitFileReference:
|
|
|
|
return string_builder << "DownloadWaitFileReference";
|
2019-12-23 15:32:03 +01:00
|
|
|
case FileManager::Query::Type::DownloadReloadDialog:
|
|
|
|
return string_builder << "DownloadReloadDialog";
|
|
|
|
case FileManager::Query::Type::Download:
|
|
|
|
return string_builder << "Download";
|
|
|
|
case FileManager::Query::Type::SetContent:
|
|
|
|
return string_builder << "SetContent";
|
|
|
|
case FileManager::Query::Type::Generate:
|
|
|
|
return string_builder << "Generate";
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return string_builder << "Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 12:20:40 +01:00
|
|
|
NewRemoteFileLocation::NewRemoteFileLocation(RemoteFileLocation remote, FileLocationSource source) {
|
|
|
|
switch (remote.type()) {
|
2019-02-14 22:19:48 +01:00
|
|
|
case RemoteFileLocation::Type::Empty:
|
|
|
|
break;
|
|
|
|
case RemoteFileLocation::Type::Partial:
|
2019-02-14 12:20:40 +01:00
|
|
|
partial = make_unique<PartialRemoteFileLocation>(remote.partial());
|
2019-02-14 22:19:48 +01:00
|
|
|
break;
|
|
|
|
case RemoteFileLocation::Type::Full:
|
2019-02-14 12:20:40 +01:00
|
|
|
full = remote.full();
|
|
|
|
full_source = source;
|
|
|
|
is_full_alive = true;
|
2019-02-14 22:19:48 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2019-02-14 12:20:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoteFileLocation NewRemoteFileLocation::partial_or_empty() const {
|
|
|
|
if (partial) {
|
|
|
|
return RemoteFileLocation(*partial);
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-01-30 21:04:22 +01:00
|
|
|
FileNode *FileNodePtr::operator->() const {
|
|
|
|
return get();
|
|
|
|
}
|
2018-02-19 23:28:06 +01:00
|
|
|
|
2018-01-30 21:04:22 +01:00
|
|
|
FileNode &FileNodePtr::operator*() const {
|
|
|
|
return *get();
|
|
|
|
}
|
2018-02-19 23:28:06 +01:00
|
|
|
|
2018-01-30 21:04:22 +01:00
|
|
|
FileNode *FileNodePtr::get() const {
|
|
|
|
auto res = get_unsafe();
|
|
|
|
CHECK(res);
|
|
|
|
return res;
|
|
|
|
}
|
2018-02-19 23:28:06 +01:00
|
|
|
|
2018-03-06 19:31:20 +01:00
|
|
|
FullRemoteFileLocation *FileNodePtr::get_remote() const {
|
|
|
|
return file_manager_->get_remote(file_id_.get_remote());
|
|
|
|
}
|
|
|
|
|
2018-01-30 21:04:22 +01:00
|
|
|
FileNode *FileNodePtr::get_unsafe() const {
|
|
|
|
CHECK(file_manager_ != nullptr);
|
|
|
|
return file_manager_->get_file_node_raw(file_id_);
|
|
|
|
}
|
2018-02-19 23:28:06 +01:00
|
|
|
|
2022-10-09 18:30:18 +02:00
|
|
|
FileNodePtr::operator bool() const noexcept {
|
2018-01-30 21:04:22 +01:00
|
|
|
return file_manager_ != nullptr && get_unsafe() != nullptr;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-12-26 17:11:15 +01:00
|
|
|
void FileNode::recalc_ready_prefix_size(int64 prefix_offset, int64 ready_prefix_size) {
|
2018-11-11 12:38:04 +01:00
|
|
|
if (local_.type() != LocalFileLocation::Type::Partial) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int64 new_local_ready_prefix_size;
|
2018-12-26 17:11:15 +01:00
|
|
|
if (download_offset_ == prefix_offset) {
|
|
|
|
new_local_ready_prefix_size = ready_prefix_size;
|
2018-11-11 12:38:04 +01:00
|
|
|
} else {
|
|
|
|
new_local_ready_prefix_size = Bitmask(Bitmask::Decode{}, local_.partial().ready_bitmask_)
|
2018-12-26 17:11:15 +01:00
|
|
|
.get_ready_prefix_size(download_offset_, local_.partial().part_size_, size_);
|
2018-11-11 12:38:04 +01:00
|
|
|
}
|
|
|
|
if (new_local_ready_prefix_size != local_ready_prefix_size_) {
|
2018-12-27 01:56:03 +01:00
|
|
|
VLOG(update_file) << "File " << main_file_id_ << " has changed local_ready_prefix_size from "
|
|
|
|
<< local_ready_prefix_size_ << " to " << new_local_ready_prefix_size;
|
2018-11-11 12:38:04 +01:00
|
|
|
local_ready_prefix_size_ = new_local_ready_prefix_size;
|
|
|
|
on_info_changed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileNode::init_ready_size() {
|
|
|
|
if (local_.type() != LocalFileLocation::Type::Partial) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto bitmask = Bitmask(Bitmask::Decode{}, local_.partial().ready_bitmask_);
|
2018-12-26 17:11:15 +01:00
|
|
|
local_ready_prefix_size_ = bitmask.get_ready_prefix_size(0, local_.partial().part_size_, size_);
|
2018-12-27 09:34:36 +01:00
|
|
|
local_ready_size_ = bitmask.get_total_size(local_.partial().part_size_, size_);
|
2018-11-11 12:38:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileNode::set_download_offset(int64 download_offset) {
|
|
|
|
if (download_offset < 0 || download_offset > MAX_FILE_SIZE) {
|
2022-02-28 13:15:00 +01:00
|
|
|
// KEEP_DOWNLOAD_OFFSET is handled here
|
2018-11-11 12:38:04 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (download_offset == download_offset_) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-27 01:56:03 +01:00
|
|
|
|
|
|
|
VLOG(update_file) << "File " << main_file_id_ << " has changed download_offset from " << download_offset_ << " to "
|
|
|
|
<< download_offset;
|
2018-11-11 12:38:04 +01:00
|
|
|
download_offset_ = download_offset;
|
|
|
|
is_download_offset_dirty_ = true;
|
2018-12-26 17:11:15 +01:00
|
|
|
recalc_ready_prefix_size(-1, -1);
|
2018-11-11 12:38:04 +01:00
|
|
|
on_info_changed();
|
|
|
|
}
|
2022-02-28 13:15:00 +01:00
|
|
|
|
|
|
|
int64 FileNode::get_download_limit() const {
|
|
|
|
if (ignore_download_limit_) {
|
|
|
|
return 0;
|
2019-02-18 20:08:05 +01:00
|
|
|
}
|
2022-02-28 13:15:00 +01:00
|
|
|
return private_download_limit_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileNode::update_effective_download_limit(int64 old_download_limit) {
|
|
|
|
if (get_download_limit() == old_download_limit) {
|
2019-02-18 20:08:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-28 16:25:07 +01:00
|
|
|
// There should be no false positives here
|
2022-02-28 13:15:00 +01:00
|
|
|
// When we use IGNORE_DOWNLOAD_LIMIT, set_download_limit will be ignored
|
2022-02-28 16:25:07 +01:00
|
|
|
// And in case we turn off ignore_download_limit, set_download_limit will not change effective download limit
|
2022-02-28 13:15:00 +01:00
|
|
|
VLOG(update_file) << "File " << main_file_id_ << " has changed download_limit from " << old_download_limit << " to "
|
|
|
|
<< get_download_limit() << " (limit=" << private_download_limit_
|
|
|
|
<< ";ignore=" << ignore_download_limit_ << ")";
|
2019-02-26 22:42:54 +01:00
|
|
|
is_download_limit_dirty_ = true;
|
2019-02-18 20:08:05 +01:00
|
|
|
}
|
2018-11-11 12:38:04 +01:00
|
|
|
|
2022-02-28 13:15:00 +01:00
|
|
|
void FileNode::set_download_limit(int64 download_limit) {
|
|
|
|
if (download_limit < 0) {
|
|
|
|
// KEEP_DOWNLOAD_LIMIT is handled here
|
|
|
|
return;
|
|
|
|
}
|
2022-05-11 16:17:20 +02:00
|
|
|
if (download_limit > MAX_FILE_SIZE) {
|
|
|
|
download_limit = MAX_FILE_SIZE;
|
|
|
|
}
|
2022-02-28 13:15:00 +01:00
|
|
|
auto old_download_limit = get_download_limit();
|
|
|
|
private_download_limit_ = download_limit;
|
|
|
|
update_effective_download_limit(old_download_limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileNode::set_ignore_download_limit(bool ignore_download_limit) {
|
|
|
|
auto old_download_limit = get_download_limit();
|
|
|
|
ignore_download_limit_ = ignore_download_limit;
|
|
|
|
update_effective_download_limit(old_download_limit);
|
|
|
|
}
|
|
|
|
|
2018-12-26 17:11:15 +01:00
|
|
|
void FileNode::drop_local_location() {
|
|
|
|
set_local_location(LocalFileLocation(), 0, -1, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileNode::set_local_location(const LocalFileLocation &local, int64 ready_size, int64 prefix_offset,
|
|
|
|
int64 ready_prefix_size) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (local_ready_size_ != ready_size) {
|
2018-12-26 17:11:15 +01:00
|
|
|
VLOG(update_file) << "File " << main_file_id_ << " has changed local ready size from " << local_ready_size_
|
|
|
|
<< " to " << ready_size;
|
2018-12-31 20:04:05 +01:00
|
|
|
local_ready_size_ = ready_size;
|
|
|
|
on_info_changed();
|
|
|
|
}
|
|
|
|
if (local_ != local) {
|
|
|
|
VLOG(update_file) << "File " << main_file_id_ << " has changed local location";
|
|
|
|
local_ = local;
|
2018-11-11 12:38:04 +01:00
|
|
|
|
2018-12-26 17:11:15 +01:00
|
|
|
recalc_ready_prefix_size(prefix_offset, ready_prefix_size);
|
2018-11-11 12:38:04 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
on_changed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 12:20:40 +01:00
|
|
|
void FileNode::set_new_remote_location(NewRemoteFileLocation new_remote) {
|
|
|
|
if (new_remote.full) {
|
|
|
|
if (remote_.full && remote_.full.value() == new_remote.full.value()) {
|
2019-02-14 22:19:48 +01:00
|
|
|
if (remote_.full.value().get_access_hash() != new_remote.full.value().get_access_hash() ||
|
2019-06-18 17:40:46 +02:00
|
|
|
remote_.full.value().get_file_reference() != new_remote.full.value().get_file_reference() ||
|
|
|
|
remote_.full.value().get_source() != new_remote.full.value().get_source()) {
|
2019-01-24 20:52:24 +01:00
|
|
|
on_pmc_changed();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-02-14 12:20:40 +01:00
|
|
|
} else {
|
|
|
|
VLOG(update_file) << "File " << main_file_id_ << " has changed remote location";
|
|
|
|
on_changed();
|
|
|
|
}
|
|
|
|
remote_.full = new_remote.full;
|
2019-02-15 20:40:09 +01:00
|
|
|
remote_.full_source = new_remote.full_source;
|
2019-02-14 12:20:40 +01:00
|
|
|
remote_.is_full_alive = new_remote.is_full_alive;
|
|
|
|
} else {
|
|
|
|
if (remote_.full) {
|
2019-02-14 22:19:48 +01:00
|
|
|
VLOG(update_file) << "File " << main_file_id_ << " has lost remote location";
|
2019-02-14 12:20:40 +01:00
|
|
|
remote_.full = {};
|
|
|
|
remote_.is_full_alive = false;
|
2019-02-15 20:40:09 +01:00
|
|
|
remote_.full_source = FileLocationSource::None;
|
2019-02-14 12:20:40 +01:00
|
|
|
on_changed();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 12:20:40 +01:00
|
|
|
if (new_remote.partial) {
|
|
|
|
set_partial_remote_location(*new_remote.partial, new_remote.ready_size);
|
|
|
|
} else {
|
|
|
|
delete_partial_remote_location();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void FileNode::delete_partial_remote_location() {
|
|
|
|
if (remote_.partial) {
|
2019-02-14 22:19:48 +01:00
|
|
|
VLOG(update_file) << "File " << main_file_id_ << " has lost partial remote location";
|
2019-02-14 12:20:40 +01:00
|
|
|
remote_.partial.reset();
|
|
|
|
on_changed();
|
|
|
|
}
|
2023-05-30 11:29:12 +02:00
|
|
|
if (remote_.ready_size != 0) {
|
|
|
|
VLOG(update_file) << "File " << main_file_id_ << " has changed remote ready size from " << remote_.ready_size
|
|
|
|
<< " to " << 0;
|
|
|
|
remote_.ready_size = 0;
|
|
|
|
on_info_changed();
|
|
|
|
}
|
2019-02-14 12:20:40 +01:00
|
|
|
}
|
2019-02-14 22:19:48 +01:00
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
void FileNode::set_partial_remote_location(PartialRemoteFileLocation remote, int64 ready_size) {
|
2019-02-14 12:20:40 +01:00
|
|
|
if (remote_.is_full_alive) {
|
2019-02-14 22:19:48 +01:00
|
|
|
VLOG(update_file) << "File " << main_file_id_ << " remote is still alive, so there is NO reason to update partial";
|
2019-02-14 12:20:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (remote_.ready_size != ready_size) {
|
|
|
|
VLOG(update_file) << "File " << main_file_id_ << " has changed remote ready size from " << remote_.ready_size
|
|
|
|
<< " to " << ready_size;
|
|
|
|
remote_.ready_size = ready_size;
|
|
|
|
on_info_changed();
|
|
|
|
}
|
|
|
|
if (remote_.partial && *remote_.partial == remote) {
|
2019-02-14 22:19:48 +01:00
|
|
|
VLOG(update_file) << "Partial location of " << main_file_id_ << " is NOT changed";
|
2019-02-14 12:20:40 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-02-14 22:19:48 +01:00
|
|
|
if (!remote_.partial && remote.ready_part_count_ == 0) {
|
|
|
|
// empty partial remote is equal to empty remote
|
|
|
|
VLOG(update_file) << "Partial location of " << main_file_id_
|
|
|
|
<< " is still empty, so there is NO reason to update it";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
VLOG(update_file) << "File " << main_file_id_ << " partial location has changed to " << remote;
|
2021-10-19 17:11:16 +02:00
|
|
|
remote_.partial = make_unique<PartialRemoteFileLocation>(std::move(remote));
|
2018-12-31 20:04:05 +01:00
|
|
|
on_changed();
|
|
|
|
}
|
|
|
|
|
2019-01-25 02:38:11 +01:00
|
|
|
bool FileNode::delete_file_reference(Slice file_reference) {
|
2019-02-14 12:20:40 +01:00
|
|
|
if (!remote_.full) {
|
2019-01-31 13:24:32 +01:00
|
|
|
VLOG(file_references) << "Can't delete file reference, because there is no remote location";
|
2019-01-31 00:57:14 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-14 12:20:40 +01:00
|
|
|
if (!remote_.full.value().delete_file_reference(file_reference)) {
|
2019-01-31 13:24:32 +01:00
|
|
|
VLOG(file_references) << "Can't delete unmatching file reference " << format::escaped(file_reference) << ", have "
|
2019-06-17 04:37:57 +02:00
|
|
|
<< format::escaped(remote_.full.value().get_file_reference());
|
2019-01-31 09:33:35 +01:00
|
|
|
return false;
|
2018-12-05 10:32:31 +01:00
|
|
|
}
|
2019-01-31 09:33:35 +01:00
|
|
|
|
|
|
|
VLOG(file_references) << "Do delete file reference of main file " << main_file_id_;
|
|
|
|
upload_was_update_file_reference_ = false;
|
|
|
|
download_was_update_file_reference_ = false;
|
|
|
|
on_pmc_changed();
|
|
|
|
return true;
|
2018-12-05 10:32:31 +01:00
|
|
|
}
|
|
|
|
|
2018-02-04 18:27:05 +01:00
|
|
|
void FileNode::set_generate_location(unique_ptr<FullGenerateFileLocation> &&generate) {
|
|
|
|
bool is_changed = generate_ == nullptr ? generate != nullptr : generate == nullptr || *generate_ != *generate;
|
|
|
|
if (is_changed) {
|
|
|
|
generate_ = std::move(generate);
|
2018-12-31 20:04:05 +01:00
|
|
|
on_pmc_changed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileNode::set_size(int64 size) {
|
|
|
|
if (size_ != size) {
|
|
|
|
VLOG(update_file) << "File " << main_file_id_ << " has changed size to " << size;
|
|
|
|
size_ = size;
|
|
|
|
on_changed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileNode::set_expected_size(int64 expected_size) {
|
|
|
|
if (expected_size_ != expected_size) {
|
|
|
|
VLOG(update_file) << "File " << main_file_id_ << " has changed expected size to " << expected_size;
|
|
|
|
expected_size_ = expected_size;
|
|
|
|
on_changed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-18 17:29:44 +01:00
|
|
|
void FileNode::set_remote_name(string remote_name) {
|
|
|
|
if (remote_name_ != remote_name) {
|
|
|
|
remote_name_ = std::move(remote_name);
|
2018-12-31 20:04:05 +01:00
|
|
|
on_pmc_changed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileNode::set_url(string url) {
|
|
|
|
if (url_ != url) {
|
|
|
|
VLOG(update_file) << "File " << main_file_id_ << " has changed URL to " << url;
|
|
|
|
url_ = std::move(url);
|
|
|
|
on_changed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileNode::set_owner_dialog_id(DialogId owner_id) {
|
|
|
|
if (owner_dialog_id_ != owner_id) {
|
|
|
|
owner_dialog_id_ = owner_id;
|
|
|
|
on_pmc_changed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileNode::set_encryption_key(FileEncryptionKey key) {
|
|
|
|
if (encryption_key_ != key) {
|
|
|
|
encryption_key_ = std::move(key);
|
|
|
|
on_pmc_changed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-10 22:12:51 +01:00
|
|
|
void FileNode::set_upload_pause(FileId upload_pause) {
|
|
|
|
if (upload_pause_ != upload_pause) {
|
|
|
|
LOG(INFO) << "Change file " << main_file_id_ << " upload_pause from " << upload_pause_ << " to " << upload_pause;
|
2022-11-21 13:28:32 +01:00
|
|
|
if (upload_pause_.is_valid() != upload_pause.is_valid()) {
|
|
|
|
on_info_changed();
|
|
|
|
}
|
2019-01-10 22:12:51 +01:00
|
|
|
upload_pause_ = upload_pause;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-20 12:47:53 +01:00
|
|
|
void FileNode::set_download_priority(int8 priority) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if ((download_priority_ == 0) != (priority == 0)) {
|
|
|
|
VLOG(update_file) << "File " << main_file_id_ << " has changed download priority to " << priority;
|
|
|
|
on_info_changed();
|
|
|
|
}
|
|
|
|
download_priority_ = priority;
|
|
|
|
}
|
|
|
|
|
2018-01-20 12:47:53 +01:00
|
|
|
void FileNode::set_upload_priority(int8 priority) {
|
2019-02-14 12:20:40 +01:00
|
|
|
if (!remote_.is_full_alive && (upload_priority_ == 0) != (priority == 0)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
VLOG(update_file) << "File " << main_file_id_ << " has changed upload priority to " << priority;
|
|
|
|
on_info_changed();
|
|
|
|
}
|
|
|
|
upload_priority_ = priority;
|
|
|
|
}
|
|
|
|
|
2018-01-20 12:47:53 +01:00
|
|
|
void FileNode::set_generate_priority(int8 download_priority, int8 upload_priority) {
|
2018-08-13 22:18:27 +02:00
|
|
|
if ((generate_download_priority_ == 0) != (download_priority == 0) ||
|
|
|
|
(generate_upload_priority_ == 0) != (upload_priority == 0)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
VLOG(update_file) << "File " << main_file_id_ << " has changed generate priority to " << download_priority << "/"
|
|
|
|
<< upload_priority;
|
|
|
|
on_info_changed();
|
|
|
|
}
|
2018-02-12 11:37:54 +01:00
|
|
|
generate_priority_ = max(download_priority, upload_priority);
|
2018-12-31 20:04:05 +01:00
|
|
|
generate_download_priority_ = download_priority;
|
|
|
|
generate_upload_priority_ = upload_priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileNode::on_changed() {
|
|
|
|
on_pmc_changed();
|
|
|
|
on_info_changed();
|
|
|
|
}
|
2019-02-14 22:19:48 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void FileNode::on_info_changed() {
|
|
|
|
info_changed_flag_ = true;
|
|
|
|
}
|
2019-02-14 22:19:48 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void FileNode::on_pmc_changed() {
|
|
|
|
pmc_changed_flag_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileNode::need_info_flush() const {
|
|
|
|
return info_changed_flag_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileNode::need_pmc_flush() const {
|
|
|
|
if (!pmc_changed_flag_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// already in pmc
|
2019-01-06 21:39:10 +01:00
|
|
|
if (pmc_id_.is_valid()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We must save encryption key
|
|
|
|
if (!encryption_key_.empty()) {
|
2018-01-20 15:57:52 +01:00
|
|
|
// && remote_.type() != RemoteFileLocation::Type::Empty
|
2018-12-31 20:04:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-04 18:27:05 +01:00
|
|
|
bool has_generate_location = generate_ != nullptr;
|
2018-12-31 20:04:05 +01:00
|
|
|
// Do not save "#file_id#" conversion.
|
2018-02-04 18:27:05 +01:00
|
|
|
if (has_generate_location && begins_with(generate_->conversion_, "#file_id#")) {
|
2018-12-31 20:04:05 +01:00
|
|
|
has_generate_location = false;
|
|
|
|
}
|
|
|
|
|
2019-02-14 12:20:40 +01:00
|
|
|
if (remote_.full/* &&
|
2019-01-30 22:37:38 +01:00
|
|
|
(has_generate_location || local_.type() != LocalFileLocation::Type::Empty)*/) {
|
|
|
|
// we need to always save file sources
|
2018-12-31 20:04:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
2019-02-14 12:20:40 +01:00
|
|
|
if (local_.type() == LocalFileLocation::Type::Full && (has_generate_location || remote_.full || remote_.partial)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Generate location with constant conversion
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileNode::on_pmc_flushed() {
|
|
|
|
pmc_changed_flag_ = false;
|
|
|
|
}
|
|
|
|
void FileNode::on_info_flushed() {
|
|
|
|
info_changed_flag_ = false;
|
|
|
|
}
|
|
|
|
|
2021-01-26 00:39:48 +01:00
|
|
|
string FileNode::suggested_path() const {
|
2018-02-19 23:28:06 +01:00
|
|
|
if (!remote_name_.empty()) {
|
|
|
|
return remote_name_;
|
|
|
|
}
|
|
|
|
if (!url_.empty()) {
|
2018-02-20 00:29:19 +01:00
|
|
|
auto file_name = get_url_file_name(url_);
|
|
|
|
if (!file_name.empty()) {
|
|
|
|
return file_name;
|
|
|
|
}
|
2018-02-19 23:28:06 +01:00
|
|
|
}
|
|
|
|
if (generate_ != nullptr) {
|
2018-02-20 00:29:19 +01:00
|
|
|
if (!generate_->original_path_.empty()) {
|
|
|
|
return generate_->original_path_;
|
2018-02-19 23:28:06 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-20 00:29:19 +01:00
|
|
|
return local_.file_name().str();
|
2018-02-19 23:28:06 +01:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
/*** FileView ***/
|
|
|
|
bool FileView::has_local_location() const {
|
2018-01-20 15:57:52 +01:00
|
|
|
return node_->local_.type() == LocalFileLocation::Type::Full;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-01-05 16:13:27 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
const FullLocalFileLocation &FileView::local_location() const {
|
|
|
|
CHECK(has_local_location());
|
|
|
|
return node_->local_.full();
|
|
|
|
}
|
2019-01-05 16:13:27 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
bool FileView::has_remote_location() const {
|
2019-06-18 02:21:55 +02:00
|
|
|
return static_cast<bool>(node_->remote_.full);
|
2019-02-14 12:20:40 +01:00
|
|
|
}
|
2019-10-09 01:59:25 +02:00
|
|
|
|
2019-02-14 12:20:40 +01:00
|
|
|
bool FileView::has_alive_remote_location() const {
|
|
|
|
return node_->remote_.is_full_alive;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-01-05 16:13:27 +01:00
|
|
|
|
2019-01-30 17:08:50 +01:00
|
|
|
bool FileView::has_active_upload_remote_location() const {
|
2019-11-17 20:41:28 +01:00
|
|
|
if (!has_remote_location()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-14 12:20:40 +01:00
|
|
|
if (!has_alive_remote_location()) {
|
2018-12-05 10:32:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-17 20:41:28 +01:00
|
|
|
if (main_remote_location().is_encrypted_any()) {
|
2018-12-05 10:32:31 +01:00
|
|
|
return true;
|
|
|
|
}
|
2019-11-17 20:41:28 +01:00
|
|
|
return main_remote_location().has_file_reference();
|
2019-01-30 17:08:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileView::has_active_download_remote_location() const {
|
|
|
|
if (!has_remote_location()) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-07-26 16:34:26 +02:00
|
|
|
auto &remote = remote_location();
|
|
|
|
if (remote.is_encrypted_any()) {
|
2019-01-30 17:08:50 +01:00
|
|
|
return true;
|
|
|
|
}
|
2023-07-26 16:34:26 +02:00
|
|
|
return remote.has_file_reference();
|
2018-12-05 10:32:31 +01:00
|
|
|
}
|
2019-01-05 16:13:27 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
const FullRemoteFileLocation &FileView::remote_location() const {
|
|
|
|
CHECK(has_remote_location());
|
2018-03-06 19:31:20 +01:00
|
|
|
auto *remote = node_.get_remote();
|
|
|
|
if (remote) {
|
|
|
|
return *remote;
|
|
|
|
}
|
2019-02-14 12:20:40 +01:00
|
|
|
return node_->remote_.full.value();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-01-05 16:13:27 +01:00
|
|
|
|
2019-11-17 20:41:28 +01:00
|
|
|
const FullRemoteFileLocation &FileView::main_remote_location() const {
|
|
|
|
CHECK(has_remote_location());
|
|
|
|
return node_->remote_.full.value();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
bool FileView::has_generate_location() const {
|
2018-02-04 18:27:05 +01:00
|
|
|
return node_->generate_ != nullptr;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-01-05 16:13:27 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
const FullGenerateFileLocation &FileView::generate_location() const {
|
|
|
|
CHECK(has_generate_location());
|
2018-02-04 18:27:05 +01:00
|
|
|
return *node_->generate_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int64 FileView::size() const {
|
|
|
|
return node_->size_;
|
|
|
|
}
|
|
|
|
|
2020-01-03 02:08:22 +01:00
|
|
|
int64 FileView::get_allocated_local_size() const {
|
|
|
|
auto file_path = path();
|
|
|
|
if (file_path.empty()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
auto r_stat = stat(file_path);
|
|
|
|
if (r_stat.is_error()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return r_stat.ok().real_size_;
|
|
|
|
}
|
|
|
|
|
2023-07-26 17:07:33 +02:00
|
|
|
int64 FileNode::expected_size(bool may_guess) const {
|
|
|
|
if (size_ != 0) {
|
|
|
|
return size_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-12-27 09:34:36 +01:00
|
|
|
int64 current_size = local_total_size(); // TODO: this is not the best approximation
|
2023-07-26 17:07:33 +02:00
|
|
|
if (expected_size_ != 0) {
|
|
|
|
return max(current_size, expected_size_);
|
2018-12-27 09:34:36 +01:00
|
|
|
}
|
2023-07-26 17:07:33 +02:00
|
|
|
if (may_guess && local_.type() == LocalFileLocation::Type::Partial) {
|
2018-12-27 09:34:36 +01:00
|
|
|
current_size *= 3;
|
|
|
|
}
|
|
|
|
return current_size;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-07-26 17:07:33 +02:00
|
|
|
int64 FileView::expected_size(bool may_guess) const {
|
|
|
|
return node_->expected_size(may_guess);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileNode::is_downloading() const {
|
|
|
|
return download_priority_ != 0 || generate_download_priority_ != 0;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
bool FileView::is_downloading() const {
|
2023-07-26 17:07:33 +02:00
|
|
|
return node_->is_downloading();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-11-11 12:38:04 +01:00
|
|
|
int64 FileView::download_offset() const {
|
|
|
|
return node_->download_offset_;
|
|
|
|
}
|
2018-12-27 02:21:08 +01:00
|
|
|
|
2023-07-26 17:07:33 +02:00
|
|
|
int64 FileNode::downloaded_prefix(int64 offset) const {
|
|
|
|
switch (local_.type()) {
|
2018-11-11 12:38:04 +01:00
|
|
|
case LocalFileLocation::Type::Empty:
|
|
|
|
return 0;
|
|
|
|
case LocalFileLocation::Type::Full:
|
2023-07-26 17:07:33 +02:00
|
|
|
if (offset < size_) {
|
|
|
|
return size_ - offset;
|
2018-11-11 12:38:04 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
case LocalFileLocation::Type::Partial:
|
2023-07-26 17:07:33 +02:00
|
|
|
if (get_type() == FileType::SecureEncrypted) {
|
2018-12-26 17:11:15 +01:00
|
|
|
// File is not decrypted and verified yet
|
|
|
|
return 0;
|
|
|
|
}
|
2023-07-26 17:07:33 +02:00
|
|
|
return Bitmask(Bitmask::Decode{}, local_.partial().ready_bitmask_)
|
|
|
|
.get_ready_prefix_size(offset, local_.partial().part_size_, size_);
|
2018-11-11 12:38:04 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-26 17:07:33 +02:00
|
|
|
int64 FileView::downloaded_prefix(int64 offset) const {
|
|
|
|
return node_->downloaded_prefix(offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 FileNode::local_prefix_size() const {
|
|
|
|
switch (local_.type()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
case LocalFileLocation::Type::Full:
|
2023-07-26 17:07:33 +02:00
|
|
|
return download_offset_ <= size_ ? size_ - download_offset_ : 0;
|
2018-04-03 19:49:07 +02:00
|
|
|
case LocalFileLocation::Type::Partial: {
|
2023-07-26 17:07:33 +02:00
|
|
|
if (get_type() == FileType::SecureEncrypted) {
|
2018-12-26 17:11:15 +01:00
|
|
|
// File is not decrypted and verified yet
|
2018-04-03 19:49:07 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2023-07-26 17:07:33 +02:00
|
|
|
return local_ready_prefix_size_;
|
2018-04-03 19:49:07 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2023-07-26 17:07:33 +02:00
|
|
|
|
|
|
|
int64 FileView::local_prefix_size() const {
|
|
|
|
return node_->local_prefix_size();
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 FileNode::local_total_size() const {
|
|
|
|
switch (local_.type()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
case LocalFileLocation::Type::Empty:
|
|
|
|
return 0;
|
|
|
|
case LocalFileLocation::Type::Full:
|
2023-07-26 17:07:33 +02:00
|
|
|
return size_;
|
2018-12-31 20:04:05 +01:00
|
|
|
case LocalFileLocation::Type::Partial:
|
2023-07-26 17:07:33 +02:00
|
|
|
VLOG(update_file) << "Have local_ready_prefix_size = " << local_ready_prefix_size_
|
|
|
|
<< " and local_ready_size = " << local_ready_size_;
|
|
|
|
return max(local_ready_prefix_size_, local_ready_size_);
|
2018-12-31 20:04:05 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-26 17:07:33 +02:00
|
|
|
int64 FileView::local_total_size() const {
|
|
|
|
return node_->local_total_size();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileNode::is_uploading() const {
|
|
|
|
return upload_priority_ != 0 || generate_upload_priority_ != 0 || upload_pause_.is_valid();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
bool FileView::is_uploading() const {
|
2023-07-26 17:07:33 +02:00
|
|
|
return node_->is_uploading();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-07-26 17:07:33 +02:00
|
|
|
int64 FileNode::remote_size() const {
|
|
|
|
if (remote_.is_full_alive) {
|
|
|
|
return size_;
|
|
|
|
}
|
|
|
|
if (remote_.partial) {
|
|
|
|
auto part_size = static_cast<int64>(remote_.partial->part_size_);
|
|
|
|
auto ready_part_count = remote_.partial->ready_part_count_;
|
|
|
|
auto remote_ready_size = remote_.ready_size;
|
2019-02-14 12:20:40 +01:00
|
|
|
VLOG(update_file) << "Have part_size = " << part_size << ", remote_ready_part_count = " << ready_part_count
|
2023-07-26 17:07:33 +02:00
|
|
|
<< ", remote_ready_size = " << remote_ready_size << ", size = " << size_;
|
2019-02-14 12:20:40 +01:00
|
|
|
auto res = max(part_size * ready_part_count, remote_ready_size);
|
2023-07-26 17:07:33 +02:00
|
|
|
if (size_ != 0 && size_ < res) {
|
|
|
|
res = size_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-02-14 12:20:40 +01:00
|
|
|
return res;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-07-26 17:07:33 +02:00
|
|
|
return remote_.ready_size; //???
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-07-26 17:07:33 +02:00
|
|
|
int64 FileView::remote_size() const {
|
|
|
|
return node_->remote_size();
|
|
|
|
}
|
|
|
|
|
|
|
|
string FileNode::path() const {
|
|
|
|
switch (local_.type()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
case LocalFileLocation::Type::Full:
|
2023-07-26 17:07:33 +02:00
|
|
|
return local_.full().path_;
|
2018-12-31 20:04:05 +01:00
|
|
|
case LocalFileLocation::Type::Partial:
|
2023-07-26 17:07:33 +02:00
|
|
|
return local_.partial().path_;
|
2018-12-31 20:04:05 +01:00
|
|
|
default:
|
2022-05-11 16:17:20 +02:00
|
|
|
return string();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-26 17:07:33 +02:00
|
|
|
string FileView::path() const {
|
|
|
|
return node_->path();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
bool FileView::has_url() const {
|
|
|
|
return !node_->url_.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
const string &FileView::url() const {
|
|
|
|
return node_->url_;
|
|
|
|
}
|
|
|
|
|
2018-02-18 17:29:44 +01:00
|
|
|
const string &FileView::remote_name() const {
|
|
|
|
return node_->remote_name_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-01-26 00:39:48 +01:00
|
|
|
string FileView::suggested_path() const {
|
|
|
|
return node_->suggested_path();
|
2018-02-19 23:28:06 +01:00
|
|
|
}
|
|
|
|
|
2018-01-20 11:49:06 +01:00
|
|
|
DialogId FileView::owner_dialog_id() const {
|
2018-12-31 20:04:05 +01:00
|
|
|
return node_->owner_dialog_id_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileView::get_by_hash() const {
|
|
|
|
return node_->get_by_hash_;
|
|
|
|
}
|
|
|
|
|
2018-01-30 17:54:17 +01:00
|
|
|
FileView::FileView(ConstFileNodePtr node) : node_(node) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileView::empty() const {
|
2018-01-30 21:04:22 +01:00
|
|
|
return !node_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileView::can_download_from_server() const {
|
|
|
|
if (!has_remote_location()) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-07-26 16:34:26 +02:00
|
|
|
auto &remote = remote_location();
|
|
|
|
if (remote.file_type_ == FileType::Encrypted && encryption_key().empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
2023-07-26 16:34:26 +02:00
|
|
|
if (remote.is_web()) {
|
2018-06-25 23:10:53 +02:00
|
|
|
return true;
|
|
|
|
}
|
2023-07-26 16:34:26 +02:00
|
|
|
if (remote.get_dc_id().is_empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
2023-07-26 16:34:26 +02:00
|
|
|
if (!remote.is_encrypted_any() && !remote.has_file_reference() &&
|
2019-10-18 03:31:28 +02:00
|
|
|
((node_->download_id_ == 0 && node_->download_was_update_file_reference_) || !node_->remote_.is_full_alive)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
2018-06-25 23:10:53 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
bool FileView::can_generate() const {
|
|
|
|
return has_generate_location();
|
|
|
|
}
|
|
|
|
|
2023-07-26 17:07:33 +02:00
|
|
|
bool FileNode::can_delete() const {
|
|
|
|
if (local_.type() == LocalFileLocation::Type::Full) {
|
|
|
|
return begins_with(local_.full().path_, get_files_dir(get_type()));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-07-26 17:07:33 +02:00
|
|
|
return local_.type() == LocalFileLocation::Type::Partial;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileView::can_delete() const {
|
|
|
|
return node_->can_delete();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-07-26 15:57:32 +02:00
|
|
|
string FileNode::get_unique_id(const FullGenerateFileLocation &location) {
|
2020-07-06 17:31:26 +02:00
|
|
|
return base64url_encode(zero_encode('\xff' + serialize(location)));
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:57:32 +02:00
|
|
|
string FileNode::get_unique_id(const FullRemoteFileLocation &location) {
|
2020-07-06 17:31:26 +02:00
|
|
|
return base64url_encode(zero_encode(serialize(location.as_unique())));
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:57:32 +02:00
|
|
|
string FileNode::get_persistent_id(const FullGenerateFileLocation &location) {
|
2020-07-06 17:31:26 +02:00
|
|
|
auto binary = serialize(location);
|
|
|
|
|
|
|
|
binary = zero_encode(binary);
|
2023-07-26 15:57:32 +02:00
|
|
|
binary.push_back(PERSISTENT_ID_VERSION_GENERATED);
|
2020-07-06 17:31:26 +02:00
|
|
|
return base64url_encode(binary);
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:57:32 +02:00
|
|
|
string FileNode::get_persistent_id(const FullRemoteFileLocation &location) {
|
2020-07-06 17:31:26 +02:00
|
|
|
auto binary = serialize(location);
|
|
|
|
|
|
|
|
binary = zero_encode(binary);
|
|
|
|
binary.push_back(static_cast<char>(narrow_cast<uint8>(Version::Next) - 1));
|
2023-07-26 15:57:32 +02:00
|
|
|
binary.push_back(PERSISTENT_ID_VERSION);
|
2020-07-06 17:31:26 +02:00
|
|
|
return base64url_encode(binary);
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:57:32 +02:00
|
|
|
string FileNode::get_persistent_file_id() const {
|
|
|
|
if (remote_.is_full_alive) {
|
|
|
|
return get_persistent_id(remote_.full.value());
|
|
|
|
} else if (!url_.empty()) {
|
|
|
|
return url_;
|
|
|
|
} else if (generate_ != nullptr && FileManager::is_remotely_generated_file(generate_->conversion_)) {
|
|
|
|
return get_persistent_id(*generate_);
|
2020-07-06 17:31:26 +02:00
|
|
|
}
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:57:32 +02:00
|
|
|
string FileNode::get_unique_file_id() const {
|
|
|
|
if (remote_.is_full_alive) {
|
|
|
|
if (!remote_.full.value().is_web()) {
|
|
|
|
return get_unique_id(remote_.full.value());
|
2020-07-06 17:31:26 +02:00
|
|
|
}
|
2023-07-26 15:57:32 +02:00
|
|
|
} else if (generate_ != nullptr && FileManager::is_remotely_generated_file(generate_->conversion_)) {
|
|
|
|
return get_unique_id(*generate_);
|
2020-07-06 17:31:26 +02:00
|
|
|
}
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
/*** FileManager ***/
|
2019-02-14 12:20:40 +01:00
|
|
|
static int merge_choose_remote_location(const FullRemoteFileLocation &x, FileLocationSource x_source,
|
|
|
|
const FullRemoteFileLocation &y, FileLocationSource y_source);
|
2019-02-04 16:59:01 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace {
|
|
|
|
void prepare_path_for_pmc(FileType file_type, string &path) {
|
|
|
|
path = PathView::relative(path, get_files_base_dir(file_type)).str();
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
FileManager::FileManager(unique_ptr<Context> context) : context_(std::move(context)) {
|
2023-03-13 17:47:38 +01:00
|
|
|
if (G()->use_file_database()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
file_db_ = G()->td_db()->get_file_db_shared();
|
|
|
|
}
|
|
|
|
|
|
|
|
parent_ = context_->create_reference();
|
|
|
|
next_file_id();
|
|
|
|
next_file_node_id();
|
|
|
|
|
2021-10-23 20:17:00 +02:00
|
|
|
G()->td_db()->with_db_path([bad_paths = &bad_paths_](CSlice path) { bad_paths->insert(path.str()); });
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileManager::init_actor() {
|
2024-07-12 17:15:44 +02:00
|
|
|
file_download_manager_ = create_actor_on_scheduler<FileDownloadManager>(
|
|
|
|
"FileDownloadManager", G()->get_slow_net_scheduler_id(), make_unique<FileDownloadManagerCallback>(actor_id(this)),
|
|
|
|
context_->create_reference());
|
|
|
|
file_load_manager_ = create_actor_on_scheduler<FileLoadManager>("FileLoadManager", G()->get_slow_net_scheduler_id());
|
2024-07-12 16:27:26 +02:00
|
|
|
file_upload_manager_ = create_actor_on_scheduler<FileUploadManager>(
|
|
|
|
"FileUploadManager", G()->get_slow_net_scheduler_id(), make_unique<FileUploadManagerCallback>(actor_id(this)),
|
|
|
|
context_->create_reference());
|
2018-12-31 20:04:05 +01:00
|
|
|
file_generate_manager_ = create_actor_on_scheduler<FileGenerateManager>(
|
|
|
|
"FileGenerateManager", G()->get_slow_net_scheduler_id(), context_->create_reference());
|
|
|
|
}
|
2019-01-03 16:26:44 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
FileManager::~FileManager() {
|
2023-05-10 14:41:57 +02:00
|
|
|
Scheduler::instance()->destroy_on_scheduler(
|
|
|
|
G()->get_gc_scheduler_id(), remote_location_info_, file_hash_to_file_id_, remote_location_to_file_id_,
|
|
|
|
local_location_to_file_id_, generate_location_to_file_id_, file_id_info_, empty_file_ids_, file_nodes_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
string FileManager::fix_file_extension(Slice file_name, Slice file_type, Slice file_extension) {
|
2022-02-17 13:14:49 +01:00
|
|
|
return PSTRING() << (file_name.empty() ? file_type : file_name) << '.' << file_extension;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
string FileManager::get_file_name(FileType file_type, Slice path) {
|
|
|
|
PathView path_view(path);
|
|
|
|
auto file_name = path_view.file_name();
|
|
|
|
auto extension = path_view.extension();
|
|
|
|
switch (file_type) {
|
|
|
|
case FileType::Thumbnail:
|
|
|
|
if (extension != "jpg" && extension != "jpeg" && extension != "webp") {
|
|
|
|
return fix_file_extension(file_name, "thumbnail", "jpg");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FileType::ProfilePhoto:
|
|
|
|
case FileType::Photo:
|
2023-05-25 00:26:26 +02:00
|
|
|
case FileType::PhotoStory:
|
2018-12-31 20:04:05 +01:00
|
|
|
if (extension != "jpg" && extension != "jpeg" && extension != "gif" && extension != "png" && extension != "tif" &&
|
|
|
|
extension != "bmp") {
|
|
|
|
return fix_file_extension(file_name, "photo", "jpg");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FileType::VoiceNote:
|
|
|
|
if (extension != "ogg" && extension != "oga" && extension != "mp3" && extension != "mpeg3" &&
|
2021-01-26 00:55:48 +01:00
|
|
|
extension != "m4a" && extension != "opus") {
|
2018-12-31 20:04:05 +01:00
|
|
|
return fix_file_extension(file_name, "voice", "oga");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FileType::Video:
|
|
|
|
case FileType::VideoNote:
|
2022-02-17 13:14:49 +01:00
|
|
|
if (extension != "mov" && extension != "3gp" && extension != "mpeg4" && extension != "mp4" &&
|
|
|
|
extension != "mkv") {
|
2018-12-31 20:04:05 +01:00
|
|
|
return fix_file_extension(file_name, "video", "mp4");
|
|
|
|
}
|
|
|
|
break;
|
2023-05-25 13:19:54 +02:00
|
|
|
case FileType::VideoStory:
|
|
|
|
if (extension != "mp4") {
|
|
|
|
return fix_file_extension(file_name, "video", "mp4");
|
|
|
|
}
|
|
|
|
break;
|
2018-12-31 20:04:05 +01:00
|
|
|
case FileType::Audio:
|
|
|
|
if (extension != "ogg" && extension != "oga" && extension != "mp3" && extension != "mpeg3" &&
|
|
|
|
extension != "m4a") {
|
|
|
|
return fix_file_extension(file_name, "audio", "mp3");
|
|
|
|
}
|
|
|
|
break;
|
2019-05-07 04:51:56 +02:00
|
|
|
case FileType::Wallpaper:
|
|
|
|
case FileType::Background:
|
|
|
|
if (extension != "jpg" && extension != "jpeg" && extension != "png") {
|
|
|
|
return fix_file_extension(file_name, "wallpaper", "jpg");
|
|
|
|
}
|
|
|
|
break;
|
2018-12-31 20:04:05 +01:00
|
|
|
case FileType::Sticker:
|
2022-01-31 13:56:44 +01:00
|
|
|
if (extension != "webp" && extension != "tgs" && extension != "webm") {
|
2019-07-15 02:43:05 +02:00
|
|
|
return fix_file_extension(file_name, "sticker", "webp");
|
|
|
|
}
|
|
|
|
break;
|
2022-04-12 21:50:20 +02:00
|
|
|
case FileType::Ringtone:
|
|
|
|
if (extension != "ogg" && extension != "oga" && extension != "mp3" && extension != "mpeg3") {
|
2022-04-15 13:07:54 +02:00
|
|
|
return fix_file_extension(file_name, "notification_sound", "mp3");
|
2022-04-12 21:50:20 +02:00
|
|
|
}
|
|
|
|
break;
|
2019-07-15 02:43:05 +02:00
|
|
|
case FileType::Document:
|
2022-12-15 20:41:55 +01:00
|
|
|
case FileType::DocumentAsFile:
|
2018-12-31 20:04:05 +01:00
|
|
|
case FileType::Animation:
|
|
|
|
case FileType::Encrypted:
|
|
|
|
case FileType::Temp:
|
|
|
|
case FileType::EncryptedThumbnail:
|
2022-04-25 18:00:40 +02:00
|
|
|
case FileType::SecureEncrypted:
|
|
|
|
case FileType::SecureDecrypted:
|
2022-04-21 21:34:26 +02:00
|
|
|
case FileType::CallLog:
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
return file_name.str();
|
|
|
|
}
|
|
|
|
|
2022-07-28 14:59:57 +02:00
|
|
|
bool FileManager::is_remotely_generated_file(Slice conversion) {
|
2022-07-28 15:47:19 +02:00
|
|
|
return begins_with(conversion, "#map#") || begins_with(conversion, "#audio_t#");
|
2022-07-28 14:59:57 +02:00
|
|
|
}
|
|
|
|
|
2023-07-05 09:55:16 +02:00
|
|
|
vector<int> FileManager::get_missing_file_parts(const Status &error) {
|
|
|
|
vector<int> result;
|
|
|
|
auto error_message = error.message();
|
|
|
|
if (begins_with(error_message, "FILE_PART_") && ends_with(error_message, "_MISSING")) {
|
|
|
|
auto r_file_part = to_integer_safe<int>(error_message.substr(10, error_message.size() - 18));
|
|
|
|
if (r_file_part.is_error()) {
|
|
|
|
LOG(ERROR) << "Receive error " << error;
|
|
|
|
} else {
|
|
|
|
result.push_back(r_file_part.ok());
|
|
|
|
}
|
|
|
|
}
|
2023-07-05 12:08:10 +02:00
|
|
|
if (error_message == "FILE_PART_INVALID") {
|
|
|
|
result.push_back(0);
|
|
|
|
}
|
2023-07-05 09:55:16 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-10-09 20:20:57 +02:00
|
|
|
void FileManager::check_local_location(FileId file_id, bool skip_file_size_checks) {
|
2022-07-20 15:46:55 +02:00
|
|
|
auto node = get_sync_file_node(file_id);
|
|
|
|
if (node) {
|
2022-10-09 20:20:57 +02:00
|
|
|
check_local_location(node, skip_file_size_checks).ignore();
|
2022-07-20 15:46:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-25 17:25:04 +01:00
|
|
|
void FileManager::check_local_location_async(FileId file_id, bool skip_file_size_checks) {
|
|
|
|
auto node = get_sync_file_node(file_id);
|
|
|
|
if (node) {
|
|
|
|
check_local_location_async(node, skip_file_size_checks, Promise<Unit>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 20:20:57 +02:00
|
|
|
Status FileManager::check_local_location(FileNodePtr node, bool skip_file_size_checks) {
|
2018-12-31 20:04:05 +01:00
|
|
|
Status status;
|
2018-01-20 15:57:52 +01:00
|
|
|
if (node->local_.type() == LocalFileLocation::Type::Full) {
|
2022-10-10 11:58:48 +02:00
|
|
|
auto r_info = check_full_local_location({node->local_.full(), node->size_}, skip_file_size_checks);
|
2022-10-10 11:45:20 +02:00
|
|
|
if (r_info.is_error()) {
|
|
|
|
status = r_info.move_as_error();
|
|
|
|
} else if (bad_paths_.count(r_info.ok().location_.path_) != 0) {
|
2022-10-09 21:15:46 +02:00
|
|
|
status = Status::Error(400, "Sending of internal database files is forbidden");
|
2022-10-10 11:45:20 +02:00
|
|
|
} else if (r_info.ok().location_ != node->local_.full() || r_info.ok().size_ != node->size_) {
|
|
|
|
LOG(ERROR) << "Local location changed from " << node->local_.full() << " with size " << node->size_ << " to "
|
|
|
|
<< r_info.ok().location_ << " with size " << r_info.ok().size_;
|
2022-10-09 21:15:46 +02:00
|
|
|
}
|
2018-01-20 15:57:52 +01:00
|
|
|
} else if (node->local_.type() == LocalFileLocation::Type::Partial) {
|
2018-12-31 20:04:05 +01:00
|
|
|
status = check_partial_local_location(node->local_.partial());
|
|
|
|
}
|
|
|
|
if (status.is_error()) {
|
2022-10-10 13:19:35 +02:00
|
|
|
on_failed_check_local_location(node);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2022-10-10 13:19:35 +02:00
|
|
|
void FileManager::on_failed_check_local_location(FileNodePtr node) {
|
|
|
|
send_closure(G()->download_manager(), &DownloadManager::remove_file_if_finished, node->main_file_id_);
|
|
|
|
node->drop_local_location();
|
|
|
|
try_flush_node(node, "on_failed_check_local_location");
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileManager::check_local_location_async(FileNodePtr node, bool skip_file_size_checks, Promise<Unit> promise) {
|
|
|
|
if (node->local_.type() == LocalFileLocation::Type::Empty) {
|
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node->local_.type() == LocalFileLocation::Type::Full) {
|
|
|
|
send_closure(file_load_manager_, &FileLoadManager::check_full_local_location,
|
|
|
|
FullLocalLocationInfo{node->local_.full(), node->size_}, skip_file_size_checks,
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), file_id = node->main_file_id_,
|
|
|
|
checked_location = node->local_,
|
|
|
|
promise = std::move(promise)](Result<FullLocalLocationInfo> result) mutable {
|
|
|
|
send_closure(actor_id, &FileManager::on_check_full_local_location, file_id,
|
|
|
|
std::move(checked_location), std::move(result), std::move(promise));
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
CHECK(node->local_.type() == LocalFileLocation::Type::Partial);
|
|
|
|
send_closure(file_load_manager_, &FileLoadManager::check_partial_local_location, node->local_.partial(),
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), file_id = node->main_file_id_,
|
|
|
|
checked_location = node->local_,
|
|
|
|
promise = std::move(promise)](Result<Unit> result) mutable {
|
|
|
|
send_closure(actor_id, &FileManager::on_check_partial_local_location, file_id,
|
|
|
|
std::move(checked_location), std::move(result), std::move(promise));
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileManager::on_check_full_local_location(FileId file_id, LocalFileLocation checked_location,
|
|
|
|
Result<FullLocalLocationInfo> r_info, Promise<Unit> promise) {
|
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
2022-10-10 13:51:24 +02:00
|
|
|
|
2022-10-10 13:19:35 +02:00
|
|
|
auto node = get_file_node(file_id);
|
2022-10-10 13:51:24 +02:00
|
|
|
if (!node) {
|
|
|
|
return;
|
|
|
|
}
|
2022-10-10 13:19:35 +02:00
|
|
|
if (node->local_ != checked_location) {
|
|
|
|
LOG(INFO) << "Full location changed while being checked; ignore check result";
|
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
Status status;
|
|
|
|
if (r_info.is_error()) {
|
|
|
|
status = r_info.move_as_error();
|
|
|
|
} else if (bad_paths_.count(r_info.ok().location_.path_) != 0) {
|
|
|
|
status = Status::Error(400, "Sending of internal database files is forbidden");
|
|
|
|
} else if (r_info.ok().location_ != node->local_.full() || r_info.ok().size_ != node->size_) {
|
|
|
|
LOG(ERROR) << "Local location changed from " << node->local_.full() << " with size " << node->size_ << " to "
|
|
|
|
<< r_info.ok().location_ << " with size " << r_info.ok().size_;
|
|
|
|
}
|
|
|
|
if (status.is_error()) {
|
|
|
|
on_failed_check_local_location(node);
|
|
|
|
promise.set_error(std::move(status));
|
|
|
|
} else {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileManager::on_check_partial_local_location(FileId file_id, LocalFileLocation checked_location,
|
|
|
|
Result<Unit> result, Promise<Unit> promise) {
|
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
2022-10-10 13:51:24 +02:00
|
|
|
|
2022-10-10 13:19:35 +02:00
|
|
|
auto node = get_file_node(file_id);
|
|
|
|
CHECK(node);
|
|
|
|
if (node->local_ != checked_location) {
|
|
|
|
LOG(INFO) << "Partial location changed while being checked; ignore check result";
|
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
if (result.is_error()) {
|
|
|
|
on_failed_check_local_location(node);
|
|
|
|
promise.set_error(result.move_as_error());
|
|
|
|
} else {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-10 13:51:24 +02:00
|
|
|
void FileManager::recheck_full_local_location(FullLocalLocationInfo location_info, bool skip_file_size_checks) {
|
|
|
|
auto promise = PromiseCreator::lambda([actor_id = actor_id(this), checked_location = location_info.location_](
|
|
|
|
Result<FullLocalLocationInfo> result) mutable {
|
|
|
|
send_closure(actor_id, &FileManager::on_recheck_full_local_location, std::move(checked_location),
|
|
|
|
std::move(result));
|
|
|
|
});
|
|
|
|
send_closure(file_load_manager_, &FileLoadManager::check_full_local_location, std::move(location_info),
|
|
|
|
skip_file_size_checks, std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileManager::on_recheck_full_local_location(FullLocalFileLocation checked_location,
|
|
|
|
Result<FullLocalLocationInfo> r_info) {
|
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto file_id_it = local_location_to_file_id_.find(checked_location);
|
|
|
|
if (file_id_it == local_location_to_file_id_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto file_id = file_id_it->second;
|
|
|
|
|
|
|
|
on_check_full_local_location(file_id, LocalFileLocation(checked_location), std::move(r_info), Promise<Unit>());
|
|
|
|
}
|
|
|
|
|
2019-08-30 03:56:01 +02:00
|
|
|
bool FileManager::try_fix_partial_local_location(FileNodePtr node) {
|
2019-08-19 11:31:25 +02:00
|
|
|
LOG(INFO) << "Trying to fix partial local location";
|
|
|
|
if (node->local_.type() != LocalFileLocation::Type::Partial) {
|
|
|
|
LOG(INFO) << " failed - not a partial location";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto partial = node->local_.partial();
|
|
|
|
if (!partial.iv_.empty()) {
|
|
|
|
// can't recalc iv_
|
|
|
|
LOG(INFO) << " failed - partial location has nonempty iv";
|
|
|
|
return false;
|
|
|
|
}
|
2022-05-11 16:17:20 +02:00
|
|
|
if (partial.part_size_ >= 512 * (1 << 10) || (partial.part_size_ & (partial.part_size_ - 1)) != 0) {
|
2019-08-19 11:31:25 +02:00
|
|
|
LOG(INFO) << " failed - too big part_size already: " << partial.part_size_;
|
|
|
|
return false;
|
|
|
|
}
|
2022-05-11 16:17:20 +02:00
|
|
|
auto old_part_size = narrow_cast<int32>(partial.part_size_);
|
2019-08-19 11:31:25 +02:00
|
|
|
int new_part_size = 512 * (1 << 10);
|
|
|
|
auto k = new_part_size / old_part_size;
|
|
|
|
Bitmask mask(Bitmask::Decode(), partial.ready_bitmask_);
|
|
|
|
auto new_mask = mask.compress(k);
|
|
|
|
|
|
|
|
partial.part_size_ = new_part_size;
|
|
|
|
partial.ready_bitmask_ = new_mask.encode();
|
|
|
|
|
|
|
|
auto ready_size = new_mask.get_total_size(partial.part_size_, node->size_);
|
2021-10-19 17:11:16 +02:00
|
|
|
node->set_local_location(LocalFileLocation(std::move(partial)), ready_size, -1, -1);
|
2019-08-19 11:31:25 +02:00
|
|
|
LOG(INFO) << " ok: increase part_size " << old_part_size << "->" << new_part_size;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
FileManager::FileIdInfo *FileManager::get_file_id_info(FileId file_id) {
|
2021-07-20 17:49:29 +02:00
|
|
|
CHECK(static_cast<size_t>(file_id.get()) < file_id_info_.size());
|
2018-12-31 20:04:05 +01:00
|
|
|
return &file_id_info_[file_id.get()];
|
|
|
|
}
|
|
|
|
|
2022-11-21 09:40:43 +01:00
|
|
|
FileId FileManager::dup_file_id(FileId file_id, const char *source) {
|
2018-12-31 20:04:05 +01:00
|
|
|
int32 file_node_id;
|
2018-01-30 21:04:22 +01:00
|
|
|
auto *file_node = get_file_node_raw(file_id, &file_node_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!file_node) {
|
|
|
|
return FileId();
|
|
|
|
}
|
2023-05-24 14:28:40 +02:00
|
|
|
auto result_file_id = FileId(create_file_id(file_node_id, file_node).get(), file_id.get_remote());
|
|
|
|
LOG(INFO) << "Dup file " << file_id << " to " << result_file_id << " from " << source;
|
|
|
|
return result_file_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileId FileManager::copy_file_id(FileId file_id, FileType file_type, DialogId owner_dialog_id, const char *source) {
|
|
|
|
auto file_view = get_file_view(file_id);
|
|
|
|
auto download_file_id = dup_file_id(file_id, source);
|
|
|
|
auto result_file_id =
|
|
|
|
register_generate(file_type, FileLocationSource::FromServer, file_view.suggested_path(),
|
|
|
|
PSTRING() << "#file_id#" << download_file_id.get(), owner_dialog_id, file_view.size())
|
|
|
|
.ok();
|
|
|
|
LOG(INFO) << "Copy file " << file_id << " to " << result_file_id << " from " << source;
|
|
|
|
return result_file_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-01-30 21:04:22 +01:00
|
|
|
FileId FileManager::create_file_id(int32 file_node_id, FileNode *file_node) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto file_id = next_file_id();
|
|
|
|
get_file_id_info(file_id)->node_id_ = file_node_id;
|
|
|
|
file_node->file_ids_.push_back(file_id);
|
|
|
|
return file_id;
|
|
|
|
}
|
2019-01-21 18:19:02 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void FileManager::try_forget_file_id(FileId file_id) {
|
|
|
|
auto *info = get_file_id_info(file_id);
|
2019-01-21 18:19:02 +01:00
|
|
|
if (info->send_updates_flag_ || info->pin_flag_ || info->sent_file_id_flag_) {
|
2022-11-13 09:11:18 +01:00
|
|
|
LOG(DEBUG) << "Can't forget file " << file_id << ", because of"
|
|
|
|
<< (info->send_updates_flag_ ? " (sent updates)" : "") << (info->pin_flag_ ? " (pin)" : "")
|
|
|
|
<< (info->sent_file_id_flag_ ? " (sent file identifier)" : "");
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-01-30 21:04:22 +01:00
|
|
|
auto file_node = get_file_node(file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (file_node->main_file_id_ == file_id) {
|
2022-11-13 09:11:18 +01:00
|
|
|
LOG(DEBUG) << "Can't forget main file " << file_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-05 01:40:00 +02:00
|
|
|
LOG(DEBUG) << "Forget file " << file_id;
|
2019-10-22 01:12:58 +02:00
|
|
|
bool is_removed = td::remove(file_node->file_ids_, file_id);
|
|
|
|
CHECK(is_removed);
|
2018-01-30 21:04:22 +01:00
|
|
|
*info = FileIdInfo();
|
2018-03-07 00:37:39 +01:00
|
|
|
empty_file_ids_.push_back(file_id.get());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FileId FileManager::register_empty(FileType type) {
|
|
|
|
return register_local(FullLocalFileLocation(type, "", 0), DialogId(), 0, false, true).ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileManager::on_file_unlink(const FullLocalFileLocation &location) {
|
|
|
|
auto it = local_location_to_file_id_.find(location);
|
|
|
|
if (it == local_location_to_file_id_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto file_id = it->second;
|
2018-01-30 21:04:22 +01:00
|
|
|
auto file_node = get_sync_file_node(file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(file_node);
|
2022-07-16 16:25:28 +02:00
|
|
|
clear_from_pmc(file_node);
|
2022-03-05 02:11:18 +01:00
|
|
|
send_closure(G()->download_manager(), &DownloadManager::remove_file_if_finished, file_node->main_file_id_);
|
2018-12-26 17:11:15 +01:00
|
|
|
file_node->drop_local_location();
|
2022-10-02 22:42:33 +02:00
|
|
|
try_flush_node(file_node, "on_file_unlink");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result<FileId> FileManager::register_local(FullLocalFileLocation location, DialogId owner_dialog_id, int64 size,
|
2022-11-12 07:47:33 +01:00
|
|
|
bool get_by_hash, bool force, bool skip_file_size_checks,
|
|
|
|
FileId merge_file_id) {
|
2018-12-31 20:04:05 +01:00
|
|
|
// TODO: use get_by_hash
|
|
|
|
FileData data;
|
|
|
|
data.local_ = LocalFileLocation(std::move(location));
|
|
|
|
data.owner_dialog_id_ = owner_dialog_id;
|
|
|
|
data.size_ = size;
|
2022-11-12 07:47:33 +01:00
|
|
|
return register_file(std::move(data), FileLocationSource::None /*won't be used*/, merge_file_id, "register_local",
|
|
|
|
force, skip_file_size_checks);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
FileId FileManager::register_remote(FullRemoteFileLocation location, FileLocationSource file_location_source,
|
2020-07-20 16:46:54 +02:00
|
|
|
DialogId owner_dialog_id, int64 size, int64 expected_size, string remote_name) {
|
2018-12-31 20:04:05 +01:00
|
|
|
FileData data;
|
2021-10-19 17:11:16 +02:00
|
|
|
auto url = location.get_url();
|
|
|
|
data.remote_ = RemoteFileLocation(std::move(location));
|
2018-12-31 20:04:05 +01:00
|
|
|
data.owner_dialog_id_ = owner_dialog_id;
|
|
|
|
data.size_ = size;
|
|
|
|
data.expected_size_ = expected_size;
|
2020-07-20 16:46:54 +02:00
|
|
|
data.remote_name_ = std::move(remote_name);
|
2018-04-01 22:15:44 +02:00
|
|
|
|
2022-11-12 07:47:33 +01:00
|
|
|
auto file_id = register_file(std::move(data), file_location_source, FileId(), "register_remote", false).move_as_ok();
|
2018-04-01 22:15:44 +02:00
|
|
|
if (!url.empty()) {
|
|
|
|
auto file_node = get_file_node(file_id);
|
|
|
|
CHECK(file_node);
|
|
|
|
file_node->set_url(url);
|
|
|
|
}
|
|
|
|
return file_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-01-26 13:56:19 +01:00
|
|
|
FileId FileManager::register_url(string url, FileType file_type, FileLocationSource file_location_source,
|
|
|
|
DialogId owner_dialog_id) {
|
|
|
|
auto file_id = register_generate(file_type, file_location_source, url, "#url#", owner_dialog_id, 0).ok();
|
2018-01-30 21:04:22 +01:00
|
|
|
auto file_node = get_file_node(file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(file_node);
|
|
|
|
file_node->set_url(url);
|
|
|
|
return file_id;
|
|
|
|
}
|
|
|
|
|
2018-01-26 13:56:19 +01:00
|
|
|
Result<FileId> FileManager::register_generate(FileType file_type, FileLocationSource file_location_source,
|
|
|
|
string original_path, string conversion, DialogId owner_dialog_id,
|
|
|
|
int64 expected_size) {
|
2018-10-30 15:25:31 +01:00
|
|
|
// add #mtime# into conversion
|
2018-11-01 16:54:08 +01:00
|
|
|
if (!original_path.empty() && conversion[0] != '#' && PathView(original_path).is_absolute()) {
|
2019-01-17 23:25:11 +01:00
|
|
|
auto file_paths = log_interface->get_file_paths();
|
2019-10-22 01:12:58 +02:00
|
|
|
if (!td::contains(file_paths, original_path)) {
|
2019-01-17 23:25:11 +01:00
|
|
|
auto r_stat = stat(original_path);
|
|
|
|
uint64 mtime = r_stat.is_ok() ? r_stat.ok().mtime_nsec_ : 0;
|
|
|
|
conversion = PSTRING() << "#mtime#" << lpad0(to_string(mtime), 20) << '#' << conversion;
|
|
|
|
}
|
2018-10-30 15:25:31 +01:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
FileData data;
|
2018-09-27 03:19:03 +02:00
|
|
|
data.generate_ =
|
|
|
|
td::make_unique<FullGenerateFileLocation>(file_type, std::move(original_path), std::move(conversion));
|
2018-12-31 20:04:05 +01:00
|
|
|
data.owner_dialog_id_ = owner_dialog_id;
|
|
|
|
data.expected_size_ = expected_size;
|
2022-11-12 07:47:33 +01:00
|
|
|
return register_file(std::move(data), file_location_source, FileId(), "register_generate", false);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-11-12 07:47:33 +01:00
|
|
|
Result<FileId> FileManager::register_file(FileData &&data, FileLocationSource file_location_source,
|
|
|
|
FileId merge_file_id, const char *source, bool force,
|
|
|
|
bool skip_file_size_checks) {
|
2018-01-20 15:57:52 +01:00
|
|
|
bool has_remote = data.remote_.type() == RemoteFileLocation::Type::Full;
|
2018-02-04 18:27:05 +01:00
|
|
|
bool has_generate = data.generate_ != nullptr;
|
2018-01-20 15:57:52 +01:00
|
|
|
if (data.local_.type() == LocalFileLocation::Type::Full && !force) {
|
2022-06-24 13:59:42 +02:00
|
|
|
bool is_from_database = file_location_source == FileLocationSource::FromBinlog ||
|
|
|
|
file_location_source == FileLocationSource::FromDatabase;
|
|
|
|
if (is_from_database) {
|
2018-12-31 20:04:05 +01:00
|
|
|
PathView path_view(data.local_.full().path_);
|
|
|
|
if (path_view.is_relative()) {
|
2019-01-19 23:59:37 +01:00
|
|
|
data.local_.full().path_ = PSTRING()
|
|
|
|
<< get_files_base_dir(data.local_.full().file_type_) << data.local_.full().path_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-10 13:51:24 +02:00
|
|
|
if (file_location_source != FileLocationSource::FromDatabase) {
|
2022-10-10 11:45:20 +02:00
|
|
|
Status status;
|
2022-10-10 11:58:48 +02:00
|
|
|
auto r_info = check_full_local_location({data.local_.full(), data.size_}, skip_file_size_checks);
|
2022-10-10 11:45:20 +02:00
|
|
|
if (r_info.is_error()) {
|
|
|
|
status = r_info.move_as_error();
|
|
|
|
} else if (bad_paths_.count(r_info.ok().location_.path_) != 0) {
|
2022-10-09 21:15:46 +02:00
|
|
|
status = Status::Error(400, "Sending of internal database files is forbidden");
|
|
|
|
}
|
2022-06-24 13:59:42 +02:00
|
|
|
if (status.is_error()) {
|
|
|
|
LOG(INFO) << "Invalid " << data.local_.full() << ": " << status << " from " << source;
|
|
|
|
data.local_ = LocalFileLocation();
|
|
|
|
if (data.remote_.type() == RemoteFileLocation::Type::Partial) {
|
|
|
|
data.remote_ = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!has_remote && !has_generate) {
|
|
|
|
return std::move(status);
|
|
|
|
}
|
2022-10-10 11:45:20 +02:00
|
|
|
} else {
|
|
|
|
data.local_ = LocalFileLocation(std::move(r_info.ok().location_));
|
|
|
|
data.size_ = r_info.ok().size_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-10-10 13:51:24 +02:00
|
|
|
} else {
|
|
|
|
// the location has been checked previously, but recheck it just in case
|
|
|
|
recheck_full_local_location({data.local_.full(), data.size_}, skip_file_size_checks);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-20 15:57:52 +01:00
|
|
|
bool has_local = data.local_.type() == LocalFileLocation::Type::Full;
|
2018-12-31 20:04:05 +01:00
|
|
|
bool has_location = has_local || has_remote || has_generate;
|
|
|
|
if (!has_location) {
|
2021-04-29 15:03:50 +02:00
|
|
|
return Status::Error(400, "No location");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-05-10 16:13:16 +02:00
|
|
|
if (data.size_ < 0) {
|
2023-07-22 18:03:29 +02:00
|
|
|
LOG(ERROR) << "Receive file of size " << data.size_ << " from " << source;
|
2022-05-10 16:13:16 +02:00
|
|
|
data.size_ = 0;
|
|
|
|
}
|
|
|
|
if (data.expected_size_ < 0) {
|
2023-07-22 18:03:29 +02:00
|
|
|
LOG(ERROR) << "Receive file of expected size " << data.expected_size_ << " from " << source;
|
2022-05-10 16:13:16 +02:00
|
|
|
data.expected_size_ = 0;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
FileId file_id = next_file_id();
|
|
|
|
|
|
|
|
LOG(INFO) << "Register file data " << data << " as " << file_id << " from " << source;
|
|
|
|
// create FileNode
|
|
|
|
auto file_node_id = next_file_node_id();
|
|
|
|
auto &node = file_nodes_[file_node_id];
|
2019-02-14 12:20:40 +01:00
|
|
|
node = td::make_unique<FileNode>(std::move(data.local_), NewRemoteFileLocation(data.remote_, file_location_source),
|
|
|
|
std::move(data.generate_), data.size_, data.expected_size_,
|
|
|
|
std::move(data.remote_name_), std::move(data.url_), data.owner_dialog_id_,
|
|
|
|
std::move(data.encryption_key_), file_id, static_cast<int8>(has_remote));
|
2019-01-06 21:39:10 +01:00
|
|
|
node->pmc_id_ = FileDbId(data.pmc_id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
get_file_id_info(file_id)->node_id_ = file_node_id;
|
|
|
|
node->file_ids_.push_back(file_id);
|
|
|
|
|
2018-01-30 21:04:22 +01:00
|
|
|
FileView file_view(get_file_node(file_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-08-02 13:45:22 +02:00
|
|
|
vector<FileId> to_merge;
|
2022-11-11 19:09:57 +01:00
|
|
|
auto register_location = [&](const auto &location, auto &mp) -> FileId * {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &other_id = mp[location];
|
|
|
|
if (other_id.empty()) {
|
2022-11-13 09:56:52 +01:00
|
|
|
other_id = file_id;
|
2022-11-11 19:09:57 +01:00
|
|
|
return &other_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
to_merge.push_back(other_id);
|
2022-11-11 19:09:57 +01:00
|
|
|
return nullptr;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
bool new_remote = false;
|
2023-05-10 14:29:38 +02:00
|
|
|
FileId *new_remote_file_id = nullptr;
|
2018-03-06 19:31:20 +01:00
|
|
|
int32 remote_key = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (file_view.has_remote_location()) {
|
2023-05-10 14:29:38 +02:00
|
|
|
if (context_->keep_exact_remote_location()) {
|
|
|
|
RemoteInfo info{file_view.remote_location(), file_location_source, file_id};
|
|
|
|
remote_key = remote_location_info_.add(info);
|
|
|
|
auto &stored_info = remote_location_info_.get(remote_key);
|
|
|
|
if (stored_info.file_id_ == file_id) {
|
|
|
|
get_file_id_info(file_id)->pin_flag_ = true;
|
|
|
|
new_remote = true;
|
|
|
|
} else {
|
|
|
|
to_merge.push_back(stored_info.file_id_);
|
|
|
|
if (merge_choose_remote_location(file_view.remote_location(), file_location_source, stored_info.remote_,
|
|
|
|
stored_info.file_location_source_) == 0) {
|
|
|
|
stored_info.remote_ = file_view.remote_location();
|
|
|
|
stored_info.file_location_source_ = file_location_source;
|
|
|
|
}
|
2018-03-06 19:31:20 +01:00
|
|
|
}
|
2023-05-10 14:29:38 +02:00
|
|
|
} else {
|
|
|
|
new_remote_file_id = register_location(file_view.remote_location(), remote_location_to_file_id_);
|
|
|
|
new_remote = new_remote_file_id != nullptr;
|
2018-03-06 19:31:20 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-11-11 19:09:57 +01:00
|
|
|
FileId *new_local_file_id = nullptr;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (file_view.has_local_location()) {
|
2022-11-11 19:09:57 +01:00
|
|
|
new_local_file_id = register_location(file_view.local_location(), local_location_to_file_id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-11-11 19:09:57 +01:00
|
|
|
FileId *new_generate_file_id = nullptr;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (file_view.has_generate_location()) {
|
2022-11-11 19:09:57 +01:00
|
|
|
new_generate_file_id = register_location(file_view.generate_location(), generate_location_to_file_id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-12-30 16:50:57 +01:00
|
|
|
td::unique(to_merge);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-11-11 19:09:57 +01:00
|
|
|
int new_cnt = new_remote + (new_local_file_id != nullptr) + (new_generate_file_id != nullptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (data.pmc_id_ == 0 && file_db_ && new_cnt > 0) {
|
|
|
|
node->need_load_from_pmc_ = true;
|
|
|
|
}
|
|
|
|
bool no_sync_merge = to_merge.size() == 1 && new_cnt == 0;
|
|
|
|
for (auto id : to_merge) {
|
2018-01-25 14:11:05 +01:00
|
|
|
// may invalidate node
|
2018-12-31 20:04:05 +01:00
|
|
|
merge(file_id, id, no_sync_merge).ignore();
|
|
|
|
}
|
2022-11-12 07:47:33 +01:00
|
|
|
Status status;
|
|
|
|
if (merge_file_id.is_valid()) {
|
2022-11-12 07:54:23 +01:00
|
|
|
status = merge(file_id, merge_file_id);
|
2022-11-12 07:47:33 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-12-27 18:23:19 +01:00
|
|
|
try_flush_node(get_file_node(file_id), "register_file");
|
2018-12-31 20:04:05 +01:00
|
|
|
auto main_file_id = get_file_node(file_id)->main_file_id_;
|
2022-11-11 19:09:57 +01:00
|
|
|
if (main_file_id != file_id) {
|
2023-05-10 14:29:38 +02:00
|
|
|
if (new_remote_file_id != nullptr) {
|
|
|
|
*new_remote_file_id = main_file_id;
|
|
|
|
}
|
2022-11-13 09:56:52 +01:00
|
|
|
if (new_local_file_id != nullptr) {
|
2022-11-11 19:09:57 +01:00
|
|
|
*new_local_file_id = main_file_id;
|
|
|
|
}
|
2022-11-13 09:56:52 +01:00
|
|
|
if (new_generate_file_id != nullptr) {
|
2022-11-11 19:09:57 +01:00
|
|
|
*new_generate_file_id = main_file_id;
|
|
|
|
}
|
|
|
|
try_forget_file_id(file_id);
|
|
|
|
}
|
2023-05-10 14:29:38 +02:00
|
|
|
if (new_cnt > 0) {
|
2022-11-11 19:09:57 +01:00
|
|
|
get_file_id_info(main_file_id)->pin_flag_ = true;
|
|
|
|
}
|
2022-10-06 15:29:35 +02:00
|
|
|
|
|
|
|
if (!data.file_source_ids_.empty()) {
|
2019-01-30 22:37:38 +01:00
|
|
|
VLOG(file_references) << "Loaded " << data.file_source_ids_ << " for file " << main_file_id << " from " << source;
|
2022-10-06 15:29:35 +02:00
|
|
|
for (auto file_source_id : data.file_source_ids_) {
|
|
|
|
CHECK(file_source_id.is_valid());
|
|
|
|
context_->add_file_source(main_file_id, file_source_id);
|
|
|
|
}
|
2019-01-30 22:37:38 +01:00
|
|
|
}
|
2022-11-12 07:47:33 +01:00
|
|
|
if (status.is_error()) {
|
|
|
|
return std::move(status);
|
|
|
|
}
|
2018-03-06 19:31:20 +01:00
|
|
|
return FileId(main_file_id.get(), remote_key);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 0 -- choose x
|
|
|
|
// 1 -- choose y
|
|
|
|
// 2 -- choose any
|
2018-09-07 18:29:18 +02:00
|
|
|
static int merge_choose_local_location(const LocalFileLocation &x, const LocalFileLocation &y) {
|
2021-10-19 17:11:16 +02:00
|
|
|
auto x_type = static_cast<int32>(x.type());
|
|
|
|
auto y_type = static_cast<int32>(y.type());
|
2018-12-31 20:04:05 +01:00
|
|
|
if (x_type != y_type) {
|
|
|
|
return x_type < y_type;
|
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2019-02-14 12:20:40 +01:00
|
|
|
static int merge_choose_file_source_location(FileLocationSource x, FileLocationSource y) {
|
|
|
|
return static_cast<int>(x) < static_cast<int>(y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int merge_choose_remote_location(const FullRemoteFileLocation &x, FileLocationSource x_source,
|
|
|
|
const FullRemoteFileLocation &y, FileLocationSource y_source) {
|
2019-02-15 21:09:18 +01:00
|
|
|
LOG(INFO) << "Choose between " << x << " from " << x_source << " and " << y << " from " << y_source;
|
2019-02-04 16:59:01 +01:00
|
|
|
if (x.is_web() != y.is_web()) {
|
|
|
|
return x.is_web(); // prefer non-web
|
|
|
|
}
|
2019-06-17 04:37:57 +02:00
|
|
|
auto x_ref = x.has_file_reference();
|
|
|
|
auto y_ref = y.has_file_reference();
|
2019-02-04 16:59:01 +01:00
|
|
|
if (x_ref || y_ref) {
|
|
|
|
if (x_ref != y_ref) {
|
|
|
|
return !x_ref;
|
|
|
|
}
|
2019-06-17 04:37:57 +02:00
|
|
|
if (x.get_file_reference() != y.get_file_reference()) {
|
2019-02-14 12:20:40 +01:00
|
|
|
return merge_choose_file_source_location(x_source, y_source);
|
2019-02-04 16:59:01 +01:00
|
|
|
}
|
|
|
|
}
|
2019-06-18 17:40:46 +02:00
|
|
|
if ((x.get_access_hash() != y.get_access_hash() || x.get_source() != y.get_source()) &&
|
|
|
|
(x_source != y_source || x.is_web() || x.get_id() == y.get_id())) {
|
2019-02-14 12:20:40 +01:00
|
|
|
return merge_choose_file_source_location(x_source, y_source);
|
2019-02-04 16:59:01 +01:00
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|
2019-02-15 21:09:18 +01:00
|
|
|
|
2019-02-14 12:20:40 +01:00
|
|
|
static int merge_choose_remote_location(const NewRemoteFileLocation &x, const NewRemoteFileLocation &y) {
|
|
|
|
if (x.is_full_alive != y.is_full_alive) {
|
|
|
|
return !x.is_full_alive;
|
|
|
|
}
|
|
|
|
if (x.is_full_alive) {
|
|
|
|
return merge_choose_remote_location(x.full.value(), x.full_source, y.full.value(), y.full_source);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-02-14 12:20:40 +01:00
|
|
|
if (!x.partial != !y.partial) {
|
|
|
|
return !x.partial;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|
2018-10-31 19:11:02 +01:00
|
|
|
|
2018-09-07 18:29:18 +02:00
|
|
|
static int merge_choose_generate_location(const unique_ptr<FullGenerateFileLocation> &x,
|
|
|
|
const unique_ptr<FullGenerateFileLocation> &y) {
|
2018-10-31 19:11:02 +01:00
|
|
|
int x_empty = (x == nullptr);
|
|
|
|
int y_empty = (y == nullptr);
|
|
|
|
if (x_empty != y_empty) {
|
|
|
|
return x_empty ? 1 : 0;
|
|
|
|
}
|
2019-01-19 20:42:06 +01:00
|
|
|
if (!x_empty && *x != *y) {
|
2018-10-31 19:11:02 +01:00
|
|
|
bool x_has_mtime = begins_with(x->conversion_, "#mtime#");
|
|
|
|
bool y_has_mtime = begins_with(y->conversion_, "#mtime#");
|
|
|
|
if (x_has_mtime != y_has_mtime) {
|
|
|
|
return x_has_mtime ? 0 : 1;
|
|
|
|
}
|
2019-01-19 20:42:06 +01:00
|
|
|
return x->conversion_ >= y->conversion_
|
|
|
|
? 0
|
|
|
|
: 1; // the bigger conversion, the bigger mtime or at least more stable choise
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -1 -- error
|
|
|
|
static int merge_choose_size(int64 x, int64 y) {
|
|
|
|
if (x == 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (y == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (x != y) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|
2019-01-11 03:45:03 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
static int merge_choose_expected_size(int64 x, int64 y) {
|
|
|
|
if (x == 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (y == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int merge_choose_name(Slice x, Slice y) {
|
|
|
|
if (x.empty() != y.empty()) {
|
|
|
|
return x.empty() > y.empty();
|
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int merge_choose_owner(DialogId x, DialogId y) {
|
|
|
|
if (x.is_valid() != y.is_valid()) {
|
|
|
|
return x.is_valid() < y.is_valid();
|
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2018-01-20 11:49:06 +01:00
|
|
|
static int merge_choose_main_file_id(FileId a, int8 a_priority, FileId b, int8 b_priority) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (a_priority != b_priority) {
|
|
|
|
return a_priority < b_priority;
|
|
|
|
}
|
|
|
|
return a.get() > b.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int merge_choose_encryption_key(const FileEncryptionKey &a, const FileEncryptionKey &b) {
|
|
|
|
if (a.empty() != b.empty()) {
|
|
|
|
return a.empty() > b.empty();
|
|
|
|
}
|
2019-10-24 19:17:37 +02:00
|
|
|
if (a != b) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2019-01-25 02:52:38 +01:00
|
|
|
void FileManager::do_cancel_download(FileNodePtr node) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (node->download_id_ == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2024-07-12 17:15:44 +02:00
|
|
|
send_closure(file_download_manager_, &FileDownloadManager::cancel, node->download_id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
node->download_id_ = 0;
|
2018-01-09 12:57:11 +01:00
|
|
|
node->is_download_started_ = false;
|
2019-01-17 21:42:00 +01:00
|
|
|
node->download_was_update_file_reference_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
node->set_download_priority(0);
|
|
|
|
}
|
|
|
|
|
2019-01-25 02:52:38 +01:00
|
|
|
void FileManager::do_cancel_upload(FileNodePtr node) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (node->upload_id_ == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2024-07-12 16:27:26 +02:00
|
|
|
send_closure(file_upload_manager_, &FileUploadManager::cancel, node->upload_id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
node->upload_id_ = 0;
|
2019-01-17 21:42:00 +01:00
|
|
|
node->upload_was_update_file_reference_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
node->set_upload_priority(0);
|
|
|
|
}
|
|
|
|
|
2019-01-25 02:52:38 +01:00
|
|
|
void FileManager::do_cancel_generate(FileNodePtr node) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (node->generate_id_ == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
send_closure(file_generate_manager_, &FileGenerateManager::cancel, node->generate_id_);
|
|
|
|
node->generate_id_ = 0;
|
|
|
|
node->generate_was_update_ = false;
|
|
|
|
node->set_generate_priority(0, 0);
|
|
|
|
}
|
|
|
|
|
2022-11-12 07:54:23 +01:00
|
|
|
Status FileManager::merge(FileId x_file_id, FileId y_file_id, bool no_sync) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!x_file_id.is_valid()) {
|
2023-02-09 16:31:02 +01:00
|
|
|
return Status::Error(400, "First file_id is invalid");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-30 21:04:22 +01:00
|
|
|
FileNodePtr x_node = no_sync ? get_file_node(x_file_id) : get_sync_file_node(x_file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!x_node) {
|
2023-02-09 16:31:02 +01:00
|
|
|
return Status::Error(
|
|
|
|
400, PSLICE() << "Can't merge files. First identifier is invalid: " << x_file_id << " and " << y_file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!y_file_id.is_valid()) {
|
2019-11-16 23:43:47 +01:00
|
|
|
LOG(DEBUG) << "Old file is invalid";
|
2022-11-12 07:54:23 +01:00
|
|
|
return Status::OK();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-03-20 13:18:16 +01:00
|
|
|
FileNodePtr y_node = get_file_node(y_file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!y_node) {
|
2023-02-09 16:31:02 +01:00
|
|
|
return Status::Error(
|
|
|
|
400, PSLICE() << "Can't merge files. Second identifier is invalid: " << x_file_id << " and " << y_file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (x_file_id == x_node->upload_pause_) {
|
2019-01-10 22:12:51 +01:00
|
|
|
x_node->set_upload_pause(FileId());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-30 21:04:22 +01:00
|
|
|
if (x_node.get() == y_node.get()) {
|
2022-07-20 17:01:26 +02:00
|
|
|
if (x_file_id != y_file_id) {
|
|
|
|
LOG(DEBUG) << "New file " << x_file_id << " and old file " << y_file_id << " are already merged";
|
|
|
|
}
|
2022-11-21 13:28:32 +01:00
|
|
|
try_flush_node_info(x_node, "merge 1");
|
2022-11-12 07:54:23 +01:00
|
|
|
return Status::OK();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (y_file_id == y_node->upload_pause_) {
|
2019-01-10 22:12:51 +01:00
|
|
|
y_node->set_upload_pause(FileId());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-11-21 12:33:19 +01:00
|
|
|
LOG(INFO) << "Merge new file " << x_file_id << " and old file " << y_file_id;
|
2019-02-14 12:20:40 +01:00
|
|
|
if (x_node->remote_.full && y_node->remote_.full && !x_node->remote_.full.value().is_web() &&
|
2019-02-15 21:09:18 +01:00
|
|
|
!y_node->remote_.full.value().is_web() && y_node->remote_.is_full_alive &&
|
2020-03-31 01:16:23 +02:00
|
|
|
x_node->remote_.full_source == FileLocationSource::FromServer &&
|
|
|
|
y_node->remote_.full_source == FileLocationSource::FromServer &&
|
2019-02-14 12:20:40 +01:00
|
|
|
x_node->remote_.full.value().get_dc_id() != y_node->remote_.full.value().get_dc_id()) {
|
2020-03-31 01:16:23 +02:00
|
|
|
LOG(ERROR) << "File remote location was changed from " << y_node->remote_.full.value() << " to "
|
|
|
|
<< x_node->remote_.full.value();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-04-10 02:43:35 +02:00
|
|
|
|
|
|
|
bool drop_last_successful_force_reupload_time = x_node->last_successful_force_reupload_time_ <= 0 &&
|
|
|
|
x_node->remote_.full &&
|
|
|
|
x_node->remote_.full_source == FileLocationSource::FromServer;
|
|
|
|
|
2019-12-24 16:51:41 +01:00
|
|
|
auto count_local = [](auto &node) {
|
|
|
|
return std::accumulate(node->file_ids_.begin(), node->file_ids_.end(), 0,
|
|
|
|
[](const auto &x, const auto &y) { return x + (y.get_remote() != 0); });
|
|
|
|
};
|
2021-07-20 17:49:29 +02:00
|
|
|
auto x_local_file_ids = count_local(x_node);
|
|
|
|
auto y_local_file_ids = count_local(y_node);
|
|
|
|
if (x_local_file_ids + y_local_file_ids > 100) {
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y_node->file_ids_.size() >= 100 || x_node->file_ids_.size() >= 100) {
|
|
|
|
LOG(INFO) << "Merge files with " << x_local_file_ids << '/' << x_node->file_ids_.size() << " and "
|
|
|
|
<< y_local_file_ids << '/' << y_node->file_ids_.size() << " file IDs";
|
2019-12-24 16:51:41 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-01-30 17:54:17 +01:00
|
|
|
FileNodePtr nodes[] = {x_node, y_node, x_node};
|
2018-01-30 21:04:22 +01:00
|
|
|
FileNodeId node_ids[] = {get_file_id_info(x_file_id)->node_id_, get_file_id_info(y_file_id)->node_id_};
|
2019-02-14 12:20:40 +01:00
|
|
|
int trusted_by_source = merge_choose_file_source_location(x_node->remote_.full_source, y_node->remote_.full_source);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-09-07 18:29:18 +02:00
|
|
|
int local_i = merge_choose_local_location(x_node->local_, y_node->local_);
|
2019-02-14 12:20:40 +01:00
|
|
|
int remote_i = merge_choose_remote_location(x_node->remote_, y_node->remote_);
|
2018-09-07 18:29:18 +02:00
|
|
|
int generate_i = merge_choose_generate_location(x_node->generate_, y_node->generate_);
|
2018-12-31 20:04:05 +01:00
|
|
|
int size_i = merge_choose_size(x_node->size_, y_node->size_);
|
|
|
|
int expected_size_i = merge_choose_expected_size(x_node->expected_size_, y_node->expected_size_);
|
2018-02-18 17:29:44 +01:00
|
|
|
int remote_name_i = merge_choose_name(x_node->remote_name_, y_node->remote_name_);
|
2018-12-31 20:04:05 +01:00
|
|
|
int url_i = merge_choose_name(x_node->url_, y_node->url_);
|
|
|
|
int owner_i = merge_choose_owner(x_node->owner_dialog_id_, y_node->owner_dialog_id_);
|
|
|
|
int encryption_key_i = merge_choose_encryption_key(x_node->encryption_key_, y_node->encryption_key_);
|
|
|
|
int main_file_id_i = merge_choose_main_file_id(x_node->main_file_id_, x_node->main_file_id_priority_,
|
|
|
|
y_node->main_file_id_, y_node->main_file_id_priority_);
|
|
|
|
|
|
|
|
if (size_i == -1) {
|
2022-11-21 13:28:32 +01:00
|
|
|
try_flush_node_info(x_node, "merge 2");
|
|
|
|
try_flush_node_info(y_node, "merge 3");
|
2024-01-28 22:39:07 +01:00
|
|
|
return Status::Error(400, PSLICE() << "Can't merge files " << x_node->local_ << '/' << x_node->remote_ << " and "
|
|
|
|
<< y_node->local_ << '/' << y_node->remote_
|
|
|
|
<< ". Different size: " << x_node->size_ << " and " << y_node->size_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (encryption_key_i == -1) {
|
2019-02-14 12:20:40 +01:00
|
|
|
if (nodes[remote_i]->remote_.full && nodes[local_i]->local_.type() != LocalFileLocation::Type::Partial) {
|
2019-02-15 21:09:18 +01:00
|
|
|
LOG(ERROR) << "Different encryption key in files, but lets choose same key as remote location";
|
2018-12-31 20:04:05 +01:00
|
|
|
encryption_key_i = remote_i;
|
|
|
|
} else {
|
2022-11-21 13:28:32 +01:00
|
|
|
try_flush_node_info(x_node, "merge 4");
|
|
|
|
try_flush_node_info(y_node, "merge 5");
|
2023-02-09 16:31:02 +01:00
|
|
|
return Status::Error(400, "Can't merge files. Different encryption keys");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2019-07-18 18:47:29 +02:00
|
|
|
|
|
|
|
// prefer more trusted source
|
|
|
|
if (remote_name_i == 2) {
|
|
|
|
remote_name_i = trusted_by_source;
|
|
|
|
}
|
|
|
|
if (url_i == 2) {
|
|
|
|
url_i = trusted_by_source;
|
|
|
|
}
|
|
|
|
if (expected_size_i == 2) {
|
|
|
|
expected_size_i = trusted_by_source;
|
2018-06-01 00:21:35 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-01-06 21:39:10 +01:00
|
|
|
int node_i =
|
|
|
|
std::make_tuple(y_node->pmc_id_.is_valid(), x_node->pmc_id_, y_node->file_ids_.size(), main_file_id_i == 1) >
|
|
|
|
std::make_tuple(x_node->pmc_id_.is_valid(), y_node->pmc_id_, x_node->file_ids_.size(), main_file_id_i == 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
auto other_node_i = 1 - node_i;
|
2018-01-30 17:54:17 +01:00
|
|
|
FileNodePtr node = nodes[node_i];
|
|
|
|
FileNodePtr other_node = nodes[other_node_i];
|
2018-12-31 20:04:05 +01:00
|
|
|
auto file_view = FileView(node);
|
|
|
|
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(DEBUG) << "Have x_node->pmc_id_ = " << x_node->pmc_id_.get() << ", y_node->pmc_id_ = " << y_node->pmc_id_.get()
|
2018-06-05 01:40:00 +02:00
|
|
|
<< ", x_node_size = " << x_node->file_ids_.size() << ", y_node_size = " << y_node->file_ids_.size()
|
|
|
|
<< ", node_i = " << node_i << ", local_i = " << local_i << ", remote_i = " << remote_i
|
|
|
|
<< ", generate_i = " << generate_i << ", size_i = " << size_i << ", remote_name_i = " << remote_name_i
|
|
|
|
<< ", url_i = " << url_i << ", owner_i = " << owner_i << ", encryption_key_i = " << encryption_key_i
|
2019-02-15 21:09:18 +01:00
|
|
|
<< ", main_file_id_i = " << main_file_id_i << ", trusted_by_source = " << trusted_by_source
|
|
|
|
<< ", x_source = " << x_node->remote_.full_source << ", y_source = " << y_node->remote_.full_source;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (local_i == other_node_i) {
|
2019-01-25 02:52:38 +01:00
|
|
|
do_cancel_download(node);
|
2018-12-26 17:11:15 +01:00
|
|
|
node->set_download_offset(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_);
|
2018-12-31 20:04:05 +01:00
|
|
|
node->download_id_ = other_node->download_id_;
|
2019-01-17 21:42:00 +01:00
|
|
|
node->download_was_update_file_reference_ = other_node->download_was_update_file_reference_;
|
2018-01-09 12:57:11 +01:00
|
|
|
node->is_download_started_ |= other_node->is_download_started_;
|
2018-12-31 20:04:05 +01:00
|
|
|
node->set_download_priority(other_node->download_priority_);
|
|
|
|
other_node->download_id_ = 0;
|
2019-01-17 21:42:00 +01:00
|
|
|
other_node->download_was_update_file_reference_ = false;
|
2018-01-09 12:57:11 +01:00
|
|
|
other_node->is_download_started_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
other_node->download_priority_ = 0;
|
2018-11-11 12:38:04 +01:00
|
|
|
other_node->download_offset_ = 0;
|
2018-12-26 17:11:15 +01:00
|
|
|
other_node->local_ready_prefix_size_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-01-25 02:52:38 +01:00
|
|
|
//do_cancel_generate(node);
|
2018-02-04 18:27:05 +01:00
|
|
|
//node->set_generate_location(std::move(other_node->generate_));
|
2018-12-31 20:04:05 +01:00
|
|
|
//node->generate_id_ = other_node->generate_id_;
|
|
|
|
//node->set_generate_priority(other_node->generate_download_priority_, other_node->generate_upload_priority_);
|
|
|
|
//other_node->generate_id_ = 0;
|
|
|
|
//other_node->generate_was_update_ = false;
|
|
|
|
//other_node->generate_priority_ = 0;
|
|
|
|
//other_node->generate_download_priority_ = 0;
|
|
|
|
//other_node->generate_upload_priority_ = 0;
|
|
|
|
} else {
|
2019-01-25 02:52:38 +01:00
|
|
|
do_cancel_download(other_node);
|
|
|
|
//do_cancel_generate(other_node);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (remote_i == other_node_i) {
|
2019-01-25 02:52:38 +01:00
|
|
|
do_cancel_upload(node);
|
2019-02-14 12:20:40 +01:00
|
|
|
node->set_new_remote_location(std::move(other_node->remote_));
|
2018-12-31 20:04:05 +01:00
|
|
|
node->upload_id_ = other_node->upload_id_;
|
2019-01-17 21:42:00 +01:00
|
|
|
node->upload_was_update_file_reference_ = other_node->upload_was_update_file_reference_;
|
2018-12-31 20:04:05 +01:00
|
|
|
node->set_upload_priority(other_node->upload_priority_);
|
2019-01-10 22:12:51 +01:00
|
|
|
node->set_upload_pause(other_node->upload_pause_);
|
2018-12-31 20:04:05 +01:00
|
|
|
other_node->upload_id_ = 0;
|
2019-01-17 21:42:00 +01:00
|
|
|
other_node->upload_was_update_file_reference_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
other_node->upload_priority_ = 0;
|
2022-11-21 13:28:32 +01:00
|
|
|
other_node->upload_pause_ = FileId();
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2019-01-25 02:52:38 +01:00
|
|
|
do_cancel_upload(other_node);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (generate_i == other_node_i) {
|
2019-01-25 02:52:38 +01:00
|
|
|
do_cancel_generate(node);
|
2018-02-04 18:27:05 +01:00
|
|
|
node->set_generate_location(std::move(other_node->generate_));
|
2018-12-31 20:04:05 +01:00
|
|
|
node->generate_id_ = other_node->generate_id_;
|
|
|
|
node->set_generate_priority(other_node->generate_download_priority_, other_node->generate_upload_priority_);
|
|
|
|
other_node->generate_id_ = 0;
|
|
|
|
other_node->generate_priority_ = 0;
|
|
|
|
other_node->generate_download_priority_ = 0;
|
|
|
|
other_node->generate_upload_priority_ = 0;
|
|
|
|
} else {
|
2019-01-25 02:52:38 +01:00
|
|
|
do_cancel_generate(other_node);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (size_i == other_node_i) {
|
|
|
|
node->set_size(other_node->size_);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expected_size_i == other_node_i) {
|
|
|
|
node->set_expected_size(other_node->expected_size_);
|
|
|
|
}
|
|
|
|
|
2018-02-18 17:29:44 +01:00
|
|
|
if (remote_name_i == other_node_i) {
|
|
|
|
node->set_remote_name(other_node->remote_name_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (url_i == other_node_i) {
|
|
|
|
node->set_url(other_node->url_);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (owner_i == other_node_i) {
|
|
|
|
node->set_owner_dialog_id(other_node->owner_dialog_id_);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encryption_key_i == other_node_i) {
|
|
|
|
node->set_encryption_key(other_node->encryption_key_);
|
|
|
|
nodes[node_i]->set_encryption_key(nodes[encryption_key_i]->encryption_key_);
|
|
|
|
}
|
|
|
|
node->need_load_from_pmc_ |= other_node->need_load_from_pmc_;
|
2018-02-26 11:05:14 +01:00
|
|
|
node->can_search_locally_ &= other_node->can_search_locally_;
|
2021-01-22 16:13:38 +01:00
|
|
|
node->upload_prefer_small_ |= other_node->upload_prefer_small_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-04-10 02:43:35 +02:00
|
|
|
if (drop_last_successful_force_reupload_time) {
|
|
|
|
node->last_successful_force_reupload_time_ = -1e10;
|
|
|
|
} else if (other_node->last_successful_force_reupload_time_ > node->last_successful_force_reupload_time_) {
|
2019-11-16 18:09:27 +01:00
|
|
|
node->last_successful_force_reupload_time_ = other_node->last_successful_force_reupload_time_;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
if (main_file_id_i == other_node_i) {
|
2019-01-30 22:37:38 +01:00
|
|
|
context_->on_merge_files(other_node->main_file_id_, node->main_file_id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
node->main_file_id_ = other_node->main_file_id_;
|
|
|
|
node->main_file_id_priority_ = other_node->main_file_id_priority_;
|
2019-01-17 21:42:00 +01:00
|
|
|
} else {
|
2019-01-30 22:37:38 +01:00
|
|
|
context_->on_merge_files(node->main_file_id_, other_node->main_file_id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool send_updates_flag = false;
|
2018-01-30 21:04:22 +01:00
|
|
|
auto other_pmc_id = other_node->pmc_id_;
|
2023-06-10 22:14:03 +02:00
|
|
|
append(node->file_ids_, other_node->file_ids_);
|
2018-01-30 21:04:22 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto file_id : other_node->file_ids_) {
|
|
|
|
auto file_id_info = get_file_id_info(file_id);
|
2021-07-20 17:49:29 +02:00
|
|
|
CHECK(file_id_info->node_id_ == node_ids[other_node_i]);
|
2018-12-31 20:04:05 +01:00
|
|
|
file_id_info->node_id_ = node_ids[node_i];
|
|
|
|
send_updates_flag |= file_id_info->send_updates_flag_;
|
|
|
|
}
|
2018-01-30 21:04:22 +01:00
|
|
|
other_node = {};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (send_updates_flag) {
|
|
|
|
// node might not changed, but other_node might changed, so we need to send update anyway
|
|
|
|
VLOG(update_file) << "File " << node->main_file_id_ << " has been merged";
|
|
|
|
node->on_info_changed();
|
|
|
|
}
|
|
|
|
|
2022-10-08 20:56:51 +02:00
|
|
|
if (node->file_ids_.size() > (static_cast<size_t>(1) << file_node_size_warning_exp_)) {
|
2022-10-05 15:32:16 +02:00
|
|
|
LOG(WARNING) << "File of type " << file_view.get_type() << " has " << node->file_ids_.size() << " file identifiers";
|
|
|
|
file_node_size_warning_exp_++;
|
|
|
|
}
|
|
|
|
|
2021-07-20 17:49:29 +02:00
|
|
|
// Check if some download/upload queries are ready
|
2018-02-01 15:12:32 +01:00
|
|
|
for (auto file_id : vector<FileId>(node->file_ids_)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto *info = get_file_id_info(file_id);
|
|
|
|
if (info->download_priority_ != 0 && file_view.has_local_location()) {
|
|
|
|
info->download_priority_ = 0;
|
|
|
|
if (info->download_callback_) {
|
|
|
|
info->download_callback_->on_download_ok(file_id);
|
|
|
|
info->download_callback_.reset();
|
|
|
|
}
|
|
|
|
}
|
2019-02-15 10:25:21 +01:00
|
|
|
if (info->upload_priority_ != 0 && file_view.has_active_upload_remote_location()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
info->upload_priority_ = 0;
|
|
|
|
if (info->upload_callback_) {
|
|
|
|
info->upload_callback_->on_upload_ok(file_id, nullptr);
|
|
|
|
info->upload_callback_.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
file_nodes_[node_ids[other_node_i]] = nullptr;
|
|
|
|
|
|
|
|
run_generate(node);
|
2020-09-11 17:57:49 +02:00
|
|
|
run_download(node, false);
|
2018-12-31 20:04:05 +01:00
|
|
|
run_upload(node, {});
|
|
|
|
|
2019-01-06 21:39:10 +01:00
|
|
|
if (other_pmc_id.is_valid()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
// node might not changed, but we need to merge nodes in pmc anyway
|
|
|
|
node->on_pmc_changed();
|
|
|
|
}
|
2018-12-27 18:23:19 +01:00
|
|
|
try_flush_node_full(node, node_i != remote_i, node_i != local_i, node_i != generate_i, other_pmc_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-11-12 07:54:23 +01:00
|
|
|
return Status::OK();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2024-04-17 23:21:17 +02:00
|
|
|
void FileManager::try_merge_documents(FileId old_file_id, FileId new_file_id) {
|
|
|
|
if (!old_file_id.is_valid() || !new_file_id.is_valid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
FileView old_file_view = get_file_view(old_file_id);
|
|
|
|
FileView new_file_view = get_file_view(new_file_id);
|
|
|
|
// if file type has changed, but file size remains the same, we are trying to update local location of the new
|
|
|
|
// file with the old local location
|
|
|
|
if (old_file_view.has_local_location() && !new_file_view.has_local_location() && old_file_view.size() != 0 &&
|
|
|
|
old_file_view.size() == new_file_view.size()) {
|
|
|
|
auto old_file_type = old_file_view.get_type();
|
|
|
|
auto new_file_type = new_file_view.get_type();
|
|
|
|
|
|
|
|
if (is_document_file_type(old_file_type) && is_document_file_type(new_file_type)) {
|
|
|
|
auto &old_location = old_file_view.local_location();
|
|
|
|
auto r_file_id =
|
|
|
|
register_local(FullLocalFileLocation(new_file_type, old_location.path_, old_location.mtime_nsec_), DialogId(),
|
|
|
|
old_file_view.size());
|
|
|
|
if (r_file_id.is_ok()) {
|
|
|
|
LOG_STATUS(merge(new_file_id, r_file_id.ok()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 10:32:31 +01:00
|
|
|
void FileManager::add_file_source(FileId file_id, FileSourceId file_source_id) {
|
2022-10-06 12:48:52 +02:00
|
|
|
auto node = get_sync_file_node(file_id); // synchronously load the file to preload known file sources
|
2018-12-05 10:32:31 +01:00
|
|
|
if (!node) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-17 21:42:00 +01:00
|
|
|
|
2019-01-18 23:19:15 +01:00
|
|
|
CHECK(file_source_id.is_valid());
|
2019-01-30 22:37:38 +01:00
|
|
|
if (context_->add_file_source(node->main_file_id_, file_source_id)) {
|
|
|
|
node->on_pmc_changed();
|
|
|
|
try_flush_node_pmc(node, "add_file_source");
|
|
|
|
}
|
2018-12-05 10:32:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileManager::remove_file_source(FileId file_id, FileSourceId file_source_id) {
|
2022-10-06 12:48:52 +02:00
|
|
|
auto node = get_sync_file_node(file_id); // synchronously load the file to preload known file sources
|
2018-12-05 10:32:31 +01:00
|
|
|
if (!node) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-18 23:19:15 +01:00
|
|
|
|
|
|
|
CHECK(file_source_id.is_valid());
|
2019-01-30 22:37:38 +01:00
|
|
|
if (context_->remove_file_source(node->main_file_id_, file_source_id)) {
|
|
|
|
node->on_pmc_changed();
|
|
|
|
try_flush_node_pmc(node, "remove_file_source");
|
|
|
|
}
|
2018-12-05 10:32:31 +01:00
|
|
|
}
|
|
|
|
|
2019-01-18 21:12:09 +01:00
|
|
|
void FileManager::change_files_source(FileSourceId file_source_id, const vector<FileId> &old_file_ids,
|
|
|
|
const vector<FileId> &new_file_ids) {
|
2019-01-18 21:40:23 +01:00
|
|
|
if (old_file_ids == new_file_ids) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-18 23:19:15 +01:00
|
|
|
CHECK(file_source_id.is_valid());
|
2019-01-18 21:40:23 +01:00
|
|
|
|
2019-01-18 21:12:09 +01:00
|
|
|
auto old_main_file_ids = get_main_file_ids(old_file_ids);
|
|
|
|
auto new_main_file_ids = get_main_file_ids(new_file_ids);
|
|
|
|
for (auto file_id : old_main_file_ids) {
|
|
|
|
auto it = new_main_file_ids.find(file_id);
|
|
|
|
if (it == new_main_file_ids.end()) {
|
2019-01-29 12:07:58 +01:00
|
|
|
remove_file_source(file_id, file_source_id);
|
2019-01-18 21:12:09 +01:00
|
|
|
} else {
|
|
|
|
new_main_file_ids.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto file_id : new_main_file_ids) {
|
2019-01-29 12:07:58 +01:00
|
|
|
add_file_source(file_id, file_source_id);
|
2019-01-18 21:12:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-04 11:00:14 +02:00
|
|
|
void FileManager::on_file_reference_repaired(FileId file_id, FileSourceId file_source_id, Result<Unit> &&result,
|
|
|
|
Promise<Unit> &&promise) {
|
2021-10-07 15:36:21 +02:00
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
2021-10-07 14:28:52 +02:00
|
|
|
|
2020-07-04 11:00:14 +02:00
|
|
|
auto file_view = get_file_view(file_id);
|
|
|
|
CHECK(!file_view.empty());
|
|
|
|
if (result.is_ok() &&
|
|
|
|
(!file_view.has_active_upload_remote_location() || !file_view.has_active_download_remote_location())) {
|
|
|
|
result = Status::Error("No active remote location");
|
|
|
|
}
|
|
|
|
if (result.is_error() && result.error().code() != 429 && result.error().code() < 500) {
|
|
|
|
VLOG(file_references) << "Invalid " << file_source_id << " " << result.error();
|
|
|
|
remove_file_source(file_id, file_source_id);
|
|
|
|
}
|
|
|
|
promise.set_result(std::move(result));
|
|
|
|
}
|
|
|
|
|
2022-03-11 19:38:48 +01:00
|
|
|
FlatHashSet<FileId, FileIdHash> FileManager::get_main_file_ids(const vector<FileId> &file_ids) {
|
|
|
|
FlatHashSet<FileId, FileIdHash> result;
|
2019-01-18 21:12:09 +01:00
|
|
|
for (auto file_id : file_ids) {
|
|
|
|
auto node = get_file_node(file_id);
|
|
|
|
if (node) {
|
|
|
|
result.insert(node->main_file_id_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-12-27 18:23:19 +01:00
|
|
|
void FileManager::try_flush_node_full(FileNodePtr node, bool new_remote, bool new_local, bool new_generate,
|
|
|
|
FileDbId other_pmc_id) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (node->need_pmc_flush()) {
|
|
|
|
if (file_db_) {
|
2018-01-30 21:04:22 +01:00
|
|
|
load_from_pmc(node, true, true, true);
|
2019-01-30 22:37:38 +01:00
|
|
|
flush_to_pmc(node, new_remote, new_local, new_generate, "try_flush_node_full");
|
2019-01-06 21:39:10 +01:00
|
|
|
if (other_pmc_id.is_valid() && node->pmc_id_ != other_pmc_id) {
|
2018-12-31 20:04:05 +01:00
|
|
|
file_db_->set_file_data_ref(other_pmc_id, node->pmc_id_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node->on_pmc_flushed();
|
|
|
|
}
|
|
|
|
|
2018-12-27 18:23:19 +01:00
|
|
|
try_flush_node_info(node, "try_flush_node_full");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-12-27 18:23:19 +01:00
|
|
|
void FileManager::try_flush_node(FileNodePtr node, const char *source) {
|
2019-01-30 17:40:23 +01:00
|
|
|
try_flush_node_pmc(node, source);
|
|
|
|
try_flush_node_info(node, source);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileManager::try_flush_node_pmc(FileNodePtr node, const char *source) {
|
2018-12-27 18:23:19 +01:00
|
|
|
if (node->need_pmc_flush()) {
|
|
|
|
if (file_db_) {
|
|
|
|
load_from_pmc(node, true, true, true);
|
2019-01-30 22:37:38 +01:00
|
|
|
flush_to_pmc(node, false, false, false, source);
|
2018-12-27 18:23:19 +01:00
|
|
|
}
|
|
|
|
node->on_pmc_flushed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileManager::try_flush_node_info(FileNodePtr node, const char *source) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (node->need_info_flush()) {
|
2018-02-01 15:12:32 +01:00
|
|
|
for (auto file_id : vector<FileId>(node->file_ids_)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto *info = get_file_id_info(file_id);
|
|
|
|
if (info->send_updates_flag_) {
|
2018-12-27 18:23:19 +01:00
|
|
|
VLOG(update_file) << "Send UpdateFile about file " << file_id << " from " << source;
|
2018-12-31 20:04:05 +01:00
|
|
|
context_->on_file_updated(file_id);
|
|
|
|
}
|
2022-02-25 23:17:30 +01:00
|
|
|
if (info->download_callback_) {
|
2022-02-28 16:25:07 +01:00
|
|
|
// For DownloadManager. For everybody else it is just an empty function call (I hope).
|
2022-02-25 23:17:30 +01:00
|
|
|
info->download_callback_->on_progress(file_id);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
node->on_info_flushed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 17:54:17 +01:00
|
|
|
void FileManager::clear_from_pmc(FileNodePtr node) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!file_db_) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-06 21:39:10 +01:00
|
|
|
if (node->pmc_id_.empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Delete files " << format::as_array(node->file_ids_) << " from pmc";
|
|
|
|
FileData data;
|
|
|
|
auto file_view = FileView(node);
|
|
|
|
if (file_view.has_local_location()) {
|
|
|
|
data.local_ = node->local_;
|
2022-10-02 22:06:37 +02:00
|
|
|
prepare_path_for_pmc(data.local_.full().file_type_, data.local_.full().path_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (file_view.has_remote_location()) {
|
2019-02-14 12:20:40 +01:00
|
|
|
data.remote_ = RemoteFileLocation(*node->remote_.full);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (file_view.has_generate_location()) {
|
2018-09-27 03:19:03 +02:00
|
|
|
data.generate_ = make_unique<FullGenerateFileLocation>(*node->generate_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
file_db_->clear_file_data(node->pmc_id_, data);
|
2019-01-06 21:39:10 +01:00
|
|
|
node->pmc_id_ = FileDbId();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-30 22:37:38 +01:00
|
|
|
void FileManager::flush_to_pmc(FileNodePtr node, bool new_remote, bool new_local, bool new_generate,
|
|
|
|
const char *source) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!file_db_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
FileView view(node);
|
|
|
|
bool create_flag = false;
|
2019-01-06 21:39:10 +01:00
|
|
|
if (node->pmc_id_.empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
create_flag = true;
|
2023-01-16 11:30:03 +01:00
|
|
|
node->pmc_id_ = file_db_->get_next_file_db_id();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FileData data;
|
2019-01-06 21:39:10 +01:00
|
|
|
data.pmc_id_ = node->pmc_id_.get();
|
2018-12-31 20:04:05 +01:00
|
|
|
data.local_ = node->local_;
|
2018-01-20 15:57:52 +01:00
|
|
|
if (data.local_.type() == LocalFileLocation::Type::Full) {
|
|
|
|
prepare_path_for_pmc(data.local_.full().file_type_, data.local_.full().path_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-02-14 22:19:48 +01:00
|
|
|
if (node->remote_.full) {
|
2019-02-14 12:20:40 +01:00
|
|
|
data.remote_ = RemoteFileLocation(node->remote_.full.value());
|
|
|
|
} else if (node->remote_.partial) {
|
|
|
|
data.remote_ = RemoteFileLocation(*node->remote_.partial);
|
|
|
|
}
|
2018-02-04 18:27:05 +01:00
|
|
|
if (node->generate_ != nullptr && !begins_with(node->generate_->conversion_, "#file_id#")) {
|
2018-09-27 03:19:03 +02:00
|
|
|
data.generate_ = make_unique<FullGenerateFileLocation>(*node->generate_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-09-27 20:14:32 +02:00
|
|
|
// TODO: not needed when GenerateLocation has constant conversion
|
2018-01-20 15:57:52 +01:00
|
|
|
if (data.remote_.type() != RemoteFileLocation::Type::Full && data.local_.type() != LocalFileLocation::Type::Full) {
|
2018-12-31 20:04:05 +01:00
|
|
|
data.local_ = LocalFileLocation();
|
|
|
|
data.remote_ = RemoteFileLocation();
|
|
|
|
}
|
2018-04-03 19:49:07 +02:00
|
|
|
if (data.remote_.type() != RemoteFileLocation::Type::Full && node->encryption_key_.is_secure()) {
|
|
|
|
data.remote_ = RemoteFileLocation();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
data.size_ = node->size_;
|
|
|
|
data.expected_size_ = node->expected_size_;
|
2018-02-18 17:29:44 +01:00
|
|
|
data.remote_name_ = node->remote_name_;
|
2018-12-31 20:04:05 +01:00
|
|
|
data.encryption_key_ = node->encryption_key_;
|
|
|
|
data.url_ = node->url_;
|
|
|
|
data.owner_dialog_id_ = node->owner_dialog_id_;
|
2022-07-03 11:01:08 +02:00
|
|
|
data.file_source_ids_ = context_->get_some_file_sources(view.get_main_file_id());
|
|
|
|
VLOG(file_references) << "Save file " << view.get_main_file_id() << " to database with " << data.file_source_ids_
|
|
|
|
<< " from " << source;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
file_db_->set_file_data(node->pmc_id_, data, (create_flag || new_remote), (create_flag || new_local),
|
|
|
|
(create_flag || new_generate));
|
|
|
|
}
|
|
|
|
|
2018-01-30 21:04:22 +01:00
|
|
|
FileNode *FileManager::get_file_node_raw(FileId file_id, FileNodeId *file_node_id) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (file_id.get() <= 0 || file_id.get() >= static_cast<int32>(file_id_info_.size())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
FileNodeId node_id = file_id_info_[file_id.get()].node_id_;
|
|
|
|
if (node_id == 0) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (file_node_id != nullptr) {
|
|
|
|
*file_node_id = node_id;
|
|
|
|
}
|
|
|
|
return file_nodes_[node_id].get();
|
|
|
|
}
|
|
|
|
|
2018-01-30 21:04:22 +01:00
|
|
|
FileNodePtr FileManager::get_sync_file_node(FileId file_id) {
|
|
|
|
auto file_node = get_file_node(file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!file_node) {
|
2018-01-30 21:04:22 +01:00
|
|
|
return {};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-30 21:04:22 +01:00
|
|
|
load_from_pmc(file_node, true, true, true);
|
|
|
|
return file_node;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-01-30 21:04:22 +01:00
|
|
|
void FileManager::load_from_pmc(FileNodePtr node, bool new_remote, bool new_local, bool new_generate) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!node->need_load_from_pmc_) {
|
2018-01-30 21:04:22 +01:00
|
|
|
return;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
auto file_id = node->main_file_id_;
|
|
|
|
node->need_load_from_pmc_ = false;
|
|
|
|
if (!file_db_) {
|
2018-01-30 21:04:22 +01:00
|
|
|
return;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
auto file_view = get_file_view(file_id);
|
2021-09-16 15:58:30 +02:00
|
|
|
CHECK(!file_view.empty());
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
FullRemoteFileLocation remote;
|
|
|
|
FullLocalFileLocation local;
|
|
|
|
FullGenerateFileLocation generate;
|
|
|
|
new_remote &= file_view.has_remote_location();
|
|
|
|
if (new_remote) {
|
|
|
|
remote = file_view.remote_location();
|
|
|
|
}
|
|
|
|
new_local &= file_view.has_local_location();
|
|
|
|
if (new_local) {
|
2021-09-16 15:58:30 +02:00
|
|
|
local = file_view.local_location();
|
2018-01-20 15:57:52 +01:00
|
|
|
prepare_path_for_pmc(local.file_type_, local.path_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
new_generate &= file_view.has_generate_location();
|
|
|
|
if (new_generate) {
|
|
|
|
generate = file_view.generate_location();
|
|
|
|
}
|
|
|
|
|
2022-10-06 12:48:52 +02:00
|
|
|
LOG(DEBUG) << "Load from pmc file " << file_id << '/' << file_view.get_main_file_id()
|
|
|
|
<< ", new_remote = " << new_remote << ", new_local = " << new_local << ", new_generate = " << new_generate;
|
2022-10-06 15:29:35 +02:00
|
|
|
auto load = [&](auto location, const char *source) {
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_RESULT(file_data, file_db_->get_file_data_sync(location));
|
2022-11-12 07:47:33 +01:00
|
|
|
TRY_RESULT(new_file_id,
|
|
|
|
register_file(std::move(file_data), FileLocationSource::FromDatabase, FileId(), source, false));
|
2022-11-12 07:54:23 +01:00
|
|
|
TRY_STATUS(merge(file_id, new_file_id)); // merge manually to keep merge parameters order
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
};
|
|
|
|
if (new_remote) {
|
2022-10-06 15:29:35 +02:00
|
|
|
load(remote, "load remote from database").ignore();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (new_local) {
|
2022-10-06 15:29:35 +02:00
|
|
|
load(local, "load local from database").ignore();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (new_generate) {
|
2022-10-06 15:29:35 +02:00
|
|
|
load(generate, "load generate from database").ignore();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileManager::set_encryption_key(FileId file_id, FileEncryptionKey key) {
|
|
|
|
auto node = get_sync_file_node(file_id);
|
|
|
|
if (!node) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto view = FileView(node);
|
|
|
|
if (view.has_local_location() && view.has_remote_location()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!node->encryption_key_.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
node->set_encryption_key(std::move(key));
|
2019-01-30 17:40:23 +01:00
|
|
|
try_flush_node_pmc(node, "set_encryption_key");
|
2018-12-31 20:04:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileManager::set_content(FileId file_id, BufferSlice bytes) {
|
2022-08-17 15:11:13 +02:00
|
|
|
if (G()->get_option_boolean("ignore_inline_thumbnails")) {
|
2018-06-24 22:48:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
auto node = get_sync_file_node(file_id);
|
|
|
|
if (!node) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-20 15:57:52 +01:00
|
|
|
if (node->local_.type() == LocalFileLocation::Type::Full) {
|
2018-12-31 20:04:05 +01:00
|
|
|
// There was no download so we don't need an update
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node->download_priority_ == FROM_BYTES_PRIORITY) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-25 02:52:38 +01:00
|
|
|
do_cancel_download(node);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
auto *file_info = get_file_id_info(file_id);
|
|
|
|
file_info->download_priority_ = FROM_BYTES_PRIORITY;
|
|
|
|
|
|
|
|
node->set_download_priority(FROM_BYTES_PRIORITY);
|
|
|
|
|
2024-07-12 17:15:44 +02:00
|
|
|
FileDownloadManager::QueryId query_id = queries_container_.create(Query{file_id, Query::Type::SetContent});
|
2023-01-16 11:30:03 +01:00
|
|
|
node->download_id_ = query_id;
|
2018-01-09 12:57:11 +01:00
|
|
|
node->is_download_started_ = true;
|
2024-07-12 17:15:44 +02:00
|
|
|
send_closure(file_download_manager_, &FileDownloadManager::from_bytes, query_id,
|
|
|
|
node->remote_.full.value().file_type_, std::move(bytes), node->suggested_path());
|
2018-12-31 20:04:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileManager::get_content(FileId file_id, Promise<BufferSlice> promise) {
|
|
|
|
auto node = get_sync_file_node(file_id);
|
|
|
|
if (!node) {
|
|
|
|
return promise.set_error(Status::Error("Unknown file_id"));
|
|
|
|
}
|
2022-10-10 13:19:35 +02:00
|
|
|
check_local_location(node, true).ignore();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
auto file_view = FileView(node);
|
|
|
|
if (!file_view.has_local_location()) {
|
|
|
|
return promise.set_error(Status::Error("No local location"));
|
|
|
|
}
|
|
|
|
|
2022-06-15 20:40:23 +02:00
|
|
|
send_closure(file_load_manager_, &FileLoadManager::get_content, node->local_.full().path_, std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-05-11 16:17:20 +02:00
|
|
|
void FileManager::read_file_part(FileId file_id, int64 offset, int64 count, int left_tries,
|
2019-04-26 00:03:31 +02:00
|
|
|
Promise<td_api::object_ptr<td_api::filePart>> promise) {
|
2021-10-07 15:36:21 +02:00
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
2019-04-26 00:03:31 +02:00
|
|
|
|
|
|
|
if (!file_id.is_valid()) {
|
2020-04-19 12:45:37 +02:00
|
|
|
return promise.set_error(Status::Error(400, "File identifier is invalid"));
|
2019-04-26 00:03:31 +02:00
|
|
|
}
|
|
|
|
auto node = get_sync_file_node(file_id);
|
|
|
|
if (!node) {
|
|
|
|
return promise.set_error(Status::Error(400, "File not found"));
|
|
|
|
}
|
|
|
|
if (offset < 0) {
|
|
|
|
return promise.set_error(Status::Error(400, "Parameter offset must be non-negative"));
|
|
|
|
}
|
2019-04-27 16:14:45 +02:00
|
|
|
if (count < 0) {
|
|
|
|
return promise.set_error(Status::Error(400, "Parameter count must be non-negative"));
|
2019-04-26 00:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto file_view = FileView(node);
|
|
|
|
|
2019-04-27 16:14:45 +02:00
|
|
|
if (count == 0) {
|
2022-05-11 16:17:20 +02:00
|
|
|
count = file_view.downloaded_prefix(offset);
|
2019-04-27 16:14:45 +02:00
|
|
|
if (count == 0) {
|
|
|
|
return promise.set_value(td_api::make_object<td_api::filePart>());
|
|
|
|
}
|
2022-05-11 16:17:20 +02:00
|
|
|
} else if (file_view.downloaded_prefix(offset) < count) {
|
2019-04-27 16:14:45 +02:00
|
|
|
// TODO this check is safer to do in another thread
|
2019-04-26 00:03:31 +02:00
|
|
|
return promise.set_error(Status::Error(400, "There is not enough downloaded bytes in the file to read"));
|
|
|
|
}
|
2022-05-11 16:17:20 +02:00
|
|
|
if (count >= static_cast<int64>(std::numeric_limits<size_t>::max() / 2 - 1)) {
|
|
|
|
return promise.set_error(Status::Error(400, "Part length is too big"));
|
|
|
|
}
|
2019-04-26 00:03:31 +02:00
|
|
|
|
|
|
|
const string *path = nullptr;
|
|
|
|
bool is_partial = false;
|
|
|
|
if (file_view.has_local_location()) {
|
|
|
|
path = &file_view.local_location().path_;
|
|
|
|
if (!begins_with(*path, get_files_dir(file_view.get_type()))) {
|
|
|
|
return promise.set_error(Status::Error(400, "File is not inside the cache"));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CHECK(node->local_.type() == LocalFileLocation::Type::Partial);
|
|
|
|
path = &node->local_.partial().path_;
|
|
|
|
is_partial = true;
|
|
|
|
}
|
|
|
|
|
2022-06-15 23:25:47 +02:00
|
|
|
auto read_file_part_promise =
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), file_id, offset, count, left_tries, is_partial,
|
|
|
|
promise = std::move(promise)](Result<string> r_bytes) mutable {
|
|
|
|
if (r_bytes.is_error()) {
|
|
|
|
LOG(INFO) << "Failed to read file bytes: " << r_bytes.error();
|
|
|
|
if (left_tries == 1 || !is_partial) {
|
|
|
|
return promise.set_error(Status::Error(400, "Failed to read the file"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// the temporary file could be moved from temp to persistent directory
|
|
|
|
// we need to wait for the corresponding update and repeat the reading
|
|
|
|
create_actor<SleepActor>("RepeatReadFilePartActor", 0.01,
|
|
|
|
PromiseCreator::lambda([actor_id, file_id, offset, count, left_tries,
|
|
|
|
promise = std::move(promise)](Result<Unit> result) mutable {
|
|
|
|
send_closure(actor_id, &FileManager::read_file_part, file_id, offset, count,
|
|
|
|
left_tries - 1, std::move(promise));
|
|
|
|
}))
|
|
|
|
.release();
|
|
|
|
} else {
|
|
|
|
auto result = td_api::make_object<td_api::filePart>();
|
|
|
|
result->data_ = r_bytes.move_as_ok();
|
|
|
|
promise.set_value(std::move(result));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
send_closure(file_load_manager_, &FileLoadManager::read_file_part, *path, offset, count,
|
|
|
|
std::move(read_file_part_promise));
|
2019-04-26 00:03:31 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void FileManager::delete_file(FileId file_id, Promise<Unit> promise, const char *source) {
|
|
|
|
LOG(INFO) << "Trying to delete file " << file_id << " from " << source;
|
|
|
|
auto node = get_sync_file_node(file_id);
|
|
|
|
if (!node) {
|
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto file_view = FileView(node);
|
|
|
|
|
2022-07-03 11:01:08 +02:00
|
|
|
send_closure(G()->download_manager(), &DownloadManager::remove_file_if_finished, file_view.get_main_file_id());
|
2022-06-16 12:32:14 +02:00
|
|
|
string path;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (file_view.has_local_location()) {
|
|
|
|
if (begins_with(file_view.local_location().path_, get_files_dir(file_view.get_type()))) {
|
|
|
|
clear_from_pmc(node);
|
2022-07-23 22:50:28 +02:00
|
|
|
if (context_->need_notify_on_new_files()) {
|
|
|
|
context_->on_new_file(-file_view.size(), -file_view.get_allocated_local_size(), -1);
|
|
|
|
}
|
2022-06-16 12:32:14 +02:00
|
|
|
path = std::move(node->local_.full().path_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (file_view.get_type() == FileType::Encrypted) {
|
|
|
|
clear_from_pmc(node);
|
|
|
|
}
|
2018-01-20 15:57:52 +01:00
|
|
|
if (node->local_.type() == LocalFileLocation::Type::Partial) {
|
2022-06-16 12:32:14 +02:00
|
|
|
path = std::move(node->local_.partial().path_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-16 12:32:14 +02:00
|
|
|
if (path.empty()) {
|
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Unlink file " << file_id << " at " << path;
|
|
|
|
node->drop_local_location();
|
|
|
|
try_flush_node(node, "delete_file");
|
|
|
|
send_closure(file_load_manager_, &FileLoadManager::unlink_file, path, std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-18 20:08:05 +01:00
|
|
|
void FileManager::download(FileId file_id, std::shared_ptr<DownloadCallback> callback, int32 new_priority, int64 offset,
|
2022-10-10 14:40:39 +02:00
|
|
|
int64 limit, Promise<td_api::object_ptr<td_api::file>> promise) {
|
2022-10-10 15:24:32 +02:00
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
auto node = get_sync_file_node(file_id);
|
|
|
|
if (!node) {
|
2018-04-24 19:10:12 +02:00
|
|
|
LOG(INFO) << "File " << file_id << " not found";
|
2022-10-10 14:40:39 +02:00
|
|
|
auto error = Status::Error(400, "File not found");
|
2018-12-31 20:04:05 +01:00
|
|
|
if (callback) {
|
2022-10-10 14:40:39 +02:00
|
|
|
callback->on_download_error(file_id, error.clone());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-10-10 14:40:39 +02:00
|
|
|
return promise.set_error(std::move(error));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-10-10 15:24:32 +02:00
|
|
|
if ((callback == nullptr && new_priority <= 0) || node->local_.type() == LocalFileLocation::Type::Empty) {
|
|
|
|
// skip local location check if download is canceled or there is no local location
|
|
|
|
return download_impl(file_id, std::move(callback), new_priority, offset, limit, Status::OK(), std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Asynchronously check location of file " << file_id << " before downloading";
|
|
|
|
auto check_promise =
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), file_id, callback = std::move(callback), new_priority, offset,
|
|
|
|
limit, promise = std::move(promise)](Result<Unit> result) mutable {
|
|
|
|
Status check_status;
|
|
|
|
if (result.is_error()) {
|
|
|
|
check_status = result.move_as_error();
|
|
|
|
}
|
|
|
|
send_closure(actor_id, &FileManager::download_impl, file_id, std::move(callback), new_priority, offset, limit,
|
|
|
|
std::move(check_status), std::move(promise));
|
|
|
|
});
|
|
|
|
check_local_location_async(node, true, std::move(check_promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileManager::download_impl(FileId file_id, std::shared_ptr<DownloadCallback> callback, int32 new_priority,
|
|
|
|
int64 offset, int64 limit, Status check_status,
|
|
|
|
Promise<td_api::object_ptr<td_api::file>> promise) {
|
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
|
|
|
|
|
|
|
LOG(INFO) << "Download file " << file_id << " with priority " << new_priority;
|
|
|
|
auto node = get_file_node(file_id);
|
|
|
|
CHECK(node);
|
|
|
|
|
|
|
|
if (check_status.is_error()) {
|
|
|
|
LOG(WARNING) << "Need to redownload file " << file_id << ": " << check_status;
|
2022-10-10 11:11:40 +02:00
|
|
|
}
|
2018-01-20 15:57:52 +01:00
|
|
|
if (node->local_.type() == LocalFileLocation::Type::Full) {
|
2022-10-10 11:11:40 +02:00
|
|
|
LOG(INFO) << "File " << file_id << " is already downloaded";
|
|
|
|
if (callback) {
|
|
|
|
callback->on_download_ok(file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-10-10 14:40:39 +02:00
|
|
|
return promise.set_value(get_file_object(file_id, false));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FileView file_view(node);
|
|
|
|
if (!file_view.can_download_from_server() && !file_view.can_generate()) {
|
2018-04-24 19:10:12 +02:00
|
|
|
LOG(INFO) << "File " << file_id << " can't be downloaded";
|
2022-10-10 14:40:39 +02:00
|
|
|
auto error = Status::Error(400, "Can't download or generate the file");
|
2018-12-31 20:04:05 +01:00
|
|
|
if (callback) {
|
2022-10-10 14:40:39 +02:00
|
|
|
callback->on_download_error(file_id, error.clone());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-10-10 14:40:39 +02:00
|
|
|
return promise.set_error(std::move(error));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 15:41:47 +01:00
|
|
|
if (new_priority == -1) {
|
|
|
|
if (node->is_download_started_) {
|
2018-04-24 19:10:12 +02:00
|
|
|
LOG(INFO) << "File " << file_id << " is being downloaded";
|
2022-10-10 14:40:39 +02:00
|
|
|
return promise.set_value(get_file_object(file_id, false));
|
2018-01-16 15:41:47 +01:00
|
|
|
}
|
|
|
|
new_priority = 0;
|
|
|
|
}
|
|
|
|
|
2022-10-26 11:29:12 +02:00
|
|
|
LOG(INFO) << "Change download priority of file " << file_id << " to " << new_priority << " with callback "
|
|
|
|
<< callback.get();
|
2018-11-11 12:38:04 +01:00
|
|
|
node->set_download_offset(offset);
|
2019-02-18 20:08:05 +01:00
|
|
|
node->set_download_limit(limit);
|
2018-12-31 20:04:05 +01:00
|
|
|
auto *file_info = get_file_id_info(file_id);
|
|
|
|
CHECK(new_priority == 0 || callback);
|
2019-03-27 19:43:46 +01:00
|
|
|
if (file_info->download_callback_ != nullptr && file_info->download_callback_.get() != callback.get()) {
|
2022-10-26 11:29:12 +02:00
|
|
|
// the old callback will be destroyed soon and lost forever
|
|
|
|
// this is a bug and must never happen, unless we cancel previous download query
|
|
|
|
// but still there is no way to prevent this with the current FileManager implementation
|
|
|
|
if (new_priority == 0) {
|
|
|
|
file_info->download_callback_->on_download_error(file_id, Status::Error(200, "Canceled"));
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "File " << file_id << " is used with different download callbacks";
|
|
|
|
file_info->download_callback_->on_download_error(file_id, Status::Error(500, "Internal Server Error"));
|
|
|
|
}
|
2019-03-27 19:43:46 +01:00
|
|
|
}
|
2022-02-28 13:15:00 +01:00
|
|
|
file_info->ignore_download_limit = limit == IGNORE_DOWNLOAD_LIMIT;
|
2018-01-20 12:47:53 +01:00
|
|
|
file_info->download_priority_ = narrow_cast<int8>(new_priority);
|
2018-12-31 20:04:05 +01:00
|
|
|
file_info->download_callback_ = std::move(callback);
|
2022-02-26 19:55:12 +01:00
|
|
|
|
|
|
|
if (file_info->download_callback_) {
|
|
|
|
file_info->download_callback_->on_progress(file_id);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
// TODO: send current progress?
|
|
|
|
|
|
|
|
run_generate(node);
|
2020-09-11 17:57:49 +02:00
|
|
|
run_download(node, true);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-12-27 18:23:19 +01:00
|
|
|
try_flush_node(node, "download");
|
2022-10-10 14:40:39 +02:00
|
|
|
promise.set_value(get_file_object(file_id, false));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-09-11 17:57:49 +02:00
|
|
|
void FileManager::run_download(FileNodePtr node, bool force_update_priority) {
|
2018-01-20 12:47:53 +01:00
|
|
|
int8 priority = 0;
|
2022-02-28 13:15:00 +01:00
|
|
|
bool ignore_download_limit = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto id : node->file_ids_) {
|
|
|
|
auto *info = get_file_id_info(id);
|
|
|
|
if (info->download_priority_ > priority) {
|
|
|
|
priority = info->download_priority_;
|
|
|
|
}
|
2022-02-28 13:15:00 +01:00
|
|
|
ignore_download_limit |= info->ignore_download_limit;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto old_priority = node->download_priority_;
|
|
|
|
|
|
|
|
if (priority == 0) {
|
2020-04-09 11:53:00 +02:00
|
|
|
node->set_download_priority(priority);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (old_priority != 0) {
|
2021-10-12 10:15:25 +02:00
|
|
|
LOG(INFO) << "Cancel downloading of file " << node->main_file_id_;
|
2019-01-25 02:52:38 +01:00
|
|
|
do_cancel_download(node);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2020-04-09 11:53:00 +02:00
|
|
|
|
|
|
|
if (node->need_load_from_pmc_) {
|
|
|
|
LOG(INFO) << "Skip run_download, because file " << node->main_file_id_ << " needs to be loaded from PMC";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (node->generate_id_) {
|
|
|
|
LOG(INFO) << "Skip run_download, because file " << node->main_file_id_ << " is being generated";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto file_view = FileView(node);
|
|
|
|
if (!file_view.can_download_from_server()) {
|
|
|
|
LOG(INFO) << "Skip run_download, because file " << node->main_file_id_ << " can't be downloaded from server";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
node->set_download_priority(priority);
|
2022-02-28 13:15:00 +01:00
|
|
|
node->set_ignore_download_limit(ignore_download_limit);
|
2018-11-11 12:38:04 +01:00
|
|
|
bool need_update_offset = node->is_download_offset_dirty_;
|
|
|
|
node->is_download_offset_dirty_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-02-18 20:08:05 +01:00
|
|
|
bool need_update_limit = node->is_download_limit_dirty_;
|
|
|
|
node->is_download_limit_dirty_ = false;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
if (old_priority != 0) {
|
2019-10-18 03:31:28 +02:00
|
|
|
LOG(INFO) << "Update download offset and limits of file " << node->main_file_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(node->download_id_ != 0);
|
2020-09-11 17:57:49 +02:00
|
|
|
if (force_update_priority || priority != old_priority) {
|
2024-07-12 17:15:44 +02:00
|
|
|
send_closure(file_download_manager_, &FileDownloadManager::update_priority, node->download_id_, priority);
|
2020-09-11 17:57:49 +02:00
|
|
|
}
|
2020-08-25 17:58:37 +02:00
|
|
|
if (need_update_limit || need_update_offset) {
|
|
|
|
auto download_offset = node->download_offset_;
|
2022-02-28 13:15:00 +01:00
|
|
|
auto download_limit = node->get_download_limit();
|
2020-08-25 17:58:37 +02:00
|
|
|
if (file_view.is_encrypted_any()) {
|
|
|
|
CHECK(download_offset <= MAX_FILE_SIZE);
|
2022-05-11 16:17:20 +02:00
|
|
|
CHECK(download_limit <= MAX_FILE_SIZE);
|
2020-08-25 17:58:37 +02:00
|
|
|
download_limit += download_offset;
|
|
|
|
download_offset = 0;
|
|
|
|
}
|
2024-07-12 17:15:44 +02:00
|
|
|
send_closure(file_download_manager_, &FileDownloadManager::update_downloaded_part, node->download_id_,
|
|
|
|
download_offset, download_limit);
|
2018-11-11 12:38:04 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(node->download_id_ == 0);
|
|
|
|
CHECK(!node->file_ids_.empty());
|
2019-01-17 21:42:00 +01:00
|
|
|
auto file_id = node->main_file_id_;
|
2018-12-05 10:32:31 +01:00
|
|
|
|
2019-07-30 16:42:36 +02:00
|
|
|
if (node->need_reload_photo_ && file_view.may_reload_photo()) {
|
2019-10-18 03:31:28 +02:00
|
|
|
LOG(INFO) << "Reload photo from file " << node->main_file_id_;
|
2024-07-12 17:15:44 +02:00
|
|
|
FileDownloadManager::QueryId query_id =
|
|
|
|
queries_container_.create(Query{file_id, Query::Type::DownloadReloadDialog});
|
2023-01-16 11:30:03 +01:00
|
|
|
node->download_id_ = query_id;
|
2019-07-30 16:42:36 +02:00
|
|
|
context_->reload_photo(file_view.remote_location().get_source(),
|
2023-07-11 16:53:43 +02:00
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), query_id, file_id](Result<Unit> res) {
|
2019-07-30 16:42:36 +02:00
|
|
|
Status error;
|
|
|
|
if (res.is_ok()) {
|
|
|
|
error = Status::Error("FILE_DOWNLOAD_ID_INVALID");
|
|
|
|
} else {
|
|
|
|
error = res.move_as_error();
|
|
|
|
}
|
|
|
|
VLOG(file_references)
|
2023-01-16 13:16:36 +01:00
|
|
|
<< "Receive result from reload photo for file " << file_id << ": " << error;
|
2024-07-12 16:27:26 +02:00
|
|
|
send_closure(actor_id, &FileManager::on_download_error, query_id, std::move(error));
|
2019-07-30 16:42:36 +02:00
|
|
|
}));
|
|
|
|
node->need_reload_photo_ = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-05 10:32:31 +01:00
|
|
|
// If file reference is needed
|
2019-01-30 17:08:50 +01:00
|
|
|
if (!file_view.has_active_download_remote_location()) {
|
2019-02-21 16:58:20 +01:00
|
|
|
VLOG(file_references) << "Do not have valid file_reference for file " << file_id;
|
2024-07-12 17:15:44 +02:00
|
|
|
FileDownloadManager::QueryId query_id =
|
2024-07-12 15:24:07 +02:00
|
|
|
queries_container_.create(Query{file_id, Query::Type::DownloadWaitFileReference});
|
2023-01-16 11:30:03 +01:00
|
|
|
node->download_id_ = query_id;
|
2019-01-17 21:42:00 +01:00
|
|
|
if (node->download_was_update_file_reference_) {
|
2024-07-12 16:27:26 +02:00
|
|
|
return on_download_error(query_id, Status::Error("Can't download file: have no valid file reference"));
|
2018-12-09 08:05:26 +01:00
|
|
|
}
|
2019-01-17 21:42:00 +01:00
|
|
|
node->download_was_update_file_reference_ = true;
|
2018-12-05 10:32:31 +01:00
|
|
|
|
2019-01-30 22:37:38 +01:00
|
|
|
context_->repair_file_reference(
|
2023-07-11 16:53:43 +02:00
|
|
|
file_id, PromiseCreator::lambda([actor_id = actor_id(this), query_id, file_id](Result<Unit> res) {
|
2019-01-30 22:37:38 +01:00
|
|
|
Status error;
|
|
|
|
if (res.is_ok()) {
|
|
|
|
error = Status::Error("FILE_DOWNLOAD_RESTART_WITH_FILE_REFERENCE");
|
|
|
|
} else {
|
|
|
|
error = res.move_as_error();
|
|
|
|
}
|
2023-01-16 13:16:36 +01:00
|
|
|
VLOG(file_references) << "Receive result from FileSourceManager for file " << file_id << ": " << error;
|
2024-07-12 16:27:26 +02:00
|
|
|
send_closure(actor_id, &FileManager::on_download_error, query_id, std::move(error));
|
2019-01-30 22:37:38 +01:00
|
|
|
}));
|
2018-12-05 10:32:31 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-12 17:15:44 +02:00
|
|
|
FileDownloadManager::QueryId query_id = queries_container_.create(Query{file_id, Query::Type::Download});
|
2023-01-16 11:30:03 +01:00
|
|
|
node->download_id_ = query_id;
|
2018-01-09 12:57:11 +01:00
|
|
|
node->is_download_started_ = false;
|
2019-10-18 03:31:28 +02:00
|
|
|
LOG(INFO) << "Run download of file " << file_id << " of size " << node->size_ << " from "
|
2021-01-26 00:39:48 +01:00
|
|
|
<< node->remote_.full.value() << " with suggested name " << node->suggested_path() << " and encyption key "
|
2019-10-18 03:31:28 +02:00
|
|
|
<< node->encryption_key_;
|
2020-08-25 17:58:37 +02:00
|
|
|
auto download_offset = node->download_offset_;
|
2022-02-28 13:15:00 +01:00
|
|
|
auto download_limit = node->get_download_limit();
|
2020-08-25 17:58:37 +02:00
|
|
|
if (file_view.is_encrypted_any()) {
|
|
|
|
CHECK(download_offset <= MAX_FILE_SIZE);
|
2022-05-11 16:17:20 +02:00
|
|
|
CHECK(download_limit <= MAX_FILE_SIZE);
|
2020-08-25 17:58:37 +02:00
|
|
|
download_limit += download_offset;
|
|
|
|
download_offset = 0;
|
|
|
|
}
|
2024-07-12 17:15:44 +02:00
|
|
|
send_closure(file_download_manager_, &FileDownloadManager::download, query_id, node->remote_.full.value(),
|
|
|
|
node->local_, node->size_, node->suggested_path(), node->encryption_key_, node->can_search_locally_,
|
|
|
|
download_offset, download_limit, priority);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class FileManager::ForceUploadActor final : public Actor {
|
2019-02-14 12:20:40 +01:00
|
|
|
public:
|
2019-02-17 14:52:34 +01:00
|
|
|
ForceUploadActor(FileManager *file_manager, FileId file_id, std::shared_ptr<FileManager::UploadCallback> callback,
|
2021-01-22 16:13:38 +01:00
|
|
|
int32 new_priority, uint64 upload_order, bool prefer_small, ActorShared<> parent)
|
2019-02-17 14:52:34 +01:00
|
|
|
: file_manager_(file_manager)
|
|
|
|
, file_id_(file_id)
|
2019-02-14 12:20:40 +01:00
|
|
|
, callback_(std::move(callback))
|
|
|
|
, new_priority_(new_priority)
|
|
|
|
, upload_order_(upload_order)
|
2021-01-22 16:13:38 +01:00
|
|
|
, prefer_small_(prefer_small)
|
2019-02-14 12:20:40 +01:00
|
|
|
, parent_(std::move(parent)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-02-17 14:52:34 +01:00
|
|
|
FileManager *file_manager_;
|
2019-02-14 12:20:40 +01:00
|
|
|
FileId file_id_;
|
|
|
|
std::shared_ptr<FileManager::UploadCallback> callback_;
|
|
|
|
int32 new_priority_;
|
|
|
|
uint64 upload_order_;
|
2021-01-22 16:13:38 +01:00
|
|
|
bool prefer_small_;
|
2019-02-14 12:20:40 +01:00
|
|
|
ActorShared<> parent_;
|
|
|
|
bool is_active_{false};
|
|
|
|
int attempt_{0};
|
2021-01-22 16:13:38 +01:00
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class UploadCallback final : public FileManager::UploadCallback {
|
2019-02-14 12:20:40 +01:00
|
|
|
public:
|
2019-02-14 22:19:48 +01:00
|
|
|
explicit UploadCallback(ActorId<ForceUploadActor> callback) : callback_(std::move(callback)) {
|
2019-02-14 12:20:40 +01:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_upload_ok(FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file) final {
|
2020-08-13 19:04:06 +02:00
|
|
|
send_closure(std::move(callback_), &ForceUploadActor::on_upload_ok, std::move(input_file));
|
2019-02-14 12:20:40 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_upload_encrypted_ok(FileId file_id, tl_object_ptr<telegram_api::InputEncryptedFile> input_file) final {
|
2020-08-13 19:04:06 +02:00
|
|
|
send_closure(std::move(callback_), &ForceUploadActor::on_upload_encrypted_ok, std::move(input_file));
|
2019-02-14 12:20:40 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_upload_secure_ok(FileId file_id, tl_object_ptr<telegram_api::InputSecureFile> input_file) final {
|
2020-08-13 19:04:06 +02:00
|
|
|
send_closure(std::move(callback_), &ForceUploadActor::on_upload_secure_ok, std::move(input_file));
|
2019-02-14 12:20:40 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_upload_error(FileId file_id, Status error) final {
|
2020-08-13 19:04:06 +02:00
|
|
|
send_closure(std::move(callback_), &ForceUploadActor::on_upload_error, std::move(error));
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
~UploadCallback() final {
|
2020-08-13 19:04:06 +02:00
|
|
|
if (callback_.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-09-20 01:02:28 +02:00
|
|
|
send_closure(std::move(callback_), &ForceUploadActor::on_upload_error, Status::Error(200, "Canceled"));
|
2019-02-14 12:20:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ActorId<ForceUploadActor> callback_;
|
|
|
|
};
|
|
|
|
|
|
|
|
void on_upload_ok(tl_object_ptr<telegram_api::InputFile> input_file) {
|
|
|
|
is_active_ = false;
|
|
|
|
if (input_file || is_ready()) {
|
|
|
|
callback_->on_upload_ok(file_id_, std::move(input_file));
|
|
|
|
on_ok();
|
|
|
|
} else {
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_upload_encrypted_ok(tl_object_ptr<telegram_api::InputEncryptedFile> input_file) {
|
|
|
|
is_active_ = false;
|
|
|
|
if (input_file || is_ready()) {
|
|
|
|
callback_->on_upload_encrypted_ok(file_id_, std::move(input_file));
|
|
|
|
on_ok();
|
|
|
|
} else {
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_upload_secure_ok(tl_object_ptr<telegram_api::InputSecureFile> input_file) {
|
|
|
|
is_active_ = false;
|
|
|
|
if (input_file || is_ready()) {
|
|
|
|
callback_->on_upload_secure_ok(file_id_, std::move(input_file));
|
|
|
|
on_ok();
|
|
|
|
} else {
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:19:48 +01:00
|
|
|
bool is_ready() const {
|
2019-02-17 14:52:34 +01:00
|
|
|
return !G()->close_flag() && file_manager_->get_file_view(file_id_).has_active_upload_remote_location();
|
2019-02-14 12:20:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void on_ok() {
|
|
|
|
callback_.reset();
|
2019-11-16 18:09:27 +01:00
|
|
|
send_closure(G()->file_manager(), &FileManager::on_force_reupload_success, file_id_);
|
2019-02-14 12:20:40 +01:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_upload_error(Status error) {
|
|
|
|
if (attempt_ == 2) {
|
|
|
|
callback_->on_upload_error(file_id_, std::move(error));
|
|
|
|
callback_.reset();
|
|
|
|
stop();
|
2019-02-14 22:19:48 +01:00
|
|
|
} else {
|
|
|
|
is_active_ = false;
|
|
|
|
loop();
|
2019-02-14 12:20:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto create_callback() {
|
|
|
|
return std::make_shared<UploadCallback>(actor_id(this));
|
|
|
|
}
|
2019-02-14 22:19:48 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void loop() final {
|
2019-02-14 12:20:40 +01:00
|
|
|
if (is_active_) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-13 19:04:06 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return stop();
|
|
|
|
}
|
2019-02-14 12:20:40 +01:00
|
|
|
|
|
|
|
is_active_ = true;
|
|
|
|
attempt_++;
|
2022-08-02 13:45:22 +02:00
|
|
|
send_closure(G()->file_manager(), &FileManager::resume_upload, file_id_, vector<int>(), create_callback(),
|
2021-01-22 16:13:38 +01:00
|
|
|
new_priority_, upload_order_, attempt_ == 2, prefer_small_);
|
2019-02-14 12:20:40 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void tear_down() final {
|
2019-02-14 12:20:40 +01:00
|
|
|
if (callback_) {
|
2022-09-20 01:02:28 +02:00
|
|
|
callback_->on_upload_error(file_id_, Status::Error(200, "Canceled"));
|
2019-02-14 12:20:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-16 18:09:27 +01:00
|
|
|
void FileManager::on_force_reupload_success(FileId file_id) {
|
|
|
|
auto node = get_sync_file_node(file_id);
|
|
|
|
CHECK(node);
|
2020-04-11 02:56:19 +02:00
|
|
|
if (!node->remote_.is_full_alive) { // do not update for multiple simultaneous uploads
|
|
|
|
node->last_successful_force_reupload_time_ = Time::now();
|
|
|
|
}
|
2019-11-16 18:09:27 +01:00
|
|
|
}
|
|
|
|
|
2022-08-02 13:45:22 +02:00
|
|
|
void FileManager::resume_upload(FileId file_id, vector<int> bad_parts, std::shared_ptr<UploadCallback> callback,
|
2021-01-22 16:13:38 +01:00
|
|
|
int32 new_priority, uint64 upload_order, bool force, bool prefer_small) {
|
2018-01-30 21:04:22 +01:00
|
|
|
auto node = get_sync_file_node(file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!node) {
|
2018-04-24 19:10:12 +02:00
|
|
|
LOG(INFO) << "File " << file_id << " not found";
|
2018-12-31 20:04:05 +01:00
|
|
|
if (callback) {
|
2022-09-20 01:02:28 +02:00
|
|
|
callback->on_upload_error(file_id, Status::Error(400, "File not found"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-11-16 18:09:27 +01:00
|
|
|
|
|
|
|
if (bad_parts.size() == 1 && bad_parts[0] == -1) {
|
|
|
|
if (node->last_successful_force_reupload_time_ >= Time::now() - 60) {
|
|
|
|
LOG(INFO) << "Recently reuploaded file " << file_id << ", do not try again";
|
|
|
|
if (callback) {
|
2022-09-20 01:02:28 +02:00
|
|
|
callback->on_upload_error(file_id, Status::Error(400, "Failed to reupload file"));
|
2019-11-16 18:09:27 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
create_actor<ForceUploadActor>("ForceUploadActor", this, file_id, std::move(callback), new_priority, upload_order,
|
2021-01-22 16:13:38 +01:00
|
|
|
prefer_small, context_->create_reference())
|
2019-11-16 18:09:27 +01:00
|
|
|
.release();
|
|
|
|
return;
|
|
|
|
}
|
2024-04-29 21:08:58 +02:00
|
|
|
if (new_priority == 0) {
|
|
|
|
LOG(INFO) << "Cancel upload of file " << file_id;
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Resume upload of file " << file_id << " with priority " << new_priority << " and force = " << force;
|
|
|
|
}
|
2019-11-16 18:09:27 +01:00
|
|
|
|
2019-02-14 12:20:40 +01:00
|
|
|
if (force) {
|
|
|
|
node->remote_.is_full_alive = false;
|
|
|
|
}
|
2021-01-22 16:13:38 +01:00
|
|
|
if (prefer_small) {
|
|
|
|
node->upload_prefer_small_ = true;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (node->upload_pause_ == file_id) {
|
2019-01-10 22:12:51 +01:00
|
|
|
node->set_upload_pause(FileId());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-11-21 13:28:32 +01:00
|
|
|
SCOPE_EXIT {
|
|
|
|
try_flush_node(node, "resume_upload");
|
|
|
|
};
|
2018-12-31 20:04:05 +01:00
|
|
|
FileView file_view(node);
|
2022-04-22 13:28:51 +02:00
|
|
|
if (file_view.has_active_upload_remote_location() && can_reuse_remote_file(file_view.get_type())) {
|
2024-04-29 21:08:58 +02:00
|
|
|
LOG(INFO) << "Upload of file " << file_id << " has already been completed";
|
2018-12-31 20:04:05 +01:00
|
|
|
if (callback) {
|
|
|
|
callback->on_upload_ok(file_id, nullptr);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-15 12:13:23 +02:00
|
|
|
if (file_view.has_local_location() && new_priority != 0) {
|
2022-10-09 20:20:57 +02:00
|
|
|
auto status = check_local_location(node, false);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (status.is_error()) {
|
|
|
|
LOG(INFO) << "Full local location of file " << file_id << " for upload is invalid: " << status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 12:20:40 +01:00
|
|
|
if (!file_view.has_local_location() && !file_view.has_generate_location() && !file_view.has_alive_remote_location()) {
|
2018-04-24 19:10:12 +02:00
|
|
|
LOG(INFO) << "File " << file_id << " can't be uploaded";
|
2018-12-31 20:04:05 +01:00
|
|
|
if (callback) {
|
2022-09-20 01:02:28 +02:00
|
|
|
callback->on_upload_error(
|
|
|
|
file_id, Status::Error(400, "Need full local (or generate, or inactive remote) location for upload"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-10-18 03:31:28 +02:00
|
|
|
if (file_view.get_type() == FileType::Thumbnail &&
|
|
|
|
(!file_view.has_local_location() && file_view.can_download_from_server())) {
|
|
|
|
// TODO
|
|
|
|
if (callback) {
|
2022-09-20 01:02:28 +02:00
|
|
|
callback->on_upload_error(file_id, Status::Error(400, "Failed to upload thumbnail without local location"));
|
2019-10-18 03:31:28 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-10-26 11:29:12 +02:00
|
|
|
LOG(INFO) << "Change upload priority of file " << file_id << " to " << new_priority << " with callback "
|
|
|
|
<< callback.get();
|
2018-12-31 20:04:05 +01:00
|
|
|
auto *file_info = get_file_id_info(file_id);
|
|
|
|
CHECK(new_priority == 0 || callback);
|
2022-10-26 11:29:12 +02:00
|
|
|
if (file_info->upload_callback_ != nullptr && file_info->upload_callback_.get() != callback.get()) {
|
|
|
|
// the old callback will be destroyed soon and lost forever
|
|
|
|
// this is a bug and must never happen, unless we cancel previous upload query
|
|
|
|
// but still there is no way to prevent this with the current FileManager implementation
|
|
|
|
if (new_priority == 0) {
|
|
|
|
file_info->upload_callback_->on_upload_error(file_id, Status::Error(200, "Canceled"));
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "File " << file_id << " is used with different upload callbacks";
|
|
|
|
file_info->upload_callback_->on_upload_error(file_id, Status::Error(500, "Internal Server Error"));
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
file_info->upload_order_ = upload_order;
|
2018-01-20 12:47:53 +01:00
|
|
|
file_info->upload_priority_ = narrow_cast<int8>(new_priority);
|
2018-12-31 20:04:05 +01:00
|
|
|
file_info->upload_callback_ = std::move(callback);
|
|
|
|
// TODO: send current progress?
|
|
|
|
|
|
|
|
run_generate(node);
|
|
|
|
run_upload(node, std::move(bad_parts));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileManager::delete_partial_remote_location(FileId file_id) {
|
2018-01-30 21:04:22 +01:00
|
|
|
auto node = get_sync_file_node(file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!node) {
|
2020-04-19 12:45:37 +02:00
|
|
|
LOG(INFO) << "Wrong file identifier " << file_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (node->upload_pause_ == file_id) {
|
2019-01-10 22:12:51 +01:00
|
|
|
node->set_upload_pause(FileId());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-11-21 13:28:32 +01:00
|
|
|
SCOPE_EXIT {
|
|
|
|
try_flush_node(node, "delete_partial_remote_location");
|
|
|
|
};
|
2019-02-14 12:20:40 +01:00
|
|
|
if (node->remote_.is_full_alive) {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(INFO) << "File " << file_id << " is already uploaded";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-14 12:20:40 +01:00
|
|
|
node->delete_partial_remote_location();
|
2018-12-31 20:04:05 +01:00
|
|
|
auto *file_info = get_file_id_info(file_id);
|
|
|
|
file_info->upload_priority_ = 0;
|
|
|
|
|
2018-01-20 15:57:52 +01:00
|
|
|
if (node->local_.type() != LocalFileLocation::Type::Full) {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(INFO) << "Need full local location to upload file " << file_id;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-09 20:20:57 +02:00
|
|
|
auto status = check_local_location(node, false);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (status.is_error()) {
|
|
|
|
LOG(INFO) << "Need full local location to upload file " << file_id << ": " << status;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-08-02 13:45:22 +02:00
|
|
|
run_upload(node, vector<int>());
|
2018-12-31 20:04:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-03-12 16:52:23 +01:00
|
|
|
void FileManager::delete_partial_remote_location_if_needed(FileId file_id, const Status &error) {
|
|
|
|
if (error.code() != 429 && error.code() < 500 && !G()->close_flag()) {
|
|
|
|
delete_partial_remote_location(file_id);
|
|
|
|
} else {
|
|
|
|
cancel_upload(file_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
void FileManager::delete_file_reference(FileId file_id, Slice file_reference) {
|
2019-01-14 19:46:04 +01:00
|
|
|
VLOG(file_references) << "Delete file reference of file " << file_id << " "
|
2019-01-23 20:20:48 +01:00
|
|
|
<< tag("reference_base64", base64_encode(file_reference));
|
2018-12-05 10:32:31 +01:00
|
|
|
auto node = get_sync_file_node(file_id);
|
|
|
|
if (!node) {
|
2020-04-19 12:45:37 +02:00
|
|
|
LOG(ERROR) << "Wrong file identifier " << file_id;
|
2018-12-05 10:32:31 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
node->delete_file_reference(file_reference);
|
2019-01-31 00:57:14 +01:00
|
|
|
auto remote = get_remote(file_id.get_remote());
|
|
|
|
if (remote != nullptr) {
|
|
|
|
VLOG(file_references) << "Do delete file reference of remote file " << file_id;
|
|
|
|
if (remote->delete_file_reference(file_reference)) {
|
2019-11-16 23:43:47 +01:00
|
|
|
VLOG(file_references) << "Successfully deleted file reference of remote file " << file_id;
|
2019-01-31 00:57:14 +01:00
|
|
|
node->upload_was_update_file_reference_ = false;
|
|
|
|
node->download_was_update_file_reference_ = false;
|
|
|
|
node->on_pmc_changed();
|
|
|
|
}
|
|
|
|
}
|
2019-01-30 17:40:23 +01:00
|
|
|
try_flush_node_pmc(node, "delete_file_reference");
|
2018-12-05 10:32:31 +01:00
|
|
|
}
|
|
|
|
|
2023-01-16 11:30:03 +01:00
|
|
|
void FileManager::external_file_generate_write_part(int64 generation_id, int64 offset, string data, Promise<> promise) {
|
|
|
|
send_closure(file_generate_manager_, &FileGenerateManager::external_file_generate_write_part,
|
|
|
|
static_cast<uint64>(generation_id), offset, std::move(data), std::move(promise));
|
2019-04-26 02:18:00 +02:00
|
|
|
}
|
|
|
|
|
2023-01-16 11:30:03 +01:00
|
|
|
void FileManager::external_file_generate_progress(int64 generation_id, int64 expected_size, int64 local_prefix_size,
|
2018-12-31 20:04:05 +01:00
|
|
|
Promise<> promise) {
|
2023-01-16 11:30:03 +01:00
|
|
|
send_closure(file_generate_manager_, &FileGenerateManager::external_file_generate_progress,
|
|
|
|
static_cast<uint64>(generation_id), expected_size, local_prefix_size, std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-01-11 03:45:03 +01:00
|
|
|
|
2023-01-16 11:30:03 +01:00
|
|
|
void FileManager::external_file_generate_finish(int64 generation_id, Status status, Promise<> promise) {
|
|
|
|
send_closure(file_generate_manager_, &FileGenerateManager::external_file_generate_finish,
|
|
|
|
static_cast<uint64>(generation_id), std::move(status), std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-01-30 17:54:17 +01:00
|
|
|
void FileManager::run_generate(FileNodePtr node) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (node->need_load_from_pmc_) {
|
2019-10-18 03:31:28 +02:00
|
|
|
LOG(INFO) << "Skip run_generate, because file " << node->main_file_id_ << " needs to be loaded from PMC";
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
FileView file_view(node);
|
2020-05-24 20:35:47 +02:00
|
|
|
if (!file_view.can_generate()) {
|
2021-10-12 10:15:25 +02:00
|
|
|
// LOG(INFO) << "Skip run_generate, because file " << node->main_file_id_ << " can't be generated";
|
2020-05-24 20:35:47 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-10-18 03:31:28 +02:00
|
|
|
if (file_view.has_local_location()) {
|
|
|
|
LOG(INFO) << "Skip run_generate, because file " << node->main_file_id_ << " has local location";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (file_view.can_download_from_server()) {
|
|
|
|
LOG(INFO) << "Skip run_generate, because file " << node->main_file_id_ << " can be downloaded from server";
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-01-20 12:47:53 +01:00
|
|
|
int8 download_priority = 0;
|
|
|
|
int8 upload_priority = 0;
|
2018-03-18 11:45:59 +01:00
|
|
|
FileId file_id = node->main_file_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto id : node->file_ids_) {
|
|
|
|
auto *info = get_file_id_info(id);
|
|
|
|
if (info->download_priority_ > download_priority) {
|
|
|
|
download_priority = info->download_priority_;
|
|
|
|
if (download_priority > upload_priority) {
|
|
|
|
file_id = id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (info->upload_priority_ > upload_priority) {
|
|
|
|
upload_priority = info->upload_priority_;
|
|
|
|
if (upload_priority > download_priority) {
|
|
|
|
file_id = id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto old_priority = node->generate_priority_;
|
|
|
|
node->set_generate_priority(download_priority, upload_priority);
|
|
|
|
|
|
|
|
if (node->generate_priority_ == 0) {
|
|
|
|
if (old_priority != 0) {
|
|
|
|
LOG(INFO) << "Cancel file " << file_id << " generation";
|
2019-01-25 02:52:38 +01:00
|
|
|
do_cancel_generate(node);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old_priority != 0) {
|
|
|
|
LOG(INFO) << "TODO: change file " << file_id << " generation priority";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-12 17:15:44 +02:00
|
|
|
FileDownloadManager::QueryId query_id = queries_container_.create(Query{file_id, Query::Type::Generate});
|
2023-01-16 11:30:03 +01:00
|
|
|
node->generate_id_ = query_id;
|
2024-07-12 15:24:07 +02:00
|
|
|
send_closure(file_generate_manager_, &FileGenerateManager::generate_file, query_id, *node->generate_, node->local_,
|
|
|
|
node->suggested_path(), [file_manager = this, query_id] {
|
|
|
|
class Callback final : public FileGenerateCallback {
|
|
|
|
ActorId<FileManager> actor_;
|
|
|
|
uint64 query_id_;
|
|
|
|
|
|
|
|
public:
|
2024-07-12 17:15:44 +02:00
|
|
|
Callback(ActorId<FileManager> actor, FileDownloadManager::QueryId query_id)
|
2024-07-12 15:24:07 +02:00
|
|
|
: actor_(std::move(actor)), query_id_(query_id) {
|
|
|
|
}
|
|
|
|
void on_partial_generate(PartialLocalFileLocation partial_local, int64 expected_size) final {
|
|
|
|
send_closure(actor_, &FileManager::on_partial_generate, query_id_, std::move(partial_local),
|
|
|
|
expected_size);
|
|
|
|
}
|
|
|
|
void on_ok(FullLocalFileLocation local) final {
|
|
|
|
send_closure(actor_, &FileManager::on_generate_ok, query_id_, std::move(local));
|
|
|
|
}
|
|
|
|
void on_error(Status error) final {
|
2024-07-12 16:27:26 +02:00
|
|
|
send_closure(actor_, &FileManager::on_download_error, query_id_, std::move(error));
|
2024-07-12 15:24:07 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
return make_unique<Callback>(file_manager->actor_id(file_manager), query_id);
|
|
|
|
}());
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
LOG(INFO) << "File " << file_id << " generate request has sent to FileGenerateManager";
|
|
|
|
}
|
|
|
|
|
2022-08-02 13:45:22 +02:00
|
|
|
void FileManager::run_upload(FileNodePtr node, vector<int> bad_parts) {
|
2020-04-09 11:53:00 +02:00
|
|
|
int8 priority = 0;
|
|
|
|
FileId file_id = node->main_file_id_;
|
|
|
|
for (auto id : node->file_ids_) {
|
|
|
|
auto *info = get_file_id_info(id);
|
|
|
|
if (info->upload_priority_ > priority) {
|
|
|
|
priority = info->upload_priority_;
|
|
|
|
file_id = id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto old_priority = node->upload_priority_;
|
|
|
|
|
|
|
|
if (priority == 0) {
|
|
|
|
node->set_upload_priority(priority);
|
|
|
|
if (old_priority != 0) {
|
|
|
|
LOG(INFO) << "Cancel file " << file_id << " uploading";
|
|
|
|
do_cancel_upload(node);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
if (node->need_load_from_pmc_) {
|
2019-01-10 22:12:51 +01:00
|
|
|
LOG(INFO) << "File " << node->main_file_id_ << " needs to be loaded from database before upload";
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (node->upload_pause_.is_valid()) {
|
2019-01-10 22:12:51 +01:00
|
|
|
LOG(INFO) << "File " << node->main_file_id_ << " upload is paused: " << node->upload_pause_;
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-04-09 11:53:00 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
FileView file_view(node);
|
2019-01-25 12:03:17 +01:00
|
|
|
if (!file_view.has_local_location() && !file_view.has_remote_location()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (node->get_by_hash_ || node->generate_id_ == 0 || !node->generate_was_update_) {
|
2019-01-10 22:12:51 +01:00
|
|
|
LOG(INFO) << "Have no local location for file: get_by_hash = " << node->get_by_hash_
|
|
|
|
<< ", generate_id = " << node->generate_id_ << ", generate_was_update = " << node->generate_was_update_;
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-04-25 18:00:40 +02:00
|
|
|
if (file_view.has_generate_location() && file_view.generate_location().file_type_ == FileType::SecureEncrypted) {
|
2020-05-24 20:35:47 +02:00
|
|
|
// Can't upload secure file before its size is known
|
2019-01-10 22:12:51 +01:00
|
|
|
LOG(INFO) << "Can't upload secure file " << node->main_file_id_ << " before it's size is known";
|
2018-04-03 19:49:07 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
node->set_upload_priority(priority);
|
|
|
|
|
|
|
|
// create encryption key if necessary
|
2018-01-20 15:57:52 +01:00
|
|
|
if (((file_view.has_generate_location() && file_view.generate_location().file_type_ == FileType::Encrypted) ||
|
|
|
|
(file_view.has_local_location() && file_view.local_location().file_type_ == FileType::Encrypted)) &&
|
2018-12-31 20:04:05 +01:00
|
|
|
file_view.encryption_key().empty()) {
|
|
|
|
CHECK(!node->file_ids_.empty());
|
|
|
|
bool success = set_encryption_key(node->file_ids_[0], FileEncryptionKey::create());
|
|
|
|
LOG_IF(FATAL, !success) << "Failed to set encryption key for file " << file_id;
|
|
|
|
}
|
|
|
|
|
2018-04-03 19:49:07 +02:00
|
|
|
// create encryption key if necessary
|
2022-04-25 18:00:40 +02:00
|
|
|
if (file_view.has_local_location() && file_view.local_location().file_type_ == FileType::SecureEncrypted &&
|
2018-04-03 19:49:07 +02:00
|
|
|
file_view.encryption_key().empty()) {
|
|
|
|
CHECK(!node->file_ids_.empty());
|
|
|
|
bool success = set_encryption_key(node->file_ids_[0], FileEncryptionKey::create_secure_key());
|
|
|
|
LOG_IF(FATAL, !success) << "Failed to set encryption key for file " << file_id;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
if (old_priority != 0) {
|
|
|
|
LOG(INFO) << "File " << file_id << " is already uploading";
|
|
|
|
CHECK(node->upload_id_ != 0);
|
2024-07-12 16:27:26 +02:00
|
|
|
send_closure(file_upload_manager_, &FileUploadManager::update_priority, node->upload_id_,
|
|
|
|
narrow_cast<int8>(-priority));
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(node->upload_id_ == 0);
|
2019-02-15 10:25:21 +01:00
|
|
|
if (file_view.has_alive_remote_location() && !file_view.has_active_upload_remote_location() &&
|
2022-04-22 13:28:51 +02:00
|
|
|
can_reuse_remote_file(file_view.get_type())) {
|
2024-07-12 16:27:26 +02:00
|
|
|
FileUploadManager::QueryId query_id =
|
|
|
|
queries_container_.create(Query{file_id, Query::Type::UploadWaitFileReference});
|
2023-01-16 11:30:03 +01:00
|
|
|
node->upload_id_ = query_id;
|
2019-02-15 10:25:21 +01:00
|
|
|
if (node->upload_was_update_file_reference_) {
|
2024-07-12 16:27:26 +02:00
|
|
|
return on_upload_error(query_id, Status::Error("Can't upload file: have no valid file reference"));
|
2019-02-15 10:25:21 +01:00
|
|
|
}
|
2019-01-17 21:42:00 +01:00
|
|
|
node->upload_was_update_file_reference_ = true;
|
2018-12-09 08:05:26 +01:00
|
|
|
|
2023-01-16 11:30:03 +01:00
|
|
|
context_->repair_file_reference(node->main_file_id_,
|
2023-07-11 16:53:43 +02:00
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), query_id](Result<Unit> res) {
|
2024-07-12 16:27:26 +02:00
|
|
|
send_closure(actor_id, &FileManager::on_upload_error, query_id,
|
2023-01-16 11:30:03 +01:00
|
|
|
Status::Error("FILE_UPLOAD_RESTART_WITH_FILE_REFERENCE"));
|
|
|
|
}));
|
2018-12-05 10:32:31 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-14 12:20:40 +01:00
|
|
|
if (!node->remote_.partial && node->get_by_hash_) {
|
2019-01-10 22:12:51 +01:00
|
|
|
LOG(INFO) << "Get file " << node->main_file_id_ << " by hash";
|
2024-07-12 16:27:26 +02:00
|
|
|
FileUploadManager::QueryId query_id = queries_container_.create(Query{file_id, Query::Type::UploadByHash});
|
2023-01-16 11:30:03 +01:00
|
|
|
node->upload_id_ = query_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2024-07-12 16:27:26 +02:00
|
|
|
send_closure(file_upload_manager_, &FileUploadManager::upload_by_hash, query_id, node->local_.full(), node->size_,
|
2018-01-20 12:47:53 +01:00
|
|
|
narrow_cast<int8>(-priority));
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-25 15:31:06 +01:00
|
|
|
auto new_priority = narrow_cast<int8>(bad_parts.empty() ? -priority : priority);
|
2019-10-21 15:25:56 +02:00
|
|
|
td::remove_if(bad_parts, [](auto part_id) { return part_id < 0; });
|
2019-01-25 04:01:09 +01:00
|
|
|
|
2021-01-22 16:13:38 +01:00
|
|
|
auto expected_size = file_view.expected_size(true);
|
|
|
|
if (node->upload_prefer_small_ && (10 << 20) < expected_size && expected_size < (30 << 20)) {
|
|
|
|
expected_size = 10 << 20;
|
|
|
|
}
|
|
|
|
|
2024-07-12 16:27:26 +02:00
|
|
|
FileUploadManager::QueryId query_id = queries_container_.create(Query{file_id, Query::Type::Upload});
|
2023-01-16 11:30:03 +01:00
|
|
|
node->upload_id_ = query_id;
|
2024-07-12 16:27:26 +02:00
|
|
|
send_closure(file_upload_manager_, &FileUploadManager::upload, query_id, node->local_,
|
|
|
|
node->remote_.partial_or_empty(), expected_size, node->encryption_key_, new_priority,
|
|
|
|
std::move(bad_parts));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2024-07-12 16:27:26 +02:00
|
|
|
LOG(INFO) << "File " << file_id << " upload request has sent to FileUploadManager";
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileManager::upload(FileId file_id, std::shared_ptr<UploadCallback> callback, int32 new_priority,
|
|
|
|
uint64 upload_order) {
|
2022-08-02 13:45:22 +02:00
|
|
|
return resume_upload(file_id, vector<int>(), std::move(callback), new_priority, upload_order);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-25 02:52:38 +01:00
|
|
|
void FileManager::cancel_upload(FileId file_id) {
|
2022-08-02 13:45:22 +02:00
|
|
|
return resume_upload(file_id, vector<int>(), nullptr, 0, 0);
|
2019-01-25 02:52:38 +01:00
|
|
|
}
|
|
|
|
|
2019-05-07 04:51:56 +02:00
|
|
|
static bool is_background_type(FileType type) {
|
|
|
|
return type == FileType::Wallpaper || type == FileType::Background;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result<FileId> FileManager::from_persistent_id(CSlice persistent_id, FileType file_type) {
|
|
|
|
if (persistent_id.find('.') != string::npos) {
|
2021-06-26 03:20:59 +02:00
|
|
|
auto r_http_url = parse_url(persistent_id);
|
|
|
|
if (r_http_url.is_error()) {
|
|
|
|
return Status::Error(400, PSLICE() << "Invalid file HTTP URL specified: " << r_http_url.error().message());
|
|
|
|
}
|
|
|
|
auto url = r_http_url.ok().get_url();
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!clean_input_string(url)) {
|
|
|
|
return Status::Error(400, "URL must be in UTF-8");
|
|
|
|
}
|
2018-01-26 13:56:19 +01:00
|
|
|
return register_url(std::move(url), file_type, FileLocationSource::FromUser, DialogId());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto r_binary = base64url_decode(persistent_id);
|
|
|
|
if (r_binary.is_error()) {
|
2021-04-29 15:03:50 +02:00
|
|
|
return Status::Error(400, PSLICE() << "Wrong remote file identifier specified: " << r_binary.error().message());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
auto binary = r_binary.move_as_ok();
|
|
|
|
if (binary.empty()) {
|
2022-09-10 21:48:34 +02:00
|
|
|
return Status::Error(400, "Remote file identifier must be non-empty");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-07-06 17:31:26 +02:00
|
|
|
if (binary.back() == FileNode::PERSISTENT_ID_VERSION_OLD) {
|
2018-12-17 20:56:47 +01:00
|
|
|
return from_persistent_id_v2(binary, file_type);
|
|
|
|
}
|
2020-07-06 17:31:26 +02:00
|
|
|
if (binary.back() == FileNode::PERSISTENT_ID_VERSION) {
|
2019-06-12 16:28:00 +02:00
|
|
|
return from_persistent_id_v3(binary, file_type);
|
|
|
|
}
|
2022-07-28 14:59:57 +02:00
|
|
|
if (binary.back() == FileNode::PERSISTENT_ID_VERSION_GENERATED) {
|
|
|
|
return from_persistent_id_generated(binary, file_type);
|
2018-12-17 20:56:47 +01:00
|
|
|
}
|
2021-04-29 15:03:50 +02:00
|
|
|
return Status::Error(400, "Wrong remote file identifier specified: can't unserialize it. Wrong last symbol");
|
2018-12-17 20:56:47 +01:00
|
|
|
}
|
|
|
|
|
2022-07-28 14:59:57 +02:00
|
|
|
Result<FileId> FileManager::from_persistent_id_generated(Slice binary, FileType file_type) {
|
2018-12-17 20:56:47 +01:00
|
|
|
binary.remove_suffix(1);
|
2018-12-17 21:59:24 +01:00
|
|
|
auto decoded_binary = zero_decode(binary);
|
2018-12-17 20:56:47 +01:00
|
|
|
FullGenerateFileLocation generate_location;
|
2018-12-17 21:59:24 +01:00
|
|
|
auto status = unserialize(generate_location, decoded_binary);
|
2018-12-17 20:56:47 +01:00
|
|
|
if (status.is_error()) {
|
2021-04-29 15:03:50 +02:00
|
|
|
return Status::Error(400, "Wrong remote file identifier specified: can't unserialize it");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-12-17 20:56:47 +01:00
|
|
|
auto real_file_type = generate_location.file_type_;
|
2018-12-17 21:59:24 +01:00
|
|
|
if ((real_file_type != file_type && file_type != FileType::Temp) ||
|
|
|
|
(real_file_type != FileType::Thumbnail && real_file_type != FileType::EncryptedThumbnail)) {
|
2022-08-07 10:55:58 +02:00
|
|
|
return Status::Error(400, PSLICE() << "Can't use file of type " << real_file_type << " as " << file_type);
|
2018-12-17 20:56:47 +01:00
|
|
|
}
|
2022-07-28 14:59:57 +02:00
|
|
|
if (!is_remotely_generated_file(generate_location.conversion_)) {
|
2021-04-29 15:03:50 +02:00
|
|
|
return Status::Error(400, "Unexpected conversion type");
|
2018-12-17 20:56:47 +01:00
|
|
|
}
|
|
|
|
FileData data;
|
|
|
|
data.generate_ = make_unique<FullGenerateFileLocation>(std::move(generate_location));
|
2022-11-12 07:47:33 +01:00
|
|
|
return register_file(std::move(data), FileLocationSource::FromUser, FileId(), "from_persistent_id_generated", false)
|
2022-07-28 14:59:57 +02:00
|
|
|
.move_as_ok();
|
2018-12-17 20:56:47 +01:00
|
|
|
}
|
|
|
|
|
2019-06-12 16:28:00 +02:00
|
|
|
Result<FileId> FileManager::from_persistent_id_v23(Slice binary, FileType file_type, int32 version) {
|
|
|
|
if (version < 0 || version >= static_cast<int32>(Version::Next)) {
|
2021-04-29 15:03:50 +02:00
|
|
|
return Status::Error(400, "Invalid remote file identifier");
|
2019-06-12 16:28:00 +02:00
|
|
|
}
|
2018-12-17 21:59:24 +01:00
|
|
|
auto decoded_binary = zero_decode(binary);
|
2018-12-31 20:04:05 +01:00
|
|
|
FullRemoteFileLocation remote_location;
|
2020-09-22 01:15:09 +02:00
|
|
|
log_event::WithVersion<TlParser> parser(decoded_binary);
|
2019-06-12 16:28:00 +02:00
|
|
|
parser.set_version(version);
|
2019-06-11 12:10:14 +02:00
|
|
|
parse(remote_location, parser);
|
|
|
|
parser.fetch_end();
|
|
|
|
auto status = parser.get_status();
|
2018-12-31 20:04:05 +01:00
|
|
|
if (status.is_error()) {
|
2021-04-29 15:03:50 +02:00
|
|
|
return Status::Error(400, "Wrong remote file identifier specified: can't unserialize it");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-20 15:57:52 +01:00
|
|
|
auto &real_file_type = remote_location.file_type_;
|
2022-04-21 20:54:33 +02:00
|
|
|
if (is_document_file_type(real_file_type) && is_document_file_type(file_type)) {
|
2018-01-20 15:57:52 +01:00
|
|
|
real_file_type = file_type;
|
2019-05-07 04:51:56 +02:00
|
|
|
} else if (is_background_type(real_file_type) && is_background_type(file_type)) {
|
2019-06-27 02:13:27 +02:00
|
|
|
// type of file matches, but real type is in the stored remote location
|
2018-01-20 15:57:52 +01:00
|
|
|
} else if (real_file_type != file_type && file_type != FileType::Temp) {
|
2022-08-07 10:55:58 +02:00
|
|
|
return Status::Error(400, PSLICE() << "Can't use file of type " << real_file_type << " as " << file_type);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
FileData data;
|
|
|
|
data.remote_ = RemoteFileLocation(std::move(remote_location));
|
2022-11-12 07:47:33 +01:00
|
|
|
auto file_id = register_file(std::move(data), FileLocationSource::FromUser, FileId(), "from_persistent_id_v23", false)
|
|
|
|
.move_as_ok();
|
2019-01-22 15:35:29 +01:00
|
|
|
return file_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2019-06-12 16:28:00 +02:00
|
|
|
Result<FileId> FileManager::from_persistent_id_v2(Slice binary, FileType file_type) {
|
|
|
|
binary.remove_suffix(1);
|
|
|
|
return from_persistent_id_v23(binary, file_type, 0);
|
|
|
|
}
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2019-06-12 16:28:00 +02:00
|
|
|
Result<FileId> FileManager::from_persistent_id_v3(Slice binary, FileType file_type) {
|
|
|
|
binary.remove_suffix(1);
|
|
|
|
if (binary.empty()) {
|
2021-04-29 15:03:50 +02:00
|
|
|
return Status::Error(400, "Invalid remote file identifier");
|
2019-06-12 16:28:00 +02:00
|
|
|
}
|
2019-07-01 18:52:48 +02:00
|
|
|
int32 version = static_cast<uint8>(binary.back());
|
2019-06-12 16:28:00 +02:00
|
|
|
binary.remove_suffix(1);
|
|
|
|
return from_persistent_id_v23(binary, file_type, version);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
FileView FileManager::get_file_view(FileId file_id) const {
|
2018-01-30 21:04:22 +01:00
|
|
|
auto file_node = get_file_node(file_id);
|
|
|
|
if (!file_node) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return FileView();
|
|
|
|
}
|
|
|
|
return FileView(file_node);
|
|
|
|
}
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
FileView FileManager::get_sync_file_view(FileId file_id) {
|
2018-01-30 21:04:22 +01:00
|
|
|
auto file_node = get_sync_file_node(file_id);
|
|
|
|
if (!file_node) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return FileView();
|
|
|
|
}
|
|
|
|
return FileView(file_node);
|
|
|
|
}
|
|
|
|
|
2019-01-21 18:19:02 +01:00
|
|
|
td_api::object_ptr<td_api::file> FileManager::get_file_object(FileId file_id, bool with_main_file_id) {
|
2023-07-26 15:57:32 +02:00
|
|
|
auto file_node_ptr = get_sync_file_node(file_id);
|
|
|
|
if (!file_node_ptr) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return td_api::make_object<td_api::file>(0, 0, 0, td_api::make_object<td_api::localFile>(),
|
|
|
|
td_api::make_object<td_api::remoteFile>());
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:57:32 +02:00
|
|
|
const FileNode *file_node = file_node_ptr.get();
|
|
|
|
string persistent_file_id = file_node->get_persistent_file_id();
|
|
|
|
string unique_file_id = file_node->get_unique_file_id();
|
2023-07-26 17:07:33 +02:00
|
|
|
bool is_downloading_completed = file_node->local_.type() == LocalFileLocation::Type::Full;
|
2018-12-17 20:56:47 +01:00
|
|
|
bool is_uploading_completed = !persistent_file_id.empty();
|
2023-07-26 16:09:08 +02:00
|
|
|
auto size = file_node->size_;
|
|
|
|
auto download_offset = file_node->download_offset_;
|
2023-07-26 17:07:33 +02:00
|
|
|
auto expected_size = file_node->expected_size();
|
|
|
|
auto local_prefix_size = file_node->local_prefix_size();
|
|
|
|
auto local_total_size = file_node->local_total_size();
|
|
|
|
auto remote_size = file_node->remote_size();
|
|
|
|
string path = file_node->path();
|
|
|
|
bool can_be_deleted = file_node->can_delete();
|
2023-07-26 15:57:32 +02:00
|
|
|
|
|
|
|
auto file_view = FileView(file_node_ptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
bool can_be_downloaded = file_view.can_download_from_server() || file_view.can_generate();
|
|
|
|
|
|
|
|
auto result_file_id = file_id;
|
|
|
|
auto *file_info = get_file_id_info(result_file_id);
|
2023-07-26 16:09:08 +02:00
|
|
|
FileId main_file_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (with_main_file_id) {
|
2023-07-26 17:07:33 +02:00
|
|
|
main_file_id = file_node->main_file_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!file_info->send_updates_flag_) {
|
2023-07-26 16:09:08 +02:00
|
|
|
result_file_id = main_file_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-07-26 16:09:08 +02:00
|
|
|
file_info = get_file_id_info(main_file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
file_info->send_updates_flag_ = true;
|
2018-01-25 23:09:07 +01:00
|
|
|
VLOG(update_file) << "Send file " << file_id << " as " << result_file_id << " and update send_updates_flag_ for file "
|
2023-07-26 16:09:08 +02:00
|
|
|
<< (with_main_file_id ? main_file_id : result_file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
return td_api::make_object<td_api::file>(
|
|
|
|
result_file_id.get(), size, expected_size,
|
|
|
|
td_api::make_object<td_api::localFile>(std::move(path), can_be_downloaded, can_be_deleted,
|
2023-07-26 17:07:33 +02:00
|
|
|
file_node->is_downloading(), is_downloading_completed, download_offset,
|
|
|
|
local_prefix_size, local_total_size),
|
2019-10-25 16:04:01 +02:00
|
|
|
td_api::make_object<td_api::remoteFile>(std::move(persistent_file_id), std::move(unique_file_id),
|
2023-07-26 17:07:33 +02:00
|
|
|
file_node->is_uploading(), is_uploading_completed, remote_size));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-21 18:19:02 +01:00
|
|
|
vector<int32> FileManager::get_file_ids_object(const vector<FileId> &file_ids, bool with_main_file_id) {
|
|
|
|
return transform(file_ids, [this, with_main_file_id](FileId file_id) {
|
|
|
|
auto file_view = get_sync_file_view(file_id);
|
|
|
|
auto result_file_id = file_id;
|
|
|
|
auto *file_info = get_file_id_info(result_file_id);
|
|
|
|
if (with_main_file_id) {
|
2023-07-26 16:09:08 +02:00
|
|
|
auto main_file_id = file_view.get_main_file_id();
|
2019-01-21 18:19:02 +01:00
|
|
|
if (!file_info->sent_file_id_flag_ && !file_info->send_updates_flag_) {
|
2023-07-26 16:09:08 +02:00
|
|
|
result_file_id = main_file_id;
|
2019-01-21 18:19:02 +01:00
|
|
|
}
|
2023-07-26 16:09:08 +02:00
|
|
|
file_info = get_file_id_info(main_file_id);
|
2019-01-21 18:19:02 +01:00
|
|
|
}
|
|
|
|
file_info->sent_file_id_flag_ = true;
|
|
|
|
|
|
|
|
return result_file_id.get();
|
|
|
|
});
|
2018-03-27 15:11:15 +02:00
|
|
|
}
|
|
|
|
|
2018-01-05 15:24:41 +01:00
|
|
|
Result<FileId> FileManager::check_input_file_id(FileType type, Result<FileId> result, bool is_encrypted,
|
2018-04-03 19:49:07 +02:00
|
|
|
bool allow_zero, bool is_secure) {
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_RESULT(file_id, std::move(result));
|
2018-01-05 15:24:41 +01:00
|
|
|
if (allow_zero && !file_id.is_valid()) {
|
|
|
|
return FileId();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-02-14 22:19:48 +01:00
|
|
|
auto file_node = get_sync_file_node(file_id); // we need full data about sent files
|
2018-01-30 21:04:22 +01:00
|
|
|
if (!file_node) {
|
2021-06-26 03:20:59 +02:00
|
|
|
return Status::Error(400, "File not found");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
auto file_view = FileView(file_node);
|
|
|
|
FileType real_type = file_view.get_type();
|
2020-05-04 02:01:01 +02:00
|
|
|
LOG(INFO) << "Checking file " << file_id << " of type " << type << "/" << real_type;
|
2018-04-03 19:49:07 +02:00
|
|
|
if (!is_encrypted && !is_secure) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (real_type != type && !(real_type == FileType::Temp && file_view.has_url()) &&
|
2022-04-21 20:54:33 +02:00
|
|
|
!(is_document_file_type(real_type) && is_document_file_type(type)) &&
|
2022-04-15 13:37:01 +02:00
|
|
|
!(is_background_type(real_type) && is_background_type(type)) &&
|
2023-05-25 13:19:54 +02:00
|
|
|
!(file_view.is_encrypted() && type == FileType::Ringtone) &&
|
|
|
|
!(real_type == FileType::PhotoStory && type == FileType::Photo) &&
|
|
|
|
!(real_type == FileType::Photo && type == FileType::PhotoStory)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
// TODO: send encrypted file to unencrypted chat
|
2022-08-07 10:55:58 +02:00
|
|
|
return Status::Error(400, PSLICE() << "Can't use file of type " << real_type << " as " << type);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file_view.has_remote_location()) {
|
2022-11-21 10:06:05 +01:00
|
|
|
// There are no reasons to dup file_id, because it will be duped anyway before upload/reupload
|
|
|
|
// It will not be duped in dup_message_content only if has_input_media(),
|
|
|
|
// but currently in this case the file never needs to be reuploaded
|
2021-07-21 02:16:55 +02:00
|
|
|
|
2022-11-21 10:06:05 +01:00
|
|
|
if (!is_encrypted) {
|
2021-07-21 02:16:55 +02:00
|
|
|
// URLs in non-secret chats never needs to be reuploaded, so they don't need to be duped
|
2022-11-21 10:06:05 +01:00
|
|
|
// non-URLs without remote location will be duped at dup_message_content, because they have no input media
|
2021-07-21 02:16:55 +02:00
|
|
|
return file_node->main_file_id_;
|
|
|
|
}
|
2022-11-21 10:06:05 +01:00
|
|
|
|
2022-11-21 09:40:43 +01:00
|
|
|
return dup_file_id(file_id, "check_input_file_id");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-02-16 03:29:28 +01:00
|
|
|
|
|
|
|
int32 remote_id = file_id.get_remote();
|
2023-05-10 14:29:38 +02:00
|
|
|
if (remote_id == 0 && context_->keep_exact_remote_location()) {
|
2019-02-16 03:29:28 +01:00
|
|
|
RemoteInfo info{file_view.remote_location(), FileLocationSource::FromUser, file_id};
|
|
|
|
remote_id = remote_location_info_.add(info);
|
|
|
|
if (remote_location_info_.get(remote_id).file_id_ == file_id) {
|
|
|
|
get_file_id_info(file_id)->pin_flag_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FileId(file_node->main_file_id_.get(), remote_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result<FileId> FileManager::get_input_thumbnail_file_id(const tl_object_ptr<td_api::InputFile> &thumbnail_input_file,
|
|
|
|
DialogId owner_dialog_id, bool is_encrypted) {
|
|
|
|
if (thumbnail_input_file == nullptr) {
|
2021-06-26 03:20:59 +02:00
|
|
|
return Status::Error(400, "inputThumbnail not specified");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (thumbnail_input_file->get_id()) {
|
|
|
|
case td_api::inputFileLocal::ID: {
|
|
|
|
const string &path = static_cast<const td_api::inputFileLocal *>(thumbnail_input_file.get())->path_;
|
|
|
|
return register_local(
|
|
|
|
FullLocalFileLocation(is_encrypted ? FileType::EncryptedThumbnail : FileType::Thumbnail, path, 0),
|
|
|
|
owner_dialog_id, 0, false);
|
|
|
|
}
|
|
|
|
case td_api::inputFileId::ID:
|
2021-06-26 03:20:59 +02:00
|
|
|
return Status::Error(400, "InputFileId is not supported for thumbnails");
|
2018-12-31 20:04:05 +01:00
|
|
|
case td_api::inputFileRemote::ID:
|
2021-06-26 03:20:59 +02:00
|
|
|
return Status::Error(400, "InputFileRemote is not supported for thumbnails");
|
2018-12-31 20:04:05 +01:00
|
|
|
case td_api::inputFileGenerated::ID: {
|
|
|
|
auto *generated_thumbnail = static_cast<const td_api::inputFileGenerated *>(thumbnail_input_file.get());
|
|
|
|
return register_generate(is_encrypted ? FileType::EncryptedThumbnail : FileType::Thumbnail,
|
2018-01-26 13:56:19 +01:00
|
|
|
FileLocationSource::FromUser, generated_thumbnail->original_path_,
|
|
|
|
generated_thumbnail->conversion_, owner_dialog_id, generated_thumbnail->expected_size_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return Status::Error(500, "Unreachable");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<FileId> FileManager::get_input_file_id(FileType type, const tl_object_ptr<td_api::InputFile> &file,
|
|
|
|
DialogId owner_dialog_id, bool allow_zero, bool is_encrypted,
|
2022-09-15 15:08:19 +02:00
|
|
|
bool get_by_hash, bool is_secure, bool force_reuse) {
|
2020-03-20 14:36:07 +01:00
|
|
|
if (file == nullptr) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (allow_zero) {
|
|
|
|
return FileId();
|
|
|
|
}
|
2021-06-26 03:20:59 +02:00
|
|
|
return Status::Error(400, "InputFile is not specified");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-04-11 16:21:24 +02:00
|
|
|
if (is_encrypted || is_secure) {
|
|
|
|
get_by_hash = false;
|
|
|
|
}
|
|
|
|
|
2022-04-25 18:00:40 +02:00
|
|
|
auto new_type = is_encrypted ? FileType::Encrypted : (is_secure ? FileType::SecureEncrypted : type);
|
2018-04-03 19:49:07 +02:00
|
|
|
|
2018-01-05 15:24:41 +01:00
|
|
|
auto r_file_id = [&]() -> Result<FileId> {
|
|
|
|
switch (file->get_id()) {
|
|
|
|
case td_api::inputFileLocal::ID: {
|
|
|
|
const string &path = static_cast<const td_api::inputFileLocal *>(file.get())->path_;
|
|
|
|
if (allow_zero && path.empty()) {
|
|
|
|
return FileId();
|
|
|
|
}
|
2020-04-23 23:50:14 +02:00
|
|
|
string hash;
|
2022-12-15 20:41:55 +01:00
|
|
|
if (G()->get_option_boolean("reuse_uploaded_photos_by_hash") &&
|
|
|
|
get_main_file_type(new_type) == FileType::Photo) {
|
2020-04-23 23:50:14 +02:00
|
|
|
auto r_stat = stat(path);
|
2021-07-21 02:26:47 +02:00
|
|
|
if (r_stat.is_ok() && r_stat.ok().size_ > 0 && r_stat.ok().size_ < 11000000) {
|
2020-04-23 23:50:14 +02:00
|
|
|
auto r_file_content = read_file_str(path, r_stat.ok().size_);
|
|
|
|
if (r_file_content.is_ok()) {
|
|
|
|
hash = sha256(r_file_content.ok());
|
2022-08-04 13:48:10 +02:00
|
|
|
auto file_id = file_hash_to_file_id_.get(hash);
|
2022-11-14 16:30:10 +01:00
|
|
|
LOG(INFO) << "Found file " << file_id << " by hash " << hex_encode(hash);
|
2022-08-04 13:48:10 +02:00
|
|
|
if (file_id.is_valid()) {
|
2024-05-17 17:25:46 +02:00
|
|
|
auto file_node = get_file_node(file_id);
|
|
|
|
auto file_view = FileView(file_node);
|
2022-09-15 15:08:19 +02:00
|
|
|
if (!file_view.empty()) {
|
|
|
|
if (force_reuse) {
|
|
|
|
return file_id;
|
|
|
|
}
|
|
|
|
if (file_view.has_remote_location() && !file_view.remote_location().is_web()) {
|
|
|
|
return file_id;
|
|
|
|
}
|
2022-11-14 16:30:10 +01:00
|
|
|
if (file_view.is_uploading()) {
|
2024-05-17 17:25:46 +02:00
|
|
|
CHECK(file_node);
|
|
|
|
LOG(DEBUG) << "File " << file_id << " is still uploading: " << file_node->upload_priority_ << ' '
|
|
|
|
<< file_node->generate_upload_priority_ << ' ' << file_node->upload_pause_;
|
2022-11-14 16:30:10 +01:00
|
|
|
hash.clear();
|
|
|
|
}
|
2024-05-17 17:25:46 +02:00
|
|
|
} else {
|
|
|
|
LOG(DEBUG) << "File " << file_id << " isn't found";
|
2020-05-04 15:01:35 +02:00
|
|
|
}
|
2020-04-23 23:50:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TRY_RESULT(file_id, register_local(FullLocalFileLocation(new_type, path, 0), owner_dialog_id, 0, get_by_hash));
|
|
|
|
if (!hash.empty()) {
|
2022-08-04 13:48:10 +02:00
|
|
|
file_hash_to_file_id_.set(hash, file_id);
|
2020-04-23 23:50:14 +02:00
|
|
|
}
|
|
|
|
return file_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-05 15:24:41 +01:00
|
|
|
case td_api::inputFileId::ID: {
|
2018-03-06 19:31:20 +01:00
|
|
|
FileId file_id(static_cast<const td_api::inputFileId *>(file.get())->id_, 0);
|
2018-01-05 15:24:41 +01:00
|
|
|
if (!file_id.is_valid()) {
|
|
|
|
return FileId();
|
|
|
|
}
|
|
|
|
return file_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-05 15:24:41 +01:00
|
|
|
case td_api::inputFileRemote::ID: {
|
|
|
|
const string &file_persistent_id = static_cast<const td_api::inputFileRemote *>(file.get())->id_;
|
|
|
|
if (allow_zero && file_persistent_id.empty()) {
|
|
|
|
return FileId();
|
|
|
|
}
|
|
|
|
return from_persistent_id(file_persistent_id, type);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-05 15:24:41 +01:00
|
|
|
case td_api::inputFileGenerated::ID: {
|
|
|
|
auto *generated_file = static_cast<const td_api::inputFileGenerated *>(file.get());
|
2018-04-03 19:49:07 +02:00
|
|
|
return register_generate(new_type, FileLocationSource::FromUser, generated_file->original_path_,
|
|
|
|
generated_file->conversion_, owner_dialog_id, generated_file->expected_size_);
|
2018-01-05 15:24:41 +01:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2023-02-09 16:31:02 +01:00
|
|
|
return FileId();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-05 15:24:41 +01:00
|
|
|
}();
|
|
|
|
|
2018-04-03 19:49:07 +02:00
|
|
|
return check_input_file_id(type, std::move(r_file_id), is_encrypted, allow_zero, is_secure);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-08-13 22:18:27 +02:00
|
|
|
Result<FileId> FileManager::get_map_thumbnail_file_id(Location location, int32 zoom, int32 width, int32 height,
|
|
|
|
int32 scale, DialogId owner_dialog_id) {
|
|
|
|
if (!location.is_valid_map_point()) {
|
2021-06-26 03:20:59 +02:00
|
|
|
return Status::Error(400, "Invalid location specified");
|
2018-08-13 22:18:27 +02:00
|
|
|
}
|
|
|
|
if (zoom < 13 || zoom > 20) {
|
2021-06-26 03:20:59 +02:00
|
|
|
return Status::Error(400, "Wrong zoom");
|
2018-08-13 22:18:27 +02:00
|
|
|
}
|
|
|
|
if (width < 16 || width > 1024) {
|
2021-06-26 03:20:59 +02:00
|
|
|
return Status::Error(400, "Wrong width");
|
2018-08-13 22:18:27 +02:00
|
|
|
}
|
|
|
|
if (height < 16 || height > 1024) {
|
2021-06-26 03:20:59 +02:00
|
|
|
return Status::Error(400, "Wrong height");
|
2018-08-13 22:18:27 +02:00
|
|
|
}
|
|
|
|
if (scale < 1 || scale > 3) {
|
2021-06-26 03:20:59 +02:00
|
|
|
return Status::Error(400, "Wrong scale");
|
2018-08-13 22:18:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const double PI = 3.14159265358979323846;
|
|
|
|
double sin_latitude = std::sin(location.get_latitude() * PI / 180);
|
|
|
|
int32 size = 256 * (1 << zoom);
|
2021-10-19 17:11:16 +02:00
|
|
|
auto x = static_cast<int32>((location.get_longitude() + 180) / 360 * size);
|
|
|
|
auto y = static_cast<int32>((0.5 - std::log((1 + sin_latitude) / (1 - sin_latitude)) / (4 * PI)) * size);
|
2018-08-13 22:18:27 +02:00
|
|
|
x = clamp(x, 0, size - 1); // just in case
|
|
|
|
y = clamp(y, 0, size - 1); // just in case
|
|
|
|
|
2021-03-10 20:50:14 +01:00
|
|
|
string conversion = PSTRING() << "#map#" << zoom << '#' << x << '#' << y << '#' << width << '#' << height << '#'
|
|
|
|
<< scale << '#';
|
2018-12-17 21:59:24 +01:00
|
|
|
return register_generate(
|
|
|
|
owner_dialog_id.get_type() == DialogType::SecretChat ? FileType::EncryptedThumbnail : FileType::Thumbnail,
|
|
|
|
FileLocationSource::FromUser, string(), std::move(conversion), owner_dialog_id, 0);
|
2018-08-13 22:18:27 +02:00
|
|
|
}
|
|
|
|
|
2022-08-05 21:43:53 +02:00
|
|
|
Result<FileId> FileManager::get_audio_thumbnail_file_id(string title, string performer, bool is_small,
|
|
|
|
DialogId owner_dialog_id) {
|
2022-07-28 15:47:19 +02:00
|
|
|
if (!clean_input_string(title)) {
|
|
|
|
return Status::Error(400, "Title must be encoded in UTF-8");
|
|
|
|
}
|
|
|
|
if (!clean_input_string(performer)) {
|
|
|
|
return Status::Error(400, "Performer must be encoded in UTF-8");
|
|
|
|
}
|
|
|
|
for (auto &c : title) {
|
|
|
|
if (c == '\n' || c == '#') {
|
|
|
|
c = ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto &c : performer) {
|
|
|
|
if (c == '\n' || c == '#') {
|
|
|
|
c = ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
title = trim(title);
|
|
|
|
performer = trim(performer);
|
|
|
|
if (title.empty() && performer.empty()) {
|
|
|
|
return Status::Error(400, "Title or performer must be non-empty");
|
|
|
|
}
|
|
|
|
|
2022-08-05 21:43:53 +02:00
|
|
|
string conversion = PSTRING() << "#audio_t#" << title << '#' << performer << '#' << (is_small ? '1' : '0') << '#';
|
2022-07-28 15:47:19 +02:00
|
|
|
return register_generate(
|
|
|
|
owner_dialog_id.get_type() == DialogType::SecretChat ? FileType::EncryptedThumbnail : FileType::Thumbnail,
|
|
|
|
FileLocationSource::FromUser, string(), std::move(conversion), owner_dialog_id, 0);
|
|
|
|
}
|
|
|
|
|
2021-01-22 13:17:56 +01:00
|
|
|
FileType FileManager::guess_file_type(const tl_object_ptr<td_api::InputFile> &file) {
|
|
|
|
if (file == nullptr) {
|
|
|
|
return FileType::Temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (file->get_id()) {
|
|
|
|
case td_api::inputFileLocal::ID:
|
|
|
|
return guess_file_type_by_path(static_cast<const td_api::inputFileLocal *>(file.get())->path_);
|
|
|
|
case td_api::inputFileId::ID: {
|
|
|
|
FileId file_id(static_cast<const td_api::inputFileId *>(file.get())->id_, 0);
|
|
|
|
auto file_view = get_file_view(file_id);
|
|
|
|
if (file_view.empty()) {
|
|
|
|
return FileType::Temp;
|
|
|
|
}
|
|
|
|
return file_view.get_type();
|
|
|
|
}
|
|
|
|
case td_api::inputFileRemote::ID: {
|
|
|
|
const string &file_persistent_id = static_cast<const td_api::inputFileRemote *>(file.get())->id_;
|
|
|
|
Result<FileId> r_file_id = from_persistent_id(file_persistent_id, FileType::Temp);
|
|
|
|
if (r_file_id.is_error()) {
|
|
|
|
return FileType::Temp;
|
|
|
|
}
|
|
|
|
auto file_view = get_file_view(r_file_id.ok());
|
|
|
|
if (file_view.empty()) {
|
|
|
|
return FileType::Temp;
|
|
|
|
}
|
|
|
|
return file_view.get_type();
|
|
|
|
}
|
|
|
|
case td_api::inputFileGenerated::ID:
|
|
|
|
return guess_file_type_by_path(static_cast<const td_api::inputFileGenerated *>(file.get())->original_path_);
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return FileType::Temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
vector<tl_object_ptr<telegram_api::InputDocument>> FileManager::get_input_documents(const vector<FileId> &file_ids) {
|
|
|
|
vector<tl_object_ptr<telegram_api::InputDocument>> result;
|
|
|
|
result.reserve(file_ids.size());
|
|
|
|
for (auto file_id : file_ids) {
|
|
|
|
auto file_view = get_file_view(file_id);
|
|
|
|
CHECK(!file_view.empty());
|
|
|
|
CHECK(file_view.has_remote_location());
|
|
|
|
CHECK(!file_view.remote_location().is_web());
|
|
|
|
result.push_back(file_view.remote_location().as_input_document());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-06-24 17:57:32 +02:00
|
|
|
bool FileManager::extract_was_uploaded(const telegram_api::object_ptr<telegram_api::InputMedia> &input_media) {
|
2019-01-25 02:38:11 +01:00
|
|
|
if (input_media == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto input_media_id = input_media->get_id();
|
2024-06-26 13:55:57 +02:00
|
|
|
if (input_media_id == telegram_api::inputMediaPaidMedia::ID) {
|
|
|
|
auto &extended_media = static_cast<const telegram_api::inputMediaPaidMedia *>(input_media.get())->extended_media_;
|
|
|
|
if (extended_media.size() > 1u) {
|
|
|
|
for (auto &media : extended_media) {
|
|
|
|
CHECK(!extract_was_uploaded(media));
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
CHECK(extended_media.size() == 1u);
|
|
|
|
return extract_was_uploaded(extended_media[0]);
|
|
|
|
}
|
2019-01-25 02:38:11 +01:00
|
|
|
return input_media_id == telegram_api::inputMediaUploadedPhoto::ID ||
|
|
|
|
input_media_id == telegram_api::inputMediaUploadedDocument::ID;
|
|
|
|
}
|
|
|
|
|
2024-06-24 17:57:32 +02:00
|
|
|
bool FileManager::extract_was_thumbnail_uploaded(
|
|
|
|
const telegram_api::object_ptr<telegram_api::InputMedia> &input_media) {
|
2024-06-26 13:55:57 +02:00
|
|
|
if (input_media == nullptr) {
|
2019-01-25 02:38:11 +01:00
|
|
|
return false;
|
|
|
|
}
|
2024-06-26 13:55:57 +02:00
|
|
|
switch (input_media->get_id()) {
|
2024-06-30 08:29:53 +02:00
|
|
|
case telegram_api::inputMediaUploadedDocument::ID:
|
2024-06-26 13:55:57 +02:00
|
|
|
return static_cast<const telegram_api::inputMediaUploadedDocument *>(input_media.get())->thumb_ != nullptr;
|
|
|
|
case telegram_api::inputMediaPaidMedia::ID: {
|
|
|
|
auto &extended_media = static_cast<const telegram_api::inputMediaPaidMedia *>(input_media.get())->extended_media_;
|
|
|
|
if (extended_media.size() > 1u) {
|
|
|
|
for (auto &media : extended_media) {
|
|
|
|
CHECK(!extract_was_thumbnail_uploaded(media));
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
CHECK(extended_media.size() == 1u);
|
|
|
|
return extract_was_thumbnail_uploaded(extended_media[0]);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-25 02:38:11 +01:00
|
|
|
}
|
|
|
|
|
2024-06-24 17:57:32 +02:00
|
|
|
string FileManager::extract_file_reference(const telegram_api::object_ptr<telegram_api::InputMedia> &input_media) {
|
2019-01-29 00:32:26 +01:00
|
|
|
if (input_media == nullptr) {
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (input_media->get_id()) {
|
|
|
|
case telegram_api::inputMediaDocument::ID:
|
|
|
|
return extract_file_reference(static_cast<const telegram_api::inputMediaDocument *>(input_media.get())->id_);
|
|
|
|
case telegram_api::inputMediaPhoto::ID:
|
|
|
|
return extract_file_reference(static_cast<const telegram_api::inputMediaPhoto *>(input_media.get())->id_);
|
2024-06-24 17:57:32 +02:00
|
|
|
case telegram_api::inputMediaPaidMedia::ID:
|
|
|
|
UNREACHABLE();
|
2024-06-26 13:55:57 +02:00
|
|
|
return string();
|
2019-01-29 00:32:26 +01:00
|
|
|
default:
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-24 17:57:32 +02:00
|
|
|
vector<string> FileManager::extract_file_references(
|
|
|
|
const telegram_api::object_ptr<telegram_api::InputMedia> &input_media) {
|
|
|
|
if (input_media == nullptr) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
switch (input_media->get_id()) {
|
|
|
|
case telegram_api::inputMediaDocument::ID:
|
|
|
|
case telegram_api::inputMediaPhoto::ID:
|
|
|
|
return {extract_file_reference(input_media)};
|
|
|
|
case telegram_api::inputMediaPaidMedia::ID:
|
|
|
|
return transform(static_cast<const telegram_api::inputMediaPaidMedia *>(input_media.get())->extended_media_,
|
|
|
|
[](const telegram_api::object_ptr<telegram_api::InputMedia> &media) {
|
|
|
|
return extract_file_reference(media);
|
|
|
|
});
|
|
|
|
default:
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
string FileManager::extract_file_reference(
|
|
|
|
const telegram_api::object_ptr<telegram_api::InputDocument> &input_document) {
|
2019-01-29 00:32:26 +01:00
|
|
|
if (input_document == nullptr || input_document->get_id() != telegram_api::inputDocument::ID) {
|
|
|
|
return string();
|
2019-01-25 04:01:09 +01:00
|
|
|
}
|
2019-01-29 00:32:26 +01:00
|
|
|
|
|
|
|
return static_cast<const telegram_api::inputDocument *>(input_document.get())->file_reference_.as_slice().str();
|
|
|
|
}
|
|
|
|
|
2024-06-24 17:57:32 +02:00
|
|
|
string FileManager::extract_file_reference(const telegram_api::object_ptr<telegram_api::InputPhoto> &input_photo) {
|
2019-01-29 00:32:26 +01:00
|
|
|
if (input_photo == nullptr || input_photo->get_id() != telegram_api::inputPhoto::ID) {
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
|
|
|
|
return static_cast<const telegram_api::inputPhoto *>(input_photo.get())->file_reference_.as_slice().str();
|
|
|
|
}
|
|
|
|
|
2024-06-24 17:57:32 +02:00
|
|
|
bool FileManager::extract_was_uploaded(const telegram_api::object_ptr<telegram_api::InputChatPhoto> &input_chat_photo) {
|
2019-01-29 00:32:26 +01:00
|
|
|
return input_chat_photo != nullptr && input_chat_photo->get_id() == telegram_api::inputChatUploadedPhoto::ID;
|
|
|
|
}
|
|
|
|
|
2024-06-24 17:57:32 +02:00
|
|
|
string FileManager::extract_file_reference(
|
|
|
|
const telegram_api::object_ptr<telegram_api::InputChatPhoto> &input_chat_photo) {
|
2019-01-29 00:32:26 +01:00
|
|
|
if (input_chat_photo == nullptr || input_chat_photo->get_id() != telegram_api::inputChatPhoto::ID) {
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
|
|
|
|
return extract_file_reference(static_cast<const telegram_api::inputChatPhoto *>(input_chat_photo.get())->id_);
|
2019-01-25 04:01:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
FileId FileManager::next_file_id() {
|
|
|
|
if (!empty_file_ids_.empty()) {
|
|
|
|
auto res = empty_file_ids_.back();
|
|
|
|
empty_file_ids_.pop_back();
|
2018-03-07 00:37:39 +01:00
|
|
|
return FileId{res, 0};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-04-25 16:33:31 +02:00
|
|
|
CHECK(file_id_info_.size() <= static_cast<size_t>(std::numeric_limits<int32>::max()));
|
2018-03-06 19:31:20 +01:00
|
|
|
FileId res(static_cast<int32>(file_id_info_.size()), 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
file_id_info_.push_back({});
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileManager::FileNodeId FileManager::next_file_node_id() {
|
2022-04-25 16:33:31 +02:00
|
|
|
CHECK(file_nodes_.size() <= static_cast<size_t>(std::numeric_limits<FileNodeId>::max()));
|
2021-10-19 17:11:16 +02:00
|
|
|
auto res = static_cast<FileNodeId>(file_nodes_.size());
|
2018-12-31 20:04:05 +01:00
|
|
|
file_nodes_.emplace_back(nullptr);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2024-07-12 17:15:44 +02:00
|
|
|
void FileManager::on_start_download(FileDownloadManager::QueryId query_id) {
|
2018-05-24 12:43:50 +02:00
|
|
|
if (is_closed_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-09 12:57:11 +01:00
|
|
|
auto query = queries_container_.get(query_id);
|
|
|
|
CHECK(query != nullptr);
|
|
|
|
|
|
|
|
auto file_id = query->file_id_;
|
2018-01-30 21:04:22 +01:00
|
|
|
auto file_node = get_file_node(file_id);
|
2018-05-30 21:45:42 +02:00
|
|
|
LOG(DEBUG) << "Receive on_start_download for file " << file_id;
|
2018-01-30 21:04:22 +01:00
|
|
|
if (!file_node) {
|
2018-01-09 12:57:11 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (file_node->download_id_ != query_id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(DEBUG) << "Start to download part of file " << file_id;
|
|
|
|
file_node->is_download_started_ = true;
|
|
|
|
}
|
|
|
|
|
2024-07-12 17:15:44 +02:00
|
|
|
void FileManager::on_partial_download(FileDownloadManager::QueryId query_id, PartialLocalFileLocation partial_local,
|
2024-07-12 15:24:07 +02:00
|
|
|
int64 ready_size, int64 size) {
|
2018-05-24 12:43:50 +02:00
|
|
|
if (is_closed_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
auto query = queries_container_.get(query_id);
|
|
|
|
CHECK(query != nullptr);
|
|
|
|
|
|
|
|
auto file_id = query->file_id_;
|
2018-01-30 21:04:22 +01:00
|
|
|
auto file_node = get_file_node(file_id);
|
2018-12-27 18:46:28 +01:00
|
|
|
LOG(DEBUG) << "Receive on_partial_download for file " << file_id << " with " << partial_local
|
|
|
|
<< ", ready_size = " << ready_size << " and size = " << size;
|
2018-01-30 21:04:22 +01:00
|
|
|
if (!file_node) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (file_node->download_id_ != query_id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-26 22:42:26 +01:00
|
|
|
if (size != 0) {
|
2018-12-28 00:29:03 +01:00
|
|
|
FileView file_view(file_node);
|
|
|
|
if (!file_view.is_encrypted_secure()) {
|
|
|
|
file_node->set_size(size);
|
|
|
|
}
|
2018-12-26 22:42:26 +01:00
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
file_node->set_local_location(LocalFileLocation(std::move(partial_local)), ready_size, -1, -1 /* TODO */);
|
2018-12-27 18:23:19 +01:00
|
|
|
try_flush_node(file_node, "on_partial_download");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2024-07-12 17:15:44 +02:00
|
|
|
void FileManager::on_hash(FileUploadManager::QueryId query_id, string hash) {
|
2018-05-24 12:43:50 +02:00
|
|
|
if (is_closed_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-03 19:49:07 +02:00
|
|
|
auto query = queries_container_.get(query_id);
|
|
|
|
CHECK(query != nullptr);
|
|
|
|
|
|
|
|
auto file_id = query->file_id_;
|
|
|
|
|
|
|
|
auto file_node = get_file_node(file_id);
|
2018-05-30 21:45:42 +02:00
|
|
|
LOG(DEBUG) << "Receive on_hash for file " << file_id;
|
2018-04-03 19:49:07 +02:00
|
|
|
if (!file_node) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (file_node->upload_id_ != query_id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
file_node->encryption_key_.set_value_hash(secure_storage::ValueHash::create(hash).move_as_ok());
|
|
|
|
}
|
|
|
|
|
2024-07-12 16:27:26 +02:00
|
|
|
void FileManager::on_partial_upload(FileUploadManager::QueryId query_id, PartialRemoteFileLocation partial_remote,
|
2024-07-12 15:24:07 +02:00
|
|
|
int64 ready_size) {
|
2018-05-24 12:43:50 +02:00
|
|
|
if (is_closed_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
auto query = queries_container_.get(query_id);
|
|
|
|
CHECK(query != nullptr);
|
|
|
|
|
|
|
|
auto file_id = query->file_id_;
|
2018-01-30 21:04:22 +01:00
|
|
|
auto file_node = get_file_node(file_id);
|
2021-08-26 20:44:29 +02:00
|
|
|
LOG(DEBUG) << "Receive on_partial_upload for file " << file_id << " with " << partial_remote << " and ready size "
|
|
|
|
<< ready_size;
|
2018-01-30 21:04:22 +01:00
|
|
|
if (!file_node) {
|
2022-11-21 13:39:14 +01:00
|
|
|
LOG(ERROR) << "Can't find being uploaded file " << file_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (file_node->upload_id_ != query_id) {
|
2022-11-21 13:39:14 +01:00
|
|
|
LOG(DEBUG) << "Upload identifier of file " << file_id << " is " << file_node->upload_id_ << " instead of "
|
|
|
|
<< query_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
file_node->set_partial_remote_location(std::move(partial_remote), ready_size);
|
2018-12-27 18:23:19 +01:00
|
|
|
try_flush_node(file_node, "on_partial_upload");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-05-24 12:43:50 +02:00
|
|
|
|
2024-07-12 17:15:44 +02:00
|
|
|
void FileManager::on_download_ok(FileDownloadManager::QueryId query_id, FullLocalFileLocation local, int64 size,
|
2024-07-12 15:24:07 +02:00
|
|
|
bool is_new) {
|
2018-05-24 12:43:50 +02:00
|
|
|
if (is_closed_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-01 19:48:18 +02:00
|
|
|
Query query;
|
|
|
|
bool was_active;
|
2024-07-12 16:27:26 +02:00
|
|
|
std::tie(query, was_active) = finish_query(static_cast<Container<Query>::Id>(query_id));
|
2019-01-11 03:45:03 +01:00
|
|
|
auto file_id = query.file_id_;
|
2019-01-11 18:08:56 +01:00
|
|
|
LOG(INFO) << "ON DOWNLOAD OK of " << (is_new ? "new" : "checked") << " file " << file_id << " of size " << size;
|
2022-11-12 07:47:33 +01:00
|
|
|
auto r_new_file_id = register_local(std::move(local), DialogId(), size, false, false, true, file_id);
|
2020-06-01 19:48:18 +02:00
|
|
|
Status status = Status::OK();
|
2018-12-31 20:04:05 +01:00
|
|
|
if (r_new_file_id.is_error()) {
|
2020-06-01 20:35:06 +02:00
|
|
|
status = Status::Error(PSLICE() << "Can't register local file after download: " << r_new_file_id.error().message());
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2022-07-23 22:50:28 +02:00
|
|
|
if (is_new && context_->need_notify_on_new_files()) {
|
2020-02-19 15:57:30 +01:00
|
|
|
context_->on_new_file(size, get_file_view(r_new_file_id.ok()).get_allocated_local_size(), 1);
|
2019-01-11 18:08:56 +01:00
|
|
|
}
|
2020-06-01 19:48:18 +02:00
|
|
|
}
|
|
|
|
if (status.is_error()) {
|
|
|
|
LOG(ERROR) << status.message();
|
|
|
|
return on_error_impl(get_file_node(file_id), query.type_, was_active, std::move(status));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2018-05-24 12:43:50 +02:00
|
|
|
|
2024-07-12 16:27:26 +02:00
|
|
|
void FileManager::on_upload_ok(FileUploadManager::QueryId query_id, FileType file_type,
|
2024-07-12 15:24:07 +02:00
|
|
|
PartialRemoteFileLocation partial_remote, int64 size) {
|
2018-05-24 12:43:50 +02:00
|
|
|
if (is_closed_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(partial_remote.ready_part_count_ == partial_remote.part_count_);
|
2024-07-12 16:27:26 +02:00
|
|
|
auto some_file_id = finish_query(static_cast<Container<Query>::Id>(query_id)).first.file_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(INFO) << "ON UPLOAD OK file " << some_file_id << " of size " << size;
|
|
|
|
|
2018-01-30 21:04:22 +01:00
|
|
|
auto file_node = get_file_node(some_file_id);
|
|
|
|
if (!file_node) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileId file_id;
|
|
|
|
uint64 file_id_upload_order{std::numeric_limits<uint64>::max()};
|
|
|
|
for (auto id : file_node->file_ids_) {
|
|
|
|
auto *info = get_file_id_info(id);
|
|
|
|
if (info->upload_priority_ != 0 && info->upload_order_ < file_id_upload_order) {
|
|
|
|
file_id = id;
|
|
|
|
file_id_upload_order = info->upload_order_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!file_id.is_valid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto *file_info = get_file_id_info(file_id);
|
2019-01-10 22:12:51 +01:00
|
|
|
LOG(INFO) << "Found being uploaded file " << file_id << " with priority " << file_info->upload_priority_;
|
2018-12-31 20:04:05 +01:00
|
|
|
file_info->upload_priority_ = 0;
|
|
|
|
file_info->download_priority_ = 0;
|
|
|
|
|
|
|
|
FileView file_view(file_node);
|
2021-01-26 00:39:48 +01:00
|
|
|
string file_name = get_file_name(file_type, file_view.suggested_path());
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-03-27 15:11:15 +02:00
|
|
|
if (file_view.is_encrypted_secret()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
tl_object_ptr<telegram_api::InputEncryptedFile> input_file;
|
|
|
|
if (partial_remote.is_big_) {
|
|
|
|
input_file = make_tl_object<telegram_api::inputEncryptedFileBigUploaded>(
|
|
|
|
partial_remote.file_id_, partial_remote.part_count_, file_view.encryption_key().calc_fingerprint());
|
|
|
|
} else {
|
|
|
|
input_file = make_tl_object<telegram_api::inputEncryptedFileUploaded>(
|
|
|
|
partial_remote.file_id_, partial_remote.part_count_, "", file_view.encryption_key().calc_fingerprint());
|
|
|
|
}
|
|
|
|
if (file_info->upload_callback_) {
|
2019-01-10 22:12:51 +01:00
|
|
|
file_node->set_upload_pause(file_id);
|
2022-11-21 13:28:32 +01:00
|
|
|
file_info->upload_callback_->on_upload_encrypted_ok(file_id, std::move(input_file));
|
2018-12-31 20:04:05 +01:00
|
|
|
file_info->upload_callback_.reset();
|
|
|
|
}
|
2018-03-27 15:11:15 +02:00
|
|
|
} else if (file_view.is_secure()) {
|
|
|
|
tl_object_ptr<telegram_api::InputSecureFile> input_file;
|
|
|
|
input_file = make_tl_object<telegram_api::inputSecureFileUploaded>(
|
|
|
|
partial_remote.file_id_, partial_remote.part_count_, "" /*md5*/, BufferSlice() /*file_hash*/,
|
|
|
|
BufferSlice() /*encrypted_secret*/);
|
|
|
|
if (file_info->upload_callback_) {
|
2022-11-21 13:28:32 +01:00
|
|
|
file_node->set_upload_pause(file_id);
|
2018-03-27 15:11:15 +02:00
|
|
|
file_info->upload_callback_->on_upload_secure_ok(file_id, std::move(input_file));
|
|
|
|
file_info->upload_callback_.reset();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
tl_object_ptr<telegram_api::InputFile> input_file;
|
|
|
|
if (partial_remote.is_big_) {
|
|
|
|
input_file = make_tl_object<telegram_api::inputFileBig>(partial_remote.file_id_, partial_remote.part_count_,
|
|
|
|
std::move(file_name));
|
|
|
|
} else {
|
|
|
|
input_file = make_tl_object<telegram_api::inputFile>(partial_remote.file_id_, partial_remote.part_count_,
|
|
|
|
std::move(file_name), "");
|
|
|
|
}
|
|
|
|
if (file_info->upload_callback_) {
|
2019-01-10 22:12:51 +01:00
|
|
|
file_node->set_upload_pause(file_id);
|
2022-11-21 13:28:32 +01:00
|
|
|
file_info->upload_callback_->on_upload_ok(file_id, std::move(input_file));
|
2018-12-31 20:04:05 +01:00
|
|
|
file_info->upload_callback_.reset();
|
|
|
|
}
|
|
|
|
}
|
2022-11-21 13:28:32 +01:00
|
|
|
// don't flush node info, because nothing actually changed
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-11-12 07:47:33 +01:00
|
|
|
// for upload by hash
|
2024-07-12 16:27:26 +02:00
|
|
|
void FileManager::on_upload_full_ok(FileUploadManager::QueryId query_id, FullRemoteFileLocation remote) {
|
2018-05-24 12:43:50 +02:00
|
|
|
if (is_closed_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-12 16:27:26 +02:00
|
|
|
auto file_id = finish_query(static_cast<Container<Query>::Id>(query_id)).first.file_id_;
|
2018-05-30 19:50:11 +02:00
|
|
|
LOG(INFO) << "ON UPLOAD FULL OK for file " << file_id;
|
2021-10-19 17:11:16 +02:00
|
|
|
auto new_file_id = register_remote(std::move(remote), FileLocationSource::FromServer, DialogId(), 0, 0, "");
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG_STATUS(merge(new_file_id, file_id));
|
|
|
|
}
|
|
|
|
|
2024-07-12 17:15:44 +02:00
|
|
|
void FileManager::on_partial_generate(FileDownloadManager::QueryId query_id, PartialLocalFileLocation partial_local,
|
2024-07-12 15:24:07 +02:00
|
|
|
int64 expected_size) {
|
2018-05-24 12:43:50 +02:00
|
|
|
if (is_closed_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
auto query = queries_container_.get(query_id);
|
|
|
|
CHECK(query != nullptr);
|
|
|
|
|
|
|
|
auto file_id = query->file_id_;
|
2018-01-30 21:04:22 +01:00
|
|
|
auto file_node = get_file_node(file_id);
|
2018-12-27 17:50:45 +01:00
|
|
|
auto bitmask = Bitmask(Bitmask::Decode{}, partial_local.ready_bitmask_);
|
|
|
|
LOG(DEBUG) << "Receive on_partial_generate for file " << file_id << ": " << partial_local.path_ << " " << bitmask;
|
2018-01-30 21:04:22 +01:00
|
|
|
if (!file_node) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (file_node->generate_id_ != query_id) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-27 17:50:45 +01:00
|
|
|
auto ready_size = bitmask.get_total_size(partial_local.part_size_, file_node->size_);
|
2018-12-27 16:37:57 +01:00
|
|
|
file_node->set_local_location(LocalFileLocation(partial_local), ready_size, -1, -1 /* TODO */);
|
2018-12-31 20:04:05 +01:00
|
|
|
// TODO check for size and local_size, abort generation if needed
|
2018-08-16 19:31:42 +02:00
|
|
|
if (expected_size > 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
file_node->set_expected_size(expected_size);
|
|
|
|
}
|
|
|
|
if (!file_node->generate_was_update_) {
|
|
|
|
file_node->generate_was_update_ = true;
|
|
|
|
run_upload(file_node, {});
|
|
|
|
}
|
|
|
|
if (file_node->upload_id_ != 0) {
|
2024-07-12 16:27:26 +02:00
|
|
|
send_closure(file_upload_manager_, &FileUploadManager::update_local_file_location, file_node->upload_id_,
|
2021-10-19 17:11:16 +02:00
|
|
|
LocalFileLocation(std::move(partial_local)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-12-27 18:23:19 +01:00
|
|
|
try_flush_node(file_node, "on_partial_generate");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-05-24 12:43:50 +02:00
|
|
|
|
2024-07-12 17:15:44 +02:00
|
|
|
void FileManager::on_generate_ok(FileDownloadManager::QueryId query_id, FullLocalFileLocation local) {
|
2018-05-24 12:43:50 +02:00
|
|
|
if (is_closed_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Query query;
|
|
|
|
bool was_active;
|
2024-07-12 16:27:26 +02:00
|
|
|
std::tie(query, was_active) = finish_query(static_cast<Container<Query>::Id>(query_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
auto generate_file_id = query.file_id_;
|
|
|
|
|
2018-05-30 19:50:11 +02:00
|
|
|
LOG(INFO) << "Receive on_generate_ok for file " << generate_file_id << ": " << local;
|
2018-01-30 21:04:22 +01:00
|
|
|
auto file_node = get_file_node(generate_file_id);
|
|
|
|
if (!file_node) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto old_upload_id = file_node->upload_id_;
|
|
|
|
|
2022-11-12 07:47:33 +01:00
|
|
|
auto r_new_file_id = register_local(local, DialogId(), 0, false, false, false, generate_file_id);
|
2018-01-25 14:11:05 +01:00
|
|
|
file_node = get_file_node(generate_file_id);
|
2022-11-12 07:47:33 +01:00
|
|
|
if (r_new_file_id.is_error()) {
|
|
|
|
return on_error_impl(
|
|
|
|
file_node, query.type_, was_active,
|
|
|
|
Status::Error(PSLICE() << "Can't register local file after generate: " << r_new_file_id.error()));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-30 21:04:22 +01:00
|
|
|
CHECK(file_node);
|
2019-01-11 18:54:27 +01:00
|
|
|
|
|
|
|
FileView file_view(file_node);
|
2022-07-23 22:50:28 +02:00
|
|
|
if (context_->need_notify_on_new_files()) {
|
|
|
|
if (!file_view.has_generate_location() || !begins_with(file_view.generate_location().conversion_, "#file_id#")) {
|
|
|
|
context_->on_new_file(file_view.size(), file_view.get_allocated_local_size(), 1);
|
|
|
|
}
|
2019-01-11 18:54:27 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
run_upload(file_node, {});
|
|
|
|
|
|
|
|
if (was_active) {
|
|
|
|
if (old_upload_id != 0 && old_upload_id == file_node->upload_id_) {
|
2024-07-12 16:27:26 +02:00
|
|
|
send_closure(file_upload_manager_, &FileUploadManager::update_local_file_location, file_node->upload_id_,
|
2021-10-19 17:11:16 +02:00
|
|
|
LocalFileLocation(std::move(local)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-12 17:15:44 +02:00
|
|
|
void FileManager::on_download_error(FileDownloadManager::QueryId query_id, Status status) {
|
2024-07-12 16:27:26 +02:00
|
|
|
if (is_closed_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Query query;
|
|
|
|
bool was_active;
|
|
|
|
std::tie(query, was_active) = finish_query(static_cast<Container<Query>::Id>(query_id));
|
|
|
|
auto node = get_file_node(query.file_id_);
|
|
|
|
if (!node) {
|
|
|
|
LOG(ERROR) << "Can't find file node for " << query.file_id_ << " " << status;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
on_error_impl(node, query.type_, was_active, std::move(status));
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileManager::on_upload_error(FileUploadManager::QueryId query_id, Status status) {
|
2018-05-24 12:43:50 +02:00
|
|
|
if (is_closed_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Query query;
|
|
|
|
bool was_active;
|
2024-07-12 16:27:26 +02:00
|
|
|
std::tie(query, was_active) = finish_query(static_cast<Container<Query>::Id>(query_id));
|
2018-01-30 21:04:22 +01:00
|
|
|
auto node = get_file_node(query.file_id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!node) {
|
2018-02-27 09:48:47 +01:00
|
|
|
LOG(ERROR) << "Can't find file node for " << query.file_id_ << " " << status;
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-23 15:32:03 +01:00
|
|
|
if (query.type_ == Query::Type::UploadByHash && !G()->close_flag()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(INFO) << "Upload By Hash failed: " << status << ", restart upload";
|
|
|
|
node->get_by_hash_ = false;
|
|
|
|
run_upload(node, {});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
on_error_impl(node, query.type_, was_active, std::move(status));
|
|
|
|
}
|
|
|
|
|
2019-12-23 15:32:03 +01:00
|
|
|
void FileManager::on_error_impl(FileNodePtr node, Query::Type type, bool was_active, Status status) {
|
2018-12-31 20:04:05 +01:00
|
|
|
SCOPE_EXIT {
|
2022-11-21 13:39:14 +01:00
|
|
|
try_flush_node(node, "on_error_impl");
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2018-12-27 09:34:36 +01:00
|
|
|
if (status.message() == "FILE_PART_INVALID") {
|
2019-02-14 12:20:40 +01:00
|
|
|
bool has_partial_small_location = node->remote_.partial && !node->remote_.partial->is_big_;
|
2018-12-27 16:09:09 +01:00
|
|
|
FileView file_view(node);
|
|
|
|
auto expected_size = file_view.expected_size(true);
|
|
|
|
bool should_be_big_location = is_file_big(file_view.get_type(), expected_size);
|
2018-12-27 09:34:36 +01:00
|
|
|
|
2019-02-14 12:20:40 +01:00
|
|
|
node->delete_partial_remote_location();
|
2018-12-27 09:34:36 +01:00
|
|
|
if (has_partial_small_location && should_be_big_location) {
|
|
|
|
run_upload(node, {});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-16 12:07:31 +02:00
|
|
|
LOG(ERROR) << "Failed to upload file " << node->main_file_id_ << ": unexpected " << status
|
|
|
|
<< ", is_small = " << has_partial_small_location << ", should_be_big = " << should_be_big_location
|
|
|
|
<< ", expected size = " << expected_size;
|
2018-12-27 09:34:36 +01:00
|
|
|
}
|
|
|
|
|
2018-11-06 17:02:19 +01:00
|
|
|
if (begins_with(status.message(), "FILE_GENERATE_LOCATION_INVALID")) {
|
|
|
|
node->set_generate_location(nullptr);
|
|
|
|
}
|
2019-07-30 16:42:36 +02:00
|
|
|
|
2020-01-27 12:39:19 +01:00
|
|
|
if ((status.message() == "FILE_ID_INVALID" || status.message() == "LOCATION_INVALID") &&
|
|
|
|
FileView(node).may_reload_photo()) {
|
2019-07-30 16:42:36 +02:00
|
|
|
node->need_reload_photo_ = true;
|
2020-09-11 17:57:49 +02:00
|
|
|
run_download(node, true);
|
2019-07-30 16:42:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-23 17:00:56 +01:00
|
|
|
if (FileReferenceManager::is_file_reference_error(status)) {
|
2018-12-05 10:32:31 +01:00
|
|
|
string file_reference;
|
2019-01-23 17:00:56 +01:00
|
|
|
Slice prefix = "#BASE64";
|
2021-10-23 20:17:00 +02:00
|
|
|
Slice error_message = status.message();
|
|
|
|
auto pos = error_message.rfind('#');
|
|
|
|
if (pos < error_message.size() && begins_with(error_message.substr(pos), prefix)) {
|
|
|
|
auto r_file_reference = base64_decode(error_message.substr(pos + prefix.size()));
|
2019-01-05 16:13:27 +01:00
|
|
|
if (r_file_reference.is_ok()) {
|
|
|
|
file_reference = r_file_reference.move_as_ok();
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Can't decode file reference from error " << status << ": " << r_file_reference.error();
|
2018-12-05 10:32:31 +01:00
|
|
|
}
|
2018-12-08 23:00:27 +01:00
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Unexpected error, file_reference will be deleted just in case " << status;
|
2018-12-05 10:32:31 +01:00
|
|
|
}
|
|
|
|
CHECK(!node->file_ids_.empty());
|
|
|
|
delete_file_reference(node->file_ids_.back(), file_reference);
|
2020-09-11 17:57:49 +02:00
|
|
|
run_download(node, true);
|
2018-12-05 10:32:31 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-11-06 17:02:19 +01:00
|
|
|
|
2019-02-10 00:39:58 +01:00
|
|
|
if (begins_with(status.message(), "FILE_UPLOAD_RESTART")) {
|
2019-01-17 21:42:00 +01:00
|
|
|
if (ends_with(status.message(), "WITH_FILE_REFERENCE")) {
|
|
|
|
node->upload_was_update_file_reference_ = true;
|
|
|
|
}
|
2018-01-29 18:46:06 +01:00
|
|
|
run_upload(node, {});
|
|
|
|
return;
|
|
|
|
}
|
2022-06-16 12:07:31 +02:00
|
|
|
|
2018-12-05 10:32:31 +01:00
|
|
|
if (begins_with(status.message(), "FILE_DOWNLOAD_RESTART")) {
|
2019-01-17 21:42:00 +01:00
|
|
|
if (ends_with(status.message(), "WITH_FILE_REFERENCE")) {
|
|
|
|
node->download_was_update_file_reference_ = true;
|
2020-09-11 17:57:49 +02:00
|
|
|
run_download(node, true);
|
2019-08-19 11:31:25 +02:00
|
|
|
return;
|
|
|
|
} else if (ends_with(status.message(), "INCREASE_PART_SIZE")) {
|
2019-08-30 03:56:01 +02:00
|
|
|
if (try_fix_partial_local_location(node)) {
|
2020-09-11 17:57:49 +02:00
|
|
|
run_download(node, true);
|
2019-08-19 11:31:25 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-01-17 21:42:00 +01:00
|
|
|
} else {
|
2018-12-05 10:32:31 +01:00
|
|
|
node->can_search_locally_ = false;
|
2020-09-11 17:57:49 +02:00
|
|
|
run_download(node, true);
|
2019-08-19 11:31:25 +02:00
|
|
|
return;
|
2018-12-05 10:32:31 +01:00
|
|
|
}
|
2018-02-26 11:05:14 +01:00
|
|
|
}
|
2022-06-16 12:07:31 +02:00
|
|
|
|
2022-06-06 19:36:15 +02:00
|
|
|
if (status.message() == "MTPROTO_CLUSTER_INVALID") {
|
2023-05-09 00:10:14 +02:00
|
|
|
send_closure(G()->config_manager(), &ConfigManager::request_config, true);
|
2022-06-06 19:36:15 +02:00
|
|
|
run_download(node, true);
|
|
|
|
return;
|
|
|
|
}
|
2018-01-29 18:46:06 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!was_active) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-17 12:52:47 +02:00
|
|
|
if (G()->close_flag() && (status.code() < 400 || (status.code() == Global::request_aborted_error().code() &&
|
|
|
|
status.message() == Global::request_aborted_error().message()))) {
|
2022-09-20 01:02:28 +02:00
|
|
|
status = Global::request_aborted_error();
|
|
|
|
} else {
|
|
|
|
if (status.code() != -1) {
|
2022-11-22 12:15:37 +01:00
|
|
|
if (type == Query::Type::Generate && node->generate_ != nullptr) {
|
|
|
|
LOG(WARNING) << "Failed to generate file " << node->main_file_id_ << " with " << *node->generate_ << ": "
|
|
|
|
<< status;
|
|
|
|
} else {
|
|
|
|
LOG(WARNING) << "Failed to " << type << " file " << node->main_file_id_ << " of type "
|
|
|
|
<< FileView(node).get_type() << ": " << status;
|
|
|
|
}
|
2022-09-20 01:02:28 +02:00
|
|
|
}
|
2022-06-16 12:07:31 +02:00
|
|
|
if (status.code() == 0) {
|
|
|
|
// Remove partial locations
|
|
|
|
if (node->local_.type() == LocalFileLocation::Type::Partial &&
|
|
|
|
!begins_with(status.message(), "FILE_DOWNLOAD_ID_INVALID") &&
|
|
|
|
!begins_with(status.message(), "FILE_DOWNLOAD_LIMIT")) {
|
|
|
|
CSlice path = node->local_.partial().path_;
|
2022-10-02 21:26:03 +02:00
|
|
|
if (begins_with(path, get_files_temp_dir(FileType::SecureDecrypted)) ||
|
2022-06-16 12:07:31 +02:00
|
|
|
begins_with(path, get_files_temp_dir(FileType::Video))) {
|
|
|
|
LOG(INFO) << "Unlink file " << path;
|
2022-06-16 12:32:14 +02:00
|
|
|
send_closure(file_load_manager_, &FileLoadManager::unlink_file, std::move(node->local_.partial().path_),
|
|
|
|
Promise<Unit>());
|
2022-06-16 12:07:31 +02:00
|
|
|
node->drop_local_location();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node->delete_partial_remote_location();
|
|
|
|
}
|
2022-09-20 01:02:28 +02:00
|
|
|
status = Status::Error(400, status.message());
|
2022-06-16 12:07:31 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
// Stop everything on error
|
2019-01-25 02:52:38 +01:00
|
|
|
do_cancel_generate(node);
|
|
|
|
do_cancel_download(node);
|
|
|
|
do_cancel_upload(node);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-02-01 15:12:32 +01:00
|
|
|
for (auto file_id : vector<FileId>(node->file_ids_)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto *info = get_file_id_info(file_id);
|
|
|
|
if (info->download_priority_ != 0) {
|
|
|
|
info->download_priority_ = 0;
|
|
|
|
if (info->download_callback_) {
|
|
|
|
info->download_callback_->on_download_error(file_id, status.clone());
|
|
|
|
info->download_callback_.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (info->upload_priority_ != 0) {
|
|
|
|
info->upload_priority_ = 0;
|
|
|
|
if (info->upload_callback_) {
|
|
|
|
info->upload_callback_->on_upload_error(file_id, status.clone());
|
|
|
|
info->upload_callback_.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-12 16:27:26 +02:00
|
|
|
std::pair<FileManager::Query, bool> FileManager::finish_query(Container<Query>::Id query_id) {
|
2018-12-31 20:04:05 +01:00
|
|
|
SCOPE_EXIT {
|
|
|
|
queries_container_.erase(query_id);
|
|
|
|
};
|
|
|
|
auto query = queries_container_.get(query_id);
|
|
|
|
CHECK(query != nullptr);
|
|
|
|
|
|
|
|
auto res = *query;
|
2018-01-30 21:04:22 +01:00
|
|
|
auto node = get_file_node(res.file_id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!node) {
|
|
|
|
return std::make_pair(res, false);
|
|
|
|
}
|
|
|
|
bool was_active = false;
|
|
|
|
if (node->generate_id_ == query_id) {
|
|
|
|
node->generate_id_ = 0;
|
|
|
|
node->generate_was_update_ = false;
|
|
|
|
node->set_generate_priority(0, 0);
|
|
|
|
was_active = true;
|
|
|
|
}
|
|
|
|
if (node->download_id_ == query_id) {
|
|
|
|
node->download_id_ = 0;
|
2019-01-17 21:42:00 +01:00
|
|
|
node->download_was_update_file_reference_ = false;
|
2018-01-09 12:57:11 +01:00
|
|
|
node->is_download_started_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
node->set_download_priority(0);
|
|
|
|
was_active = true;
|
|
|
|
}
|
|
|
|
if (node->upload_id_ == query_id) {
|
|
|
|
node->upload_id_ = 0;
|
2019-01-17 21:42:00 +01:00
|
|
|
node->upload_was_update_file_reference_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
node->set_upload_priority(0);
|
|
|
|
was_active = true;
|
|
|
|
}
|
|
|
|
return std::make_pair(res, was_active);
|
|
|
|
}
|
2018-01-20 12:47:53 +01:00
|
|
|
|
2018-03-06 19:31:20 +01:00
|
|
|
FullRemoteFileLocation *FileManager::get_remote(int32 key) {
|
2023-05-10 14:29:38 +02:00
|
|
|
if (key == 0 || !context_->keep_exact_remote_location()) {
|
2018-03-06 19:31:20 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return &remote_location_info_.get(key).remote_;
|
|
|
|
}
|
|
|
|
|
2021-07-24 06:19:22 +02:00
|
|
|
Result<string> FileManager::get_suggested_file_name(FileId file_id, const string &directory) {
|
|
|
|
if (!file_id.is_valid()) {
|
|
|
|
return Status::Error(400, "Invalid file identifier");
|
|
|
|
}
|
|
|
|
auto node = get_sync_file_node(file_id);
|
|
|
|
if (!node) {
|
|
|
|
return Status::Error(400, "Wrong file identifier");
|
|
|
|
}
|
|
|
|
|
|
|
|
return ::td::get_suggested_file_name(directory, PathView(node->suggested_path()).file_name());
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void FileManager::hangup() {
|
|
|
|
file_db_.reset();
|
|
|
|
file_generate_manager_.reset();
|
2024-07-12 17:15:44 +02:00
|
|
|
file_download_manager_.reset();
|
2024-07-12 16:27:26 +02:00
|
|
|
file_upload_manager_.reset();
|
2018-05-24 00:30:47 +02:00
|
|
|
while (!queries_container_.empty()) {
|
2023-01-16 11:30:03 +01:00
|
|
|
auto query_ids = queries_container_.ids();
|
|
|
|
for (auto query_id : query_ids) {
|
2024-07-12 16:27:26 +02:00
|
|
|
Query query;
|
|
|
|
bool was_active;
|
|
|
|
std::tie(query, was_active) = finish_query(static_cast<Container<Query>::Id>(query_id));
|
|
|
|
auto node = get_file_node(query.file_id_);
|
|
|
|
if (node) {
|
|
|
|
on_error_impl(node, query.type_, was_active, Global::request_aborted_error());
|
|
|
|
}
|
2018-05-24 00:30:47 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-24 12:43:50 +02:00
|
|
|
is_closed_ = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
}
|
2018-01-20 12:47:53 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void FileManager::tear_down() {
|
|
|
|
parent_.reset();
|
2022-04-25 19:52:44 +02:00
|
|
|
|
|
|
|
LOG(DEBUG) << "Have " << file_id_info_.size() << " files with " << file_nodes_.size() << " file nodes, "
|
|
|
|
<< local_location_to_file_id_.size() << " local locations and " << remote_location_info_.size()
|
|
|
|
<< " remote locations to free";
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-03-01 16:01:50 +01:00
|
|
|
|
2022-03-01 15:34:51 +01:00
|
|
|
constexpr int64 FileManager::KEEP_DOWNLOAD_LIMIT;
|
|
|
|
constexpr int64 FileManager::KEEP_DOWNLOAD_OFFSET;
|
|
|
|
constexpr int64 FileManager::IGNORE_DOWNLOAD_LIMIT;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
} // namespace td
|