Fix for outgoing file names being truncated, and renaming input files should not occur, only output files.

PiperOrigin-RevId: 457857046
This commit is contained in:
jfcarroll
2022-06-28 17:41:29 -07:00
committed by Copybara-Service
parent 407222b104
commit bd9d01ec29
4 changed files with 39 additions and 38 deletions
+7 -36
View File
@@ -14,6 +14,7 @@
#include "connections/payload.h"
#include <algorithm>
#include <string>
namespace location {
@@ -21,46 +22,16 @@ namespace nearby {
namespace connections {
namespace {
std::string SepFinder(std::string s, size_t index) {
std::string filename = s.substr(index + 1, s.length() - index);
size_t lastindex = filename.find_last_of('.');
std::string rawname = filename.substr(0, lastindex);
return rawname;
}
std::string getFileName(const std::string& s) {
char forwardSep = '/';
char backwardSep = '\\';
std::string s_copy(s);
std::replace(s_copy.begin(), s_copy.end(), '\\',
'/'); // replace all '\\' to '/'
size_t lastForwardSepIndex = s.rfind(forwardSep, s.length());
size_t lastBackwardSepIndex = s.rfind(backwardSep, s.length());
size_t lastForwardSepIndex = s_copy.rfind('/', s.length());
if (lastForwardSepIndex == std::string::npos &&
lastBackwardSepIndex == std::string::npos) {
// no file name found
return ("");
}
// If we have a forward sep
if (lastForwardSepIndex != std::string::npos) {
// and if backward sep doesn't exist
if (lastBackwardSepIndex == std::string::npos) {
// Construct filename from forward sep
std::string rawname = SepFinder(s, lastForwardSepIndex);
return (rawname);
}
// backward sep also exists
if (lastForwardSepIndex > lastBackwardSepIndex) {
// the forward sep is the last
std::string rawname = SepFinder(s, lastForwardSepIndex);
return (rawname);
}
// The backward sep is the last
std::string rawname = SepFinder(s, lastBackwardSepIndex);
return (rawname);
}
std::string rawname = SepFinder(s, lastBackwardSepIndex);
return (rawname);
return s_copy.substr(lastForwardSepIndex + 1,
s_copy.length() - lastForwardSepIndex);
}
} // namespace
+23
View File
@@ -52,6 +52,7 @@ TEST(PayloadTest, SupportsFileType) {
Payload payload(payload_id, std::move(file));
payload.SetOffset(kOffset);
EXPECT_EQ(payload.GetFileName(), std::to_string(payload_id));
EXPECT_EQ(payload.GetType(), PayloadType::kFile);
EXPECT_EQ(payload.AsStream(), nullptr);
EXPECT_EQ(&payload.AsFile()->GetInputStream(), &stream);
@@ -59,6 +60,28 @@ TEST(PayloadTest, SupportsFileType) {
EXPECT_EQ(payload.GetOffset(), kOffset);
}
TEST(PayloadTest, SupportsMultiDotNamedFileType) {
constexpr char expected[] = "this.is.a.multidot.file";
InputFile file(expected, 0);
Payload payload(std::move(file));
EXPECT_EQ(payload.GetFileName(), expected);
}
TEST(PayloadTest,
SupportsBackSlashFolderSeparatorsByExtractingFileNameBeforeStoring) {
constexpr char file_name[] =
"test_folder.here\\this.is.a.multidot.backslash.folder.separated.file";
constexpr char expected[] =
"this.is.a.multidot.backslash.folder.separated.file";
InputFile file(file_name, 0);
Payload payload(std::move(file));
EXPECT_EQ(payload.GetFileName(), expected);
}
TEST(PayloadTest, SupportsStreamType) {
constexpr size_t kOffset = 1234456;
auto pipe = std::make_shared<Pipe>();
@@ -67,6 +67,8 @@ class ImplementationPlatform {
static std::string GetDownloadPath(std::string& parent_folder,
std::string& file_name);
static std::string GetDownloadPath(std::string& file_name);
static OSName GetCurrentOS();
// Atomics:
@@ -173,6 +173,11 @@ std::string ImplementationPlatform::GetDownloadPath(std::string& parent_folder,
GetDownloadPathInternal(parent_folder, file_name));
}
std::string ImplementationPlatform::GetDownloadPath(std::string& file_name) {
std::string fake_parent_path;
return GetDownloadPathInternal(fake_parent_path, file_name);
}
OSName ImplementationPlatform::GetCurrentOS() { return OSName::kWindows; }
std::unique_ptr<AtomicBoolean> ImplementationPlatform::CreateAtomicBoolean(
@@ -204,8 +209,8 @@ std::unique_ptr<InputFile> ImplementationPlatform::CreateInputFile(
PayloadId payload_id, std::int64_t total_size) {
std::string parent_folder("");
std::string file_name(std::to_string(payload_id));
return shared::IOFile::CreateInputFile(
GetDownloadPath(parent_folder, file_name), total_size);
return shared::IOFile::CreateInputFile(GetDownloadPath(file_name),
total_size);
}
std::unique_ptr<InputFile> ImplementationPlatform::CreateInputFile(