2020-12-17 18:24:55 +01:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
2023-10-21 00:11:11 +02:00
|
|
|
"time"
|
2020-12-17 18:24:55 +01:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
2023-10-21 00:11:11 +02:00
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-12-17 18:24:55 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var albumYamlMutex = sync.Mutex{}
|
|
|
|
|
|
|
|
// Yaml returns album data as YAML string.
|
2020-12-18 20:42:12 +01:00
|
|
|
func (m *Album) Yaml() (out []byte, err error) {
|
2023-10-21 00:11:11 +02:00
|
|
|
m.CreatedAt = m.CreatedAt.UTC().Truncate(time.Second)
|
|
|
|
m.UpdatedAt = m.UpdatedAt.UTC().Truncate(time.Second)
|
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
if err := Db().Model(m).Association("Photos").Find(&m.Photos).Error; err != nil {
|
|
|
|
log.Errorf("album: %s (yaml)", err)
|
2020-12-18 20:42:12 +01:00
|
|
|
return out, err
|
2020-12-17 18:24:55 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
return yaml.Marshal(m)
|
2020-12-17 18:24:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SaveAsYaml saves album data as YAML file.
|
|
|
|
func (m *Album) SaveAsYaml(fileName string) error {
|
|
|
|
data, err := m.Yaml()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure directory exists.
|
2022-10-31 15:01:48 +01:00
|
|
|
if err := os.MkdirAll(filepath.Dir(fileName), fs.ModeDir); err != nil {
|
2020-12-17 18:24:55 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
albumYamlMutex.Lock()
|
|
|
|
defer albumYamlMutex.Unlock()
|
|
|
|
|
|
|
|
// Write YAML data to file.
|
2022-10-31 15:01:48 +01:00
|
|
|
if err := os.WriteFile(fileName, data, fs.ModeFile); err != nil {
|
2020-12-17 18:24:55 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadFromYaml photo data from a YAML file.
|
|
|
|
func (m *Album) LoadFromYaml(fileName string) error {
|
2021-10-06 07:10:50 +02:00
|
|
|
data, err := os.ReadFile(fileName)
|
2020-12-17 18:24:55 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := yaml.Unmarshal(data, m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-06 13:21:42 +01:00
|
|
|
// YamlFileName returns the YAML file name.
|
2020-12-17 18:24:55 +01:00
|
|
|
func (m *Album) YamlFileName(albumsPath string) string {
|
2022-04-15 09:42:07 +02:00
|
|
|
return filepath.Join(albumsPath, m.AlbumType, m.AlbumUID+fs.ExtYAML)
|
2020-12-17 18:24:55 +01:00
|
|
|
}
|