Skip to content

Commit

Permalink
src: added URL::FromFilePath method
Browse files Browse the repository at this point in the history
Method returns file URL from native file path.

Backport-PR-URL: #22918
PR-URL: #22251
Reviewed-By: Eugene Ostroukhov <eostroukhov@google.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
  • Loading branch information
alexkozy authored and targos committed Sep 25, 2018
1 parent e668815 commit fa83382
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2348,6 +2348,19 @@ std::string URL::ToFilePath() const {
#endif
}

URL URL::FromFilePath(const std::string& file_path) {
URL url("file://");
std::string escaped_file_path;
for (size_t i = 0; i < file_path.length(); ++i) {
escaped_file_path += file_path[i];
if (file_path[i] == '%')
escaped_file_path += "25";
}
URL::Parse(escaped_file_path.c_str(), escaped_file_path.length(), kPathStart,
&url.context_, true, nullptr, false);
return url;
}

// This function works by calling out to a JS function that creates and
// returns the JS URL object. Be mindful of the JS<->Native boundary
// crossing that is required.
Expand Down
2 changes: 2 additions & 0 deletions src/node_url.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ class URL {
// Get the path of the file: URL in a format consumable by native file system
// APIs. Returns an empty string if something went wrong.
std::string ToFilePath() const;
// Get the file URL from native file system path.
static URL FromFilePath(const std::string& file_path);

const Local<Value> ToObject(Environment* env) const;

Expand Down
33 changes: 33 additions & 0 deletions test/cctest/test_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,36 @@ TEST_F(URLTest, ToFilePath) {

#undef T
}

TEST_F(URLTest, FromFilePath) {
URL file_url;
#ifdef _WIN32
file_url = URL::FromFilePath("C:\\Program Files\\");
EXPECT_EQ("file:", file_url.protocol());
EXPECT_EQ("/C:/Program%20Files/", file_url.path());

file_url = URL::FromFilePath("C:\\a\\b\\c");
EXPECT_EQ("file:", file_url.protocol());
EXPECT_EQ("/C:/a/b/c", file_url.path());

file_url = URL::FromFilePath("b:\\a\\%%.js");
EXPECT_EQ("file:", file_url.protocol());
EXPECT_EQ("/b:/a/%25%25.js", file_url.path());

file_url = URL::FromFilePath("\\\\host\\a\\b\\c");
EXPECT_EQ("file:", file_url.protocol());
EXPECT_EQ("host/a/b/c", file_url.path());
#else
file_url = URL::FromFilePath("/");
EXPECT_EQ("file:", file_url.protocol());
EXPECT_EQ("/", file_url.path());

file_url = URL::FromFilePath("/a/b/c");
EXPECT_EQ("file:", file_url.protocol());
EXPECT_EQ("/a/b/c", file_url.path());

file_url = URL::FromFilePath("/a/%%.js");
EXPECT_EQ("file:", file_url.protocol());
EXPECT_EQ("/a/%25%25.js", file_url.path());
#endif
}

0 comments on commit fa83382

Please sign in to comment.