106 lines
2.2 KiB
Go
Executable File
106 lines
2.2 KiB
Go
Executable File
package filetree
|
|
|
|
import (
|
|
"fmt"
|
|
"gitea.inhome.blapointe.com/local/notes-server/config"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
)
|
|
|
|
type Path struct {
|
|
Local string
|
|
HREF string
|
|
Base string
|
|
BaseHREF string
|
|
}
|
|
|
|
func NewPathFromLocal(p string) Path {
|
|
root := config.Root + "/"
|
|
if strings.HasPrefix(root, "./") {
|
|
root = root[2:]
|
|
}
|
|
if strings.HasSuffix(root, "/") {
|
|
root = root[:len(root)-1]
|
|
}
|
|
splits := strings.SplitN(p, root, 2)
|
|
for len(splits) > 0 && splits[0] == "" {
|
|
splits = splits[1:]
|
|
}
|
|
if len(splits) == 0 {
|
|
splits = []string{"", ""}
|
|
}
|
|
href := splits[0]
|
|
if len(splits) == 1 && (splits[0] == root || splits[0] == config.Root) {
|
|
href = ""
|
|
} else if splits[0] == "" || splits[0] == "/" {
|
|
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]
|
|
}
|
|
baseHREF := local
|
|
local = path.Join(config.Root, local)
|
|
if strings.Trim(p, "/") == base {
|
|
local = config.Root
|
|
baseHREF = "/"
|
|
}
|
|
return Path{
|
|
Base: base,
|
|
HREF: href,
|
|
Local: local,
|
|
BaseHREF: baseHREF,
|
|
}
|
|
}
|
|
|
|
func (p Path) MultiLink() string {
|
|
href := p.BaseHREF
|
|
href = strings.TrimPrefix(href, "/")
|
|
href = strings.TrimSuffix(href, "/")
|
|
href = path.Join("notes/", 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(`/<a href=%q>%s</a>`, href, base)
|
|
full += add
|
|
}
|
|
return full
|
|
}
|
|
|
|
func (p Path) FullLI() string {
|
|
return fmt.Sprintf(`<li><a href=%q>%s</a></li>`, p.HREF, p.HREF)
|
|
}
|
|
|
|
func (p Path) LI() string {
|
|
return fmt.Sprintf(`<li><a href=%q>%s</a></li>`, 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)
|
|
}
|