From e27e4f405e6eefa35a9b0684ad3d2c5ed07b61cd Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 13 Aug 2018 19:30:52 +0300 Subject: [PATCH] Move saving bytes to file to FileLoaderUtils. GitOrigin-RevId: 18734bb0d5172832119da4e658363f6eddaad986 --- td/telegram/files/FileFromBytes.cpp | 32 +++++---------------------- td/telegram/files/FileLoaderUtils.cpp | 26 ++++++++++++++++++---- td/telegram/files/FileLoaderUtils.h | 20 ++++++++++++----- 3 files changed, 43 insertions(+), 35 deletions(-) diff --git a/td/telegram/files/FileFromBytes.cpp b/td/telegram/files/FileFromBytes.cpp index 939803d8a..0dcebc6e8 100644 --- a/td/telegram/files/FileFromBytes.cpp +++ b/td/telegram/files/FileFromBytes.cpp @@ -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 - namespace td { FileFromBytes::FileFromBytes(FileType type, BufferSlice bytes, string name, std::unique_ptr 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(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(bytes_.size())); } } // namespace td diff --git a/td/telegram/files/FileLoaderUtils.cpp b/td/telegram/files/FileLoaderUtils.cpp index edad3c639..87cd30a76 100644 --- a/td/telegram/files/FileLoaderUtils.cpp +++ b/td/telegram/files/FileLoaderUtils.cpp @@ -56,7 +56,7 @@ StringBuilder &operator<<(StringBuilder &sb, const Ext &ext) { } } // namespace -Result> open_temp_file(const FileType &file_type) { +Result> open_temp_file(FileType file_type) { auto pmc = G()->td_db()->get_binlog_pmc(); // TODO: CAS? int32 file_id = to_integer(pmc->get("tmp_file_id")); @@ -138,6 +138,24 @@ Result search_file(CSlice dir, CSlice name, int64 expected_size) { return res; } +Result 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(file_type)] + TD_DIR_SLASH; } diff --git a/td/telegram/files/FileLoaderUtils.h b/td/telegram/files/FileLoaderUtils.h index 7eaf1e12e..efbd721f0 100644 --- a/td/telegram/files/FileLoaderUtils.h +++ b/td/telegram/files/FileLoaderUtils.h @@ -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 namespace td { -enum class FileType : int8; -Result> open_temp_file(const FileType &file_type) TD_WARN_UNUSED_RESULT; +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 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 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