Fix suggested name for generate, local and URL files.

GitOrigin-RevId: 2f203b2964f4b7da08346a7147e51af027baa2d2
This commit is contained in:
levlam 2018-02-20 01:28:06 +03:00
parent d3c980e5d3
commit d475864012
5 changed files with 52 additions and 13 deletions

View File

@ -15334,15 +15334,9 @@ 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);
if (file_view.has_local_location()) { const PathView path_view(file_view.suggested_name());
const PathView path_view(file_view.local_location().path_);
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());
} else if (file_view.has_generate_location()) {
const PathView path_view(file_view.generate_location().original_path_);
file_name = path_view.file_name().str();
mime_type = MimeType::from_extension(path_view.extension());
}
} }
FileId thumbnail_file_id; FileId thumbnail_file_id;

View File

@ -903,6 +903,18 @@ class LocalFileLocation {
return variant_.get<2>(); return variant_.get<2>();
} }
CSlice file_name() const {
switch (type()) {
case Type::Partial:
return partial().path_;
case Type::Full:
return full().path_;
case Type::Empty:
default:
return CSlice();
}
}
template <class StorerT> template <class StorerT>
void store(StorerT &storer) const { void store(StorerT &storer) const {
using td::store; using td::store;

View File

@ -35,21 +35,26 @@
namespace td { namespace td {
static int VERBOSITY_NAME(update_file) = VERBOSITY_NAME(INFO); static int VERBOSITY_NAME(update_file) = VERBOSITY_NAME(INFO);
FileNode *FileNodePtr::operator->() const { FileNode *FileNodePtr::operator->() const {
return get(); return get();
} }
FileNode &FileNodePtr::operator*() const { FileNode &FileNodePtr::operator*() const {
return *get(); return *get();
} }
FileNode *FileNodePtr::get() const { FileNode *FileNodePtr::get() const {
auto res = get_unsafe(); auto res = get_unsafe();
CHECK(res); CHECK(res);
return res; return res;
} }
FileNode *FileNodePtr::get_unsafe() const { FileNode *FileNodePtr::get_unsafe() const {
CHECK(file_manager_ != nullptr); CHECK(file_manager_ != nullptr);
return file_manager_->get_file_node_raw(file_id_); return file_manager_->get_file_node_raw(file_id_);
} }
FileNodePtr::operator bool() const { FileNodePtr::operator bool() const {
return file_manager_ != nullptr && get_unsafe() != nullptr; return file_manager_ != nullptr && get_unsafe() != nullptr;
} }
@ -227,6 +232,26 @@ void FileNode::on_info_flushed() {
info_changed_flag_ = false; info_changed_flag_ = false;
} }
CSlice FileNode::suggested_name() const {
if (!remote_name_.empty()) {
return remote_name_;
}
CSlice local_name(local_.file_name());
if (!local_name.empty()) {
return local_name;
}
if (!url_.empty()) {
return url_;
}
if (generate_ != nullptr) {
CSlice generate_name(generate_->original_path_);
if (!generate_name.empty()) {
return generate_name;
}
}
return CSlice();
}
/*** 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;
@ -335,6 +360,10 @@ const string &FileView::remote_name() const {
return node_->remote_name_; return node_->remote_name_;
} }
CSlice FileView::suggested_name() const {
return node_->suggested_name();
}
DialogId FileView::owner_dialog_id() const { DialogId FileView::owner_dialog_id() const {
return node_->owner_dialog_id_; return node_->owner_dialog_id_;
} }
@ -1273,7 +1302,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->remote_name_); node->suggested_name().str());
return true; return true;
} }
@ -1423,7 +1452,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->remote_name_, node->encryption_key_, priority); node->suggested_name().str(), 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,
@ -1566,7 +1595,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->remote_name_, [file_manager = this, id] { node->suggested_name().str(), [file_manager = this, id] {
class Callback : public FileGenerateCallback { class Callback : public FileGenerateCallback {
ActorId<FileManager> actor_; ActorId<FileManager> actor_;
uint64 query_id_; uint64 query_id_;

View File

@ -73,6 +73,8 @@ class FileNode {
void on_pmc_flushed(); void on_pmc_flushed();
void on_info_flushed(); void on_info_flushed();
CSlice suggested_name() const;
private: private:
friend class FileView; friend class FileView;
friend class FileManager; friend class FileManager;
@ -181,6 +183,8 @@ class FileView {
const string &remote_name() const; const string &remote_name() const;
CSlice suggested_name() const;
DialogId owner_dialog_id() const; DialogId owner_dialog_id() const;
bool get_by_hash() const; bool get_by_hash() const;

View File

@ -28,7 +28,7 @@ void FileManager::store_file(FileId file_id, StorerT &storer, int32 ttl) const {
file_store_type = FileStoreType::Remote; file_store_type = FileStoreType::Remote;
} else if (file_view.has_local_location()) { } else if (file_view.has_local_location()) {
file_store_type = FileStoreType::Local; file_store_type = FileStoreType::Local;
} else if (!file_view.url().empty()) { } else if (file_view.has_url()) {
file_store_type = FileStoreType::Url; file_store_type = FileStoreType::Url;
} else if (file_view.has_generate_location()) { } else if (file_view.has_generate_location()) {
file_store_type = FileStoreType::Generate; file_store_type = FileStoreType::Generate;