Move saving bytes to file to FileLoaderUtils.

GitOrigin-RevId: 18734bb0d5172832119da4e658363f6eddaad986
This commit is contained in:
levlam 2018-08-13 19:30:52 +03:00
parent da438591f7
commit e27e4f405e
3 changed files with 43 additions and 35 deletions

View File

@ -7,13 +7,10 @@
#include "td/telegram/files/FileFromBytes.h"
#include "td/telegram/files/FileLoaderUtils.h"
#include "td/telegram/Global.h"
#include "td/utils/common.h"
#include "td/utils/misc.h"
#include <tuple>
namespace td {
FileFromBytes::FileFromBytes(FileType type, BufferSlice bytes, string name, std::unique_ptr<Callback> callback)
@ -21,30 +18,13 @@ FileFromBytes::FileFromBytes(FileType type, BufferSlice bytes, string name, std:
}
void FileFromBytes::wakeup() {
auto r_fd_path = open_temp_file(type_);
if (r_fd_path.is_error()) {
return callback_->on_error(r_fd_path.move_as_error());
int64 size = narrow_cast<int64>(bytes_.size());
auto r_result = save_file_bytes(type_, std::move(bytes_), std::move(name_));
if (r_result.is_error()) {
callback_->on_error(r_result.move_as_error());
} else {
callback_->on_ok(r_result.ok(), size);
}
FileFd fd;
string path;
std::tie(fd, path) = r_fd_path.move_as_ok();
auto r_size = fd.write(bytes_.as_slice());
if (r_size.is_error()) {
return callback_->on_error(r_size.move_as_error());
}
fd.close();
auto size = r_size.ok();
if (size != bytes_.size()) {
return callback_->on_error(Status::Error("Failed to write bytes to the file"));
}
auto dir = get_files_dir(type_);
auto r_perm_path = create_from_temp(path, dir, name_);
if (r_perm_path.is_error()) {
return callback_->on_error(r_perm_path.move_as_error());
}
callback_->on_ok(FullLocalFileLocation(type_, r_perm_path.move_as_ok(), 0), narrow_cast<int64>(bytes_.size()));
}
} // namespace td

View File

@ -56,7 +56,7 @@ StringBuilder &operator<<(StringBuilder &sb, const Ext &ext) {
}
} // namespace
Result<std::pair<FileFd, string>> open_temp_file(const FileType &file_type) {
Result<std::pair<FileFd, string>> open_temp_file(FileType file_type) {
auto pmc = G()->td_db()->get_binlog_pmc();
// TODO: CAS?
int32 file_id = to_integer<int32>(pmc->get("tmp_file_id"));
@ -138,6 +138,24 @@ Result<string> search_file(CSlice dir, CSlice name, int64 expected_size) {
return res;
}
Result<FullLocalFileLocation> save_file_bytes(FileType type, BufferSlice bytes, CSlice file_name) {
TRY_RESULT(fd_path, open_temp_file(type));
FileFd fd = std::move(fd_path.first);
string path = std::move(fd_path.second);
TRY_RESULT(size, fd.write(bytes.as_slice()));
fd.close();
if (size != bytes.size()) {
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));
return FullLocalFileLocation(type, std::move(perm_path), 0);
}
const char *file_type_name[file_type_size] = {"thumbnails", "profile_photos", "photos", "voice",
"videos", "documents", "secret", "temp",
"stickers", "music", "animations", "secret_thumbnails",
@ -155,13 +173,13 @@ string get_file_base_dir(const FileDirType &file_dir_type) {
}
}
string get_files_base_dir(const FileType &file_type) {
string get_files_base_dir(FileType file_type) {
return get_file_base_dir(get_file_dir_type(file_type));
}
string get_files_temp_dir(const FileType &file_type) {
string get_files_temp_dir(FileType file_type) {
return get_files_base_dir(file_type) + "temp" + TD_DIR_SLASH;
}
string get_files_dir(const FileType &file_type) {
string get_files_dir(FileType file_type) {
return get_files_base_dir(file_type) + file_type_name[static_cast<int32>(file_type)] + TD_DIR_SLASH;
}

View File

@ -6,6 +6,9 @@
//
#pragma once
#include "td/telegram/files/FileLocation.h"
#include "td/utils/buffer.h"
#include "td/utils/common.h"
#include "td/utils/port/FileFd.h"
#include "td/utils/Slice.h"
@ -14,12 +17,19 @@
#include <utility>
namespace td {
enum class FileType : int8;
Result<std::pair<FileFd, string>> open_temp_file(const 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> search_file(CSlice dir, CSlice name, int64 expected_size) TD_WARN_UNUSED_RESULT;
string get_files_base_dir(const FileType &file_type);
string get_files_temp_dir(const FileType &file_type);
string get_files_dir(const FileType &file_type);
Result<FullLocalFileLocation> save_file_bytes(FileType type, BufferSlice bytes, CSlice file_name);
string get_files_base_dir(FileType file_type);
string get_files_temp_dir(FileType file_type);
string get_files_dir(FileType file_type);
} // namespace td