TooTallNate / file-uri-to-path

Convert a `file:` URI to a file path
MIT License
27 stars 8 forks source link

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

Open BannerBomb opened 3 years ago

BannerBomb commented 3 years ago

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

mauriciogracia commented 3 years ago

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

bali182 commented 3 years ago

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