photoprism/internal/entity/photo_yaml.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

70 lines
1.3 KiB
Go

package entity
import (
"os"
"path/filepath"
"sync"
"github.com/photoprism/photoprism/pkg/fs"
"gopkg.in/yaml.v2"
)
var photoYamlMutex = sync.Mutex{}
// Yaml returns photo data as YAML string.
func (m *Photo) Yaml() ([]byte, error) {
// Load details if not done yet.
m.GetDetails()
out, err := yaml.Marshal(m)
if err != nil {
return []byte{}, err
}
return out, err
}
// SaveAsYaml saves photo data as YAML file.
func (m *Photo) SaveAsYaml(fileName string) error {
data, err := m.Yaml()
if err != nil {
return err
}
// Make sure directory exists.
if err := os.MkdirAll(filepath.Dir(fileName), os.ModePerm); err != nil {
return err
}
photoYamlMutex.Lock()
defer photoYamlMutex.Unlock()
// Write YAML data to file.
if err := os.WriteFile(fileName, data, os.ModePerm); err != nil {
return err
}
return nil
}
// LoadFromYaml photo data from a YAML file.
func (m *Photo) LoadFromYaml(fileName string) error {
data, err := os.ReadFile(fileName)
if err != nil {
return err
}
if err := yaml.Unmarshal(data, m); err != nil {
return err
}
return nil
}
// YamlFileName returns the YAML backup file name.
func (m *Photo) YamlFileName(originalsPath, sidecarPath string) string {
return fs.FileName(filepath.Join(originalsPath, m.PhotoPath, m.PhotoName), sidecarPath, originalsPath, fs.YamlExt)
}