file-uri-to-path icon indicating copy to clipboard operation
file-uri-to-path copied to clipboard

File uri containing percent encoded character doesn't seem to convert properly.

Open BannerBomb opened this issue 3 years ago • 2 comments

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

BannerBomb avatar Nov 24 '20 20:11 BannerBomb

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 avatar Apr 19 '21 15:04 mauriciogracia

@mauriciogracia that is amazing, thanks for sharing, in case anyone needs the reverse conversion too, pathToFileURL exists as well.

bali182 avatar May 20 '21 16:05 bali182