Skip to content

Commit

Permalink
Use filesystem::path internal UTF8 conversion.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 601880589
  • Loading branch information
ftsui authored and copybara-github committed Jan 26, 2024
1 parent 0591424 commit 1b91588
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 50 deletions.
1 change: 0 additions & 1 deletion sharing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ cc_library(
"//sharing/flags:nearby_sharing_feature_flags_cpp_consts_generated",
"//sharing/internal/api:platform",
"//sharing/internal/base",
"//sharing/internal/base:utf_utils",
"//sharing/internal/public:logging",
"//sharing/internal/public:nearby_context",
"//sharing/internal/public:types",
Expand Down
2 changes: 0 additions & 2 deletions sharing/internal/base/utf_string_conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#if defined(GITHUB_BUILD)
// Stub out string conversion functions for github builds.
namespace nearby::utils {
std::string WideToUtf8(std::wstring_view wide) { return std::string(); }
std::wstring Utf8ToWide(std::string_view utf8) { return std::wstring(); }

bool IsStringUtf8(std::string_view str) { return true; }

Expand Down
55 changes: 15 additions & 40 deletions sharing/nearby_connections_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
#include <utility>
#include <vector>

#include "sharing/internal/base/utf_string_conversions.h"
#include "internal/platform/file.h"
#include "sharing/common/compatible_u8_string.h"
#include "sharing/internal/public/logging.h"
#include "sharing/nearby_connections_types.h"

namespace nearby {
Expand All @@ -43,26 +45,12 @@ Payload ConvertToPayload(NcPayload payload) {
std::vector<uint8_t>(data.begin(), data.end()));
}
case NcPayloadType::kFile: {
std::filesystem::path file_path;
std::string parent_folder;
// Initialize path with UTF8 cause crash on Windows with configured
// locale.
try {
file_path = payload.AsFile()->GetFilePath();
if (!std::filesystem::exists(file_path)) {
file_path = utils::Utf8ToWide(payload.AsFile()->GetFilePath());
parent_folder = payload.GetParentFolder();
}
} catch (std::exception exception) {
file_path = utils::Utf8ToWide(payload.AsFile()->GetFilePath());
parent_folder = payload.GetParentFolder();
} catch (...) {
file_path = utils::Utf8ToWide(payload.AsFile()->GetFilePath());
parent_folder = payload.GetParentFolder();
}
std::filesystem::path file_path =
std::filesystem::u8path(payload.AsFile()->GetFilePath());
std::string parent_folder = payload.GetParentFolder();
NL_VLOG(1) << __func__ << ": Payload file_path=" << file_path
<< ", parent_folder = " << parent_folder;
return Payload(payload.GetId(), InputFile(file_path), parent_folder);
NEARBY_LOGS(VERBOSE) << __func__ << ": Payload file_path=" << file_path
<< ", parent_folder = " << parent_folder;
}
default:
return Payload();
Expand All @@ -72,28 +60,15 @@ Payload ConvertToPayload(NcPayload payload) {
NcPayload ConvertToServicePayload(Payload payload) {
switch (payload.content.type) {
case PayloadContent::Type::kFile: {
// On Windows, a crash may happen when access string() of path if it is
// using wchar. Apply UTF8 to avoid the cross-platform issues.
std::string file_path;
std::string file_name;
std::string parent_folder;
int64_t file_size = payload.content.file_payload.size;
try {
file_path =
utils::WideToUtf8(payload.content.file_payload.file.path.wstring());
file_name = utils::WideToUtf8(
payload.content.file_payload.file.path.filename().wstring());
} catch (std::exception e) {
file_path = payload.content.file_payload.file.path.string();
file_name = payload.content.file_payload.file.path.filename().string();
} catch (...) {
file_path = payload.content.file_payload.file.path.string();
file_name = payload.content.file_payload.file.path.filename().string();
}
parent_folder = payload.content.file_payload.parent_folder;
std::string file_path = GetCompatibleU8String(
payload.content.file_payload.file.path.u8string());
std::string file_name = GetCompatibleU8String(
payload.content.file_payload.file.path.filename().u8string());
std::string parent_folder = payload.content.file_payload.parent_folder;
std::replace(parent_folder.begin(), parent_folder.end(), '\\', '/');
NEARBY_LOGS(VERBOSE) << __func__ << ": NC Payload file_path=" << file_path
<< ", parent_folder = " << parent_folder;
NL_VLOG(1) << __func__ << ": NC Payload file_path=" << file_path
<< ", parent_folder = " << parent_folder;
nearby::InputFile input_file(file_path, file_size);
NcPayload nc_payload(payload.id, parent_folder, file_name,
std::move(input_file));
Expand Down
12 changes: 5 additions & 7 deletions sharing/nearby_sharing_service_extension.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "absl/synchronization/notification.h"
#include "internal/network/url.h"
#include "sharing/file_attachment.h"
#include "sharing/internal/base/utf_string_conversions.h"
#include "sharing/internal/public/logging.h"
#include "sharing/nearby_sharing_service.h"
#include "sharing/proto/wire_format.pb.h"
Expand Down Expand Up @@ -107,8 +106,7 @@ NearbySharingService::StatusCodes NearbySharingServiceExtension::Open(
NearbySharingService::StatusCodes status_codes = StatusCodes::kOk;
absl::Notification notification;
context_->GetShell().Open(
std::filesystem::path(
utils::Utf8ToWide(settings_->GetCustomSavePath())),
std::filesystem::u8path(settings_->GetCustomSavePath()),
[&status_codes, &notification](absl::Status status) {
if (!status.ok()) {
NL_LOG(ERROR)
Expand All @@ -129,10 +127,10 @@ NearbySharingService::StatusCodes NearbySharingServiceExtension::Open(
if (file_attachment.file_path().has_value()) {
file_path = *file_attachment.file_path();
} else {
file_path = std::filesystem::path(
utils::Utf8ToWide(settings_->GetCustomSavePath())) /
// NOLINTNEXTLINE cannot build without the new string creation
utils::Utf8ToWide(std::string(file_attachment.file_name()));
file_path =
std::filesystem::u8path(settings_->GetCustomSavePath()) /
// NOLINTNEXTLINE cannot build without the new string creation
std::filesystem::u8path(std::string(file_attachment.file_name()));
}

NearbySharingService::StatusCodes status_codes = StatusCodes::kOk;
Expand Down

0 comments on commit 1b91588

Please sign in to comment.