file-uri-to-path
file-uri-to-path copied to clipboard
File uri containing percent encoded character doesn't seem to convert properly.
If the file uri has a percentage in it, it seems to not convert it properly.
Such as file:///c%3A/Users/Admin/Desktop/Github_Stuff/Discord_Bots/StratoBotElite/branches/master/src/bot/commands/Logging/user-join.js
will return /c%3A/Users/Admin/Desktop/Github_Stuff/Discord_Bots/StratoBotElite/branches/master/src/bot/commands/Logging/user-join.js
while file:///c:/Users/Admin/Desktop/Github_Stuff/Discord_Bots/StratoBotElite/branches/master/src/bot/commands/Logging/user-join.js
will return c:/Users/Admin/Desktop/Github_Stuff/Discord_Bots/StratoBotElite/branches/master/src/bot/commands/Logging/user-join.js
As of year 2021 the error is still there
URI : file:///d%3A/MAO/repos/Petrel-XML-data/test.xml
converted to : d:\\d%3A\\MAO\\repos\\Petrel-XML-data\\test.xml
expected: d:\\MAO\\repos\\Petrel-XML-data\\test.xml
Good news is that node version 10 has a method to do this
import { fileURLToPath, URL } from 'url';
public async update(txtDoc: TextDocument) {
const url = new URL(txtDoc.uri);
const filePath = fileURLToPath(url); //<< convert URI to file path
const fileStream = fs.createReadStream(filePath);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
.....
}
More details https://nodejs.org/api/url.html#url_url_fileurltopath_url
@mauriciogracia that is amazing, thanks for sharing, in case anyone needs the reverse conversion too, pathToFileURL
exists as well.