From bdd70379f1c331d291c1dc1e569781707a1fd48f Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 14 Jun 2022 21:09:07 +0300 Subject: [PATCH] Simplify usage of create_from_temp and search_file. --- td/telegram/files/FileDownloader.cpp | 6 ++--- td/telegram/files/FileGenerateManager.cpp | 4 +--- td/telegram/files/FileLoaderUtils.cpp | 27 ++++++++++++----------- td/telegram/files/FileLoaderUtils.h | 6 ++--- 4 files changed, 20 insertions(+), 23 deletions(-) diff --git a/td/telegram/files/FileDownloader.cpp b/td/telegram/files/FileDownloader.cpp index 1a416a84b..967e6dc3e 100644 --- a/td/telegram/files/FileDownloader.cpp +++ b/td/telegram/files/FileDownloader.cpp @@ -89,7 +89,7 @@ Result FileDownloader::init() { } if (search_file_ && fd_.empty() && size_ > 0 && encryption_key_.empty() && !remote_.is_web()) { [&] { - TRY_RESULT(path, search_file(get_files_dir(remote_.file_type_), name_, size_)); + TRY_RESULT(path, search_file(remote_.file_type_, name_, size_)); TRY_RESULT(fd, FileFd::open(path, FileFd::Read)); LOG(INFO) << "Check hash of local file " << path; path_ = std::move(path); @@ -121,8 +121,6 @@ Result FileDownloader::init() { } Status FileDownloader::on_ok(int64 size) { - auto dir = get_files_dir(remote_.file_type_); - std::string path; fd_.close(); if (encryption_key_.is_secure()) { @@ -138,7 +136,7 @@ Status FileDownloader::on_ok(int64 size) { if (only_check_) { path = path_; } else { - TRY_RESULT_ASSIGN(path, create_from_temp(path_, dir, name_)); + TRY_RESULT_ASSIGN(path, create_from_temp(remote_.file_type_, path_, name_)); } callback_->on_ok(FullLocalFileLocation(remote_.file_type_, std::move(path), 0), size, !only_check_); return Status::OK(); diff --git a/td/telegram/files/FileGenerateManager.cpp b/td/telegram/files/FileGenerateManager.cpp index c7651d08e..57e18a866 100644 --- a/td/telegram/files/FileGenerateManager.cpp +++ b/td/telegram/files/FileGenerateManager.cpp @@ -331,9 +331,7 @@ class FileExternalGenerateActor final : public FileGenerateActor { } Status do_file_generate_finish() { - auto dir = get_files_dir(generate_location_.file_type_); - - TRY_RESULT(perm_path, create_from_temp(path_, dir, name_)); + TRY_RESULT(perm_path, create_from_temp(generate_location_.file_type_, path_, name_)); callback_->on_ok(FullLocalFileLocation(generate_location_.file_type_, std::move(perm_path), 0)); callback_.reset(); stop(); diff --git a/td/telegram/files/FileLoaderUtils.cpp b/td/telegram/files/FileLoaderUtils.cpp index f2d73fb25..1e67762c7 100644 --- a/td/telegram/files/FileLoaderUtils.cpp +++ b/td/telegram/files/FileLoaderUtils.cpp @@ -104,9 +104,10 @@ bool for_suggested_file_name(CSlice name, bool use_pmc, bool use_random, F &&cal return active; } -Result create_from_temp(CSlice temp_path, CSlice dir, CSlice name) { - LOG(INFO) << "Create file in directory " << dir << " with suggested name " << name << " from temporary file " - << temp_path; +Result create_from_temp(FileType file_type, CSlice temp_path, CSlice name) { + auto dir = get_files_dir(file_type); + LOG(INFO) << "Create file of type " << file_type << " in directory " << dir << " with suggested name " << name + << " from temporary file " << temp_path; Result> res = Status::Error(500, "Can't find suitable file name"); for_suggested_file_name(name, true, true, [&](CSlice suggested_name) { res = try_create_new_file(PSLICE() << dir << suggested_name); @@ -119,15 +120,16 @@ Result create_from_temp(CSlice temp_path, CSlice dir, CSlice name) { return perm_path; } -Result search_file(CSlice dir, CSlice name, int64 expected_size) { - Result res = Status::Error(500, "Can't find suitable file name"); +Result search_file(FileType file_type, CSlice name, int64 expected_size) { + Result res = Status::Error(500, "Can't find suitable file name"); + auto dir = get_files_dir(file_type); for_suggested_file_name(name, false, false, [&](CSlice suggested_name) { auto r_pair = try_open_file(PSLICE() << dir << suggested_name); if (r_pair.is_error()) { return false; } FileFd fd; - std::string path; + string path; std::tie(fd, path) = r_pair.move_as_ok(); auto r_size = fd.get_size(); if (r_size.is_error() || r_size.ok() != expected_size) { @@ -185,17 +187,17 @@ Result get_suggested_file_name(CSlice directory, Slice file_name) { return PSTRING() << stem << " - " << StringBuilder::FixedDouble(Clocks::system(), 3) << Ext{ext}; } -Result save_file_bytes(FileType type, BufferSlice bytes, CSlice file_name) { - auto r_old_path = search_file(get_files_dir(type), file_name, bytes.size()); +Result save_file_bytes(FileType file_type, BufferSlice bytes, CSlice file_name) { + auto r_old_path = search_file(file_type, file_name, bytes.size()); if (r_old_path.is_ok()) { auto r_old_bytes = read_file(r_old_path.ok()); if (r_old_bytes.is_ok() && r_old_bytes.ok().as_slice() == bytes.as_slice()) { LOG(INFO) << "Found previous file with the same name " << r_old_path.ok(); - return FullLocalFileLocation(type, r_old_path.ok(), 0); + return FullLocalFileLocation(file_type, r_old_path.ok(), 0); } } - TRY_RESULT(fd_path, open_temp_file(type)); + TRY_RESULT(fd_path, open_temp_file(file_type)); FileFd fd = std::move(fd_path.first); string path = std::move(fd_path.second); @@ -206,10 +208,9 @@ Result save_file_bytes(FileType type, BufferSlice bytes, return Status::Error("Failed to write bytes to the file"); } - auto dir = get_files_dir(type); - TRY_RESULT(perm_path, create_from_temp(path, dir, file_name)); + TRY_RESULT(perm_path, create_from_temp(file_type, path, file_name)); - return FullLocalFileLocation(type, std::move(perm_path), 0); + return FullLocalFileLocation(file_type, std::move(perm_path), 0); } static Slice get_file_base_dir(const FileDirType &file_dir_type) { diff --git a/td/telegram/files/FileLoaderUtils.h b/td/telegram/files/FileLoaderUtils.h index 97228c553..5fb1c1e02 100644 --- a/td/telegram/files/FileLoaderUtils.h +++ b/td/telegram/files/FileLoaderUtils.h @@ -24,13 +24,13 @@ extern int VERBOSITY_NAME(file_loader); Result> open_temp_file(FileType file_type) TD_WARN_UNUSED_RESULT; -Result create_from_temp(CSlice temp_path, CSlice dir, CSlice name) TD_WARN_UNUSED_RESULT; +Result create_from_temp(FileType file_type, CSlice temp_path, CSlice name) TD_WARN_UNUSED_RESULT; -Result search_file(CSlice dir, CSlice name, int64 expected_size) TD_WARN_UNUSED_RESULT; +Result search_file(FileType type, CSlice name, int64 expected_size) TD_WARN_UNUSED_RESULT; Result get_suggested_file_name(CSlice dir, Slice file_name) TD_WARN_UNUSED_RESULT; -Result save_file_bytes(FileType type, BufferSlice bytes, CSlice file_name); +Result save_file_bytes(FileType file_type, BufferSlice bytes, CSlice file_name); Slice get_files_base_dir(FileType file_type);