mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-04 04:26:38 +08:00
feat(organize): separate source dir from destination dir (从源目录整理到目的地目录)
Previously organize only exposed a single 'target dir' that was actually the destination, conflating 源目录 (where files to organize live) with 目的地目录 (where organized files go). Add an explicit source directory: - OrganizeOptions gains SourcePath; DestPath replaces the old TargetPath (destination) for clarity. New organize.source_dir setting + source_path request override; resolveSourceRoot falls back to library path. - OrganizeLibraryWithOptions only organizes media located under the source root, so operators can point at a specific download/staging folder and organize into a distinct destination. - Handler accepts source_path/dest_path (target_path kept as a deprecated alias for the destination, backward compatible). - Settings page splits into 整理源目录(待整理) + 整理目的地目录; Tools organize panel exposes 源目录 + 目的地目录 inputs with clear copy. Defaults are unchanged (source = destination = library path) so existing setups behave identically. Adds a regression test that organize is scoped to the source directory. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
@@ -11,8 +11,13 @@ import (
|
||||
)
|
||||
|
||||
// organizeReq carries optional per-request overrides. 留空则沿用系统设置。
|
||||
//
|
||||
// source_path = 源目录(待整理),dest_path = 目的地目录(整理输出)。
|
||||
// target_path 为 dest_path 的向后兼容别名。
|
||||
type organizeReq struct {
|
||||
TargetPath string `json:"target_path"`
|
||||
SourcePath string `json:"source_path"`
|
||||
DestPath string `json:"dest_path"`
|
||||
TargetPath string `json:"target_path"` // deprecated alias for dest_path
|
||||
TransferMode string `json:"transfer_mode"`
|
||||
}
|
||||
|
||||
@@ -21,7 +26,14 @@ type organizeReq struct {
|
||||
func bindOrganizeOptions(c *gin.Context) service.OrganizeOptions {
|
||||
var req organizeReq
|
||||
_ = c.ShouldBindJSON(&req)
|
||||
opts := service.OrganizeOptions{TargetPath: strings.TrimSpace(req.TargetPath)}
|
||||
dest := strings.TrimSpace(req.DestPath)
|
||||
if dest == "" {
|
||||
dest = strings.TrimSpace(req.TargetPath)
|
||||
}
|
||||
opts := service.OrganizeOptions{
|
||||
SourcePath: strings.TrimSpace(req.SourcePath),
|
||||
DestPath: dest,
|
||||
}
|
||||
if m := strings.TrimSpace(req.TransferMode); m != "" {
|
||||
opts.TransferMode = service.TransferMode(m)
|
||||
}
|
||||
|
||||
@@ -50,9 +50,16 @@ type OrganizeResult struct {
|
||||
|
||||
// OrganizeOptions carries per-request overrides for an organize operation.
|
||||
// 空值表示沿用系统设置中的默认值。
|
||||
//
|
||||
// 整理是「从源目录整理到目的地目录」:SourcePath 指定待整理文件所在的源目录,
|
||||
// DestPath 指定整理输出的目的地目录。两者相互独立,不再混用同一个目录。
|
||||
type OrganizeOptions struct {
|
||||
// TargetPath 本次整理的目标根路径,覆盖 organize.target_dir 设置与媒体库路径。
|
||||
TargetPath string
|
||||
// SourcePath 本次整理的源目录(待整理文件所在目录),覆盖 organize.source_dir
|
||||
// 设置与媒体库路径。仅整理位于该目录下的媒体;留空表示整个媒体库。
|
||||
SourcePath string
|
||||
// DestPath 本次整理的目的地根路径(整理输出到哪里),覆盖 organize.target_dir 设置。
|
||||
// 留空则使用设置中的默认目的地目录,再退回媒体库路径。
|
||||
DestPath string
|
||||
// TransferMode 本次整理的转移方式,覆盖 organize.transfer_mode 设置。
|
||||
TransferMode TransferMode
|
||||
}
|
||||
@@ -76,7 +83,7 @@ func (o *OrganizerService) OrganizeMediaWithOptions(ctx context.Context, mediaID
|
||||
if err != nil || lib == nil {
|
||||
return "", errors.New("library not found")
|
||||
}
|
||||
baseRoot := o.resolveBaseRoot(ctx, lib, opts.TargetPath)
|
||||
baseRoot := o.resolveBaseRoot(ctx, lib, opts.DestPath)
|
||||
mode := o.resolveTransferMode(ctx, opts.TransferMode)
|
||||
if isSeriesLibraryType(lib.Type) {
|
||||
if err := o.refreshEpisodeIdentity(m, lib); err != nil {
|
||||
@@ -166,8 +173,9 @@ func (o *OrganizerService) OrganizeMediaWithOptions(ctx context.Context, mediaID
|
||||
return dst, nil
|
||||
}
|
||||
|
||||
// resolveBaseRoot picks the organize target root: a per-request override
|
||||
// wins, then the organize.target_dir setting, then the library's own path.
|
||||
// resolveBaseRoot picks the organize destination root (目的地目录): a
|
||||
// per-request override wins, then the organize.target_dir setting, then the
|
||||
// library's own path.
|
||||
func (o *OrganizerService) resolveBaseRoot(ctx context.Context, lib *model.Library, override string) string {
|
||||
if r := strings.TrimSpace(override); r != "" {
|
||||
return r
|
||||
@@ -180,6 +188,22 @@ func (o *OrganizerService) resolveBaseRoot(ctx context.Context, lib *model.Libra
|
||||
return lib.Path
|
||||
}
|
||||
|
||||
// resolveSourceRoot picks the organize source root (源目录,待整理文件所在目录):
|
||||
// a per-request override wins, then the organize.source_dir setting, then the
|
||||
// library's own path. Library organize only touches media located under this
|
||||
// root, so operators can point at a specific download/staging folder.
|
||||
func (o *OrganizerService) resolveSourceRoot(ctx context.Context, lib *model.Library, override string) string {
|
||||
if r := strings.TrimSpace(override); r != "" {
|
||||
return r
|
||||
}
|
||||
if o.repo != nil && o.repo.Setting != nil {
|
||||
if v, err := o.repo.Setting.Get(ctx, "organize.source_dir"); err == nil && strings.TrimSpace(v) != "" {
|
||||
return strings.TrimSpace(v)
|
||||
}
|
||||
}
|
||||
return lib.Path
|
||||
}
|
||||
|
||||
// resolveTransferMode picks the transfer mode: a per-request override wins,
|
||||
// otherwise the organize.transfer_mode setting (default move). When the
|
||||
// effective mode is move and 做种保种 (organize.keep_seeding) is enabled, it is
|
||||
@@ -234,10 +258,17 @@ func (o *OrganizerService) OrganizeLibraryWithOptions(ctx context.Context, libra
|
||||
Find(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 已位于整理目标根下的文件视为已整理;目标根受 target_path 覆盖与设置影响。
|
||||
baseRoot := o.resolveBaseRoot(ctx, lib, opts.TargetPath)
|
||||
// 源目录(待整理):仅整理位于该目录下的媒体;留空 = 整个媒体库。
|
||||
sourceRoot := o.resolveSourceRoot(ctx, lib, opts.SourcePath)
|
||||
// 目的地目录:已位于该根下的文件视为已整理;受 dest_path 覆盖与设置影响。
|
||||
baseRoot := o.resolveBaseRoot(ctx, lib, opts.DestPath)
|
||||
res := &OrganizeResult{}
|
||||
for i := range rows {
|
||||
// 不在源目录内的文件跳过(不属于本次「从源目录整理」的范围)。
|
||||
if !pathWithin(rows[i].Path, sourceRoot) {
|
||||
res.Skipped++
|
||||
continue
|
||||
}
|
||||
if pathWithin(rows[i].Path, baseRoot) {
|
||||
res.Skipped++
|
||||
continue
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/ShukeBta/MediaStationGo/internal/config"
|
||||
"github.com/ShukeBta/MediaStationGo/internal/model"
|
||||
)
|
||||
|
||||
// TestOrganizeLibraryScopesToSourceDir verifies organize reads from the
|
||||
// source directory (源目录) and writes to the destination directory (目的地目录):
|
||||
// only media located under SourcePath are organized; media outside it are
|
||||
// left untouched.
|
||||
func TestOrganizeLibraryScopesToSourceDir(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
srcDir := filepath.Join(root, "downloads")
|
||||
otherDir := filepath.Join(root, "elsewhere")
|
||||
dest := filepath.Join(root, "library")
|
||||
for _, d := range []string{srcDir, otherDir} {
|
||||
if err := os.MkdirAll(d, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
inSource := filepath.Join(srcDir, "In Source.mkv")
|
||||
outside := filepath.Join(otherDir, "Outside.mkv")
|
||||
if err := os.WriteFile(inSource, []byte("a"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(outside, []byte("b"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
repos := newOrganizerTestRepo(t)
|
||||
lib := model.Library{Name: "Movies", Path: root, Type: "movie", Enabled: true}
|
||||
if err := repos.Library.Create(t.Context(), &lib); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mIn := model.Media{LibraryID: lib.ID, Title: "In Source", Path: inSource, Year: 2020, Container: "mkv", ScrapeStatus: "matched"}
|
||||
mOut := model.Media{LibraryID: lib.ID, Title: "Outside", Path: outside, Year: 2021, Container: "mkv", ScrapeStatus: "matched"}
|
||||
if err := repos.Media.Upsert(t.Context(), &mIn); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := repos.Media.Upsert(t.Context(), &mOut); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
org := NewOrganizerService(&config.Config{}, zap.NewNop(), repos)
|
||||
res, err := org.OrganizeLibraryWithOptions(t.Context(), lib.ID, OrganizeOptions{
|
||||
SourcePath: srcDir,
|
||||
DestPath: dest,
|
||||
TransferMode: TransferCopy,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("organize: %v", err)
|
||||
}
|
||||
if res.Organized != 1 {
|
||||
t.Fatalf("expected exactly 1 organized (only the in-source media), got %d (skipped %d)", res.Organized, res.Skipped)
|
||||
}
|
||||
|
||||
// The in-source media should now live under the destination dir.
|
||||
gotIn, err := repos.Media.FindByID(t.Context(), mIn.ID)
|
||||
if err != nil || gotIn == nil {
|
||||
t.Fatalf("reload in-source media: %v", err)
|
||||
}
|
||||
if !strings.HasPrefix(gotIn.Path, dest) {
|
||||
t.Fatalf("in-source media should be organized under dest %q, got %q", dest, gotIn.Path)
|
||||
}
|
||||
|
||||
// The outside media must be left untouched (not moved into dest).
|
||||
gotOut, err := repos.Media.FindByID(t.Context(), mOut.ID)
|
||||
if err != nil || gotOut == nil {
|
||||
t.Fatalf("reload outside media: %v", err)
|
||||
}
|
||||
if gotOut.Path != outside {
|
||||
t.Fatalf("outside media must not be organized; path changed to %q", gotOut.Path)
|
||||
}
|
||||
if _, err := os.Stat(outside); err != nil {
|
||||
t.Fatalf("outside source file must remain in place: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user