refactor: split modules and harden scraping workflows

This commit is contained in:
ShukeBta
2026-06-24 11:59:18 +08:00
parent efca3cbe69
commit 192f35d9fa
470 changed files with 47957 additions and 33839 deletions
-357
View File
@@ -7,8 +7,6 @@ import (
"encoding/hex"
"errors"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"time"
@@ -96,229 +94,6 @@ func (s *MediaService) CreateLibrary(ctx context.Context, name, path, kind strin
return lib, nil
}
func inferLibraryKind(name, path, requested string) string {
requested = normalizeOrganizeMediaType(requested)
text := strings.ToLower(name + " " + filepath.ToSlash(path))
switch {
case containsAnyText(text, "成人", "番号", "jav", "9kg", "adult", "nsfw"):
return "adult"
case containsAnyText(text, "综艺", "真人秀", "variety"):
return "variety"
case containsAnyText(text, "国漫", "日漫", "日番", "动漫", "动画", "anime", "bangumi") && !containsAnyText(text, "动画电影"):
return "anime"
case containsAnyText(text, "电视剧", "国产剧", "欧美剧", "日韩剧", "日剧", "韩剧", "剧集", "tv", "series"):
return "tv"
case containsAnyText(text, "电影", "movie", "film"):
return "movie"
}
if requested != "" {
return requested
}
return "movie"
}
func resolveAccessibleLibraryPath(path string) (string, error) {
input := strings.TrimSpace(path)
if input == "" {
return "", errors.New("path required")
}
for _, candidate := range mappedPathCandidates(input) {
if isAccessibleDir(candidate) {
return filepath.Clean(candidate), nil
}
}
abs, err := filepath.Abs(input)
if err != nil {
return "", fmt.Errorf("invalid path: %w", err)
}
return "", fmt.Errorf("path is not an accessible directory: %s", abs)
}
func resolveAccessibleMappedPath(path string) (string, os.FileInfo, error) {
input := strings.TrimSpace(path)
if input == "" {
return "", nil, errors.New("path required")
}
candidates := mappedPathCandidates(input)
for _, candidate := range candidates {
if info, err := os.Stat(candidate); err == nil {
return filepath.Clean(candidate), info, nil
}
}
abs, err := filepath.Abs(input)
if err != nil {
return "", nil, fmt.Errorf("invalid path: %w", err)
}
return "", nil, fmt.Errorf("path is not accessible: %s", abs)
}
func resolveMappedDestinationPath(path string) string {
path = strings.TrimSpace(path)
if path == "" {
return ""
}
clean := filepath.Clean(path)
if _, err := os.Stat(clean); err == nil {
return clean
}
for _, candidate := range mappedPathCandidates(clean) {
if candidate == clean {
continue
}
return filepath.Clean(candidate)
}
return clean
}
func mappedPathCandidates(input string) []string {
var candidates []string
add := func(candidate string) {
candidate = filepath.Clean(filepath.FromSlash(strings.TrimSpace(candidate)))
if candidate == "" || candidate == "." {
return
}
for _, existing := range candidates {
if sameLibraryPath(existing, candidate) {
return
}
}
candidates = append(candidates, candidate)
}
clean := filepath.Clean(input)
add(clean)
for _, candidate := range dockerVolumePathCandidates(input) {
add(candidate)
}
for _, candidate := range dockerVolumePathCandidates(clean) {
add(candidate)
}
if slashClean := cleanPathForVolumeMapping(input); slashClean != "" {
add(slashClean)
}
if abs, err := filepath.Abs(input); err == nil {
add(abs)
for _, candidate := range dockerVolumePathCandidates(abs) {
add(candidate)
}
}
return candidates
}
func isAccessibleDir(path string) bool {
info, err := os.Stat(path)
return err == nil && info.IsDir()
}
func dockerVolumePathCandidates(path string) []string {
normalized := cleanPathForVolumeMapping(path)
var candidates []string
addCandidate := func(candidate string) {
candidate = filepath.Clean(filepath.FromSlash(candidate))
for _, existing := range candidates {
if sameLibraryPath(existing, candidate) {
return
}
}
candidates = append(candidates, candidate)
}
for _, mapping := range []struct {
env string
container string
}{
{env: "MEDIASTATION_MEDIA_DIR", container: envOrDefault("MEDIASTATION_MEDIA_CONTAINER_DIR", "/media")},
{env: "MEDIASTATION_DOWNLOAD_DIR", container: envOrDefault("MEDIASTATION_DOWNLOAD_CONTAINER_DIR", "/downloads")},
} {
host := cleanPathForVolumeMapping(os.Getenv(mapping.env))
if host == "." || host == "" || strings.HasPrefix(host, ".") {
continue
}
if normalized == host {
addCandidate(mapping.container)
continue
}
if strings.HasPrefix(normalized, host+"/") {
addCandidate(mapping.container + strings.TrimPrefix(normalized, host))
}
container := cleanPathForVolumeMapping(mapping.container)
if container == "." || container == "" || strings.HasPrefix(container, ".") {
continue
}
if normalized == container {
addCandidate(host)
continue
}
if strings.HasPrefix(normalized, container+"/") {
addCandidate(host + strings.TrimPrefix(normalized, container))
}
}
for _, marker := range []struct {
part string
container string
}{
{part: "/media", container: envOrDefault("MEDIASTATION_MEDIA_CONTAINER_DIR", "/media")},
{part: "/downloads", container: envOrDefault("MEDIASTATION_DOWNLOAD_CONTAINER_DIR", "/downloads")},
} {
part := strings.TrimRight(marker.part, "/")
container := strings.TrimRight(filepath.ToSlash(marker.container), "/")
markerPath := pathAfterWindowsDrivePrefix(normalized)
if markerPath == part {
addCandidate(container)
continue
}
if strings.HasPrefix(markerPath, part+"/") {
addCandidate(container + strings.TrimPrefix(markerPath, part))
}
}
return candidates
}
func cleanPathForVolumeMapping(path string) string {
path = strings.TrimSpace(path)
if path == "" {
return ""
}
path = strings.ReplaceAll(path, "\\", "/")
path = trimEmbeddedWindowsDrive(path)
return filepath.ToSlash(filepath.Clean(filepath.FromSlash(path)))
}
func pathAfterWindowsDrivePrefix(path string) string {
if len(path) >= 3 && path[1] == ':' && path[2] == '/' && isASCIIAlpha(path[0]) {
return path[2:]
}
return path
}
func trimEmbeddedWindowsDrive(path string) string {
for i := 0; i+2 < len(path); i++ {
if !isASCIIAlpha(path[i]) || path[i+1] != ':' || path[i+2] != '/' {
continue
}
if i == 0 || path[i-1] == '/' {
return path[i:]
}
}
return path
}
func isASCIIAlpha(ch byte) bool {
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
}
func sameLibraryPath(a, b string) bool {
return filepath.Clean(a) == filepath.Clean(b)
}
func envOrDefault(key, fallback string) string {
if value := strings.TrimSpace(os.Getenv(key)); value != "" {
return value
}
return fallback
}
// ListLibraries returns every library configured on the server.
func (s *MediaService) ListLibraries(ctx context.Context) ([]model.Library, error) {
return s.repo.Library.List(ctx)
@@ -440,138 +215,6 @@ func (s *MediaService) invalidateMediaCache(ctx context.Context) {
}
}
func (s *MediaService) attachLibraryMetadata(ctx context.Context, items []model.Media) {
if s == nil || s.repo == nil || s.repo.Library == nil || len(items) == 0 {
return
}
libs, err := s.repo.Library.List(ctx)
if err != nil {
return
}
byID := make(map[string]model.Library, len(libs))
for _, lib := range libs {
byID[lib.ID] = lib
}
resolver := newMediaDisplayLibraryResolver(ctx, s.repo, libs)
for i := range items {
if lib, ok := byID[items[i].LibraryID]; ok {
items[i].LibraryName = lib.Name
items[i].LibraryPath = lib.Path
}
if lib, ok := resolver.DisplayLibraryForMedia(items[i]); ok {
items[i].DisplayLibraryID = lib.ID
items[i].DisplayLibraryName = lib.Name
items[i].DisplayLibraryPath = lib.Path
}
}
}
type mediaDisplayLibraryResolver struct {
byID map[string]model.Library
displayByID map[string]model.Library
displayByMergeKey map[string]model.Library
displayLibraries []model.Library
}
func newMediaDisplayLibraryResolver(ctx context.Context, repo *repository.Container, libs []model.Library) mediaDisplayLibraryResolver {
displayLibraries := FilterDisplayCloudLibraries(ctx, repo, append([]model.Library(nil), libs...))
resolver := mediaDisplayLibraryResolver{
byID: make(map[string]model.Library, len(libs)),
displayByID: make(map[string]model.Library, len(displayLibraries)),
displayByMergeKey: make(map[string]model.Library, len(displayLibraries)),
displayLibraries: displayLibraries,
}
for _, lib := range libs {
resolver.byID[lib.ID] = lib
}
for _, lib := range displayLibraries {
resolver.displayByID[lib.ID] = lib
if key, ok := CloudLibraryMergeKey(lib); ok {
if _, exists := resolver.displayByMergeKey[key]; !exists {
resolver.displayByMergeKey[key] = lib
}
}
}
return resolver
}
func (r mediaDisplayLibraryResolver) DisplayLibraryForMedia(media model.Media) (model.Library, bool) {
if lib, ok := r.bestPathDisplayLibrary(media); ok {
return lib, true
}
if lib, ok := r.displayByID[media.LibraryID]; ok {
return lib, true
}
own, hasOwn := r.byID[media.LibraryID]
if hasOwn {
if key, ok := CloudLibraryMergeKey(own); ok {
if lib, exists := r.displayByMergeKey[key]; exists {
return lib, true
}
}
return own, true
}
return model.Library{}, false
}
func (r mediaDisplayLibraryResolver) bestPathDisplayLibrary(media model.Media) (model.Library, bool) {
if strings.HasPrefix(strings.ToLower(strings.TrimSpace(media.Path)), "cloud://") {
mediaInfo, ok := ParseCloudLibraryMount(media.Path)
if !ok {
return model.Library{}, false
}
var best model.Library
bestDepth := 0
for _, lib := range r.displayLibraries {
info, ok := ParseCloudLibraryMount(lib.Path)
if !ok || info.Provider != mediaInfo.Provider || !lib.Enabled {
continue
}
dir := strings.Trim(firstNonEmpty(info.DisplayDir, info.ScanDir), "/")
if dir == "" {
continue
}
mediaDir := strings.Trim(firstNonEmpty(mediaInfo.DisplayDir, mediaInfo.ScanDir), "/")
if mediaDir != dir && !cloudMountAncestor(dir, mediaDir) {
continue
}
depth := len(strings.Split(dir, "/"))
if depth > bestDepth {
best = lib
bestDepth = depth
}
}
if bestDepth > 0 {
return best, true
}
return model.Library{}, false
}
mediaPath := cleanPathForVolumeMapping(media.Path)
var best model.Library
bestLen := 0
for _, lib := range r.displayLibraries {
if _, ok := ParseCloudLibraryMount(lib.Path); ok || !lib.Enabled {
continue
}
libPath := cleanPathForVolumeMapping(lib.Path)
if libPath == "" || libPath == "." {
continue
}
if mediaPath != libPath && !strings.HasPrefix(mediaPath, strings.TrimRight(libPath, "/")+"/") {
continue
}
if len(libPath) > bestLen {
best = lib
bestLen = len(libPath)
}
}
if bestLen > 0 {
return best, true
}
return model.Library{}, false
}
func groupMediaVersions(items []model.Media) []MediaItem {
if len(items) == 0 {
return nil