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"
|
2021-10-06 07:10:50 +02:00
|
|
|
"os"
|
2020-05-13 20:53:15 +02:00
|
|
|
"path/filepath"
|
2020-07-13 10:41:45 +02:00
|
|
|
"runtime/debug"
|
2020-05-13 20:53:15 +02:00
|
|
|
|
2020-12-30 13:33:47 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
|
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))
|
2020-12-30 13:33:47 +01:00
|
|
|
|
|
|
|
if !fs.FileExists(jsonName) {
|
2021-02-11 21:37:44 +01:00
|
|
|
return fmt.Errorf("metadata: %s not found", quotedName)
|
2020-12-30 13:33:47 +01:00
|
|
|
}
|
|
|
|
|
2021-10-06 07:10:50 +02:00
|
|
|
jsonData, err := os.ReadFile(jsonName)
|
2020-05-13 20:53:15 +02:00
|
|
|
|
|
|
|
if err != nil {
|
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-12-25 20:29:06 +01:00
|
|
|
log.Warnf("metadata: unknown json in %s", quotedName)
|
|
|
|
|
|
|
|
return fmt.Errorf("unknown json in %s", quotedName)
|
2020-05-13 20:53:15 +02:00
|
|
|
}
|