Files
MeBox/internal/service/organizer_sidecar.go
T
truewhile 38b85bb487 feat(strm): 同名多版本择优 / keep_ext,并支持合并展示与播放切换 (#30)
* feat(strm): 同名多版本择优/keep_ext,并支持合并展示与播放切换

默认对同名不同扩展(如 .mkv/.mp4)按体积→mtime→扩展名优先级择优生成一条 strm,冲突打日志;开启 keep_ext 则分别生成 name.ext.strm。媒体详情与播放器可切换版本,列表继续合并同片多版本,Emby MediaSources 名称区分容器/分辨率。

Co-authored-by: truewhile <truewhile@users.noreply.github.com>

* fix(meta): 边车匹配忽略 .mkv.strm 中间扩展,同片共享 NFO/海报/字幕

NFO、海报、字幕与刮削写出统一使用 mediaSidecarBase 词干,使竞女01.mkv.strm
与竞女01.mp4.strm 配对同一套 竞女01.nfo / 竞女01-poster / 竞女01.srt;
仍兼容历史上的 name.mkv.nfo 命名。

Co-authored-by: truewhile <truewhile@users.noreply.github.com>

* fix(emby): People NFO 按共享词干匹配 keep_ext strm

resolveMediaPeople 改为查找竞女01.nfo(并兼容旧的竞女01.mkv.nfo),
同时修正把 decodeNFOFile 的 partial 误当 ok 导致完整 NFO 被跳过的问题。

Co-authored-by: truewhile <truewhile@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: truewhile <truewhile@users.noreply.github.com>
2026-09-07 13:56:30 +08:00

116 lines
3.7 KiB
Go

package service
import (
"os"
"path/filepath"
"strings"
)
// transferSidecarNFO moves/copies/links the .nfo sidecar alongside its media
// using the same transfer mode, so metadata follows the organized file.
func transferSidecarNFO(srcMedia, dstMedia string, mode TransferMode) error {
src := nfoPath(srcMedia)
dst := nfoPath(dstMedia)
if src == dst {
return nil
}
if _, err := os.Stat(src); err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
if _, err := os.Stat(dst); err == nil {
return nil
}
if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil { // #nosec G301 -- sidecar media directories must remain readable by NAS/player users.
return err
}
return transferFile(src, dst, mode)
}
// artworkSidecarSuffixes lists the poster/backdrop sidecar name suffixes that
// scrapers write next to a media file. It is deliberately conservative: we only
// follow the canonical "<base>-poster" / "<base>-backdrop" names and their
// Emby/Jellyfin variants, all scoped to the same base name as the media.
var artworkSidecarSuffixes = []string{
"-poster", ".poster",
"-backdrop", ".backdrop",
"-fanart", ".fanart", "-landscape",
"-cover", ".cover", "-thumb", ".thumb",
}
// artworkSidecarExtensions are the image extensions a sidecar may use. Both
// "base-poster.jpg" (bare separator) and "base.poster.jpg" (dotted separator)
// rely on suffix matching, so we probe the common image extensions.
var artworkSidecarExtensions = []string{".jpg", ".jpeg", ".png", ".webp", ".gif", ".bmp", ".tbn"}
// transferSidecarArtwork moves/copies/links the scraped poster/backdrop
// sidecar files alongside its media using the same transfer mode, mirroring
// transferSidecarNFO. Previously organize moved only the .nfo and left posters
// behind in the old folder; this keeps artwork with the organized file.
func transferSidecarArtwork(srcMedia, dstMedia string, mode TransferMode) error {
srcDir := filepath.Dir(srcMedia)
bases := mediaSidecarBaseVariants(srcMedia)
if len(bases) == 0 {
return nil
}
// Find every existing sidecar by probing suffix + extension combinations.
sources := make([]string, 0, len(artworkSidecarSuffixes)*len(artworkSidecarExtensions)*len(bases))
seen := map[string]struct{}{}
for _, base := range bases {
for _, suffix := range artworkSidecarSuffixes {
for _, ext := range artworkSidecarExtensions {
path := filepath.Join(srcDir, base+suffix+ext)
key := strings.ToLower(filepath.Clean(path))
if _, ok := seen[key]; ok {
continue
}
seen[key] = struct{}{}
if _, err := os.Stat(path); err == nil {
sources = append(sources, path)
}
}
}
}
if len(sources) == 0 {
return nil
}
dstDir := filepath.Dir(dstMedia)
if err := os.MkdirAll(dstDir, 0o755); err != nil { // #nosec G301 -- sidecar media directories must remain readable by NAS/player users.
return err
}
dstBase := mediaSidecarBase(dstMedia)
if dstBase == "" {
dstBase = strings.TrimSuffix(filepath.Base(dstMedia), filepath.Ext(dstMedia))
}
for _, src := range sources {
name := filepath.Base(src)
// Remap any legacy "Title.mkv-poster.jpg" onto the shared destination stem.
suffix := ""
lowerName := strings.ToLower(name)
for _, base := range bases {
prefix := strings.ToLower(base)
if strings.HasPrefix(lowerName, prefix) {
suffix = name[len(base):]
break
}
}
dstName := name
if suffix != "" && dstBase != "" {
dstName = dstBase + suffix
}
dst := filepath.Join(dstDir, dstName)
if strings.EqualFold(filepath.Clean(src), filepath.Clean(dst)) {
continue
}
if _, err := os.Stat(dst); err == nil {
continue
}
if err := transferFile(src, dst, mode); err != nil {
return err
}
}
return nil
}