Initial DDC release
This commit is contained in:
commit
434f6441da
57 files changed
+4606
No files matched your search
@@ -0,0 +1,57 @@
|
||||
package libraries
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var ErrUnknownDestination = errors.New("destination is not a first level folder of a library root")
|
||||
|
||||
type Destination struct {
|
||||
Name string `json:"name"`
|
||||
Root string `json:"root"`
|
||||
Path string `json:"path"`
|
||||
}
|
||||
|
||||
func Discover(roots []string) []Destination {
|
||||
destinations := []Destination{}
|
||||
for _, root := range roots {
|
||||
entries, err := os.ReadDir(root)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir() || strings.HasPrefix(entry.Name(), ".") {
|
||||
continue
|
||||
}
|
||||
destinations = append(destinations, Destination{
|
||||
Name: entry.Name(),
|
||||
Root: root,
|
||||
Path: filepath.Join(root, entry.Name()),
|
||||
})
|
||||
}
|
||||
}
|
||||
return destinations
|
||||
}
|
||||
|
||||
func Resolve(roots []string, requested string) (Destination, error) {
|
||||
cleaned := filepath.Clean(requested)
|
||||
name := filepath.Base(cleaned)
|
||||
if strings.HasPrefix(name, ".") {
|
||||
return Destination{}, ErrUnknownDestination
|
||||
}
|
||||
|
||||
for _, root := range roots {
|
||||
if filepath.Dir(cleaned) != root {
|
||||
continue
|
||||
}
|
||||
info, err := os.Lstat(cleaned)
|
||||
if err != nil || !info.IsDir() {
|
||||
return Destination{}, ErrUnknownDestination
|
||||
}
|
||||
return Destination{Name: name, Root: root, Path: cleaned}, nil
|
||||
}
|
||||
return Destination{}, ErrUnknownDestination
|
||||
}
|
||||
Reference in new issue
Block a user