Use get_url_file_name whenever appropriate.

GitOrigin-RevId: 4a1f408424883c56d2017f4239cae01987bd39c0
This commit is contained in:
levlam 2018-02-20 02:29:19 +03:00
parent 824735dc8e
commit b676810b50
7 changed files with 42 additions and 30 deletions

View File

@ -1463,9 +1463,7 @@ void InlineQueriesManager::on_get_inline_query_results(UserId bot_user_id, uint6
continue; continue;
} }
auto file_id = r_file_id.move_as_ok(); auto file_id = r_file_id.move_as_ok();
auto file_name = get_url_query_file_name(http_url.query_);
auto url_path = http_url.query_.substr(0, http_url.query_.find_first_of("?#"));
auto file_name = PathView(url_path).file_name().str();
PhotoSize thumbnail; PhotoSize thumbnail;
if (result->thumb_url_.find('.') != string::npos) { if (result->thumb_url_.find('.') != string::npos) {

View File

@ -14242,12 +14242,10 @@ tl_object_ptr<telegram_api::inputWebDocument> MessagesManager::get_input_web_doc
auto file_view = td_->file_manager_->get_file_view(size.file_id); auto file_view = td_->file_manager_->get_file_view(size.file_id);
CHECK(file_view.has_url()); CHECK(file_view.has_url());
// TODO right MIME type auto file_name = get_url_file_name(file_view.url());
// auto url_path = http_url.query_.substr(0, http_url.query_.find_first_of("?#")); return make_tl_object<telegram_api::inputWebDocument>(
// auto file_name = PathView(url_path).file_name().str(); file_view.url(), size.size, MimeType::from_extension(PathView(file_name).extension(), "image/jpeg"),
std::move(attributes));
return make_tl_object<telegram_api::inputWebDocument>(file_view.url(), size.size, "image/jpeg",
std::move(attributes));
} }
tl_object_ptr<telegram_api::inputMediaInvoice> MessagesManager::get_input_media_invoice( tl_object_ptr<telegram_api::inputMediaInvoice> MessagesManager::get_input_media_invoice(
@ -15334,7 +15332,8 @@ Result<MessagesManager::InputMessageContent> MessagesManager::process_input_mess
file_id = r_file_id.ok(); file_id = r_file_id.ok();
CHECK(file_id.is_valid()); CHECK(file_id.is_valid());
file_view = td_->file_manager_->get_file_view(file_id); file_view = td_->file_manager_->get_file_view(file_id);
const PathView path_view(file_view.suggested_name()); auto suggested_name = file_view.suggested_name();
const PathView path_view(suggested_name);
file_name = path_view.file_name().str(); file_name = path_view.file_name().str();
mime_type = MimeType::from_extension(path_view.extension()); mime_type = MimeType::from_extension(path_view.extension());
} }

View File

