Config: Add fs.ExtYml file extension const for transitioning to ".yaml"

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer
2025-09-13 02:09:22 +02:00
parent dbf0fa6c25
commit c36e66c847
9 changed files with 103 additions and 8 deletions

View File

@@ -206,7 +206,7 @@ func GetPhotoYaml(router *gin.RouterGroup) {
} }
if c.Query("download") != "" { if c.Query("download") != "" {
AddDownloadHeader(c, clean.UID(c.Param("uid"))+fs.ExtYaml) AddDownloadHeader(c, clean.UID(c.Param("uid"))+fs.ExtYml)
} }
c.Data(http.StatusOK, "text/x-yaml; charset=utf-8", data) c.Data(http.StatusOK, "text/x-yaml; charset=utf-8", data)

View File

@@ -9,6 +9,10 @@ import (
) )
// VisionYaml returns the vision config YAML filename. // VisionYaml returns the vision config YAML filename.
//
// TODO: Call fs.YamlFilePath to use ".yaml" extension for new YAML files, unless a .yml" file already exists.
//
// return fs.YamlFilePath("vision", c.ConfigPath(), c.options.VisionYaml)
func (c *Config) VisionYaml() string { func (c *Config) VisionYaml() string {
if c.options.VisionYaml != "" { if c.options.VisionYaml != "" {
return fs.Abs(c.options.VisionYaml) return fs.Abs(c.options.VisionYaml)

View File

@@ -68,7 +68,7 @@ func (m *Album) YamlFileName(backupPath string) (absolute, relative string, err
return "", "", fmt.Errorf("album uid is empty") return "", "", fmt.Errorf("album uid is empty")
} }
relative = filepath.Join(m.AlbumType, m.AlbumUID+fs.ExtYaml) relative = filepath.Join(m.AlbumType, m.AlbumUID+fs.ExtYml)
if backupPath == "" { if backupPath == "" {
return "", relative, fmt.Errorf("backup path is empty") return "", relative, fmt.Errorf("backup path is empty")

View File

@@ -67,8 +67,8 @@ func (m *Photo) SaveAsYaml(fileName string) error {
// YamlFileName returns both the absolute file path and the relative name for the YAML sidecar file, e.g. for logging. // YamlFileName returns both the absolute file path and the relative name for the YAML sidecar file, e.g. for logging.
func (m *Photo) YamlFileName(originalsPath, sidecarPath string) (absolute, relative string, err error) { func (m *Photo) YamlFileName(originalsPath, sidecarPath string) (absolute, relative string, err error) {
absolute, err = fs.FileName(filepath.Join(originalsPath, m.PhotoPath, m.PhotoName), sidecarPath, originalsPath, fs.ExtYaml) absolute, err = fs.FileName(filepath.Join(originalsPath, m.PhotoPath, m.PhotoName), sidecarPath, originalsPath, fs.ExtYml)
relative = filepath.Join(m.PhotoPath, m.PhotoName) + fs.ExtYaml relative = filepath.Join(m.PhotoPath, m.PhotoName) + fs.ExtYml
return absolute, relative, err return absolute, relative, err
} }

View File

@@ -169,7 +169,7 @@ func WebDAVFileName(request *http.Request, router *gin.RouterGroup, conf *config
// WebDAVSetFavoriteFlag adds the favorite flag to files uploaded via WebDAV. // WebDAVSetFavoriteFlag adds the favorite flag to files uploaded via WebDAV.
func WebDAVSetFavoriteFlag(fileName string) { func WebDAVSetFavoriteFlag(fileName string) {
yamlName := fs.AbsPrefix(fileName, false) + fs.ExtYaml yamlName := fs.AbsPrefix(fileName, false) + fs.ExtYml
// Abort if YAML file already exists to avoid overwriting metadata. // Abort if YAML file already exists to avoid overwriting metadata.
if fs.FileExists(yamlName) { if fs.FileExists(yamlName) {

View File

@@ -44,7 +44,8 @@ const (
ExtMp4 = ".mp4" ExtMp4 = ".mp4"
ExtMov = ".mov" ExtMov = ".mov"
ExtQT = ".qt" ExtQT = ".qt"
ExtYaml = ".yml" ExtYml = ".yml"
ExtYaml = ".yaml"
ExtJson = ".json" ExtJson = ".json"
ExtXml = ".xml" ExtXml = ".xml"
ExtXMP = ".xmp" ExtXMP = ".xmp"

View File

@@ -85,8 +85,8 @@ var Extensions = FileExtensions{
ExtXMP: SidecarXMP, ExtXMP: SidecarXMP,
".aae": SidecarAppleXml, ".aae": SidecarAppleXml,
ExtXml: SidecarXml, ExtXml: SidecarXml,
ExtYaml: SidecarYaml, // .yml ExtYaml: SidecarYaml, // .yaml
".yaml": SidecarYaml, ".yml": SidecarYaml,
ExtJson: SidecarJson, ExtJson: SidecarJson,
ExtTxt: SidecarText, ExtTxt: SidecarText,
".nfo": SidecarInfo, ".nfo": SidecarInfo,

25
pkg/fs/yaml.go Normal file
View File

@@ -0,0 +1,25 @@
package fs
import (
"path/filepath"
)
// YamlFilePath returns the appropriate YAML file name to use. This can be either
// the absolute path of the custom file name passed as the first argument, the default
// name with a ".yml" extension if it already exists, or the default name with a ".yaml"
// extension if a ".yml" file does not exist. This facilitates the transition from ".yml"
// to the new default YAML file extension, ".yaml".
func YamlFilePath(yamlName, yamlDir, customFileName string) string {
// Return custom file name with absolute path.
if customFileName != "" {
return Abs(customFileName)
}
// If the file already exists, return the file path with the legacy "*.yml" extension.
if filePathYml := filepath.Join(yamlDir, yamlName+ExtYml); FileExists(filePathYml) {
return filePathYml
}
// Return file path with ".yaml" extension.
return filepath.Join(yamlDir, yamlName+ExtYaml)
}

65
pkg/fs/yaml_test.go Normal file
View File

@@ -0,0 +1,65 @@
package fs
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
// Tests for YamlFilePath in yaml.go using subtests.
func TestYamlFilePath(t *testing.T) {
t.Run("CustomPath", func(t *testing.T) {
tmp := t.TempDir()
rel := filepath.Join(tmp, "custom", "config.yaml")
// Do not create the file; function should simply return Abs(customFileName).
expected := Abs(rel)
got := YamlFilePath("", "", rel)
assert.Equal(t, expected, got)
})
t.Run("PreferYmlIfExists", func(t *testing.T) {
dir := t.TempDir()
name := "app-config"
// Create .yml file
ymlPath := filepath.Join(dir, name+ExtYml)
err := os.WriteFile(ymlPath, []byte("foo: bar\n"), 0o644)
if err != nil {
t.Fatalf("write %s: %v", ymlPath, err)
}
got := YamlFilePath(name, dir, "")
assert.Equal(t, ymlPath, got)
})
t.Run("DefaultYamlWhenYmlMissing", func(t *testing.T) {
dir := t.TempDir()
name := "settings"
// Ensure .yml does not exist; do not create it.
expected := filepath.Join(dir, name+ExtYaml)
got := YamlFilePath(name, dir, "")
assert.Equal(t, expected, got)
})
t.Run("BothExistReturnsYml", func(t *testing.T) {
dir := t.TempDir()
name := "prefs"
// Create both files
ymlPath := filepath.Join(dir, name+ExtYml)
yamlPath := filepath.Join(dir, name+ExtYaml)
if err := os.WriteFile(ymlPath, []byte("a: 1\n"), 0o644); err != nil {
t.Fatalf("write %s: %v", ymlPath, err)
}
if err := os.WriteFile(yamlPath, []byte("a: 2\n"), 0o644); err != nil {
t.Fatalf("write %s: %v", yamlPath, err)
}
got := YamlFilePath(name, dir, "")
assert.Equal(t, ymlPath, got)
})
}