Skip to content

Commit fa83382

Browse files
alexkozytargos
authored andcommittedSep 25, 2018
src: added URL::FromFilePath method
Method returns file URL from native file path. Backport-PR-URL: #22918 PR-URL: #22251 Reviewed-By: Eugene Ostroukhov <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
1 parent e668815 commit fa83382

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed
 

‎src/node_url.cc

+13
Original file line numberDiff line numberDiff line change
@@ -2348,6 +2348,19 @@ std::string URL::ToFilePath() const {
23482348
#endif
23492349
}
23502350

2351+
URL URL::FromFilePath(const std::string& file_path) {
2352+
URL url("file://");
2353+
std::string escaped_file_path;
2354+
for (size_t i = 0; i < file_path.length(); ++i) {
2355+
escaped_file_path += file_path[i];
2356+
if (file_path[i] == '%')
2357+
escaped_file_path += "25";
2358+
}
2359+
URL::Parse(escaped_file_path.c_str(), escaped_file_path.length(), kPathStart,
2360+
&url.context_, true, nullptr, false);
2361+
return url;
2362+
}
2363+
23512364
// This function works by calling out to a JS function that creates and
23522365
// returns the JS URL object. Be mindful of the JS<->Native boundary
23532366
// crossing that is required.

‎src/node_url.h

+2
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ class URL {
169169
// Get the path of the file: URL in a format consumable by native file system
170170
// APIs. Returns an empty string if something went wrong.
171171
std::string ToFilePath() const;
172+
// Get the file URL from native file system path.
173+
static URL FromFilePath(const std::string& file_path);
172174

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

‎test/cctest/test_url.cc

+33
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,36 @@ TEST_F(URLTest, ToFilePath) {
109109

110110
#undef T
111111
}
112+
113+
TEST_F(URLTest, FromFilePath) {
114+
URL file_url;
115+
#ifdef _WIN32
116+
file_url = URL::FromFilePath("C:\\Program Files\\");
117+
EXPECT_EQ("file:", file_url.protocol());
118+
EXPECT_EQ("/C:/Program%20Files/", file_url.path());
119+
120+
file_url = URL::FromFilePath("C:\\a\\b\\c");
121+
EXPECT_EQ("file:", file_url.protocol());
122+
EXPECT_EQ("/C:/a/b/c", file_url.path());
123+
124+
file_url = URL::FromFilePath("b:\\a\\%%.js");
125+
EXPECT_EQ("file:", file_url.protocol());
126+
EXPECT_EQ("/b:/a/%25%25.js", file_url.path());
127+
128+
file_url = URL::FromFilePath("\\\\host\\a\\b\\c");
129+
EXPECT_EQ("file:", file_url.protocol());
130+
EXPECT_EQ("host/a/b/c", file_url.path());
131+
#else
132+
file_url = URL::FromFilePath("/");
133+
EXPECT_EQ("file:", file_url.protocol());
134+
EXPECT_EQ("/", file_url.path());
135+
136+
file_url = URL::FromFilePath("/a/b/c");
137+
EXPECT_EQ("file:", file_url.protocol());
138+
EXPECT_EQ("/a/b/c", file_url.path());
139+
140+
file_url = URL::FromFilePath("/a/%%.js");
141+
EXPECT_EQ("file:", file_url.protocol());
142+
EXPECT_EQ("/a/%25%25.js", file_url.path());
143+
#endif
144+
}

0 commit comments

Comments
 (0)
Please sign in to comment.