Simplify usage of create_from_temp and search_file.

This commit is contained in:
levlam 2022-06-14 21:09:07 +03:00
parent dcb77ce187
commit bdd70379f1
4 changed files with 20 additions and 23 deletions

View File

@ -89,7 +89,7 @@ Result<FileLoader::FileInfo> 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<FileLoader::FileInfo> 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();

View File

@ -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();

View File

@ -104,9 +104,10 @@ bool for_suggested_file_name(CSlice name, bool use_pmc, bool use_random, F &&cal
return active;
}
Result<string> 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<string> 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<std::pair<FileFd, string>> 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<string> create_from_temp(CSlice temp_path, CSlice dir, CSlice name) {
return perm_path;
}
Result<string> search_file(CSlice dir, CSlice name, int64 expected_size) {
Result<std::string> res = Status::Error(500, "Can't find suitable file name");
Result<string> search_file(FileType file_type, CSlice name, int64 expected_size) {
Result<string> 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<string> get_suggested_file_name(CSlice directory, Slice file_name) {
return PSTRING() << stem << " - " << StringBuilder::FixedDouble(Clocks::system(), 3) << Ext{ext};
}
Result<FullLocalFileLocation> 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<FullLocalFileLocation> 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<FullLocalFileLocation> 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) {

View File

@ -24,13 +24,13 @@ extern int VERBOSITY_NAME(file_loader);
Result<std::pair<FileFd, string>> open_temp_file(FileType file_type) TD_WARN_UNUSED_RESULT;
Result<string> create_from_temp(CSlice temp_path, CSlice dir, CSlice name) TD_WARN_UNUSED_RESULT;
Result<string> create_from_temp(FileType file_type, CSlice temp_path, CSlice name) TD_WARN_UNUSED_RESULT;
Result<string> search_file(CSlice dir, CSlice name, int64 expected_size) TD_WARN_UNUSED_RESULT;
Result<string> search_file(FileType type, CSlice name, int64 expected_size) TD_WARN_UNUSED_RESULT;
Result<string> get_suggested_file_name(CSlice dir, Slice file_name) TD_WARN_UNUSED_RESULT;
Result<FullLocalFileLocation> save_file_bytes(FileType type, BufferSlice bytes, CSlice file_name);
Result<FullLocalFileLocation> save_file_bytes(FileType file_type, BufferSlice bytes, CSlice file_name);
Slice get_files_base_dir(FileType file_type);