github.com/scottcagno/storage@v1.8.0/pkg/web/path.go (about)

     1  package web
     2  
     3  import (
     4  	"github.com/scottcagno/storage/pkg/util"
     5  	"path/filepath"
     6  	"runtime"
     7  )
     8  
     9  // GetFilepath returns the absolute filepath from where it's called. It returns the
    10  // result as a split string, first the root directory up until the current file and
    11  // returns the two in this order: dir, file
    12  func GetFilepath() (string, string) {
    13  	// Caller reports file and line number information about function invocations on
    14  	// the calling goroutine's stack. The argument skip is the number of stack frames
    15  	// to ascend, with 0 identifying the caller of Caller.
    16  	_, filename, _, ok := runtime.Caller(1)
    17  	if !ok {
    18  		return "", ""
    19  	}
    20  	// clean and split the filepath
    21  	return filepath.Split(filepath.Clean(filename))
    22  }
    23  
    24  func SanitizePath(path string) string {
    25  	abs, _ := util.GetFilepath()
    26  	return filepath.Join(abs, path)
    27  }