FilePathToURI converts a filepath to URI. If relativeTo is provided not empty and path is a relative path it will be made absolute based on the provided value. Otherwise, the current working directory is used.
(path, relativeTo string)
| 29 | // a relative path it will be made absolute based on the provided value. Otherwise, the |
| 30 | // current working directory is used. |
| 31 | func FilePathToURI(path, relativeTo string) (string, error) { |
| 32 | if IsURI(path) { |
| 33 | return path, nil |
| 34 | } |
| 35 | |
| 36 | if !filepath.IsAbs(path) { |
| 37 | var err error |
| 38 | path, err = filepath.Abs(filepath.Join(relativeTo, path)) |
| 39 | if err != nil { |
| 40 | return "", err |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | if runtime.GOOS == "windows" { |
| 45 | if strings.HasPrefix(path, `\\`) { |
| 46 | return "file://" + filepath.ToSlash(strings.TrimPrefix(path, `\\`)), nil |
| 47 | } |
| 48 | return "file:///" + filepath.ToSlash(path), nil |
| 49 | } |
| 50 | return "file://" + path, nil |
| 51 | } |
| 52 | |
| 53 | // examples: |
| 54 | // |