Better FileLocationSource.

GitOrigin-RevId: bed0d4f52b129560097f0877bd4b3d3511914f27
This commit is contained in:
levlam 2018-01-26 15:56:19 +03:00
parent e66cc39f95
commit f0e660e2aa
7 changed files with 45 additions and 37 deletions

View File

@ -222,9 +222,9 @@ std::pair<DocumentsManager::DocumentType, FileId> DocumentsManager::on_get_docum
file_name += extension;
}
}
FileId file_id =
td_->file_manager_->register_remote(FullRemoteFileLocation(file_type, id, access_hash, DcId::internal(dc_id)),
owner_dialog_id, size, 0, suggested_file_name);
FileId file_id = td_->file_manager_->register_remote(
FullRemoteFileLocation(file_type, id, access_hash, DcId::internal(dc_id)), FileLocationSource::FromServer,
owner_dialog_id, size, 0, suggested_file_name);
if (!encryption_key.empty()) {
td_->file_manager_->set_encryption_key(file_id, std::move(encryption_key));
}

View File

@ -8759,7 +8759,7 @@ void MessagesManager::on_send_secret_message_success(int64 random_id, MessageId
new_file_id = td_->file_manager_->register_remote(
FullRemoteFileLocation(FileType::Encrypted, file->id_, file->access_hash_, DcId::internal(file->dc_id_)),
owner_dialog_id, 0, 0);
FileLocationSource::FromServer, owner_dialog_id, 0, 0);
}
}
@ -19963,7 +19963,7 @@ Photo MessagesManager::get_web_document_photo(tl_object_ptr<telegram_api::webDoc
// TODO real file name
FileId file_id = td_->file_manager_->register_remote(
FullRemoteFileLocation(FileType::Photo, url, web_document->access_hash_, DcId::internal(web_document->dc_id_)),
owner_dialog_id, 0, web_document->size_, "");
FileLocationSource::FromServer, owner_dialog_id, 0, web_document->size_, "");
Dimensions dimensions;
for (auto &attribute : web_document->attributes_) {
@ -20360,8 +20360,8 @@ unique_ptr<MessageContent> MessagesManager::dup_message_content(DialogId dialog_
if (to_secret && !file_view.is_encrypted()) {
auto download_file_id = file_manager->dup_file_id(file_id);
file_id = file_manager
->register_generate(FileType::Encrypted, "", PSTRING() << "#file_id#" << download_file_id.get(),
dialog_id, file_view.size())
->register_generate(FileType::Encrypted, FileLocationSource::FromServer, "",
PSTRING() << "#file_id#" << download_file_id.get(), dialog_id, file_view.size())
.ok();
}
return file_manager->dup_file_id(file_id);
@ -21945,7 +21945,7 @@ bool MessagesManager::update_message_content(DialogId dialog_id, const Message *
FileId file_id = td_->file_manager_->register_remote(
FullRemoteFileLocation(FileType::Photo, new_file_view.remote_location().get_id(),
new_file_view.remote_location().get_access_hash(), 0, 0, 0, DcId::invalid()),
dialog_id, old_photo->photos.back().size, 0, "");
FileLocationSource::FromServer, dialog_id, old_photo->photos.back().size, 0, "");
LOG_STATUS(td_->file_manager_->merge(file_id, old_file_id));
}
}

View File

