Remove support for two file references.

GitOrigin-RevId: 3bf8031e9cc8bcd95e8cb74246c32d9198991aa1
This commit is contained in:
levlam 2019-06-17 05:37:57 +03:00
parent fa234d0f36
commit cfc9cc5f28
4 changed files with 29 additions and 133 deletions

View File

@ -2904,7 +2904,7 @@ void merge_message_contents(Td *td, const MessageContent *old_content, MessageCo
FileId file_id = td->file_manager_->register_remote( FileId file_id = td->file_manager_->register_remote(
FullRemoteFileLocation({FileType::Photo, 'i'}, new_file_view.remote_location().get_id(), FullRemoteFileLocation({FileType::Photo, 'i'}, new_file_view.remote_location().get_id(),
new_file_view.remote_location().get_access_hash(), 0, 0, DcId::invalid(), new_file_view.remote_location().get_access_hash(), 0, 0, DcId::invalid(),
new_file_view.remote_location().get_upload_file_reference().str()), new_file_view.remote_location().get_file_reference().str()),
FileLocationSource::FromServer, 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)); LOG_STATUS(td->file_manager_->merge(file_id, old_file_id));
} }

View File

@ -277,8 +277,8 @@ Status FileDownloader::check_net_query(NetQueryPtr &net_query) {
auto error = net_query->move_as_error(); auto error = net_query->move_as_error();
if (FileReferenceManager::is_file_reference_error(error)) { if (FileReferenceManager::is_file_reference_error(error)) {
VLOG(file_references) << "Receive " << error << " for being downloaded file"; VLOG(file_references) << "Receive " << error << " for being downloaded file";
error = Status::Error(error.code(), PSLICE() << error.message() << "#BASE64" error = Status::Error(error.code(),
<< base64_encode(remote_.get_download_file_reference())); PSLICE() << error.message() << "#BASE64" << base64_encode(remote_.get_file_reference()));
} }
return error; return error;
} }

View File

