Split into packages only manually tested
This commit is contained in:
92
filetree/path.go
Executable file
92
filetree/path.go
Executable file
@@ -0,0 +1,92 @@
|
||||
package filetree
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"local/notes-server/config"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Path struct {
|
||||
Local string
|
||||
HREF string
|
||||
Base string
|
||||
BaseHREF string
|
||||
}
|
||||
|
||||
func NewPathFromLocal(p string) Path {
|
||||
if !strings.HasPrefix(p, "/") {
|
||||
cwd, _ := os.Getwd()
|
||||
p = path.Clean(path.Join(cwd, p))
|
||||
}
|
||||
splits := strings.SplitN(p, path.Base(config.Root), 2)
|
||||
href := splits[0]
|
||||
if len(splits) > 1 && (splits[0] == "" || splits[0] == "/") {
|
||||
href = splits[1]
|
||||
} else {
|
||||
href = strings.Join(splits, path.Base(config.Root))
|
||||
}
|
||||
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) 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)
|
||||
}
|
||||
Reference in New Issue
Block a user