fix(move): move every originals file of a photo, not just the primary

Videos, Live Photos, and RAW+JPG pairs keep several files under Root "/". The
old movePhotoFiles moved only the primary (often the poster JPG), orphaning
the .mov: PhotoPrism then saw the photo as moved (dropped from the grid) while
the video stayed behind and broke. Move the whole originals group under one
shared stem (new uniqueStem helper) so siblings re-stack after reindex; fail
the photo and report it if any sibling can't move.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-22 00:06:18 +02:00
parent ad6e733622
commit e124809ad5
2 changed files with 132 additions and 54 deletions

View File

@@ -108,6 +108,37 @@ func uniqueName(destDir, basename string) (abs, name string, ok bool) {
return "", "", false return "", "", false
} }
// uniqueStem finds a base name (extension stripped) that is free for *every*
// extension in `exts` under destDir, appending `-1`, `-2`, … on collision —
// the multi-file analogue of uniqueName. Moving a photo's originals siblings
// (e.g. IMG_1234.JPG + IMG_1234.MOV) under a single shared stem keeps
// PhotoPrism stacking them as one photo after reindex; picking the stem once
// for the whole group is what stops the video from being orphaned under a
// differently-suffixed name than its poster. Caps at 1000 attempts to match
// uniqueName. The passed extensions keep their on-disk case (we compare
// case-sensitively via os.Stat, which is correct on the case-sensitive
// volumes PhotoPrism targets).
func uniqueStem(destDir, primaryBase string, exts []string) (stem string, ok bool) {
base := strings.TrimSuffix(primaryBase, filepath.Ext(primaryBase))
for i := 0; i < 1000; i++ {
candidate := base
if i > 0 {
candidate = base + "-" + itoa(i)
}
free := true
for _, ext := range exts {
if _, err := os.Stat(filepath.Join(destDir, candidate+ext)); !errors.Is(err, os.ErrNotExist) {
free = false
break
}
}
if free {
return candidate, true
}
}
return "", false
}
// itoa is the tiny stdlib-free formatter we use inside hot loops. // itoa is the tiny stdlib-free formatter we use inside hot loops.
func itoa(n int) string { func itoa(n int) string {
if n == 0 { if n == 0 {

View File

@@ -165,77 +165,124 @@ func movePhotoFiles(cfg *Config, pp *ppClient, token string, photos []heapPhoto,
errs = []heapErr{} errs = []heapErr{}
for _, photo := range photos { for _, photo := range photos {
// Pick the file to physically move. PhotoPrism's "primary" file // Gather *every* originals-rooted file of the photo, not just the
// for a HEIC photo is the generated `.HEIC.jpg` preview that // primary. A video, Live Photo, or RAW+JPG pair keeps several files
// lives in storage/sidecar (Root=="sidecar"), not in originals // under Root "/" (e.g. the poster IMG.JPG and its IMG.MOV), and they
// — moving that path would fail "file missing on disk" every // must travel together — moving only the primary orphans the rest, so
// time. Prefer the primary that lives in originals (Root=="/") // the photo looks "moved" in PhotoPrism (the poster defines its path)
// and fall back to the first originals-rooted file. PhotoPrism // while the actual video is left behind and silently breaks. Sidecar-
// regenerates sidecars on reindex, so they don't need to follow. // rooted files (Root=="sidecar": HEIC previews, .json) are regenerated
var file ppFile // on reindex and intentionally skipped. Pick the stem from the primary
found := false // (or the first originals file) so the siblings re-stack under one name.
var group []ppFile
var primary ppFile
havePrimary := false
for _, f := range photo.Files { for _, f := range photo.Files {
if f.Root == "/" && f.Primary { if f.Root != "/" {
file, found = f, true continue
break }
group = append(group, f)
if f.Primary && !havePrimary {
primary, havePrimary = f, true
} }
} }
if !found { if len(group) == 0 {
for _, f := range photo.Files {
if f.Root == "/" {
file, found = f, true
break
}
}
}
if !found {
errs = append(errs, heapErr{UID: photo.UID, Reason: "no originals-rooted file"}) errs = append(errs, heapErr{UID: photo.UID, Reason: "no originals-rooted file"})
continue continue
} }
srcRel := file.Name if !havePrimary {
srcAbs := filepath.Join(cfg.OriginalsRoot, srcRel) primary = group[0]
if !sameOrUnder(srcAbs, cfg.OriginalsRoot) {
errs = append(errs, heapErr{UID: photo.UID, Reason: "path escapes originals"})
continue
} }
st, statErr := os.Stat(srcAbs)
if statErr != nil || !st.Mode().IsRegular() { // Choose one collision-free stem for the whole group up front, so the
errs = append(errs, heapErr{UID: photo.UID, Reason: "file missing on disk"}) // siblings land as `<stem>.JPG`, `<stem>.MOV`, … and stay stacked.
continue exts := make([]string, 0, len(group))
extSeen := map[string]struct{}{}
for _, f := range group {
ext := filepath.Ext(f.Name)
if _, dup := extSeen[ext]; !dup {
extSeen[ext] = struct{}{}
exts = append(exts, ext)
}
} }
if filepath.Dir(srcAbs) == destAbs { stem, ok := uniqueStem(destAbs, filepath.Base(primary.Name), exts)
errs = append(errs, heapErr{UID: photo.UID, Reason: "already in target"})
continue
}
_, name, ok := uniqueName(destAbs, filepath.Base(srcAbs))
if !ok { if !ok {
errs = append(errs, heapErr{UID: photo.UID, Reason: "too many collisions"}) errs = append(errs, heapErr{UID: photo.UID, Reason: "too many collisions"})
continue continue
} }
dstAbs := filepath.Join(destAbs, name)
if mode == "move" { // Move/copy each sibling. A failure on any one fails the whole photo
if mvErr := os.Rename(srcAbs, dstAbs); mvErr != nil { // (surfaced in errs) rather than leaving a half-moved stack unreported.
// Cross-device renames fail with EXDEV — fall back to var failure string
// copy+remove so a library that spans filesystems still movedAny := false
// works. usedNames := map[string]struct{}{}
if err2 := copyFile(srcAbs, dstAbs); err2 != nil { for _, f := range group {
errs = append(errs, heapErr{UID: photo.UID, Reason: mvErr.Error()}) srcRel := f.Name
continue srcAbs := filepath.Join(cfg.OriginalsRoot, srcRel)
} if !sameOrUnder(srcAbs, cfg.OriginalsRoot) {
if err2 := os.Remove(srcAbs); err2 != nil { failure = "path escapes originals"
errs = append(errs, heapErr{UID: photo.UID, Reason: "rename ok, source remove failed: " + err2.Error()}) break
continue
}
} }
moved++ st, statErr := os.Stat(srcAbs)
} else { if statErr != nil || !st.Mode().IsRegular() {
if cpErr := copyFile(srcAbs, dstAbs); cpErr != nil { failure = "file missing on disk"
errs = append(errs, heapErr{UID: photo.UID, Reason: cpErr.Error()}) break
}
if filepath.Dir(srcAbs) == destAbs {
// Already in the target folder — nothing to do for this sibling,
// but the photo isn't an error just because one file is in place.
continue continue
} }
name := stem + filepath.Ext(srcAbs)
// Two originals files sharing an extension (rare) would collide on
// the shared stem; keep the extra one's own unique name so neither
// overwrites the other.
if _, clash := usedNames[name]; clash {
_, n, uok := uniqueName(destAbs, filepath.Base(srcAbs))
if !uok {
failure = "too many collisions"
break
}
name = n
}
usedNames[name] = struct{}{}
dstAbs := filepath.Join(destAbs, name)
if mode == "move" {
if mvErr := os.Rename(srcAbs, dstAbs); mvErr != nil {
// Cross-device renames fail with EXDEV — fall back to
// copy+remove so a library that spans filesystems still
// works.
if err2 := copyFile(srcAbs, dstAbs); err2 != nil {
failure = mvErr.Error()
break
}
if err2 := os.Remove(srcAbs); err2 != nil {
failure = "rename ok, source remove failed: " + err2.Error()
break
}
}
} else {
if cpErr := copyFile(srcAbs, dstAbs); cpErr != nil {
failure = cpErr.Error()
break
}
}
movedAny = true
sourceParents[filepath.Dir(srcRel)] = struct{}{}
}
if failure != "" {
errs = append(errs, heapErr{UID: photo.UID, Reason: failure})
continue
}
if !movedAny {
errs = append(errs, heapErr{UID: photo.UID, Reason: "already in target"})
continue
}
if mode == "move" {
moved++
} else {
copied++ copied++
} }
sourceParents[filepath.Dir(srcRel)] = struct{}{}
} }
// Reindex the destination + every source parent so PhotoPrism's DB // Reindex the destination + every source parent so PhotoPrism's DB