From f7e7073f357b87bed19961708b9ead6da4145fee Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Wed, 30 Dec 2020 13:36:23 +0100 Subject: [PATCH] Metadata: Rename metadata.go to mediafile_meta.go #755 #759 --- internal/photoprism/mediafile_meta.go | 108 ++++++++++++++++++++++++++ internal/photoprism/metadata.go | 47 ----------- 2 files changed, 108 insertions(+), 47 deletions(-) create mode 100644 internal/photoprism/mediafile_meta.go delete mode 100644 internal/photoprism/metadata.go diff --git a/internal/photoprism/mediafile_meta.go b/internal/photoprism/mediafile_meta.go new file mode 100644 index 000000000..ac7d3ff6b --- /dev/null +++ b/internal/photoprism/mediafile_meta.go @@ -0,0 +1,108 @@ +package photoprism + +import ( + "fmt" + "path/filepath" + + "github.com/photoprism/photoprism/internal/entity" + + "github.com/photoprism/photoprism/internal/meta" + "github.com/photoprism/photoprism/pkg/fs" + "github.com/photoprism/photoprism/pkg/txt" +) + +// HasSidecarJson returns true if this file has or is a json sidecar file. +func (m *MediaFile) HasSidecarJson() bool { + if m.IsJson() { + return true + } + + return fs.FormatJson.FindFirst(m.FileName(), []string{Config().SidecarPath(), fs.HiddenPath}, Config().OriginalsPath(), false) != "" +} + +// SidecarJsonName returns the corresponding JSON sidecar file name as used by Google Photos (and potentially other apps). +func (m *MediaFile) SidecarJsonName() string { + jsonName := m.fileName + ".json" + + if fs.FileExists(jsonName) { + return jsonName + } + + return "" +} + +// ExifToolJsonName returns the cached ExifTool metadata file name. +func (m *MediaFile) ExifToolJsonName() (string, error) { + if Config().DisableExifTool() { + return "", fmt.Errorf("media: exiftool json files disabled") + } + + return CacheName(m.Hash(), "json", "exiftool.json") +} + +// NeedsExifToolJson tests if an ExifTool JSON file needs to be created. +func (m *MediaFile) NeedsExifToolJson() bool { + if m.Root() == entity.RootSidecar || !m.IsMedia() { + return false + } + + jsonName, err := m.ExifToolJsonName() + + if err != nil { + return false + } + + return !fs.FileExists(jsonName) +} + +// ReadExifToolJson reads metadata from a cached ExifTool JSON file. +func (m *MediaFile) ReadExifToolJson() error { + jsonName, err := m.ExifToolJsonName() + + if err != nil { + return err + } + + return m.metaData.JSON(jsonName, "") +} + +// MetaData returns exif meta data of a media file. +func (m *MediaFile) MetaData() (result meta.Data) { + m.metaDataOnce.Do(func() { + var err error + + if m.ExifSupported() { + err = m.metaData.Exif(m.FileName(), m.FileType()) + } else { + err = fmt.Errorf("exif not supported: %s", txt.Quote(m.BaseName())) + } + + // Parse regular JSON sidecar files ("img_1234.json") + if !m.IsSidecar() { + if jsonFiles := fs.FormatJson.FindAll(m.FileName(), []string{Config().SidecarPath(), fs.HiddenPath}, Config().OriginalsPath(), false); len(jsonFiles) == 0 { + log.Debugf("media: no json sidecar file found for %s", txt.Quote(filepath.Base(m.FileName()))) + } else { + for _, jsonFile := range jsonFiles { + jsonErr := m.metaData.JSON(jsonFile, m.BaseName()) + + if jsonErr != nil { + log.Debug(jsonErr) + } else { + err = nil + } + } + } + + if err := m.ReadExifToolJson(); err != nil { + log.Debug(err) + } + } + + if err != nil { + m.metaData.Error = err + log.Debugf("media: %s", err.Error()) + } + }) + + return m.metaData +} diff --git a/internal/photoprism/metadata.go b/internal/photoprism/metadata.go deleted file mode 100644 index ba303b3a5..000000000 --- a/internal/photoprism/metadata.go +++ /dev/null @@ -1,47 +0,0 @@ -package photoprism - -import ( - "fmt" - "path/filepath" - - "github.com/photoprism/photoprism/internal/meta" - "github.com/photoprism/photoprism/pkg/fs" - "github.com/photoprism/photoprism/pkg/txt" -) - -// MetaData returns exif meta data of a media file. -func (m *MediaFile) MetaData() (result meta.Data) { - m.metaDataOnce.Do(func() { - var err error - - if m.ExifSupported() { - err = m.metaData.Exif(m.FileName(), m.FileType()) - } else { - err = fmt.Errorf("exif not supported: %s", txt.Quote(m.BaseName())) - } - - // Parse regular JSON sidecar files ("img_1234.json") - if !m.IsSidecar() { - if jsonFiles := fs.FormatJson.FindAll(m.FileName(), []string{Config().SidecarPath(), fs.HiddenPath}, Config().OriginalsPath(), false); len(jsonFiles) == 0 { - log.Debugf("media: no json sidecar file found for %s", txt.Quote(filepath.Base(m.FileName()))) - } else { - for _, jsonFile := range jsonFiles { - jsonErr := m.metaData.JSON(jsonFile, m.BaseName()) - - if jsonErr != nil { - log.Debug(jsonErr) - } else { - err = nil - } - } - } - } - - if err != nil { - m.metaData.Error = err - log.Debugf("media: %s", err.Error()) - } - }) - - return m.metaData -}