mirror of
https://github.com/truewhile/MeBox.git
synced 2026-09-28 11:16:37 +08:00
71bf60c69c
初始化项目
60 lines
1.1 KiB
Go
60 lines
1.1 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
type activeDownloadGuard struct {
|
|
paths []string
|
|
}
|
|
|
|
func (o *OrganizerService) newActiveDownloadGuard(ctx context.Context) activeDownloadGuard {
|
|
if o == nil || o.activeDownloadPaths == nil {
|
|
return activeDownloadGuard{}
|
|
}
|
|
return activeDownloadGuard{paths: cleanUniqueExistingPaths(o.activeDownloadPaths(ctx))}
|
|
}
|
|
|
|
func (g activeDownloadGuard) contains(path string) bool {
|
|
path = filepath.Clean(strings.TrimSpace(path))
|
|
if path == "" || path == "." {
|
|
return false
|
|
}
|
|
for _, root := range g.paths {
|
|
if pathWithin(path, root) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func cleanUniqueExistingPaths(paths []string) []string {
|
|
if len(paths) == 0 {
|
|
return nil
|
|
}
|
|
out := make([]string, 0, len(paths))
|
|
for _, path := range paths {
|
|
path = filepath.Clean(strings.TrimSpace(path))
|
|
if path == "" || path == "." {
|
|
continue
|
|
}
|
|
if _, err := os.Stat(path); err != nil {
|
|
continue
|
|
}
|
|
duplicate := false
|
|
for _, existing := range out {
|
|
if sameLibraryPath(existing, path) {
|
|
duplicate = true
|
|
break
|
|
}
|
|
}
|
|
if !duplicate {
|
|
out = append(out, path)
|
|
}
|
|
}
|
|
return out
|
|
}
|