photoprism/internal/meta/json.go
Eng Zer Jun 44f7700c0c
Enable module graph pruning and deprecate io/ioutil (#1600)
* Backend: Enable Go module graph pruning and lazy module loading

This commit applies the changes by running `go mod tidy -go=1.17` to
enable module graph pruning and lazy module loading supported by Go 1.17
or higher.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* Backend: Move from io/ioutil to io and os package

The io/ioutil package has been deprecated as of Go 1.16, see
https://golang.org/doc/go1.16#ioutil. This commit replaces the existing
io/ioutil functions with their new definitions in io and os packages.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2021-10-06 07:10:50 +02:00

57 lines
1.4 KiB
Go

package meta
import (
"bytes"
"fmt"
"os"
"path/filepath"
"runtime/debug"
"github.com/photoprism/photoprism/pkg/fs"
"github.com/photoprism/photoprism/pkg/txt"
)
// JSON parses a json sidecar file (as used by Exiftool) and returns a Data struct.
func JSON(jsonName, originalName string) (data Data, err error) {
err = data.JSON(jsonName, originalName)
return data, err
}
// JSON parses a json sidecar file (as used by Exiftool) and returns a Data struct.
func (data *Data) JSON(jsonName, originalName string) (err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("metadata: %s in %s (json panic)\nstack: %s", e, txt.Quote(filepath.Base(jsonName)), debug.Stack())
}
}()
if data.All == nil {
data.All = make(map[string]string)
}
quotedName := txt.Quote(filepath.Base(jsonName))
if !fs.FileExists(jsonName) {
return fmt.Errorf("metadata: %s not found", quotedName)
}
jsonData, err := os.ReadFile(jsonName)
if err != nil {
return fmt.Errorf("can't read json file %s", quotedName)
}
if bytes.Contains(jsonData, []byte("ExifToolVersion")) {
return data.Exiftool(jsonData, originalName)
} 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 json in %s", quotedName)
return fmt.Errorf("unknown json in %s", quotedName)
}