Files
MeBox/internal/service/organizer_active_downloads.go
truewhile 71bf60c69c 初始化
初始化项目
2026-08-23 22:12:32 +08:00

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
}