Files: make all file references encoded with FileReferenceView

GitOrigin-RevId: c876f0d115035cb0e9030b6a4124c31c53a191b2
This commit is contained in:
Arseny Smirnov 2019-01-31 12:33:35 +04:00
parent 5e2a71d8fb
commit 8a80571903
3 changed files with 27 additions and 21 deletions

View File

@ -2122,7 +2122,7 @@ tl_object_ptr<telegram_api::InputMedia> get_input_media(const MessageContent *co
} }
if (!was_uploaded) { if (!was_uploaded) {
auto file_reference = FileManager::extract_file_reference(input_media); auto file_reference = FileManager::extract_file_reference(input_media);
if (!FileReferenceView(file_reference).has_upload()) { if (file_reference == FileReferenceView::invalid_file_reference()) {
return nullptr; return nullptr;
} }
} }
@ -2132,7 +2132,7 @@ tl_object_ptr<telegram_api::InputMedia> get_input_media(const MessageContent *co
tl_object_ptr<telegram_api::InputMedia> get_input_media(const MessageContent *content, Td *td, int32 ttl) { tl_object_ptr<telegram_api::InputMedia> get_input_media(const MessageContent *content, Td *td, int32 ttl) {
auto input_media = get_input_media(content, td, nullptr, nullptr, ttl); auto input_media = get_input_media(content, td, nullptr, nullptr, ttl);
auto file_reference = FileManager::extract_file_reference(input_media); auto file_reference = FileManager::extract_file_reference(input_media);
if (!FileReferenceView(file_reference).has_upload()) { if (file_reference == FileReferenceView::invalid_file_reference()) {
return nullptr; return nullptr;
} }
return input_media; return input_media;

View File

@ -29,14 +29,23 @@ namespace td {
class FileReferenceView { class FileReferenceView {
public: public:
static Slice invalid_file_reference() {
return Slice("#");
}
static std::string create_invalid() { static std::string create_invalid() {
return create_one(invalid_file_reference()); return create_one(invalid_file_reference());
} }
static std::string create_one(Slice first) { static std::string create_one(Slice first) {
if (first.empty()) {
return {};
}
unsigned char second_length = 255; unsigned char second_length = 255;
return PSTRING() << static_cast<char>(second_length) << first; return PSTRING() << static_cast<char>(second_length) << first;
} }
static std::string create_two(Slice first, Slice second) { static std::string create_two(Slice first, Slice second) {
if (first.empty() && second.empty()) {
return {};
}
if (second.size() >= 255) { if (second.size() >= 255) {
LOG(ERROR) << "File reference is too big " << base64_encode(second); LOG(ERROR) << "File reference is too big " << base64_encode(second);
second = invalid_file_reference(); second = invalid_file_reference();
@ -99,10 +108,6 @@ class FileReferenceView {
} }
private: private:
static Slice invalid_file_reference() {
return Slice("#");
}
std::string create(Slice first, Slice second) const { std::string create(Slice first, Slice second) const {
if (size_ == 1) { if (size_ == 1) {
return create_one(first); return create_one(first);
@ -112,7 +117,7 @@ class FileReferenceView {
Slice first_; Slice first_;
Slice second_; Slice second_;
int size_ = 1; int size_{1};
}; };
struct EmptyRemoteFileLocation { struct EmptyRemoteFileLocation {
@ -495,8 +500,6 @@ class FullRemoteFileLocation {
auto res = FileReferenceView(file_reference_).delete_file_reference(bad_file_reference); auto res = FileReferenceView(file_reference_).delete_file_reference(bad_file_reference);
if (res.second) { if (res.second) {
file_reference_ = res.first; file_reference_ = res.first;
} else if (file_reference_ == bad_file_reference) {
file_reference_ = FileReferenceView::create_invalid();
} }
return res.second; return res.second;
} }
@ -570,8 +573,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>(common().id_, common().access_hash_, return make_tl_object<telegram_api::inputDocumentFileLocation>(
BufferSlice(file_reference_)); common().id_, common().access_hash_, BufferSlice(FileReferenceView(file_reference_).download()));
} }
case LocationType::Web: case LocationType::Web:
case LocationType::None: case LocationType::None:
@ -586,7 +589,7 @@ class FullRemoteFileLocation {
CHECK(is_common()) << file << ' ' << line; CHECK(is_common()) << file << ' ' << line;
CHECK(is_document()) << file << ' ' << line; 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(file_reference_)); BufferSlice(FileReferenceView(file_reference_).upload()));
} }
#define as_input_photo() as_input_photo_impl(__FILE__, __LINE__) #define as_input_photo() as_input_photo_impl(__FILE__, __LINE__)
@ -625,7 +628,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_(std::move(file_reference)) , file_reference_(FileReferenceView::create_one(file_reference))
, variant_(CommonRemoteFileLocation{id, access_hash}) { , variant_(CommonRemoteFileLocation{id, access_hash}) {
CHECK(is_common()); CHECK(is_common());
FileReferenceView view(file_reference_); FileReferenceView view(file_reference_);
@ -694,7 +697,9 @@ 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()) {
string_builder << ", " << tag("file_reference", base64_encode(full_remote_file_location.file_reference_)); FileReferenceView view(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

@ -167,14 +167,15 @@ bool FileNode::delete_file_reference(Slice file_reference) {
return false; return false;
} }
if (remote_.full().delete_file_reference(file_reference)) { if (!remote_.full().delete_file_reference(file_reference)) {
VLOG(file_references) << "Do delete file reference of main file " << main_file_id_; return false;
upload_was_update_file_reference_ = false;
download_was_update_file_reference_ = false;
on_pmc_changed();
return true;
} }
return false;
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;
} }
void FileNode::set_generate_location(unique_ptr<FullGenerateFileLocation> &&generate) { void FileNode::set_generate_location(unique_ptr<FullGenerateFileLocation> &&generate) {