@ -92,8 +92,8 @@ static FileId register_photo(FileManager *file_manager, FileType file_type, int6
LOG(DEBUG) << "Receive photo of type " << static_cast<int8>(file_type) << " in [" << dc_id << "," << volume_id << ","
<< local_id << "]. Id: (" << id << ", " << access_hash << ")";
return file_manager->register_remote(
FullRemoteFileLocation(file_type, id, access_hash, local_id, volume_id, secret, dc_id), owner_dialog_id,
file_size, 0, to_string(std::abs(id ? id : local_id)) + ".jpg");
FullRemoteFileLocation(file_type, id, access_hash, local_id, volume_id, secret, dc_id),
FileLocationSource::FromServer, owner_dialog_id, file_size, 0, to_string(std::abs(id ? id : local_id)) + ".jpg");
}
ProfilePhoto get_profile_photo(FileManager *file_manager,
@ -221,8 +221,8 @@ PhotoSize get_thumbnail_photo_size(FileManager *file_manager, BufferSlice bytes,
auto volume_id = Random::secure_int64();
auto secret = 0;
res.file_id = file_manager->register_remote(
FullRemoteFileLocation(FileType::EncryptedThumbnail, 0, 0, local_id, volume_id, secret, dc_id), owner_dialog_id,
res.size, 0, to_string(std::abs(local_id)) + ".jpg");
FullRemoteFileLocation(FileType::EncryptedThumbnail, 0, 0, local_id, volume_id, secret, dc_id),
FileLocationSource::FromServer, owner_dialog_id, res.size, 0, to_string(std::abs(local_id)) + ".jpg");
file_manager->set_content(res.file_id, std::move(bytes));
return res;
@ -315,7 +315,7 @@ Photo get_photo(FileManager *file_manager, tl_object_ptr<telegram_api::encrypted
CHECK(DcId::is_valid(file->dc_id_));
FileId file_id = file_manager->register_remote(
FullRemoteFileLocation(FileType::Encrypted, file->id_, file->access_hash_, DcId::internal(file->dc_id_)),
owner_dialog_id, photo->size_, 0, to_string(std::abs(file->id_)) + ".jpg");
FileLocationSource::FromServer, owner_dialog_id, photo->size_, 0, to_string(std::abs(file->id_)) + ".jpg");
file_manager->set_encryption_key(file_id, FileEncryptionKey{photo->key_.as_slice(), photo->iv_.as_slice()});
Photo res;

View File

@ -955,7 +955,7 @@ std::pair<int64, FileId> StickersManager::on_get_sticker_document(tl_object_ptr<
int64 document_id = document->id_;
FileId sticker_id = td_->file_manager_->register_remote(
FullRemoteFileLocation(FileType::Sticker, document_id, document->access_hash_, DcId::internal(document->dc_id_)),
DialogId(), document->size_, 0, to_string(document_id) + ".webp");
FileLocationSource::FromServer, DialogId(), document->size_, 0, to_string(document_id) + ".webp");
PhotoSize thumbnail =
get_photo_size(td_->file_manager_.get(), FileType::Thumbnail, 0, 0, DialogId(), std::move(document->thumb_));

View File

@ -593,32 +593,34 @@ Result<FileId> FileManager::register_local(FullLocalFileLocation location, Dialo
return register_file(std::move(data), FileLocationSource::None /*won't be used*/, "register_local", force);
}
FileId FileManager::register_remote(const FullRemoteFileLocation &location, DialogId owner_dialog_id, int64 size,
int64 expected_size, string name) {
FileId FileManager::register_remote(const FullRemoteFileLocation &location, FileLocationSource file_location_source,
DialogId owner_dialog_id, int64 size, int64 expected_size, string name) {
FileData data;
data.remote_ = RemoteFileLocation(location);
data.owner_dialog_id_ = owner_dialog_id;
data.size_ = size;
data.expected_size_ = expected_size;
data.name_ = std::move(name);
return register_file(std::move(data), FileLocationSource::FromServer, "register_remote", false).move_as_ok();
return register_file(std::move(data), file_location_source, "register_remote", false).move_as_ok();
}
FileId FileManager::register_url(string url, FileType file_type, DialogId owner_dialog_id) {
auto file_id = register_generate(file_type, url, "#url#", owner_dialog_id, 0).ok();
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();
auto *file_node = get_file_node(file_id);
CHECK(file_node);
file_node->set_url(url);
return file_id;
}
Result<FileId> FileManager::register_generate(FileType file_type, string original_path, string conversion,
DialogId owner_dialog_id, int64 expected_size) {
Result<FileId> FileManager::register_generate(FileType file_type, FileLocationSource file_location_source,
string original_path, string conversion, DialogId owner_dialog_id,
int64 expected_size) {
FileData data;
data.generate_ = GenerateFileLocation(FullGenerateFileLocation(file_type, original_path, std::move(conversion)));
data.owner_dialog_id_ = owner_dialog_id;
data.expected_size_ = expected_size;
return register_file(std::move(data), FileLocationSource::FromServer, "register_generate", false);
return register_file(std::move(data), file_location_source, "register_generate", false);
}
Result<FileId> FileManager::register_file(FileData data, FileLocationSource file_location_source, const char *source,
@ -1705,7 +1707,7 @@ Result<FileId> FileManager::from_persistent_id(CSlice persistent_id, FileType fi
if (!clean_input_string(url)) {
return Status::Error(400, "URL must be in UTF-8");
}
return register_url(std::move(url), file_type, DialogId());
return register_url(std::move(url), file_type, FileLocationSource::FromUser, DialogId());
}
auto r_binary = base64url_decode(persistent_id);
@ -1851,8 +1853,8 @@ Result<FileId> FileManager::get_input_thumbnail_file_id(const tl_object_ptr<td_a
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,
generated_thumbnail->original_path_, generated_thumbnail->conversion_, owner_dialog_id,
generated_thumbnail->expected_size_);
FileLocationSource::FromUser, generated_thumbnail->original_path_,
generated_thumbnail->conversion_, owner_dialog_id, generated_thumbnail->expected_size_);
}
default:
UNREACHABLE();
@ -1899,8 +1901,9 @@ Result<FileId> FileManager::get_input_file_id(FileType type, const tl_object_ptr
}
case td_api::inputFileGenerated::ID: {
auto *generated_file = static_cast<const td_api::inputFileGenerated *>(file.get());
return register_generate(is_encrypted ? FileType::Encrypted : type, generated_file->original_path_,
generated_file->conversion_, owner_dialog_id, generated_file->expected_size_);
return register_generate(is_encrypted ? FileType::Encrypted : type, FileLocationSource::FromUser,
generated_file->original_path_, generated_file->conversion_, owner_dialog_id,
generated_file->expected_size_);
}
default:
UNREACHABLE();
@ -2070,7 +2073,7 @@ void FileManager::on_upload_ok(QueryId query_id, FileType file_type, const Parti
void FileManager::on_upload_full_ok(QueryId query_id, const FullRemoteFileLocation &remote) {
LOG(INFO) << "ON UPLOAD OK";
auto file_id = finish_query(query_id).first.file_id_;
auto new_file_id = register_remote(remote, DialogId(), 0, 0, "");
auto new_file_id = register_remote(remote, FileLocationSource::FromServer, DialogId(), 0, 0, "");
LOG_STATUS(merge(new_file_id, file_id));
}

View File

@ -246,10 +246,12 @@ class FileManager : public FileLoadManager::Callback {
FileId register_empty(FileType type);
Result<FileId> register_local(FullLocalFileLocation location, DialogId owner_dialog_id, int64 size,
bool get_by_hash = false, bool force = false) TD_WARN_UNUSED_RESULT;
FileId register_remote(const FullRemoteFileLocation &location, DialogId owner_dialog_id, int64 size,
int64 expected_size, string name = "") TD_WARN_UNUSED_RESULT;
Result<FileId> register_generate(FileType file_type, string original_path, string conversion,
DialogId owner_dialog_id, int64 expected_size) TD_WARN_UNUSED_RESULT;
FileId register_remote(const FullRemoteFileLocation &location, FileLocationSource file_location_source,
DialogId owner_dialog_id, int64 size, int64 expected_size,
string name = "") TD_WARN_UNUSED_RESULT;
Result<FileId> register_generate(FileType file_type, FileLocationSource file_location_source, string original_path,
string conversion, DialogId owner_dialog_id,
int64 expected_size) TD_WARN_UNUSED_RESULT;
Result<FileId> register_file(FileData data, FileLocationSource file_location_source, const char *source, bool force);
Result<FileId> merge(FileId x_file_id, FileId y_file_id, bool no_sync = false) TD_WARN_UNUSED_RESULT;
@ -294,7 +296,8 @@ class FileManager : public FileLoadManager::Callback {
Result<FileId> check_input_file_id(FileType type, Result<FileId> result, bool is_encrypted,
bool allow_zero) TD_WARN_UNUSED_RESULT;
FileId register_url(string url, FileType file_type, DialogId owner_dialog_id);
FileId register_url(string url, FileType file_type, FileLocationSource file_location_source,
DialogId owner_dialog_id);
static constexpr int8 FROM_BYTES_PRIORITY = 10;
using FileNodeId = int32;

View File

@ -142,7 +142,8 @@ FileId FileManager::parse_file(ParserT &parser) {
if (parser.version() >= static_cast<int32>(Version::StoreFileOwnerId)) {
parse(owner_dialog_id, parser);
}
return register_remote(full_remote_location, owner_dialog_id, size, expected_size, name);
return register_remote(full_remote_location, FileLocationSource::FromDb, owner_dialog_id, size, expected_size,
name);
}
case FileStoreType::Local: {
FullLocalFileLocation full_local_location;
@ -186,8 +187,9 @@ FileId FileManager::parse_file(ParserT &parser) {
full_generated_location.conversion_ = PSTRING() << "#file_id#" << download_file_id.get();
}
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);
auto r_file_id = register_generate(full_generated_location.file_type_, FileLocationSource::FromDb,
full_generated_location.original_path_, full_generated_location.conversion_,
owner_dialog_id, expected_size);
if (r_file_id.is_ok()) {
return r_file_id.move_as_ok();
}
@ -202,7 +204,7 @@ FileId FileManager::parse_file(ParserT &parser) {
if (parser.version() >= static_cast<int32>(Version::StoreFileOwnerId)) {
parse(owner_dialog_id, parser);
}
return register_url(url, type, owner_dialog_id);
return register_url(url, type, FileLocationSource::FromDb, owner_dialog_id);
}
}
return FileId();