@ -33,92 +33,6 @@ class FileReferenceView {
static Slice invalid_file_reference() { static Slice invalid_file_reference() {
return Slice("#"); return Slice("#");
} }
static std::string create_invalid() {
return create_one(invalid_file_reference());
}
static std::string create_one(Slice first) {
if (first.empty()) {
return {};
}
unsigned char second_length = 255;
return PSTRING() << static_cast<char>(second_length) << first;
}
static std::string create_two(Slice first, Slice second) {
if (first.empty() && second.empty()) {
return {};
}
if (second.size() >= 255) {
LOG(ERROR) << "File reference is too big " << base64_encode(second);
second = invalid_file_reference();
}
char second_length = static_cast<char>(static_cast<unsigned char>(second.size()));
return PSTRING() << second_length << first << second;
}
explicit FileReferenceView(Slice data) {
if (data.empty()) {
return;
}
unsigned char second_size = data.ubegin()[0];
if (second_size == 255) {
first_ = data.substr(1);
second_ = data.substr(1);
} else {
if (second_size > data.size() - 1) {
first_ = second_ = data;
return;
}
auto first_size = data.size() - 1 - second_size;
first_ = data.substr(1, first_size);
second_ = data.substr(1 + first_size);
size_ = 2;
}
}
Slice upload() const {
return first_;
}
Slice download() const {
return second_;
}
bool has_upload() const {
return upload() != invalid_file_reference();
}
bool has_download() const {
return download() != invalid_file_reference();
}
std::pair<std::string, bool> delete_file_reference(Slice bad_file_reference) const {
if (bad_file_reference == FileReferenceView::invalid_file_reference()) {
return {string(), false};
}
auto first = first_;
auto second = second_;
bool is_changed = false;
if (first == bad_file_reference) {
first = invalid_file_reference();
is_changed = true;
}
if (second == bad_file_reference) {
second = invalid_file_reference();
is_changed = true;
}
if (!is_changed) {
return {string(), false};
}
return {create(first, second), true};
}
private:
std::string create(Slice first, Slice second) const {
if (size_ == 1) {
return create_one(first);
}
return create_two(first, second);
}
Slice first_;
Slice second_;
int size_{1};
}; };
struct EmptyRemoteFileLocation { struct EmptyRemoteFileLocation {
@ -349,8 +263,7 @@ class FullRemoteFileLocation {
} }
void check_file_reference() { void check_file_reference() {
FileReferenceView view(file_reference_); if (file_reference_ == FileReferenceView::invalid_file_reference()) {
if (!(view.has_upload() && view.has_download())) {
LOG(ERROR) << "Tried to register file with invalid file reference"; LOG(ERROR) << "Tried to register file with invalid file reference";
file_reference_.clear(); file_reference_.clear();
} }
@ -410,30 +323,18 @@ class FullRemoteFileLocation {
} }
bool delete_file_reference(Slice bad_file_reference) { bool delete_file_reference(Slice bad_file_reference) {
auto res = FileReferenceView(file_reference_).delete_file_reference(bad_file_reference); if (file_reference_ != FileReferenceView::invalid_file_reference() && file_reference_ == bad_file_reference) {
if (res.second) { file_reference_ = FileReferenceView::invalid_file_reference().str();
file_reference_ = res.first; return true;
} }
return res.second; return false;
} }
bool has_upload_file_reference() const { bool has_file_reference() const {
return FileReferenceView(file_reference_).has_upload(); return file_reference_ != FileReferenceView::invalid_file_reference();
} }
bool has_download_file_reference() const { Slice get_file_reference() const {
return FileReferenceView(file_reference_).has_download();
}
bool has_any_file_reference() const {
return has_upload_file_reference() || has_download_file_reference();
}
Slice get_raw_file_reference() const {
return file_reference_; return file_reference_;
} }
Slice get_upload_file_reference() const {
return FileReferenceView(file_reference_).upload();
}
Slice get_download_file_reference() const {
return FileReferenceView(file_reference_).download();
}
string get_url() const { string get_url() const {
if (is_web()) { if (is_web()) {
@ -479,19 +380,18 @@ class FullRemoteFileLocation {
case LocationType::Photo: { case LocationType::Photo: {
switch (photo().source_.type) { switch (photo().source_.type) {
case PhotoSizeSource::Type::Empty: case PhotoSizeSource::Type::Empty:
return make_tl_object<telegram_api::inputFileLocation>( return make_tl_object<telegram_api::inputFileLocation>(photo().volume_id_, photo().local_id_,
photo().volume_id_, photo().local_id_, photo().secret_, photo().secret_, BufferSlice(file_reference_));
BufferSlice(FileReferenceView(file_reference_).download()));
case PhotoSizeSource::Type::Thumbnail: { case PhotoSizeSource::Type::Thumbnail: {
auto &thumbnail = photo().source_.thumbnail(); auto &thumbnail = photo().source_.thumbnail();
switch (file_type_) { switch (file_type_) {
case FileType::Photo: case FileType::Photo:
return make_tl_object<telegram_api::inputPhotoFileLocation>( return make_tl_object<telegram_api::inputPhotoFileLocation>(
photo().id_, photo().access_hash_, BufferSlice(FileReferenceView(file_reference_).download()), photo().id_, photo().access_hash_, BufferSlice(file_reference_),
std::string(1, static_cast<char>(narrow_cast<uint8>(thumbnail.thumbnail_type)))); std::string(1, static_cast<char>(narrow_cast<uint8>(thumbnail.thumbnail_type))));
case FileType::Thumbnail: case FileType::Thumbnail:
return make_tl_object<telegram_api::inputDocumentFileLocation>( return make_tl_object<telegram_api::inputDocumentFileLocation>(
photo().id_, photo().access_hash_, BufferSlice(FileReferenceView(file_reference_).download()), photo().id_, photo().access_hash_, BufferSlice(file_reference_),
std::string(1, static_cast<char>(narrow_cast<uint8>(thumbnail.thumbnail_type)))); std::string(1, static_cast<char>(narrow_cast<uint8>(thumbnail.thumbnail_type))));
default: default:
UNREACHABLE(); UNREACHABLE();
@ -516,9 +416,8 @@ class FullRemoteFileLocation {
} else if (is_secure()) { } else if (is_secure()) {
return make_tl_object<telegram_api::inputSecureFileLocation>(common().id_, common().access_hash_); return make_tl_object<telegram_api::inputSecureFileLocation>(common().id_, common().access_hash_);
} else { } else {
return make_tl_object<telegram_api::inputDocumentFileLocation>( return make_tl_object<telegram_api::inputDocumentFileLocation>(common().id_, common().access_hash_,
common().id_, common().access_hash_, BufferSlice(FileReferenceView(file_reference_).download()), BufferSlice(file_reference_), string());
string());
} }
case LocationType::Web: case LocationType::Web:
case LocationType::None: case LocationType::None:
@ -533,14 +432,13 @@ class FullRemoteFileLocation {
LOG_CHECK(is_common()) << file << ' ' << line; LOG_CHECK(is_common()) << file << ' ' << line;
LOG_CHECK(is_document()) << file << ' ' << line; LOG_CHECK(is_document()) << file << ' ' << line;
return make_tl_object<telegram_api::inputDocument>(common().id_, common().access_hash_, return make_tl_object<telegram_api::inputDocument>(common().id_, common().access_hash_,
BufferSlice(FileReferenceView(file_reference_).upload())); BufferSlice(file_reference_));
} }
#define as_input_photo() as_input_photo_impl(__FILE__, __LINE__) #define as_input_photo() as_input_photo_impl(__FILE__, __LINE__)
tl_object_ptr<telegram_api::inputPhoto> as_input_photo_impl(const char *file, int line) const { tl_object_ptr<telegram_api::inputPhoto> as_input_photo_impl(const char *file, int line) const {
LOG_CHECK(is_photo()) << file << ' ' << line; LOG_CHECK(is_photo()) << file << ' ' << line;
return make_tl_object<telegram_api::inputPhoto>(photo().id_, photo().access_hash_, return make_tl_object<telegram_api::inputPhoto>(photo().id_, photo().access_hash_, BufferSlice(file_reference_));
BufferSlice(FileReferenceView(file_reference_).upload()));
} }
tl_object_ptr<telegram_api::inputEncryptedFile> as_input_encrypted_file() const { tl_object_ptr<telegram_api::inputEncryptedFile> as_input_encrypted_file() const {
@ -562,7 +460,7 @@ class FullRemoteFileLocation {
DcId dc_id, std::string file_reference) DcId dc_id, std::string file_reference)
: file_type_(source.file_type) : file_type_(source.file_type)
, dc_id_(dc_id) , dc_id_(dc_id)
, file_reference_(FileReferenceView::create_one(file_reference)) , file_reference_(std::move(file_reference))
, variant_(PhotoRemoteFileLocation{id, access_hash, volume_id, -1, local_id, source}) { , variant_(PhotoRemoteFileLocation{id, access_hash, volume_id, -1, local_id, source}) {
CHECK(is_photo()); CHECK(is_photo());
check_file_reference(); check_file_reference();
@ -572,7 +470,7 @@ class FullRemoteFileLocation {
FullRemoteFileLocation(FileType file_type, int64 id, int64 access_hash, DcId dc_id, std::string file_reference) FullRemoteFileLocation(FileType file_type, int64 id, int64 access_hash, DcId dc_id, std::string file_reference)
: file_type_(file_type) : file_type_(file_type)
, dc_id_(dc_id) , dc_id_(dc_id)
, file_reference_(FileReferenceView::create_one(file_reference)) , file_reference_(std::move(file_reference))
, variant_(CommonRemoteFileLocation{id, access_hash}) { , variant_(CommonRemoteFileLocation{id, access_hash}) {
CHECK(is_common()); CHECK(is_common());
check_file_reference(); check_file_reference();
@ -639,9 +537,7 @@ inline StringBuilder &operator<<(StringBuilder &string_builder,
string_builder << ", " << full_remote_file_location.get_dc_id(); string_builder << ", " << full_remote_file_location.get_dc_id();
} }
if (!full_remote_file_location.file_reference_.empty()) { if (!full_remote_file_location.file_reference_.empty()) {
FileReferenceView view(full_remote_file_location.file_reference_); string_builder << ", " << tag("file_reference", base64_encode(full_remote_file_location.file_reference_));
string_builder << ", " << tag("file_reference_upload", base64_encode(view.upload()))
<< tag("file_reference_download", base64_encode(view.download()));
} }
string_builder << ", location = "; string_builder << ", location = ";

View File

@ -203,7 +203,7 @@ void FileNode::set_new_remote_location(NewRemoteFileLocation new_remote) {
if (new_remote.full) { if (new_remote.full) {
if (remote_.full && remote_.full.value() == new_remote.full.value()) { if (remote_.full && remote_.full.value() == new_remote.full.value()) {
if (remote_.full.value().get_access_hash() != new_remote.full.value().get_access_hash() || if (remote_.full.value().get_access_hash() != new_remote.full.value().get_access_hash() ||
remote_.full.value().get_raw_file_reference() != new_remote.full.value().get_raw_file_reference()) { remote_.full.value().get_file_reference() != new_remote.full.value().get_file_reference()) {
on_pmc_changed(); on_pmc_changed();
} }
} else { } else {
@ -272,7 +272,7 @@ bool FileNode::delete_file_reference(Slice file_reference) {
if (!remote_.full.value().delete_file_reference(file_reference)) { if (!remote_.full.value().delete_file_reference(file_reference)) {
VLOG(file_references) << "Can't delete unmatching file reference " << format::escaped(file_reference) << ", have " VLOG(file_references) << "Can't delete unmatching file reference " << format::escaped(file_reference) << ", have "
<< format::escaped(remote_.full.value().get_raw_file_reference()); << format::escaped(remote_.full.value().get_file_reference());
return false; return false;
} }
@ -473,7 +473,7 @@ bool FileView::has_active_upload_remote_location() const {
if (remote_location().is_encrypted_any()) { if (remote_location().is_encrypted_any()) {
return true; return true;
} }
return remote_location().has_upload_file_reference(); return remote_location().has_file_reference();
} }
bool FileView::has_active_download_remote_location() const { bool FileView::has_active_download_remote_location() const {
@ -483,7 +483,7 @@ bool FileView::has_active_download_remote_location() const {
if (remote_location().is_encrypted_any()) { if (remote_location().is_encrypted_any()) {
return true; return true;
} }
return remote_location().has_download_file_reference(); return remote_location().has_file_reference();
} }
const FullRemoteFileLocation &FileView::remote_location() const { const FullRemoteFileLocation &FileView::remote_location() const {
@ -1138,13 +1138,13 @@ static int merge_choose_remote_location(const FullRemoteFileLocation &x, FileLoc
if (x.is_web() != y.is_web()) { if (x.is_web() != y.is_web()) {
return x.is_web(); // prefer non-web return x.is_web(); // prefer non-web
} }
auto x_ref = x.has_any_file_reference(); auto x_ref = x.has_file_reference();
auto y_ref = y.has_any_file_reference(); auto y_ref = y.has_file_reference();
if (x_ref || y_ref) { if (x_ref || y_ref) {
if (x_ref != y_ref) { if (x_ref != y_ref) {
return !x_ref; return !x_ref;
} }
if (x.get_raw_file_reference() != y.get_raw_file_reference()) { if (x.get_file_reference() != y.get_file_reference()) {
return merge_choose_file_source_location(x_source, y_source); return merge_choose_file_source_location(x_source, y_source);
} }
} }