Simplify usage of create_from_temp and search_file.
This commit is contained in:
parent
dcb77ce187
commit
bdd70379f1
@ -89,7 +89,7 @@ Result<FileLoader::FileInfo> FileDownloader::init() {
|
|||||||
}
|
}
|
||||||
if (search_file_ && fd_.empty() && size_ > 0 && encryption_key_.empty() && !remote_.is_web()) {
|
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));
|
TRY_RESULT(fd, FileFd::open(path, FileFd::Read));
|
||||||
LOG(INFO) << "Check hash of local file " << path;
|
LOG(INFO) << "Check hash of local file " << path;
|
||||||
path_ = std::move(path);
|
path_ = std::move(path);
|
||||||
@ -121,8 +121,6 @@ Result<FileLoader::FileInfo> FileDownloader::init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Status FileDownloader::on_ok(int64 size) {
|
Status FileDownloader::on_ok(int64 size) {
|
||||||
auto dir = get_files_dir(remote_.file_type_);
|
|
||||||
|
|
||||||
std::string path;
|
std::string path;
|
||||||
fd_.close();
|
fd_.close();
|
||||||
if (encryption_key_.is_secure()) {
|
if (encryption_key_.is_secure()) {
|
||||||
@ -138,7 +136,7 @@ Status FileDownloader::on_ok(int64 size) {
|
|||||||
if (only_check_) {
|
if (only_check_) {
|
||||||
path = path_;
|
path = path_;
|
||||||
} else {
|
} 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_);
|
callback_->on_ok(FullLocalFileLocation(remote_.file_type_, std::move(path), 0), size, !only_check_);
|
||||||
return Status::OK();
|
return Status::OK();
|
||||||
|
@ -331,9 +331,7 @@ class FileExternalGenerateActor final : public FileGenerateActor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Status do_file_generate_finish() {
|
Status do_file_generate_finish() {
|
||||||
auto dir = get_files_dir(generate_location_.file_type_);
|
TRY_RESULT(perm_path, create_from_temp(generate_location_.file_type_, path_, name_));
|
||||||
|
|
||||||
TRY_RESULT(perm_path, create_from_temp(path_, dir, name_));
|
|
||||||
callback_->on_ok(FullLocalFileLocation(generate_location_.file_type_, std::move(perm_path), 0));
|
callback_->on_ok(FullLocalFileLocation(generate_location_.file_type_, std::move(perm_path), 0));
|
||||||
callback_.reset();
|
callback_.reset();
|
||||||
stop();
|
stop();
|
||||||
|
@ -104,9 +104,10 @@ bool for_suggested_file_name(CSlice name, bool use_pmc, bool use_random, F &&cal
|
|||||||
return active;
|
return active;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<string> create_from_temp(CSlice temp_path, CSlice dir, CSlice name) {
|
Result<string> create_from_temp(FileType file_type, CSlice temp_path, CSlice name) {
|
||||||
LOG(INFO) << "Create file in directory " << dir << " with suggested name " << name << " from temporary file "
|
auto dir = get_files_dir(file_type);
|
||||||
<< temp_path;
|
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");
|
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) {
|
for_suggested_file_name(name, true, true, [&](CSlice suggested_name) {
|
||||||
res = try_create_new_file(PSLICE() << dir << 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;
|
return perm_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<string> search_file(CSlice dir, CSlice name, int64 expected_size) {
|
Result<string> search_file(FileType file_type, CSlice name, int64 expected_size) {
|
||||||
Result<std::string> res = Status::Error(500, "Can't find suitable file name");
|
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) {
|
for_suggested_file_name(name, false, false, [&](CSlice suggested_name) {
|
||||||
auto r_pair = try_open_file(PSLICE() << dir << suggested_name);
|
auto r_pair = try_open_file(PSLICE() << dir << suggested_name);
|
||||||
if (r_pair.is_error()) {
|
if (r_pair.is_error()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
FileFd fd;
|
FileFd fd;
|
||||||
std::string path;
|
string path;
|
||||||
std::tie(fd, path) = r_pair.move_as_ok();
|
std::tie(fd, path) = r_pair.move_as_ok();
|
||||||
auto r_size = fd.get_size();
|
auto r_size = fd.get_size();
|
||||||
if (r_size.is_error() || r_size.ok() != expected_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};
|
return PSTRING() << stem << " - " << StringBuilder::FixedDouble(Clocks::system(), 3) << Ext{ext};
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
auto r_old_path = search_file(get_files_dir(type), file_name, bytes.size());
|
auto r_old_path = search_file(file_type, file_name, bytes.size());
|
||||||
if (r_old_path.is_ok()) {
|
if (r_old_path.is_ok()) {
|
||||||
auto r_old_bytes = read_file(r_old_path.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()) {
|
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();
|
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);
|
FileFd fd = std::move(fd_path.first);
|
||||||
string path = std::move(fd_path.second);
|
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");
|
return Status::Error("Failed to write bytes to the file");
|
||||||
}
|
}
|
||||||
|
|
||||||
auto dir = get_files_dir(type);
|
TRY_RESULT(perm_path, create_from_temp(file_type, path, file_name));
|
||||||
TRY_RESULT(perm_path, create_from_temp(path, dir, 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) {
|
static Slice get_file_base_dir(const FileDirType &file_dir_type) {
|
||||||
|
@ -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<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<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);
|
Slice get_files_base_dir(FileType file_type);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user