96 lines
1.9 KiB
Go
96 lines
1.9 KiB
Go
package node
|
|
|
|
import (
|
|
"errors"
|
|
"sort"
|
|
"time"
|
|
)
|
|
|
|
type NodeManager struct {
|
|
root *PathNode
|
|
current *PathNode
|
|
scan_time time.Duration
|
|
}
|
|
|
|
func NewNodeManager() *NodeManager {
|
|
root := NewPathNode("")
|
|
return &NodeManager{
|
|
root: root,
|
|
current: root,
|
|
scan_time: time.Duration(0),
|
|
}
|
|
}
|
|
|
|
func (npm *NodeManager) VaildRoot() bool {
|
|
return npm.root.VaildPath()
|
|
}
|
|
|
|
func (npm *NodeManager) SetRootPath(path string) bool {
|
|
npm.root.path = path
|
|
return npm.root.VaildPath()
|
|
}
|
|
|
|
func (npm *NodeManager) GetScanTime() time.Duration {
|
|
return npm.scan_time
|
|
}
|
|
|
|
func (npm *NodeManager) SaveToFile() error {
|
|
return npm.root.ToJSON("saved.json.gz", JSON_GZ)
|
|
}
|
|
|
|
func (npm *NodeManager) LoadFromFile() error {
|
|
return npm.root.FromJSON("saved.json.gz", JSON_GZ)
|
|
}
|
|
|
|
func (npm *NodeManager) Scan() error {
|
|
if !npm.VaildRoot() {
|
|
return errors.New("root path not vaild")
|
|
}
|
|
npm.root.ClearWithoutFlush()
|
|
start := time.Now()
|
|
err := npm.root.FastChanIterScanNode(0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
npm.current = npm.root
|
|
npm.scan_time = time.Since(start)
|
|
return nil
|
|
}
|
|
|
|
func (npm *NodeManager) GetChildrenWithSorted() []*PathNode {
|
|
nodes := npm.current.GetChildren()
|
|
sort.Slice(nodes, func(i, j int) bool {
|
|
if nodes[i].size > nodes[j].size {
|
|
return true
|
|
} else if nodes[i].size < nodes[j].size {
|
|
return false
|
|
}
|
|
return nodes[i].path > nodes[j].path
|
|
})
|
|
return nodes
|
|
}
|
|
|
|
func (npm *NodeManager) GetCurrentNode() *PathNode {
|
|
return npm.current
|
|
}
|
|
|
|
func (npm *NodeManager) GoIn(pathName string) error {
|
|
res := npm.current.GetChild(pathName)
|
|
if res == nil {
|
|
return errors.New("children not found")
|
|
}
|
|
if res.node_type != DIR {
|
|
return errors.New("not a directory")
|
|
}
|
|
npm.current = res
|
|
return nil
|
|
}
|
|
|
|
func (npm *NodeManager) GoOut() error {
|
|
if npm.current.parent == nil {
|
|
return errors.New("you can't go out of root")
|
|
}
|
|
npm.current = npm.current.parent
|
|
return nil
|
|
}
|