2020-05-18 22:18:58 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-05-19 11:00:17 +02:00
|
|
|
"path/filepath"
|
2020-12-13 15:43:01 +01:00
|
|
|
"sync"
|
2020-05-18 22:18:58 +02:00
|
|
|
|
2020-05-19 11:00:17 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-05-18 22:18:58 +02:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2020-12-13 15:43:01 +01:00
|
|
|
var photoYamlMutex = sync.Mutex{}
|
|
|
|
|
2020-05-18 22:18:58 +02:00
|
|
|
// Yaml returns photo data as YAML string.
|
|
|
|
func (m *Photo) Yaml() ([]byte, error) {
|
2021-02-05 16:32:08 +01:00
|
|
|
// Load details if not done yet.
|
|
|
|
m.GetDetails()
|
|
|
|
|
2020-05-18 22:18:58 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-19 19:49:32 +02:00
|
|
|
// Make sure directory exists.
|
|
|
|
if err := os.MkdirAll(filepath.Dir(fileName), os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-13 15:43:01 +01:00
|
|
|
photoYamlMutex.Lock()
|
|
|
|
defer photoYamlMutex.Unlock()
|
|
|
|
|
2020-05-19 19:49:32 +02:00
|
|
|
// Write YAML data to file.
|
2020-05-18 22:18:58 +02:00
|
|
|
if err := ioutil.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 := ioutil.ReadFile(fileName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := yaml.Unmarshal(data, m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-19 11:00:17 +02:00
|
|
|
|
|
|
|
// YamlFileName returns the YAML backup file name.
|
2020-06-07 10:09:35 +02:00
|
|
|
func (m *Photo) YamlFileName(originalsPath, sidecarPath string) string {
|
2020-12-12 13:05:58 +01:00
|
|
|
return fs.FileName(filepath.Join(originalsPath, m.PhotoPath, m.PhotoName), sidecarPath, originalsPath, fs.YamlExt)
|
2020-05-19 11:00:17 +02:00
|
|
|
}
|