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

66 lines
1.3 KiB
Go

package entity
import (
"os"
"path/filepath"
"sync"
"github.com/photoprism/photoprism/pkg/fs"
"gopkg.in/yaml.v2"
)
var albumYamlMutex = sync.Mutex{}
// Yaml returns album data as YAML string.
func (m *Album) Yaml() (out []byte, err error) {
if err := Db().Model(m).Association("Photos").Find(&m.Photos).Error; err != nil {
log.Errorf("album: %s (yaml)", err)
return out, err
}
return yaml.Marshal(m)
}
// 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.
if err := os.MkdirAll(filepath.Dir(fileName), os.ModePerm); err != nil {
return err
}
albumYamlMutex.Lock()
defer albumYamlMutex.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 *Album) 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 *Album) YamlFileName(albumsPath string) string {
return filepath.Join(albumsPath, m.AlbumType, m.AlbumUID+fs.YamlExt)
}