Index: Find photo stack by filename & recover original names #1823 #1892

This commit is contained in:
Michael Mayer 2022-01-06 09:55:41 +01:00
parent c5301a681d
commit 58cc89e7a4
6 changed files with 121 additions and 10 deletions

View file

@ -13,5 +13,7 @@ fi
# Reset Exif orientation flag if output image was rotated based on "QuickTime:Rotation"
if [[ $(exiftool -n -QuickTime:Rotation "$1") ]]; then
/usr/bin/exiftool -overwrite_original -P -n -Orientation=1 "$2"
fi
/usr/bin/exiftool -overwrite_original -P -n '-ModifyDate<FileModifyDate' -Orientation=1 "$2"
else
/usr/bin/exiftool -overwrite_original -P -n '-ModifyDate<FileModifyDate' "$2"
fi

View file

@ -14,6 +14,7 @@ const (
// Data represents image meta data.
type Data struct {
FileName string `meta:"FileName"`
DocumentID string `meta:"BurstUUID,MediaGroupUUID,ImageUniqueID,OriginalDocumentID,DocumentID"`
InstanceID string `meta:"InstanceID,DocumentID"`
TakenAt time.Time `meta:"DateTimeOriginal,CreationDate,CreateDate,MediaCreateDate,ContentCreateDate,DateTimeDigitized,DateTime"`

View file

@ -19,6 +19,7 @@ func TestJSON(t *testing.T) {
// t.Logf("DATA: %+v", data)
assert.Equal(t, "20170323-083538-Berlin-Zoologischer-Garten-2017-2u4.mov", data.FileName)
assert.Equal(t, CodecAvc1, data.Codec)
assert.Equal(t, "3s", data.Duration.String())
assert.Equal(t, "2018-09-08 19:20:14 +0000 UTC", data.TakenAtLocal.String())
@ -142,7 +143,7 @@ func TestJSON(t *testing.T) {
}
// t.Logf("DATA: %+v", data)
assert.Equal(t, "photoshop.xmp", data.FileName)
assert.Equal(t, CodecXMP, data.Codec)
assert.Equal(t, "0s", data.Duration.String())
assert.Equal(t, float32(52.45969), data.Lat)

View file

@ -4,11 +4,13 @@ import (
"fmt"
"path"
"path/filepath"
"strings"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/pkg/fs"
)
// FileName returns the full file name based on the root folder type.
func FileName(fileRoot, fileName string) string {
switch fileRoot {
case entity.RootSidecar:
@ -43,3 +45,56 @@ func CacheName(fileHash, namespace, cacheKey string) (cacheName string, err erro
return cacheName, nil
}
// RelName returns the relative filename.
func RelName(fileName, directory string) string {
return fs.RelName(fileName, directory)
}
// RootPath returns the file root path based on the configuration.
func RootPath(fileName string) string {
switch Root(fileName) {
case entity.RootSidecar:
return Config().SidecarPath()
case entity.RootImport:
return Config().ImportPath()
case entity.RootExamples:
return Config().ExamplesPath()
default:
return Config().OriginalsPath()
}
}
// Root returns the file root directory.
func Root(fileName string) string {
originalsPath := Config().OriginalsPath()
if originalsPath != "" && strings.HasPrefix(fileName, originalsPath) {
return entity.RootOriginals
}
importPath := Config().ImportPath()
if importPath != "" && strings.HasPrefix(fileName, importPath) {
return entity.RootImport
}
sidecarPath := Config().SidecarPath()
if sidecarPath != "" && strings.HasPrefix(fileName, sidecarPath) {
return entity.RootSidecar
}
examplesPath := Config().ExamplesPath()
if examplesPath != "" && strings.HasPrefix(fileName, examplesPath) {
return entity.RootExamples
}
return entity.RootUnknown
}
// RootRelName returns the relative filename, and automatically detects the root path.
func RootRelName(fileName string) string {
return RelName(fileName, RootPath(fileName))
}

View file

@ -3,20 +3,22 @@ package photoprism
import (
"testing"
"github.com/photoprism/photoprism/internal/config"
"github.com/stretchr/testify/assert"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/entity"
)
func TestFileName(t *testing.T) {
conf := config.TestConfig()
c := config.TestConfig()
t.Run("sidecar", func(t *testing.T) {
assert.Equal(t, conf.SidecarPath()+"/test.jpg", FileName("sidecar", "test.jpg"))
assert.Equal(t, c.SidecarPath()+"/test.jpg", FileName("sidecar", "test.jpg"))
})
t.Run("import", func(t *testing.T) {
assert.Equal(t, conf.ImportPath()+"/test.jpg", FileName("import", "test.jpg"))
assert.Equal(t, c.ImportPath()+"/test.jpg", FileName("import", "test.jpg"))
})
t.Run("examples", func(t *testing.T) {
assert.Equal(t, conf.ExamplesPath()+"/test.jpg", FileName("examples", "test.jpg"))
assert.Equal(t, c.ExamplesPath()+"/test.jpg", FileName("examples", "test.jpg"))
})
}
@ -41,3 +43,29 @@ func TestCacheName(t *testing.T) {
assert.Empty(t, r)
})
}
func TestRelName(t *testing.T) {
c := config.TestConfig()
t.Run("SidecarPath", func(t *testing.T) {
assert.Equal(t, "foo/test.jpg", RelName(FileName("sidecar", "foo/test.jpg"), c.SidecarPath()))
})
}
func TestRootPath(t *testing.T) {
c := config.TestConfig()
t.Run("SidecarPath", func(t *testing.T) {
assert.Equal(t, c.SidecarPath(), RootPath(FileName("sidecar", "test.jpg")))
})
}
func TestRoot(t *testing.T) {
t.Run("SidecarPath", func(t *testing.T) {
assert.Equal(t, entity.RootSidecar, Root(FileName("sidecar", "test.jpg")))
})
}
func TestRootRelName(t *testing.T) {
t.Run("SidecarPath", func(t *testing.T) {
assert.Equal(t, "foo/test.jpg", RootRelName(FileName("sidecar", "foo/test.jpg")))
})
}

View file

@ -154,6 +154,9 @@ func (ind *Index) MediaFile(m *MediaFile, o IndexOptions, originalName, photoUID
} else if photoQuery = entity.UnscopedDb().First(&photo, "photo_path = ? AND photo_name = ? AND photo_stack > -1", filePath, fileBase); photoQuery.Error == nil {
// Found.
fileStacked = true
} else if photoQuery = entity.UnscopedDb().First(&photo, "id IN (SELECT photo_id FROM files WHERE file_name = LIKE ? AND file_root = ? AND file_sidecar = 0 AND file_missing = 0)", fs.StripKnownExt(fileName)+".%", entity.RootOriginals); photoQuery.Error == nil {
// Found.
fileStacked = true
}
// Find existing photo by unique id or time and location?
@ -266,8 +269,8 @@ func (ind *Index) MediaFile(m *MediaFile, o IndexOptions, originalName, photoUID
log.Errorf("index: %s while updating covers of %s", err, logName)
}
// Update photo path based on main file.
if photoUID == "" && !fileStacked {
// Update photo path and name based on the main filename.
if photoUID == "" && !fileStacked && (fileRenamed || photo.PhotoName == "") {
photo.PhotoPath = filePath
if !o.Stack || !stripSequence || photo.PhotoStack == entity.IsUnstacked {
@ -386,6 +389,13 @@ func (ind *Index) MediaFile(m *MediaFile, o IndexOptions, originalName, photoUID
file.SetHDR(metaData.IsHDR())
file.SetColorProfile(metaData.ColorProfile)
if file.OriginalName == "" && filepath.Base(file.FileName) != metaData.FileName {
file.OriginalName = metaData.FileName
if photo.OriginalName == "" {
photo.OriginalName = fs.StripKnownExt(metaData.FileName)
}
}
if metaData.HasInstanceID() {
log.Infof("index: %s has instance_id %s", logName, sanitize.Log(metaData.InstanceID))
@ -438,6 +448,13 @@ func (ind *Index) MediaFile(m *MediaFile, o IndexOptions, originalName, photoUID
file.InstanceID = metaData.InstanceID
}
if file.OriginalName == "" && filepath.Base(file.FileName) != metaData.FileName {
file.OriginalName = metaData.FileName
if photo.OriginalName == "" {
photo.OriginalName = fs.StripKnownExt(metaData.FileName)
}
}
file.FileCodec = metaData.Codec
file.FileWidth = m.Width()
file.FileHeight = m.Height()
@ -491,6 +508,13 @@ func (ind *Index) MediaFile(m *MediaFile, o IndexOptions, originalName, photoUID
file.InstanceID = metaData.InstanceID
}
if file.OriginalName == "" && filepath.Base(file.FileName) != metaData.FileName {
file.OriginalName = metaData.FileName
if photo.OriginalName == "" {
photo.OriginalName = fs.StripKnownExt(metaData.FileName)
}
}
file.FileCodec = metaData.Codec
file.FileWidth = m.Width()
file.FileHeight = m.Height()