package server import ( "fmt" "local/notes-server/config" "os" "path" "strings" ) type Path struct { Local string HREF string Base string } func NewPathFromLocal(p string) Path { splits := strings.SplitN(p, path.Base(config.Root), 2) href := splits[0] if len(splits) > 1 { href = splits[1] } href = path.Join("/notes", href) return NewPathFromURL(href) } func NewPathFromURL(p string) Path { p = path.Clean(p) base := path.Base(p) href := p local := strings.TrimPrefix(p, "/") locals := strings.SplitN(local, "/", 2) local = locals[0] if len(locals) > 1 { local = locals[1] } local = path.Join(config.Root, local) if strings.Trim(p, "/") == base { local = config.Root } return Path{ Base: base, HREF: href, Local: local, } } func (p Path) MultiLink() string { href := p.HREF href = strings.TrimPrefix(href, "/") href = strings.TrimSuffix(href, "/") segments := strings.Split(href, "/") full := "" for i := range segments { href := "/" + strings.Join(segments[:i], "/") + "/" href += segments[i] href = path.Clean(href) base := segments[i] add := fmt.Sprintf(`/%s`, href, base) full += add } return full } func (p Path) LI() string { return fmt.Sprintf(`
  • %s
  • `, p.HREF, p.Base) } func (p Path) IsDir() bool { stat, err := os.Stat(p.Local) return err == nil && stat.IsDir() } func (p Path) IsFile() bool { stat, err := os.Stat(p.Local) return err == nil && !stat.IsDir() } func (p Path) String() string { return fmt.Sprintf(`[Local:%s HREF:%s Base:%s]`, p.Local, p.HREF, p.Base) }