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"
|
2020-07-13 10:41:45 +02:00
|
|
|
"runtime/debug"
|
2020-05-13 20:53:15 +02:00
|
|
|
|
|
|
|
"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-07-13 10:41:45 +02:00
|
|
|
err = fmt.Errorf("metadata: %s in %s (json panic)\nstack: %s", e, txt.Quote(filepath.Base(jsonName)), debug.Stack())
|
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)
|
2020-07-11 20:47:52 +02:00
|
|
|
} else if bytes.Contains(jsonData, []byte("albumData")) {
|
|
|
|
return data.GMeta(jsonData)
|
|
|
|
} else if bytes.Contains(jsonData, []byte("photoTakenTime")) {
|
|
|
|
return data.GPhoto(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
|
|
|
}
|