Make FileLocation.type_ private.

GitOrigin-RevId: 14841a320804eacbeef501df79c94b3bff2f4d0c
This commit is contained in:
levlam 2018-01-20 17:57:52 +03:00
parent 144b2b3aad
commit 1dffa9608d
9 changed files with 152 additions and 140 deletions

View File

@ -176,30 +176,30 @@ class FileDb : public FileDbInterface {
void clear_file_data(Id id, const FileData &file_data) override { void clear_file_data(Id id, const FileData &file_data) override {
string remote_key; string remote_key;
if (file_data.remote_.type_ == RemoteFileLocation::Type::Full) { if (file_data.remote_.type() == RemoteFileLocation::Type::Full) {
remote_key = as_key(file_data.remote_.full()); remote_key = as_key(file_data.remote_.full());
} }
string local_key; string local_key;
if (file_data.local_.type_ == LocalFileLocation::Type::Full) { if (file_data.local_.type() == LocalFileLocation::Type::Full) {
local_key = as_key(file_data.local_.full()); local_key = as_key(file_data.local_.full());
} }
string generate_key; string generate_key;
if (file_data.generate_.type_ == GenerateFileLocation::Type::Full) { if (file_data.generate_.type() == GenerateFileLocation::Type::Full) {
generate_key = as_key(file_data.generate_.full()); generate_key = as_key(file_data.generate_.full());
} }
send_closure(file_db_actor_, &FileDbActor::clear_file_data, id, remote_key, local_key, generate_key); send_closure(file_db_actor_, &FileDbActor::clear_file_data, id, remote_key, local_key, generate_key);
} }
void set_file_data(Id id, const FileData &file_data, bool new_remote, bool new_local, bool new_generate) override { void set_file_data(Id id, const FileData &file_data, bool new_remote, bool new_local, bool new_generate) override {
string remote_key; string remote_key;
if (file_data.remote_.type_ == RemoteFileLocation::Type::Full && new_remote) { if (file_data.remote_.type() == RemoteFileLocation::Type::Full && new_remote) {
remote_key = as_key(file_data.remote_.full()); remote_key = as_key(file_data.remote_.full());
} }
string local_key; string local_key;
if (file_data.local_.type_ == LocalFileLocation::Type::Full && new_local) { if (file_data.local_.type() == LocalFileLocation::Type::Full && new_local) {
local_key = as_key(file_data.local_.full()); local_key = as_key(file_data.local_.full());
} }
string generate_key; string generate_key;
if (file_data.generate_.type_ == GenerateFileLocation::Type::Full && new_generate) { if (file_data.generate_.type() == GenerateFileLocation::Type::Full && new_generate) {
generate_key = as_key(file_data.generate_.full()); generate_key = as_key(file_data.generate_.full());
} }
LOG(DEBUG) << "SAVE " << id << " -> " << file_data << " " LOG(DEBUG) << "SAVE " << id << " -> " << file_data << " "

View File

@ -45,12 +45,12 @@ Result<FileLoader::FileInfo> FileDownloader::init() {
SCOPE_EXIT { SCOPE_EXIT {
try_release_fd(); try_release_fd();
}; };
if (local_.type_ == LocalFileLocation::Type::Full) { if (local_.type() == LocalFileLocation::Type::Full) {
return Status::Error("File is already downloaded"); return Status::Error("File is already downloaded");
} }
int offset = 0; int offset = 0;
int32 part_size = 0; int32 part_size = 0;
if (local_.type_ == LocalFileLocation::Type::Partial) { if (local_.type() == LocalFileLocation::Type::Partial) {
const auto &partial = local_.partial(); const auto &partial = local_.partial();
path_ = partial.path_; path_ = partial.path_;
auto result_fd = FileFd::open(path_, FileFd::Write | FileFd::Read); auto result_fd = FileFd::open(path_, FileFd::Write | FileFd::Read);
@ -80,11 +80,11 @@ Result<FileLoader::FileInfo> FileDownloader::init() {
return res; return res;
} }
Status FileDownloader::on_ok(int64 size) { Status FileDownloader::on_ok(int64 size) {
auto dir = get_files_dir(remote_.type_); auto dir = get_files_dir(remote_.file_type_);
TRY_RESULT(perm_path, create_from_temp(path_, dir, name_)); TRY_RESULT(perm_path, create_from_temp(path_, dir, name_));
fd_.close(); fd_.close();
callback_->on_ok(FullLocalFileLocation(remote_.type_, std::move(perm_path), 0), size); callback_->on_ok(FullLocalFileLocation(remote_.file_type_, std::move(perm_path), 0), size);
return Status::OK(); return Status::OK();
} }
void FileDownloader::on_error(Status status) { void FileDownloader::on_error(Status status) {
@ -212,7 +212,7 @@ Result<std::pair<NetQueryPtr, bool>> FileDownloader::start_part(Part part, int32
cdn_part_reupload_token_.erase(it); cdn_part_reupload_token_.erase(it);
} }
} }
net_query->file_type_ = narrow_cast<int32>(remote_.type_); net_query->file_type_ = narrow_cast<int32>(remote_.file_type_);
return std::make_pair(std::move(net_query), false); return std::make_pair(std::move(net_query), false);
} }
@ -306,7 +306,7 @@ void FileDownloader::on_progress(int32 part_count, int32 part_size, int32 ready_
return; return;
} }
if (encryption_key_.empty()) { if (encryption_key_.empty()) {
callback_->on_partial_download(PartialLocalFileLocation{remote_.type_, path_, part_size, ready_part_count, ""}, callback_->on_partial_download(PartialLocalFileLocation{remote_.file_type_, path_, part_size, ready_part_count, ""},
ready_size); ready_size);
} else { } else {
UInt256 iv; UInt256 iv;
@ -315,9 +315,9 @@ void FileDownloader::on_progress(int32 part_count, int32 part_size, int32 ready_
} else { } else {
LOG(FATAL) << tag("ready_part_count", ready_part_count) << tag("next_part", next_part_); LOG(FATAL) << tag("ready_part_count", ready_part_count) << tag("next_part", next_part_);
} }
callback_->on_partial_download( callback_->on_partial_download(PartialLocalFileLocation{remote_.file_type_, path_, part_size, ready_part_count,
PartialLocalFileLocation{remote_.type_, path_, part_size, ready_part_count, Slice(iv.raw, sizeof(iv)).str()}, Slice(iv.raw, sizeof(iv)).str()},
ready_size); ready_size);
} }
} }
@ -417,7 +417,7 @@ void FileDownloader::try_release_fd() {
Status FileDownloader::acquire_fd() { Status FileDownloader::acquire_fd() {
if (fd_.empty()) { if (fd_.empty()) {
if (path_.empty()) { if (path_.empty()) {
TRY_RESULT(file_path, open_temp_file(remote_.type_)); TRY_RESULT(file_path, open_temp_file(remote_.file_type_));
std::tie(fd_, path_) = std::move(file_path); std::tie(fd_, path_) = std::move(file_path);
} else { } else {
TRY_RESULT(fd, FileFd::open(path_, FileFd::Write | FileFd::Read)); TRY_RESULT(fd, FileFd::open(path_, FileFd::Write | FileFd::Read));

View File

@ -87,7 +87,7 @@ class FileDownloadGenerateActor : public FileGenerateActor {
auto file_view = G()->td().get_actor_unsafe()->file_manager_->get_file_view(file_id); auto file_view = G()->td().get_actor_unsafe()->file_manager_->get_file_view(file_id);
if (file_view.has_local_location()) { if (file_view.has_local_location()) {
auto location = file_view.local_location(); auto location = file_view.local_location();
location.type_ = file_type; location.file_type_ = file_type;
callback->on_ok(location); callback->on_ok(location);
} else { } else {
LOG(ERROR) << "Expected to have local location"; LOG(ERROR) << "Expected to have local location";
@ -132,19 +132,19 @@ class FileExternalGenerateActor : public FileGenerateActor {
ActorShared<> parent_; ActorShared<> parent_;
void start_up() override { void start_up() override {
if (local_.type_ == LocalFileLocation::Type::Full) { if (local_.type() == LocalFileLocation::Type::Full) {
callback_->on_ok(local_.full()); callback_->on_ok(local_.full());
callback_.reset(); callback_.reset();
return stop(); return stop();
} }
if (local_.type_ == LocalFileLocation::Type::Partial) { if (local_.type() == LocalFileLocation::Type::Partial) {
const auto &partial = local_.partial(); const auto &partial = local_.partial();
path_ = partial.path_; path_ = partial.path_;
LOG(INFO) << "Unlink partially generated file at " << path_; LOG(INFO) << "Unlink partially generated file at " << path_;
unlink(path_).ignore(); unlink(path_).ignore();
} else { } else {
auto r_file_path = open_temp_file(generate_location_.type_); auto r_file_path = open_temp_file(generate_location_.file_type_);
if (r_file_path.is_error()) { if (r_file_path.is_error()) {
return check_status(r_file_path.move_as_error()); return check_status(r_file_path.move_as_error());
} }
@ -165,18 +165,18 @@ class FileExternalGenerateActor : public FileGenerateActor {
if (local_prefix_size < 0) { if (local_prefix_size < 0) {
return Status::Error(1, "Invalid local prefix size"); return Status::Error(1, "Invalid local prefix size");
} }
callback_->on_partial_generate(PartialLocalFileLocation{generate_location_.type_, path_, 1, local_prefix_size, ""}, callback_->on_partial_generate(
expected_size); PartialLocalFileLocation{generate_location_.file_type_, path_, 1, local_prefix_size, ""}, expected_size);
return Status::OK(); return Status::OK();
} }
Status do_file_generate_finish(Status status) { Status do_file_generate_finish(Status status) {
TRY_STATUS(std::move(status)); TRY_STATUS(std::move(status));
auto dir = get_files_dir(generate_location_.type_); auto dir = get_files_dir(generate_location_.file_type_);
TRY_RESULT(perm_path, create_from_temp(path_, dir, name_)); TRY_RESULT(perm_path, create_from_temp(path_, dir, name_));
callback_->on_ok(FullLocalFileLocation(generate_location_.type_, std::move(perm_path), 0)); callback_->on_ok(FullLocalFileLocation(generate_location_.file_type_, std::move(perm_path), 0));
callback_.reset(); callback_.reset();
stop(); stop();
return Status::OK(); return Status::OK();
@ -224,7 +224,7 @@ void FileGenerateManager::generate_file(uint64 query_id, const FullGenerateFileL
auto &query = it_flag.first->second; auto &query = it_flag.first->second;
if (conversion.copy().truncate(file_id_query.size()) == file_id_query) { if (conversion.copy().truncate(file_id_query.size()) == file_id_query) {
auto file_id = FileId(to_integer<int32>(conversion.substr(file_id_query.size()))); auto file_id = FileId(to_integer<int32>(conversion.substr(file_id_query.size())));
query.worker_ = create_actor<FileDownloadGenerateActor>("FileDownloadGenerateActor", generate_location.type_, query.worker_ = create_actor<FileDownloadGenerateActor>("FileDownloadGenerateActor", generate_location.file_type_,
file_id, std::move(callback), std::move(parent)); file_id, std::move(callback), std::move(parent));
} else { } else {
query.worker_ = create_actor<FileExternalGenerateActor>("FileExternalGenerationActor", query_id, generate_location, query.worker_ = create_actor<FileExternalGenerateActor>("FileExternalGenerationActor", query_id, generate_location,

View File

@ -394,7 +394,7 @@ inline StringBuilder &operator<<(StringBuilder &string_builder, const CommonRemo
class FullRemoteFileLocation { class FullRemoteFileLocation {
public: public:
FileType type_{FileType::None}; FileType file_type_{FileType::None};
private: private:
static constexpr int32 WEB_LOCATION_FLAG = 1 << 24; static constexpr int32 WEB_LOCATION_FLAG = 1 << 24;
@ -407,7 +407,7 @@ class FullRemoteFileLocation {
if (is_web()) { if (is_web()) {
return LocationType::Web; return LocationType::Web;
} }
switch (type_) { switch (file_type_) {
case FileType::Photo: case FileType::Photo:
case FileType::ProfilePhoto: case FileType::ProfilePhoto:
case FileType::Thumbnail: case FileType::Thumbnail:
@ -455,7 +455,7 @@ class FullRemoteFileLocation {
const FullRemoteFileLocation &full_remote_file_location); const FullRemoteFileLocation &full_remote_file_location);
int32 full_type() const { int32 full_type() const {
auto type = static_cast<int32>(type_); auto type = static_cast<int32>(file_type_);
if (is_web()) { if (is_web()) {
type |= WEB_LOCATION_FLAG; type |= WEB_LOCATION_FLAG;
} }
@ -483,7 +483,7 @@ class FullRemoteFileLocation {
if (raw_type < 0 || raw_type >= static_cast<int32>(FileType::Size)) { if (raw_type < 0 || raw_type >= static_cast<int32>(FileType::Size)) {
return parser.set_error("Invalid FileType in FullRemoteFileLocation"); return parser.set_error("Invalid FileType in FullRemoteFileLocation");
} }
type_ = static_cast<FileType>(raw_type); file_type_ = static_cast<FileType>(raw_type);
int32 dc_id_value; int32 dc_id_value;
parse(dc_id_value, parser); parse(dc_id_value, parser);
dc_id_ = DcId::from_value(dc_id_value); dc_id_ = DcId::from_value(dc_id_value);
@ -572,7 +572,7 @@ class FullRemoteFileLocation {
return location_type() == LocationType::Common; return location_type() == LocationType::Common;
} }
bool is_encrypted() const { bool is_encrypted() const {
return type_ == FileType::Encrypted; return file_type_ == FileType::Encrypted;
} }
tl_object_ptr<telegram_api::inputWebFileLocation> as_input_web_file_location() const { tl_object_ptr<telegram_api::inputWebFileLocation> as_input_web_file_location() const {
@ -617,17 +617,17 @@ class FullRemoteFileLocation {
FullRemoteFileLocation() = default; FullRemoteFileLocation() = default;
FullRemoteFileLocation(FileType file_type, int64 id, int64 access_hash, int32 local_id, int64 volume_id, int64 secret, FullRemoteFileLocation(FileType file_type, int64 id, int64 access_hash, int32 local_id, int64 volume_id, int64 secret,
DcId dc_id) DcId dc_id)
: type_(file_type) : file_type_(file_type)
, dc_id_(dc_id) , dc_id_(dc_id)
, variant_(PhotoRemoteFileLocation{id, access_hash, volume_id, secret, local_id}) { , variant_(PhotoRemoteFileLocation{id, access_hash, volume_id, secret, local_id}) {
CHECK(is_photo()); CHECK(is_photo());
} }
FullRemoteFileLocation(FileType file_type, int64 id, int64 access_hash, DcId dc_id) FullRemoteFileLocation(FileType file_type, int64 id, int64 access_hash, DcId dc_id)
: type_(file_type), dc_id_(dc_id), variant_(CommonRemoteFileLocation{id, access_hash}) { : file_type_(file_type), dc_id_(dc_id), variant_(CommonRemoteFileLocation{id, access_hash}) {
CHECK(is_common()); CHECK(is_common());
} }
FullRemoteFileLocation(FileType file_type, string url, int64 access_hash, DcId dc_id) FullRemoteFileLocation(FileType file_type, string url, int64 access_hash, DcId dc_id)
: type_(file_type) : file_type_(file_type)
, web_location_flag_{true} , web_location_flag_{true}
, dc_id_(dc_id) , dc_id_(dc_id)
, variant_(WebRemoteFileLocation{std::move(url), access_hash}) { , variant_(WebRemoteFileLocation{std::move(url), access_hash}) {
@ -681,7 +681,7 @@ class FullRemoteFileLocation {
inline StringBuilder &operator<<(StringBuilder &string_builder, inline StringBuilder &operator<<(StringBuilder &string_builder,
const FullRemoteFileLocation &full_remote_file_location) { const FullRemoteFileLocation &full_remote_file_location) {
string_builder << "[" << file_type_name[static_cast<int32>(full_remote_file_location.type_)] << ", " string_builder << "[" << file_type_name[static_cast<int32>(full_remote_file_location.file_type_)] << ", "
<< full_remote_file_location.get_dc_id() << ", location = "; << full_remote_file_location.get_dc_id() << ", location = ";
if (full_remote_file_location.is_web()) { if (full_remote_file_location.is_web()) {
@ -698,7 +698,10 @@ inline StringBuilder &operator<<(StringBuilder &string_builder,
class RemoteFileLocation { class RemoteFileLocation {
public: public:
enum class Type : int32 { Empty, Partial, Full }; enum class Type : int32 { Empty, Partial, Full };
Type type_;
Type type() const {
return type_;
}
template <class StorerT> template <class StorerT>
void store(StorerT &storer) const { void store(StorerT &storer) const {
@ -759,6 +762,7 @@ class RemoteFileLocation {
} }
private: private:
Type type_;
Variant<EmptyRemoteFileLocation, PartialRemoteFileLocation, FullRemoteFileLocation> variant_; Variant<EmptyRemoteFileLocation, PartialRemoteFileLocation, FullRemoteFileLocation> variant_;
friend bool operator==(const RemoteFileLocation &lhs, const RemoteFileLocation &rhs); friend bool operator==(const RemoteFileLocation &lhs, const RemoteFileLocation &rhs);
@ -790,7 +794,7 @@ inline bool operator!=(const EmptyLocalFileLocation &lhs, const EmptyLocalFileLo
} }
struct PartialLocalFileLocation { struct PartialLocalFileLocation {
FileType type_; FileType file_type_;
string path_; string path_;
int32 part_size_; int32 part_size_;
int32 ready_part_count_; int32 ready_part_count_;
@ -799,7 +803,7 @@ struct PartialLocalFileLocation {
template <class StorerT> template <class StorerT>
void store(StorerT &storer) const { void store(StorerT &storer) const {
using td::store; using td::store;
store(type_, storer); store(file_type_, storer);
store(path_, storer); store(path_, storer);
store(part_size_, storer); store(part_size_, storer);
store(ready_part_count_, storer); store(ready_part_count_, storer);
@ -808,8 +812,8 @@ struct PartialLocalFileLocation {
template <class ParserT> template <class ParserT>
void parse(ParserT &parser) { void parse(ParserT &parser) {
using td::parse; using td::parse;
parse(type_, parser); parse(file_type_, parser);
if (type_ < FileType::Thumbnail || type_ >= FileType::Size) { if (file_type_ < FileType::Thumbnail || file_type_ >= FileType::Size) {
return parser.set_error("Invalid type in PartialLocalFileLocation"); return parser.set_error("Invalid type in PartialLocalFileLocation");
} }
parse(path_, parser); parse(path_, parser);
@ -820,7 +824,7 @@ struct PartialLocalFileLocation {
}; };
inline bool operator==(const PartialLocalFileLocation &lhs, const PartialLocalFileLocation &rhs) { inline bool operator==(const PartialLocalFileLocation &lhs, const PartialLocalFileLocation &rhs) {
return lhs.type_ == rhs.type_ && lhs.path_ == rhs.path_ && lhs.part_size_ == rhs.part_size_ && return lhs.file_type_ == rhs.file_type_ && lhs.path_ == rhs.path_ && lhs.part_size_ == rhs.part_size_ &&
lhs.ready_part_count_ == rhs.ready_part_count_ && lhs.iv_ == rhs.iv_; lhs.ready_part_count_ == rhs.ready_part_count_ && lhs.iv_ == rhs.iv_;
} }
@ -829,22 +833,22 @@ inline bool operator!=(const PartialLocalFileLocation &lhs, const PartialLocalFi
} }
struct FullLocalFileLocation { struct FullLocalFileLocation {
FileType type_; FileType file_type_;
string path_; string path_;
uint64 mtime_nsec_; uint64 mtime_nsec_;
template <class StorerT> template <class StorerT>
void store(StorerT &storer) const { void store(StorerT &storer) const {
using td::store; using td::store;
store(type_, storer); store(file_type_, storer);
store(mtime_nsec_, storer); store(mtime_nsec_, storer);
store(path_, storer); store(path_, storer);
} }
template <class ParserT> template <class ParserT>
void parse(ParserT &parser) { void parse(ParserT &parser) {
using td::parse; using td::parse;
parse(type_, parser); parse(file_type_, parser);
if (type_ < FileType::Thumbnail || type_ >= FileType::Size) { if (file_type_ < FileType::Thumbnail || file_type_ >= FileType::Size) {
return parser.set_error("Invalid type in FullLocalFileLocation"); return parser.set_error("Invalid type in FullLocalFileLocation");
} }
parse(mtime_nsec_, parser); parse(mtime_nsec_, parser);
@ -855,21 +859,21 @@ struct FullLocalFileLocation {
} }
// TODO: remove this constructor // TODO: remove this constructor
FullLocalFileLocation() : type_(FileType::Photo) { FullLocalFileLocation() : file_type_(FileType::Photo) {
} }
FullLocalFileLocation(FileType file_type, string path, uint64 mtime_nsec) FullLocalFileLocation(FileType file_type, string path, uint64 mtime_nsec)
: type_(file_type), path_(std::move(path)), mtime_nsec_(mtime_nsec) { : file_type_(file_type), path_(std::move(path)), mtime_nsec_(mtime_nsec) {
} }
static const int32 KEY_MAGIC = 0x84373817; static const int32 KEY_MAGIC = 0x84373817;
}; };
inline bool operator<(const FullLocalFileLocation &lhs, const FullLocalFileLocation &rhs) { inline bool operator<(const FullLocalFileLocation &lhs, const FullLocalFileLocation &rhs) {
return std::tie(lhs.type_, lhs.mtime_nsec_, lhs.path_) < std::tie(rhs.type_, rhs.mtime_nsec_, rhs.path_); return std::tie(lhs.file_type_, lhs.mtime_nsec_, lhs.path_) < std::tie(rhs.file_type_, rhs.mtime_nsec_, rhs.path_);
} }
inline bool operator==(const FullLocalFileLocation &lhs, const FullLocalFileLocation &rhs) { inline bool operator==(const FullLocalFileLocation &lhs, const FullLocalFileLocation &rhs) {
return std::tie(lhs.type_, lhs.mtime_nsec_, lhs.path_) == std::tie(rhs.type_, rhs.mtime_nsec_, rhs.path_); return std::tie(lhs.file_type_, lhs.mtime_nsec_, lhs.path_) == std::tie(rhs.file_type_, rhs.mtime_nsec_, rhs.path_);
} }
inline bool operator!=(const FullLocalFileLocation &lhs, const FullLocalFileLocation &rhs) { inline bool operator!=(const FullLocalFileLocation &lhs, const FullLocalFileLocation &rhs) {
@ -883,7 +887,10 @@ inline StringBuilder &operator<<(StringBuilder &sb, const FullLocalFileLocation
class LocalFileLocation { class LocalFileLocation {
public: public:
enum class Type : int32 { Empty, Partial, Full }; enum class Type : int32 { Empty, Partial, Full };
Type type_;
Type type() const {
return type_;
}
PartialLocalFileLocation &partial() { PartialLocalFileLocation &partial() {
return variant_.get<1>(); return variant_.get<1>();
@ -936,6 +943,7 @@ class LocalFileLocation {
} }
private: private:
Type type_;
Variant<EmptyLocalFileLocation, PartialLocalFileLocation, FullLocalFileLocation> variant_; Variant<EmptyLocalFileLocation, PartialLocalFileLocation, FullLocalFileLocation> variant_;
friend bool operator==(const LocalFileLocation &lhs, const LocalFileLocation &rhs); friend bool operator==(const LocalFileLocation &lhs, const LocalFileLocation &rhs);
@ -950,7 +958,7 @@ inline bool operator!=(const LocalFileLocation &lhs, const LocalFileLocation &rh
} }
struct FullGenerateFileLocation { struct FullGenerateFileLocation {
FileType type_{FileType::None}; FileType file_type_{FileType::None};
string original_path_; string original_path_;
string conversion_; string conversion_;
static const int32 KEY_MAGIC = 0x8b60a1c8; static const int32 KEY_MAGIC = 0x8b60a1c8;
@ -958,14 +966,14 @@ struct FullGenerateFileLocation {
template <class StorerT> template <class StorerT>
void store(StorerT &storer) const { void store(StorerT &storer) const {
using td::store; using td::store;
store(type_, storer); store(file_type_, storer);
store(original_path_, storer); store(original_path_, storer);
store(conversion_, storer); store(conversion_, storer);
} }
template <class ParserT> template <class ParserT>
void parse(ParserT &parser) { void parse(ParserT &parser) {
using td::parse; using td::parse;
parse(type_, parser); parse(file_type_, parser);
parse(original_path_, parser); parse(original_path_, parser);
parse(conversion_, parser); parse(conversion_, parser);
} }
@ -974,19 +982,19 @@ struct FullGenerateFileLocation {
return *this; return *this;
} }
FullGenerateFileLocation() = default; FullGenerateFileLocation() = default;
FullGenerateFileLocation(FileType type, string original_path, string conversion) FullGenerateFileLocation(FileType file_type, string original_path, string conversion)
: type_(type), original_path_(std::move(original_path)), conversion_(std::move(conversion)) { : file_type_(file_type), original_path_(std::move(original_path)), conversion_(std::move(conversion)) {
} }
}; };
inline bool operator<(const FullGenerateFileLocation &lhs, const FullGenerateFileLocation &rhs) { inline bool operator<(const FullGenerateFileLocation &lhs, const FullGenerateFileLocation &rhs) {
return std::tie(lhs.type_, lhs.original_path_, lhs.conversion_) < return std::tie(lhs.file_type_, lhs.original_path_, lhs.conversion_) <
std::tie(rhs.type_, rhs.original_path_, rhs.conversion_); std::tie(rhs.file_type_, rhs.original_path_, rhs.conversion_);
} }
inline bool operator==(const FullGenerateFileLocation &lhs, const FullGenerateFileLocation &rhs) { inline bool operator==(const FullGenerateFileLocation &lhs, const FullGenerateFileLocation &rhs) {
return std::tie(lhs.type_, lhs.original_path_, lhs.conversion_) == return std::tie(lhs.file_type_, lhs.original_path_, lhs.conversion_) ==
std::tie(rhs.type_, rhs.original_path_, rhs.conversion_); std::tie(rhs.file_type_, rhs.original_path_, rhs.conversion_);
} }
inline bool operator!=(const FullGenerateFileLocation &lhs, const FullGenerateFileLocation &rhs) { inline bool operator!=(const FullGenerateFileLocation &lhs, const FullGenerateFileLocation &rhs) {
@ -996,7 +1004,7 @@ inline bool operator!=(const FullGenerateFileLocation &lhs, const FullGenerateFi
inline StringBuilder &operator<<(StringBuilder &string_builder, inline StringBuilder &operator<<(StringBuilder &string_builder,
const FullGenerateFileLocation &full_generated_file_location) { const FullGenerateFileLocation &full_generated_file_location) {
return string_builder << "[" return string_builder << "["
<< tag("file_type", file_type_name[static_cast<int32>(full_generated_file_location.type_)]) << tag("file_type", file_type_name[static_cast<int32>(full_generated_file_location.file_type_)])
<< tag("original_path", full_generated_file_location.original_path_) << tag("original_path", full_generated_file_location.original_path_)
<< tag("conversion", full_generated_file_location.conversion_) << "]"; << tag("conversion", full_generated_file_location.conversion_) << "]";
} }
@ -1004,7 +1012,10 @@ inline StringBuilder &operator<<(StringBuilder &string_builder,
class GenerateFileLocation { class GenerateFileLocation {
public: public:
enum class Type : int32 { Empty, Full }; enum class Type : int32 { Empty, Full };
Type type_;
Type type() const {
return type_;
}
FullGenerateFileLocation &full() { FullGenerateFileLocation &full() {
CHECK(type_ == Type::Full); CHECK(type_ == Type::Full);
@ -1049,14 +1060,15 @@ class GenerateFileLocation {
} }
private: private:
Type type_;
FullGenerateFileLocation full_; FullGenerateFileLocation full_;
}; };
inline bool operator==(const GenerateFileLocation &lhs, const GenerateFileLocation &rhs) { inline bool operator==(const GenerateFileLocation &lhs, const GenerateFileLocation &rhs) {
if (lhs.type_ != rhs.type_) { if (lhs.type() != rhs.type()) {
return false; return false;
} }
switch (lhs.type_) { switch (lhs.type()) {
case GenerateFileLocation::Type::Empty: case GenerateFileLocation::Type::Empty:
return true; return true;
case GenerateFileLocation::Type::Full: case GenerateFileLocation::Type::Full:
@ -1138,10 +1150,10 @@ class FileData {
}; };
inline StringBuilder &operator<<(StringBuilder &sb, const FileData &file_data) { inline StringBuilder &operator<<(StringBuilder &sb, const FileData &file_data) {
sb << "[" << tag("name", file_data.name_); sb << "[" << tag("name", file_data.name_);
if (file_data.local_.type_ == LocalFileLocation::Type::Full) { if (file_data.local_.type() == LocalFileLocation::Type::Full) {
sb << " local " << file_data.local_.full(); sb << " local " << file_data.local_.full();
} }
if (file_data.remote_.type_ == RemoteFileLocation::Type::Full) { if (file_data.remote_.type() == RemoteFileLocation::Type::Full) {
sb << " remote " << file_data.remote_.full(); sb << " remote " << file_data.remote_.full();
} }
return sb << "]"; return sb << "]";

View File

@ -57,7 +57,7 @@ void FileNode::set_remote_location(const RemoteFileLocation &remote, FileLocatio
on_info_changed(); on_info_changed();
} }
if (remote_ == remote) { if (remote_ == remote) {
if (remote_.type_ == RemoteFileLocation::Type::Full) { if (remote_.type() == RemoteFileLocation::Type::Full) {
if (remote_.full().get_access_hash() == remote.full().get_access_hash()) { if (remote_.full().get_access_hash() == remote.full().get_access_hash()) {
return; return;
} }
@ -178,22 +178,22 @@ bool FileNode::need_pmc_flush() const {
// We must save encryption key // We must save encryption key
if (!encryption_key_.empty()) { if (!encryption_key_.empty()) {
// && remote_.type_ != RemoteFileLocation::Type::Empty // && remote_.type() != RemoteFileLocation::Type::Empty
return true; return true;
} }
bool has_generate_location = generate_.type_ == GenerateFileLocation::Type::Full; bool has_generate_location = generate_.type() == GenerateFileLocation::Type::Full;
// Do not save "#file_id#" conversion. // Do not save "#file_id#" conversion.
if (has_generate_location && begins_with(generate_.full().conversion_, "#file_id#")) { if (has_generate_location && begins_with(generate_.full().conversion_, "#file_id#")) {
has_generate_location = false; has_generate_location = false;
} }
if (remote_.type_ == RemoteFileLocation::Type::Full && if (remote_.type() == RemoteFileLocation::Type::Full &&
(has_generate_location || local_.type_ != LocalFileLocation::Type::Empty)) { (has_generate_location || local_.type() != LocalFileLocation::Type::Empty)) {
return true; return true;
} }
if (local_.type_ == LocalFileLocation::Type::Full && if (local_.type() == LocalFileLocation::Type::Full &&
(has_generate_location || remote_.type_ != RemoteFileLocation::Type::Empty)) { (has_generate_location || remote_.type() != RemoteFileLocation::Type::Empty)) {
return true; return true;
} }
@ -211,21 +211,21 @@ void FileNode::on_info_flushed() {
/*** FileView ***/ /*** FileView ***/
bool FileView::has_local_location() const { bool FileView::has_local_location() const {
return node_->local_.type_ == LocalFileLocation::Type::Full; return node_->local_.type() == LocalFileLocation::Type::Full;
} }
const FullLocalFileLocation &FileView::local_location() const { const FullLocalFileLocation &FileView::local_location() const {
CHECK(has_local_location()); CHECK(has_local_location());
return node_->local_.full(); return node_->local_.full();
} }
bool FileView::has_remote_location() const { bool FileView::has_remote_location() const {
return node_->remote_.type_ == RemoteFileLocation::Type::Full; return node_->remote_.type() == RemoteFileLocation::Type::Full;
} }
const FullRemoteFileLocation &FileView::remote_location() const { const FullRemoteFileLocation &FileView::remote_location() const {
CHECK(has_remote_location()); CHECK(has_remote_location());
return node_->remote_.full(); return node_->remote_.full();
} }
bool FileView::has_generate_location() const { bool FileView::has_generate_location() const {
return node_->generate_.type_ == GenerateFileLocation::Type::Full; return node_->generate_.type() == GenerateFileLocation::Type::Full;
} }
const FullGenerateFileLocation &FileView::generate_location() const { const FullGenerateFileLocation &FileView::generate_location() const {
CHECK(has_generate_location()); CHECK(has_generate_location());
@ -248,7 +248,7 @@ bool FileView::is_downloading() const {
} }
int64 FileView::local_size() const { int64 FileView::local_size() const {
switch (node_->local_.type_) { switch (node_->local_.type()) {
case LocalFileLocation::Type::Full: case LocalFileLocation::Type::Full:
return node_->size_; return node_->size_;
case LocalFileLocation::Type::Partial: case LocalFileLocation::Type::Partial:
@ -258,7 +258,7 @@ int64 FileView::local_size() const {
} }
} }
int64 FileView::local_total_size() const { int64 FileView::local_total_size() const {
switch (node_->local_.type_) { switch (node_->local_.type()) {
case LocalFileLocation::Type::Empty: case LocalFileLocation::Type::Empty:
return 0; return 0;
case LocalFileLocation::Type::Full: case LocalFileLocation::Type::Full:
@ -278,7 +278,7 @@ bool FileView::is_uploading() const {
} }
int64 FileView::remote_size() const { int64 FileView::remote_size() const {
switch (node_->remote_.type_) { switch (node_->remote_.type()) {
case RemoteFileLocation::Type::Full: case RemoteFileLocation::Type::Full:
return node_->size_; return node_->size_;
case RemoteFileLocation::Type::Partial: { case RemoteFileLocation::Type::Partial: {
@ -296,7 +296,7 @@ int64 FileView::remote_size() const {
} }
string FileView::path() const { string FileView::path() const {
switch (node_->local_.type_) { switch (node_->local_.type()) {
case LocalFileLocation::Type::Full: case LocalFileLocation::Type::Full:
return node_->local_.full().path_; return node_->local_.full().path_;
case LocalFileLocation::Type::Partial: case LocalFileLocation::Type::Partial:
@ -337,7 +337,7 @@ bool FileView::can_download_from_server() const {
if (!has_remote_location()) { if (!has_remote_location()) {
return false; return false;
} }
if (remote_location().type_ == FileType::Encrypted && encryption_key().empty()) { if (remote_location().file_type_ == FileType::Encrypted && encryption_key().empty()) {
return false; return false;
} }
if (remote_location().get_dc_id().is_empty()) { if (remote_location().get_dc_id().is_empty()) {
@ -353,7 +353,7 @@ bool FileView::can_delete() const {
if (has_local_location()) { if (has_local_location()) {
return begins_with(local_location().path_, get_files_dir(get_type())); return begins_with(local_location().path_, get_files_dir(get_type()));
} }
return node_->local_.type_ == LocalFileLocation::Type::Partial; return node_->local_.type() == LocalFileLocation::Type::Partial;
} }
/*** FileManager ***/ /*** FileManager ***/
@ -493,7 +493,7 @@ Status FileManager::check_local_location(FullLocalFileLocation &location, int64
LOG(INFO) << "File was nodified: old mtime = " << location.mtime_nsec_ << ", new mtime = " << stat.mtime_nsec_; LOG(INFO) << "File was nodified: old mtime = " << location.mtime_nsec_ << ", new mtime = " << stat.mtime_nsec_;
return Status::Error("File was modified"); return Status::Error("File was modified");
} }
if ((location.type_ == FileType::Thumbnail || location.type_ == FileType::EncryptedThumbnail) && if ((location.file_type_ == FileType::Thumbnail || location.file_type_ == FileType::EncryptedThumbnail) &&
size >= MAX_THUMBNAIL_SIZE) { size >= MAX_THUMBNAIL_SIZE) {
return Status::Error(PSLICE() << "File is too big for thumbnail " << tag("size", format::as_size(size))); return Status::Error(PSLICE() << "File is too big for thumbnail " << tag("size", format::as_size(size)));
} }
@ -514,9 +514,9 @@ static Status check_partial_local_location(const PartialLocalFileLocation &locat
Status FileManager::check_local_location(FileNode *node) { Status FileManager::check_local_location(FileNode *node) {
Status status; Status status;
if (node->local_.type_ == LocalFileLocation::Type::Full) { if (node->local_.type() == LocalFileLocation::Type::Full) {
status = check_local_location(node->local_.full(), node->size_); status = check_local_location(node->local_.full(), node->size_);
} else if (node->local_.type_ == LocalFileLocation::Type::Partial) { } else if (node->local_.type() == LocalFileLocation::Type::Partial) {
status = check_partial_local_location(node->local_.partial()); status = check_partial_local_location(node->local_.partial());
} }
if (status.is_error()) { if (status.is_error()) {
@ -623,13 +623,13 @@ Result<FileId> FileManager::register_generate(FileType file_type, string origina
Result<FileId> FileManager::register_file(FileData data, FileLocationSource file_location_source, const char *source, Result<FileId> FileManager::register_file(FileData data, FileLocationSource file_location_source, const char *source,
bool force) { bool force) {
bool has_remote = data.remote_.type_ == RemoteFileLocation::Type::Full; bool has_remote = data.remote_.type() == RemoteFileLocation::Type::Full;
bool has_generate = data.generate_.type_ == GenerateFileLocation::Type::Full; bool has_generate = data.generate_.type() == GenerateFileLocation::Type::Full;
if (data.local_.type_ == LocalFileLocation::Type::Full && !force) { if (data.local_.type() == LocalFileLocation::Type::Full && !force) {
if (file_location_source == FileLocationSource::FromDb) { if (file_location_source == FileLocationSource::FromDb) {
PathView path_view(data.local_.full().path_); PathView path_view(data.local_.full().path_);
if (path_view.is_relative()) { if (path_view.is_relative()) {
data.local_.full().path_ = get_files_base_dir(data.local_.full().type_) + data.local_.full().path_; data.local_.full().path_ = get_files_base_dir(data.local_.full().file_type_) + data.local_.full().path_;
} }
} }
@ -643,7 +643,7 @@ Result<FileId> FileManager::register_file(FileData data, FileLocationSource file
} }
} }
} }
bool has_local = data.local_.type_ == LocalFileLocation::Type::Full; bool has_local = data.local_.type() == LocalFileLocation::Type::Full;
bool has_location = has_local || has_remote || has_generate; bool has_location = has_local || has_remote || has_generate;
if (!has_location) { if (!has_location) {
return Status::Error("No location"); return Status::Error("No location");
@ -711,8 +711,8 @@ Result<FileId> FileManager::register_file(FileData data, FileLocationSource file
// 1 -- choose y // 1 -- choose y
// 2 -- choose any // 2 -- choose any
static int merge_choose(const LocalFileLocation &x, const LocalFileLocation &y) { static int merge_choose(const LocalFileLocation &x, const LocalFileLocation &y) {
int32 x_type = static_cast<int32>(x.type_); int32 x_type = static_cast<int32>(x.type());
int32 y_type = static_cast<int32>(y.type_); int32 y_type = static_cast<int32>(y.type());
if (x_type != y_type) { if (x_type != y_type) {
return x_type < y_type; return x_type < y_type;
} }
@ -720,13 +720,13 @@ static int merge_choose(const LocalFileLocation &x, const LocalFileLocation &y)
} }
static int merge_choose(const RemoteFileLocation &x, int8 x_source, const RemoteFileLocation &y, int8 y_source) { static int merge_choose(const RemoteFileLocation &x, int8 x_source, const RemoteFileLocation &y, int8 y_source) {
int32 x_type = static_cast<int32>(x.type_); int32 x_type = static_cast<int32>(x.type());
int32 y_type = static_cast<int32>(y.type_); int32 y_type = static_cast<int32>(y.type());
if (x_type != y_type) { if (x_type != y_type) {
return x_type < y_type; return x_type < y_type;
} }
// If access_hash changed use a newer one // If access_hash changed use a newer one
if (x.type_ == RemoteFileLocation::Type::Full) { if (x.type() == RemoteFileLocation::Type::Full) {
if (x.full().get_access_hash() != y.full().get_access_hash()) { if (x.full().get_access_hash() != y.full().get_access_hash()) {
return x_source < y_source; return x_source < y_source;
} }
@ -734,8 +734,8 @@ static int merge_choose(const RemoteFileLocation &x, int8 x_source, const Remote
return 2; return 2;
} }
static int merge_choose(const GenerateFileLocation &x, const GenerateFileLocation &y) { static int merge_choose(const GenerateFileLocation &x, const GenerateFileLocation &y) {
int32 x_type = static_cast<int32>(x.type_); int32 x_type = static_cast<int32>(x.type());
int32 y_type = static_cast<int32>(y.type_); int32 y_type = static_cast<int32>(y.type());
if (x_type != y_type) { if (x_type != y_type) {
return x_type < y_type; return x_type < y_type;
} }
@ -856,8 +856,8 @@ Result<FileId> FileManager::merge(FileId x_file_id, FileId y_file_id, bool no_sy
y_node->upload_pause_ = FileId(); y_node->upload_pause_ = FileId();
} }
if (x_node->remote_.type_ == RemoteFileLocation::Type::Full && if (x_node->remote_.type() == RemoteFileLocation::Type::Full &&
y_node->remote_.type_ == RemoteFileLocation::Type::Full && y_node->remote_.type() == RemoteFileLocation::Type::Full &&
x_node->remote_.full().get_dc_id() != y_node->remote_.full().get_dc_id()) { x_node->remote_.full().get_dc_id() != y_node->remote_.full().get_dc_id()) {
LOG(ERROR) << "File remote location was changed from " << y_node->remote_.full() << " to " LOG(ERROR) << "File remote location was changed from " << y_node->remote_.full() << " to "
<< x_node->remote_.full(); << x_node->remote_.full();
@ -884,8 +884,8 @@ Result<FileId> FileManager::merge(FileId x_file_id, FileId y_file_id, bool no_sy
<< y_node->size_); << y_node->size_);
} }
if (encryption_key_i == -1) { if (encryption_key_i == -1) {
if (nodes[remote_i]->remote_.type_ == RemoteFileLocation::Type::Full && if (nodes[remote_i]->remote_.type() == RemoteFileLocation::Type::Full &&
nodes[local_i]->local_.type_ != LocalFileLocation::Type::Partial) { nodes[local_i]->local_.type() != LocalFileLocation::Type::Partial) {
//??? //???
LOG(ERROR) << "Different encryption key in files, but go Choose same key as remote location"; LOG(ERROR) << "Different encryption key in files, but go Choose same key as remote location";
encryption_key_i = remote_i; encryption_key_i = remote_i;
@ -1105,19 +1105,19 @@ void FileManager::flush_to_pmc(FileNode *node, bool new_remote, bool new_local,
FileData data; FileData data;
data.pmc_id_ = node->pmc_id_; data.pmc_id_ = node->pmc_id_;
data.local_ = node->local_; data.local_ = node->local_;
if (data.local_.type_ == LocalFileLocation::Type::Full) { if (data.local_.type() == LocalFileLocation::Type::Full) {
prepare_path_for_pmc(data.local_.full().type_, data.local_.full().path_); prepare_path_for_pmc(data.local_.full().file_type_, data.local_.full().path_);
} }
data.remote_ = node->remote_; data.remote_ = node->remote_;
data.generate_ = node->generate_; data.generate_ = node->generate_;
if (data.generate_.type_ == GenerateFileLocation::Type::Full && if (data.generate_.type() == GenerateFileLocation::Type::Full &&
begins_with(data.generate_.full().conversion_, "#file_id#")) { begins_with(data.generate_.full().conversion_, "#file_id#")) {
data.generate_ = GenerateFileLocation(); data.generate_ = GenerateFileLocation();
} }
// TODO: not needed when GenerateLocation has constant convertion // TODO: not needed when GenerateLocation has constant convertion
if (data.remote_.type_ != RemoteFileLocation::Type::Full && data.local_.type_ != LocalFileLocation::Type::Full) { if (data.remote_.type() != RemoteFileLocation::Type::Full && data.local_.type() != LocalFileLocation::Type::Full) {
data.local_ = LocalFileLocation(); data.local_ = LocalFileLocation();
data.remote_ = RemoteFileLocation(); data.remote_ = RemoteFileLocation();
} }
@ -1182,7 +1182,7 @@ bool FileManager::load_from_pmc(FileNode *node, bool new_remote, bool new_local,
new_local &= file_view.has_local_location(); new_local &= file_view.has_local_location();
if (new_local) { if (new_local) {
local = get_file_view(file_id).local_location(); local = get_file_view(file_id).local_location();
prepare_path_for_pmc(local.type_, local.path_); prepare_path_for_pmc(local.file_type_, local.path_);
} }
new_generate &= file_view.has_generate_location(); new_generate &= file_view.has_generate_location();
if (new_generate) { if (new_generate) {
@ -1230,7 +1230,7 @@ bool FileManager::set_content(FileId file_id, BufferSlice bytes) {
return false; return false;
} }
if (node->local_.type_ == LocalFileLocation::Type::Full) { if (node->local_.type() == LocalFileLocation::Type::Full) {
// There was no download so we don't need an update // There was no download so we don't need an update
return true; return true;
} }
@ -1249,7 +1249,7 @@ bool FileManager::set_content(FileId file_id, BufferSlice bytes) {
QueryId id = queries_container_.create(Query{file_id, Query::SetContent}); QueryId id = queries_container_.create(Query{file_id, Query::SetContent});
node->download_id_ = id; node->download_id_ = id;
node->is_download_started_ = true; node->is_download_started_ = true;
send_closure(file_load_manager_, &FileLoadManager::from_bytes, id, node->remote_.full().type_, std::move(bytes), send_closure(file_load_manager_, &FileLoadManager::from_bytes, id, node->remote_.full().file_type_, std::move(bytes),
node->name_); node->name_);
return true; return true;
} }
@ -1294,7 +1294,7 @@ void FileManager::delete_file(FileId file_id, Promise<Unit> promise, const char
if (file_view.get_type() == FileType::Encrypted) { if (file_view.get_type() == FileType::Encrypted) {
clear_from_pmc(node); clear_from_pmc(node);
} }
if (node->local_.type_ == LocalFileLocation::Type::Partial) { if (node->local_.type() == LocalFileLocation::Type::Partial) {
LOG(INFO) << "Unlink partial file " << file_id << " at " << node->local_.partial().path_; LOG(INFO) << "Unlink partial file " << file_id << " at " << node->local_.partial().path_;
unlink(node->local_.partial().path_).ignore(); unlink(node->local_.partial().path_).ignore();
node->set_local_location(LocalFileLocation(), 0); node->set_local_location(LocalFileLocation(), 0);
@ -1314,7 +1314,7 @@ void FileManager::download(FileId file_id, std::shared_ptr<DownloadCallback> cal
return; return;
} }
if (node->local_.type_ == LocalFileLocation::Type::Full) { if (node->local_.type() == LocalFileLocation::Type::Full) {
auto status = check_local_location(node); auto status = check_local_location(node);
if (status.is_error()) { if (status.is_error()) {
LOG(WARNING) << "Need to redownload file " << file_id << ": " << status.error(); LOG(WARNING) << "Need to redownload file " << file_id << ": " << status.error();
@ -1324,7 +1324,7 @@ void FileManager::download(FileId file_id, std::shared_ptr<DownloadCallback> cal
} }
return; return;
} }
} else if (node->local_.type_ == LocalFileLocation::Type::Partial) { } else if (node->local_.type() == LocalFileLocation::Type::Partial) {
auto status = check_local_location(node); auto status = check_local_location(node);
if (status.is_error()) { if (status.is_error()) {
LOG(WARNING) << "Need to download file " << file_id << " from beginning: " << status.error(); LOG(WARNING) << "Need to download file " << file_id << " from beginning: " << status.error();
@ -1461,7 +1461,7 @@ bool FileManager::delete_partial_remote_location(FileId file_id) {
if (node->upload_pause_ == file_id) { if (node->upload_pause_ == file_id) {
node->upload_pause_ = FileId(); node->upload_pause_ = FileId();
} }
if (node->remote_.type_ == RemoteFileLocation::Type::Full) { if (node->remote_.type() == RemoteFileLocation::Type::Full) {
LOG(INFO) << "File " << file_id << " is already uploaded"; LOG(INFO) << "File " << file_id << " is already uploaded";
return true; return true;
} }
@ -1470,7 +1470,7 @@ bool FileManager::delete_partial_remote_location(FileId file_id) {
auto *file_info = get_file_id_info(file_id); auto *file_info = get_file_id_info(file_id);
file_info->upload_priority_ = 0; file_info->upload_priority_ = 0;
if (node->local_.type_ != LocalFileLocation::Type::Full) { if (node->local_.type() != LocalFileLocation::Type::Full) {
LOG(INFO) << "Need full local location to upload file " << file_id; LOG(INFO) << "Need full local location to upload file " << file_id;
return false; return false;
} }
@ -1603,8 +1603,8 @@ void FileManager::run_upload(FileNode *node, std::vector<int> bad_parts) {
} }
// create encryption key if necessary // create encryption key if necessary
if (((file_view.has_generate_location() && file_view.generate_location().type_ == FileType::Encrypted) || if (((file_view.has_generate_location() && file_view.generate_location().file_type_ == FileType::Encrypted) ||
(file_view.has_local_location() && file_view.local_location().type_ == FileType::Encrypted)) && (file_view.has_local_location() && file_view.local_location().file_type_ == FileType::Encrypted)) &&
file_view.encryption_key().empty()) { file_view.encryption_key().empty()) {
CHECK(!node->file_ids_.empty()); CHECK(!node->file_ids_.empty());
bool success = set_encryption_key(node->file_ids_[0], FileEncryptionKey::create()); bool success = set_encryption_key(node->file_ids_[0], FileEncryptionKey::create());
@ -1619,7 +1619,7 @@ void FileManager::run_upload(FileNode *node, std::vector<int> bad_parts) {
} }
CHECK(node->upload_id_ == 0); CHECK(node->upload_id_ == 0);
if (node->remote_.type_ != RemoteFileLocation::Type::Partial && node->get_by_hash_) { if (node->remote_.type() != RemoteFileLocation::Type::Partial && node->get_by_hash_) {
QueryId id = queries_container_.create(Query{file_id, Query::UploadByHash}); QueryId id = queries_container_.create(Query{file_id, Query::UploadByHash});
node->upload_id_ = id; node->upload_id_ = id;
@ -1726,10 +1726,10 @@ Result<FileId> FileManager::from_persistent_id(CSlice persistent_id, FileType fi
if (status.is_error()) { if (status.is_error()) {
return Status::Error(10, "Wrong remote file id specified: can't unserialize it"); return Status::Error(10, "Wrong remote file id specified: can't unserialize it");
} }
auto &real_type = remote_location.type_; auto &real_file_type = remote_location.file_type_;
if (is_document_type(real_type) && is_document_type(file_type)) { if (is_document_type(real_file_type) && is_document_type(file_type)) {
real_type = file_type; real_file_type = file_type;
} else if (real_type != file_type && file_type != FileType::Temp) { } else if (real_file_type != file_type && file_type != FileType::Temp) {
return Status::Error(10, "Type of file mismatch"); return Status::Error(10, "Type of file mismatch");
} }
FileData data; FileData data;
@ -2173,12 +2173,12 @@ void FileManager::on_error_impl(FileNode *node, FileManager::Query::Type type, b
<< ". File type is " << file_type_name[static_cast<int32>(FileView(node).get_type())]; << ". File type is " << file_type_name[static_cast<int32>(FileView(node).get_type())];
if (status.code() == 0) { if (status.code() == 0) {
// Remove partial locations // Remove partial locations
if (node->local_.type_ == LocalFileLocation::Type::Partial) { if (node->local_.type() == LocalFileLocation::Type::Partial) {
LOG(INFO) << "Unlink file " << node->local_.partial().path_; LOG(INFO) << "Unlink file " << node->local_.partial().path_;
unlink(node->local_.partial().path_).ignore(); unlink(node->local_.partial().path_).ignore();
node->set_local_location(LocalFileLocation(), 0); node->set_local_location(LocalFileLocation(), 0);
} }
if (node->remote_.type_ == RemoteFileLocation::Type::Partial) { if (node->remote_.type() == RemoteFileLocation::Type::Partial) {
node->set_remote_location(RemoteFileLocation(), FileLocationSource::None, 0); node->set_remote_location(RemoteFileLocation(), FileLocationSource::None, 0);
} }
status = Status::Error(400, status.message()); status = Status::Error(400, status.message());

View File

@ -166,13 +166,13 @@ class FileView {
FileType get_type() const { FileType get_type() const {
if (has_local_location()) { if (has_local_location()) {
return local_location().type_; return local_location().file_type_;
} }
if (has_remote_location()) { if (has_remote_location()) {
return remote_location().type_; return remote_location().file_type_;
} }
if (has_generate_location()) { if (has_generate_location()) {
return generate_location().type_; return generate_location().file_type_;
} }
return FileType::Temp; return FileType::Temp;
} }

View File

@ -160,7 +160,7 @@ FileId FileManager::parse_file(ParserT &parser) {
return r_file_id.move_as_ok(); return r_file_id.move_as_ok();
} }
LOG(ERROR) << "Can't resend local file " << full_local_location.path_; LOG(ERROR) << "Can't resend local file " << full_local_location.path_;
return register_empty(full_local_location.type_); return register_empty(full_local_location.file_type_);
} }
case FileStoreType::Generate: { case FileStoreType::Generate: {
FullGenerateFileLocation full_generated_location; FullGenerateFileLocation full_generated_location;
@ -175,23 +175,23 @@ FileId FileManager::parse_file(ParserT &parser) {
} }
if (begins_with(full_generated_location.conversion_, "#file_id#")) { if (begins_with(full_generated_location.conversion_, "#file_id#")) {
LOG(ERROR) << "Can't resend message with '#file_id#...' location"; LOG(ERROR) << "Can't resend message with '#file_id#...' location";
return register_empty(full_generated_location.type_); return register_empty(full_generated_location.file_type_);
} }
if (full_generated_location.conversion_ == "#_file_id#") { if (full_generated_location.conversion_ == "#_file_id#") {
auto file_id = parse_file(parser); auto file_id = parse_file(parser);
if (file_id.empty()) { if (file_id.empty()) {
return register_empty(full_generated_location.type_); return register_empty(full_generated_location.file_type_);
} }
auto download_file_id = dup_file_id(file_id); auto download_file_id = dup_file_id(file_id);
full_generated_location.conversion_ = PSTRING() << "#file_id#" << download_file_id.get(); full_generated_location.conversion_ = PSTRING() << "#file_id#" << download_file_id.get();
} }
auto r_file_id = register_generate(full_generated_location.type_, full_generated_location.original_path_, auto r_file_id = register_generate(full_generated_location.file_type_, full_generated_location.original_path_,
full_generated_location.conversion_, owner_dialog_id, expected_size); full_generated_location.conversion_, owner_dialog_id, expected_size);
if (r_file_id.is_ok()) { if (r_file_id.is_ok()) {
return r_file_id.move_as_ok(); return r_file_id.move_as_ok();
} }
return register_empty(full_generated_location.type_); return register_empty(full_generated_location.file_type_);
} }
case FileStoreType::Url: { case FileStoreType::Url: {
FileType type; FileType type;

View File

@ -54,11 +54,11 @@ Status scan_db(CallbackT &&callback) {
return; return;
} }
DbFileInfo info; DbFileInfo info;
if (data.local_.type_ == LocalFileLocation::Type::Full) { if (data.local_.type() == LocalFileLocation::Type::Full) {
info.file_type = data.local_.full().type_; info.file_type = data.local_.full().file_type_;
info.path = data.local_.full().path_; info.path = data.local_.full().path_;
} else if (data.local_.type_ == LocalFileLocation::Type::Partial) { } else if (data.local_.type() == LocalFileLocation::Type::Partial) {
info.file_type = data.local_.partial().type_; info.file_type = data.local_.partial().file_type_;
info.path = data.local_.partial().path_; info.path = data.local_.partial().path_;
} else { } else {
return; return;
@ -69,7 +69,7 @@ Status scan_db(CallbackT &&callback) {
} }
info.owner_dialog_id = data.owner_dialog_id_; info.owner_dialog_id = data.owner_dialog_id_;
info.size = data.size_; info.size = data.size_;
if (info.size == 0 && data.local_.type_ == LocalFileLocation::Type::Full) { if (info.size == 0 && data.local_.type() == LocalFileLocation::Type::Full) {
LOG(ERROR) << "Unknown size in db"; LOG(ERROR) << "Unknown size in db";
return; return;
} }

View File

@ -36,7 +36,7 @@ FileUploader::FileUploader(const LocalFileLocation &local, const RemoteFileLocat
} }
Result<FileLoader::FileInfo> FileUploader::init() { Result<FileLoader::FileInfo> FileUploader::init() {
if (remote_.type_ == RemoteFileLocation::Type::Full) { if (remote_.type() == RemoteFileLocation::Type::Full) {
return Status::Error("File is already uploaded"); return Status::Error("File is already uploaded");
} }
@ -45,7 +45,7 @@ Result<FileLoader::FileInfo> FileUploader::init() {
int offset = 0; int offset = 0;
int part_size = 0; int part_size = 0;
if (remote_.type_ == RemoteFileLocation::Type::Partial) { if (remote_.type() == RemoteFileLocation::Type::Partial) {
const auto &partial = remote_.partial(); const auto &partial = remote_.partial();
file_id_ = partial.file_id_; file_id_ = partial.file_id_;
part_size = partial.part_size_; part_size = partial.part_size_;
@ -87,20 +87,20 @@ Result<FileLoader::PrefixInfo> FileUploader::on_update_local_location(const Loca
int64 local_size = 0; int64 local_size = 0;
bool local_is_ready{false}; bool local_is_ready{false};
FileType file_type{FileType::Temp}; FileType file_type{FileType::Temp};
if (location.type_ == LocalFileLocation::Type::Empty) { if (location.type() == LocalFileLocation::Type::Empty) {
path = ""; path = "";
local_size = 0; local_size = 0;
local_is_ready = false; local_is_ready = false;
file_type = FileType::Temp; file_type = FileType::Temp;
} else if (location.type_ == LocalFileLocation::Type::Partial) { } else if (location.type() == LocalFileLocation::Type::Partial) {
path = location.partial().path_; path = location.partial().path_;
local_size = static_cast<int64>(location.partial().part_size_) * location.partial().ready_part_count_; local_size = static_cast<int64>(location.partial().part_size_) * location.partial().ready_part_count_;
local_is_ready = false; local_is_ready = false;
file_type = location.partial().type_; file_type = location.partial().file_type_;
} else { } else {
path = location.full().path_; path = location.full().path_;
local_is_ready = true; local_is_ready = true;
file_type = location.full().type_; file_type = location.full().file_type_;
} }
if (!path.empty() && path != fd_path_) { if (!path.empty() && path != fd_path_) {
@ -108,7 +108,7 @@ Result<FileLoader::PrefixInfo> FileUploader::on_update_local_location(const Loca
// Race: partial location could be already deleted. Just ignore such locations // Race: partial location could be already deleted. Just ignore such locations
if (res_fd.is_error()) { if (res_fd.is_error()) {
if (location.type_ == LocalFileLocation::Type::Partial) { if (location.type() == LocalFileLocation::Type::Partial) {
PrefixInfo info; PrefixInfo info;
info.size = local_size_; info.size = local_size_;
info.is_ready = local_is_ready_; info.is_ready = local_is_ready_;