2020-05-13 20:53:15 +02:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2020-07-11 16:46:29 +02:00
|
|
|
"bytes"
|
2020-05-13 20:53:15 +02:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// JSON parses a json sidecar file (as used by Exiftool) and returns a Data struct.
|
2020-06-04 14:56:27 +02:00
|
|
|
func JSON(jsonName, originalName string) (data Data, err error) {
|
|
|
|
err = data.JSON(jsonName, originalName)
|
2020-05-13 20:53:15 +02:00
|
|
|
|
|
|
|
return data, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSON parses a json sidecar file (as used by Exiftool) and returns a Data struct.
|
2020-06-04 14:56:27 +02:00
|
|
|
func (data *Data) JSON(jsonName, originalName string) (err error) {
|
2020-05-13 20:53:15 +02:00
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
2020-06-29 13:16:55 +02:00
|
|
|
err = fmt.Errorf("metadata: %s (json panic)", e)
|
2020-05-13 20:53:15 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if data.All == nil {
|
|
|
|
data.All = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
2020-07-11 16:46:29 +02:00
|
|
|
quotedName := txt.Quote(filepath.Base(jsonName))
|
|
|
|
jsonData, err := ioutil.ReadFile(jsonName)
|
2020-05-13 20:53:15 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-06-29 13:16:55 +02:00
|
|
|
log.Warnf("metadata: %s (json)", err.Error())
|
2020-07-11 16:46:29 +02:00
|
|
|
return fmt.Errorf("can't read json file %s", quotedName)
|
2020-05-13 20:53:15 +02:00
|
|
|
}
|
|
|
|
|
2020-07-11 16:46:29 +02:00
|
|
|
if bytes.Contains(jsonData, []byte("ExifToolVersion")) {
|
|
|
|
return data.Exiftool(jsonData, originalName)
|
|
|
|
} else if bytes.Contains(jsonData, []byte("geoData")) {
|
|
|
|
return data.GPhotos(jsonData)
|
2020-05-14 11:57:26 +02:00
|
|
|
}
|
|
|
|
|
2020-07-11 16:46:29 +02:00
|
|
|
log.Warnf("metadata: unknown format in %s (json)", quotedName)
|
|
|
|
return fmt.Errorf("unknown json format in %s", quotedName)
|
2020-05-13 20:53:15 +02:00
|
|
|
}
|