Metadata: Read title, description, date and keywords from apple xmp

This commit is contained in:
theresa 2021-03-30 11:38:32 +02:00
parent 2bace7954d
commit defe3d5cba
2 changed files with 49 additions and 2 deletions

View file

@ -57,5 +57,14 @@ func (data *Data) XMP(fileName string) (err error) {
data.LensModel = doc.LensModel()
}
if takenAt := doc.DateCreated(); !takenAt.IsZero() {
data.TakenAt = takenAt
}
if len(doc.Keywords()) != 0 {
data.Keywords += doc.Keywords()
data.Keywords = SanitizeMeta(data.Keywords)
}
return nil
}

View file

@ -3,6 +3,8 @@ package meta
import (
"encoding/xml"
"io/ioutil"
"strings"
"time"
)
// XmpDocument represents an XMP sidecar file.
@ -75,6 +77,10 @@ type XmpDocument struct {
Text string `xml:",chardata" json:"text,omitempty"`
Li []string `xml:"li"` // desk, coffee, computer
} `xml:"Bag" json:"bag,omitempty"`
Seq struct {
Text string `xml:",chardata" json:"text,omitempty"`
Li []string `xml:"li"` // desk, coffee, computer
} `xml:"Seq" json:"seq,omitempty"`
} `xml:"subject" json:"subject,omitempty"`
Rights struct {
Text string `xml:",chardata" json:"text,omitempty"`
@ -203,7 +209,14 @@ func (doc *XmpDocument) Load(filename string) error {
}
func (doc *XmpDocument) Title() string {
return SanitizeTitle(doc.RDF.Description.Title.Alt.Li.Text)
t := doc.RDF.Description.Title.Alt.Li.Text
t2 := doc.RDF.Description.Title.Text
if t != "" {
return SanitizeTitle(t)
} else if t2 != "" {
return SanitizeTitle(t2)
}
return ""
}
func (doc *XmpDocument) Artist() string {
@ -211,7 +224,14 @@ func (doc *XmpDocument) Artist() string {
}
func (doc *XmpDocument) Description() string {
return SanitizeDescription(doc.RDF.Description.Description.Alt.Li.Text)
d := doc.RDF.Description.Description.Alt.Li.Text
d2 := doc.RDF.Description.Description.Text
if d != "" {
return SanitizeDescription(d)
} else if d2 != "" {
return SanitizeTitle(d2)
}
return ""
}
func (doc *XmpDocument) Copyright() string {
@ -229,3 +249,21 @@ func (doc *XmpDocument) CameraModel() string {
func (doc *XmpDocument) LensModel() string {
return SanitizeString(doc.RDF.Description.LensModel)
}
func (doc *XmpDocument) DateCreated() time.Time {
s := doc.RDF.Description.DateCreated
if tv, err := time.Parse(time.RFC3339, s); err == nil {
return tv
} else if tv2, err2 := time.Parse("2006-01-02T15:04:05.999999999", s); err2 == nil {
return tv2
}
return time.Time{}
}
func (doc *XmpDocument) Keywords() string {
s := doc.RDF.Description.Subject.Seq.Li
return strings.Join(s, ", ")
}