github.com/icyphox/x@v0.0.355-0.20220311094250-029bd783e8b8/urlx/path_windows.go (about)

     1  //go:build windows
     2  // +build windows
     3  
     4  package urlx
     5  
     6  import (
     7  	"net/url"
     8  	"path/filepath"
     9  	"strings"
    10  )
    11  
    12  // GetURLFilePath returns the path of a URL that is compatible with the runtime os filesystem
    13  func GetURLFilePath(u *url.URL) string {
    14  	if u == nil {
    15  		return ""
    16  	}
    17  	if !(u.Scheme == "file" || u.Scheme == "") {
    18  		return u.Path
    19  	}
    20  
    21  	fPath := u.Path
    22  	if u.Host != "" {
    23  		// Make UNC Path
    24  		fPath = "\\\\" + u.Host + filepath.FromSlash(fPath)
    25  		return fPath
    26  	}
    27  	fPathTrimmed := strings.TrimLeft(fPath, "/")
    28  	if winPathRegex.MatchString(fPathTrimmed) {
    29  		// On Windows we should remove the initial path separator in case this
    30  		// is a normal path (for example: "\c:\" -> "c:\"")
    31  		fPath = fPathTrimmed
    32  	}
    33  	return filepath.FromSlash(fPath)
    34  }