From 4762d1e3ee311537315dcd7f0b27b3ef2dd79a3a Mon Sep 17 00:00:00 2001 From: Aaron Yu Date: Tue, 6 Dec 2022 14:37:00 -0800 Subject: [PATCH] Add parent_folder support for custom save path PiperOrigin-RevId: 493415518 --- .../internal_payload_factory.cc | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/connections/implementation/internal_payload_factory.cc b/connections/implementation/internal_payload_factory.cc index 7500544f..f9d72a7d 100644 --- a/connections/implementation/internal_payload_factory.cc +++ b/connections/implementation/internal_payload_factory.cc @@ -315,23 +315,25 @@ std::unique_ptr CreateOutgoingInternalPayload( } } -std::string make_path(std::string& parent_folder, std::string& file_name) { - if (!parent_folder.empty()) { - return std::string(api::ImplementationPlatform::GetCustomSavePath( - parent_folder, file_name)); +// if custom_save_path is empty, default download path is used +std::string make_path(const std::string& custom_save_path, + std::string& parent_folder, std::string& file_name) { + if (!custom_save_path.empty()) { + std::string path = absl::StrCat(custom_save_path, "/", parent_folder); + return api::ImplementationPlatform::GetCustomSavePath(path, file_name); } - return std::string( - api::ImplementationPlatform::GetDownloadPath(parent_folder, file_name)); + return api::ImplementationPlatform::GetDownloadPath(parent_folder, file_name); } -std::string make_path(std::string& parent_folder, int64_t id) { +// if custom_save_path is empty, default download path is used +std::string make_path(const std::string& custom_save_path, + std::string& parent_folder, int64_t id) { std::string file_name(std::to_string(id)); - if (!parent_folder.empty()) { - return std::string(api::ImplementationPlatform::GetCustomSavePath( - parent_folder, file_name)); + if (!custom_save_path.empty()) { + std::string path = absl::StrCat(custom_save_path, "/", parent_folder); + return api::ImplementationPlatform::GetCustomSavePath(path, file_name); } - return std::string( - api::ImplementationPlatform::GetDownloadPath(parent_folder, file_name)); + return api::ImplementationPlatform::GetDownloadPath(parent_folder, file_name); } std::unique_ptr CreateIncomingInternalPayload( @@ -359,9 +361,9 @@ std::unique_ptr CreateIncomingInternalPayload( } case PayloadTransferFrame::PayloadHeader::FILE: { - std::string parent_folder(custom_save_path); - std::string file_name(""); - std::string file_path(""); + std::string parent_folder; + std::string file_name; + std::string file_path; int64_t total_size = 0; @@ -371,11 +373,13 @@ std::unique_ptr CreateIncomingInternalPayload( if (frame.payload_header().has_file_name()) { file_name = frame.payload_header().file_name(); - file_path = make_path(parent_folder, file_name); + // if custom_save_path is empty, default download path is used + file_path = make_path(custom_save_path, parent_folder, file_name); } else { if (frame.payload_header().has_id()) { file_name = std::to_string(frame.payload_header().id()); - file_path = make_path(parent_folder, file_name); + // if custom_save_path is empty, default download path is used + file_path = make_path(custom_save_path, parent_folder, file_name); } else { // This is an error condition, we don't have any way to generate a // file name for the output file.