github.com/puellanivis/breton@v0.2.16/lib/files/path.go (about)

     1  package files
     2  
     3  import (
     4  	"context"
     5  	"net/url"
     6  	"path/filepath"
     7  )
     8  
     9  func isPath(uri *url.URL) bool {
    10  	if uri.IsAbs() {
    11  		return false
    12  	}
    13  
    14  	if uri.User != nil {
    15  		return false
    16  	}
    17  
    18  	if len(uri.Host)+len(uri.RawQuery)+len(uri.Fragment) > 0 {
    19  		return false
    20  	}
    21  
    22  	if uri.ForceQuery {
    23  		return false
    24  	}
    25  
    26  	return true
    27  }
    28  
    29  func getPath(uri *url.URL) string {
    30  	if uri.RawPath != "" {
    31  		return uri.RawPath
    32  	}
    33  
    34  	return uri.Path
    35  }
    36  
    37  func makePath(path string) *url.URL {
    38  	return &url.URL{
    39  		Path:    path,
    40  		RawPath: path,
    41  	}
    42  }
    43  
    44  func resolveFilename(ctx context.Context, uri *url.URL) *url.URL {
    45  	if uri.IsAbs() {
    46  		return uri
    47  	}
    48  
    49  	var path string
    50  
    51  	if isPath(uri) {
    52  		path = getPath(uri)
    53  
    54  		if filepath.IsAbs(path) {
    55  			return makePath(path)
    56  		}
    57  	}
    58  
    59  	root, ok := getRoot(ctx)
    60  	if !ok {
    61  		return uri
    62  	}
    63  
    64  	if path != "" && isPath(root) {
    65  		return makePath(filepath.Join(getPath(root), path))
    66  
    67  	}
    68  
    69  	return root.ResolveReference(uri)
    70  }