@ -232,24 +232,22 @@ void FileNode::on_info_flushed() {
info_changed_flag_ = false; info_changed_flag_ = false;
} }
CSlice FileNode::suggested_name() const { string FileNode::suggested_name() const {
if (!remote_name_.empty()) { if (!remote_name_.empty()) {
return remote_name_; return remote_name_;
} }
CSlice local_name(local_.file_name());
if (!local_name.empty()) {
return local_name;
}
if (!url_.empty()) { if (!url_.empty()) {
return url_; auto file_name = get_url_file_name(url_);
} if (!file_name.empty()) {
if (generate_ != nullptr) { return file_name;
CSlice generate_name(generate_->original_path_);
if (!generate_name.empty()) {
return generate_name;
} }
} }
return CSlice(); if (generate_ != nullptr) {
if (!generate_->original_path_.empty()) {
return generate_->original_path_;
}
}
return local_.file_name().str();
} }
/*** FileView ***/ /*** FileView ***/
@ -360,7 +358,7 @@ const string &FileView::remote_name() const {
return node_->remote_name_; return node_->remote_name_;
} }
CSlice FileView::suggested_name() const { string FileView::suggested_name() const {
return node_->suggested_name(); return node_->suggested_name();
} }
@ -1302,7 +1300,7 @@ bool FileManager::set_content(FileId file_id, BufferSlice bytes) {
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().file_type_, std::move(bytes), send_closure(file_load_manager_, &FileLoadManager::from_bytes, id, node->remote_.full().file_type_, std::move(bytes),
node->suggested_name().str()); node->suggested_name());
return true; return true;
} }
@ -1452,7 +1450,7 @@ void FileManager::run_download(FileNodePtr node) {
node->download_id_ = id; node->download_id_ = id;
node->is_download_started_ = false; node->is_download_started_ = false;
send_closure(file_load_manager_, &FileLoadManager::download, id, node->remote_.full(), node->local_, node->size_, send_closure(file_load_manager_, &FileLoadManager::download, id, node->remote_.full(), node->local_, node->size_,
node->suggested_name().str(), node->encryption_key_, priority); node->suggested_name(), node->encryption_key_, priority);
} }
void FileManager::resume_upload(FileId file_id, std::vector<int> bad_parts, std::shared_ptr<UploadCallback> callback, void FileManager::resume_upload(FileId file_id, std::vector<int> bad_parts, std::shared_ptr<UploadCallback> callback,
@ -1595,7 +1593,7 @@ void FileManager::run_generate(FileNodePtr node) {
QueryId id = queries_container_.create(Query{file_id, Query::Generate}); QueryId id = queries_container_.create(Query{file_id, Query::Generate});
node->generate_id_ = id; node->generate_id_ = id;
send_closure(file_generate_manager_, &FileGenerateManager::generate_file, id, *node->generate_, node->local_, send_closure(file_generate_manager_, &FileGenerateManager::generate_file, id, *node->generate_, node->local_,
node->suggested_name().str(), [file_manager = this, id] { node->suggested_name(), [file_manager = this, id] {
class Callback : public FileGenerateCallback { class Callback : public FileGenerateCallback {
ActorId<FileManager> actor_; ActorId<FileManager> actor_;
uint64 query_id_; uint64 query_id_;
@ -2082,7 +2080,7 @@ void FileManager::on_upload_ok(QueryId query_id, FileType file_type, const Parti
file_info->download_priority_ = 0; file_info->download_priority_ = 0;
FileView file_view(file_node); FileView file_view(file_node);
string file_name = get_file_name(file_type, file_view.has_local_location() ? file_view.local_location().path_ : ""); string file_name = get_file_name(file_type, file_view.suggested_name());
if (file_view.is_encrypted()) { if (file_view.is_encrypted()) {
tl_object_ptr<telegram_api::InputEncryptedFile> input_file; tl_object_ptr<telegram_api::InputEncryptedFile> input_file;

View File

@ -73,7 +73,7 @@ class FileNode {
void on_pmc_flushed(); void on_pmc_flushed();
void on_info_flushed(); void on_info_flushed();
CSlice suggested_name() const; string suggested_name() const;
private: private:
friend class FileView; friend class FileView;
@ -183,7 +183,7 @@ class FileView {
const string &remote_name() const; const string &remote_name() const;
CSlice suggested_name() const; string suggested_name() const;
DialogId owner_dialog_id() const; DialogId owner_dialog_id() const;

View File

@ -175,4 +175,15 @@ string get_url_query_file_name(const string &query) {
return query_slice.str(); return query_slice.str();
} }
string get_url_file_name(const string &url) {
// TODO remove copy
string url_copy = url;
auto r_http_url = parse_url(url_copy);
if (r_http_url.is_error()) {
LOG(WARNING) << "Receive wrong URL \"" << url << '"';
return string();
}
return get_url_query_file_name(r_http_url.ok().query_);
}
} // namespace td } // namespace td

View File

@ -34,4 +34,6 @@ StringBuilder &operator<<(StringBuilder &sb, const HttpUrl &url);
string get_url_query_file_name(const string &query); string get_url_query_file_name(const string &query);
string get_url_file_name(const string &url);
} // namespace td } // namespace td

View File

@ -243,7 +243,11 @@ TEST(Misc, to_double) {
} }
static void test_get_url_query_file_name_one(const char *prefix, const char *suffix, const char *file_name) { static void test_get_url_query_file_name_one(const char *prefix, const char *suffix, const char *file_name) {
ASSERT_STREQ(file_name, get_url_query_file_name(string(prefix) + string(file_name) + string(suffix))); auto path = string(prefix) + string(file_name) + string(suffix);
ASSERT_STREQ(file_name, get_url_query_file_name(path));
ASSERT_STREQ(file_name, get_url_file_name("http://telegram.org" + path));
ASSERT_STREQ(file_name, get_url_file_name("http://telegram.org:80" + path));
ASSERT_STREQ(file_name, get_url_file_name("telegram.org" + path));
} }
TEST(Misc, get_url_query_file_name) { TEST(Misc, get_url_query_file_name) {