Backend: Refactor Google Photos metadata parsing

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
Michael Mayer 2020-07-11 20:47:52 +02:00
parent 4d93749b06
commit 8fd381860a
3 changed files with 30 additions and 9 deletions

View file

@ -38,8 +38,10 @@ func (data *Data) JSON(jsonName, originalName string) (err error) {
if bytes.Contains(jsonData, []byte("ExifToolVersion")) {
return data.Exiftool(jsonData, originalName)
} else if bytes.Contains(jsonData, []byte("geoData")) {
return data.GPhotos(jsonData)
} else if bytes.Contains(jsonData, []byte("albumData")) {
return data.GMeta(jsonData)
} else if bytes.Contains(jsonData, []byte("photoTakenTime")) {
return data.GPhoto(jsonData)
}
log.Warnf("metadata: unknown format in %s (json)", quotedName)

View file

@ -12,7 +12,6 @@ type GPhoto struct {
Title string `json:"title"`
Description string `json:"description"`
Views int `json:"imageViews,string"`
Album GAlbum `json:"albumData"`
Geo GGeo `json:"geoData"`
TakenAt GTime `json:"photoTakenTime"`
CreatedAt GTime `json:"creationTime"`
@ -27,6 +26,10 @@ func (m GPhoto) SanitizedDescription() string {
return SanitizeDescription(m.Description)
}
type GMeta struct {
Album GAlbum `json:"albumData"`
}
type GAlbum struct {
Title string `json:"title"`
Description string `json:"description"`
@ -64,7 +67,28 @@ func (m GTime) Time() time.Time {
}
// Parses JSON sidecar data as created by Google Photos.
func (data *Data) GPhotos(jsonData []byte) (err error) {
func (data *Data) GMeta(jsonData []byte) (err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("metadata: %s (gphotos panic)", e)
}
}()
p := GMeta{}
if err := json.Unmarshal(jsonData, &p); err != nil {
return err
}
if p.Album.Exists() {
data.Albums = append(data.Albums, p.Album.Title)
}
return nil
}
// Parses JSON photo sidecar data as created by Google Photos.
func (data *Data) GPhoto(jsonData []byte) (err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("metadata: %s (gphotos panic)", e)
@ -100,10 +124,6 @@ func (data *Data) GPhotos(jsonData []byte) (err error) {
data.Altitude = int(p.Geo.Altitude)
}
if p.Album.Exists() {
data.Albums = append(data.Albums, p.Album.Title)
}
// Set time zone and calculate UTC time.
if data.Lat != 0 && data.Lng != 0 {
zones, err := tz.GetZone(tz.Point{

View file

@ -560,7 +560,6 @@ func TestMediaFile_RelatedFiles(t *testing.T) {
assert.Equal(t, "2015-02-04.jpg", related.Main.BaseName())
assert.Equal(t, "2015-02-04.jpg", related.Files[0].BaseName())
assert.Equal(t, "2015-02-04(1).jpg", related.Files[1].BaseName())
assert.Equal(t, "2015-02-04.jpg.json", related.Files[2].BaseName())