NODE.JS - how to handle a mix of OS and URL-style “paths” correctly?

北城余情 提交于 2020-01-14 07:59:09

问题


In my node.js app I have functions which can be passed either

OS-style paths e.g. c:\my\docs\mydoc.doc (or /usr/docs/mydoc.doc or whatever is local)

File URLS e.g. file://c:/my/docs/mydoc.doc (which I'm not sure about the validity of '\'s in??)

Either way, I need to check to see if they refer to a specific location which will always exist as a local OS-style path e.g. c:\mydata\directory\ or /usr/mydata/directory

Obviously for OS-style paths I can just compare them as strings - they SHOULD always be the same (they're created with path) but FILE:// URLS don't necessarily use path.sep and so won't "string match"?

Any suggestions as to the best way to handle this (I'm personally tempted to break EVERYTHING by one-or-more slashes of either sort and then check each piece??


回答1:


Just do some manipulation of the string and check to make sure they are the same after correcting for the differences:

var path = require('path');
var pathname = "\\usr\\home\\newbeb01\\Desktop\\testinput.txt";
var pathname2 = "file://usr/home/newbeb01/Desktop/testinput.txt"

if(PreparePathNameForComparing(pathname) == PreparePathNameForComparing(pathname2))
{   console.log("Same path");   }
else
{   console.log("Not the same path");   }

function PreparePathNameForComparing(pathname)
{
    var returnString = pathname;
    //try to find the file uri prefix, if there strip it off
    if(pathname.search("file://") != -1 || pathname.search("FILE://") != -1)
    {   returnString = pathname.substring(6, pathname.length);  }

    //now make all slashes the same
    if(path.sep === '\\')    //replace all '/' with '\\'
    {   returnString = returnString.replace(/\//g, '\\');   }
    else    //replace all '\\' with '/'
    {   returnString = returnString.replace(/\\/g, '/');    }

    return returnString;
}

I checked to see if the URI path name indicator "file://" was there, if so, I deleted it off my compare string. Then I normalized based on the path separator node path module will give me. This way it should work in either Linux or Windows environment.




回答2:


I'm going to post my own take on this - as it came from a suggestion I got from someone on Facebook (no - really!) and it uses path in a way it probably wasn't intended for - e.g. I'm not sure it's the right 'solution' - Im not sure I'm not exploiting path a bit.

The Facebook tip was that path is really just a utility for handing strings with "/" and "\" separators - it ignores everything else - doesn't care what's in there at all.

On that basis, we can use

path.normalize(ourpath)

which will convert all separators to the local OS preferred ones (path.sep)

That means they will match my OS-style directory (which is also made with path) and so I can compare those - without resorting to manually gutting-out slashes...

e.g.

Before

file://awkward/use/of\\slashes\in/this/path

After

file:\awkward\use\of\slashes\in\this\path (Windows)

or

file:/awkward/use/of/slashes/in/this/path (everywhere else)

Removing file:// before (or file: + path.sep after) = local OS-style path!?



来源:https://stackoverflow.com/questions/18341808/node-js-how-to-handle-a-mix-of-os-and-url-style-paths-correctly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!