mirror of
https://github.com/photoprism/photoprism.git
synced 2025-12-12 00:34:13 +01:00
Config: Add fs.ExtYml file extension const for transitioning to ".yaml"
Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
@@ -206,7 +206,7 @@ func GetPhotoYaml(router *gin.RouterGroup) {
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
@@ -9,6 +9,10 @@ import (
|
||||
)
|
||||
|
||||
// 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 {
|
||||
if c.options.VisionYaml != "" {
|
||||
return fs.Abs(c.options.VisionYaml)
|
||||
|
||||
@@ -68,7 +68,7 @@ func (m *Album) YamlFileName(backupPath string) (absolute, relative string, err
|
||||
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 == "" {
|
||||
return "", relative, fmt.Errorf("backup path is empty")
|
||||
|
||||
@@ -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.
|
||||
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)
|
||||
relative = filepath.Join(m.PhotoPath, m.PhotoName) + 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.ExtYml
|
||||
|
||||
return absolute, relative, err
|
||||
}
|
||||
|
||||
@@ -169,7 +169,7 @@ func WebDAVFileName(request *http.Request, router *gin.RouterGroup, conf *config
|
||||
|
||||
// WebDAVSetFavoriteFlag adds the favorite flag to files uploaded via WebDAV.
|
||||
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.
|
||||
if fs.FileExists(yamlName) {
|
||||
|
||||
@@ -44,7 +44,8 @@ const (
|
||||
ExtMp4 = ".mp4"
|
||||
ExtMov = ".mov"
|
||||
ExtQT = ".qt"
|
||||
ExtYaml = ".yml"
|
||||
ExtYml = ".yml"
|
||||
ExtYaml = ".yaml"
|
||||
ExtJson = ".json"
|
||||
ExtXml = ".xml"
|
||||
ExtXMP = ".xmp"
|
||||
|
||||
@@ -85,8 +85,8 @@ var Extensions = FileExtensions{
|
||||
ExtXMP: SidecarXMP,
|
||||
".aae": SidecarAppleXml,
|
||||
ExtXml: SidecarXml,
|
||||
ExtYaml: SidecarYaml, // .yml
|
||||
".yaml": SidecarYaml,
|
||||
ExtYaml: SidecarYaml, // .yaml
|
||||
".yml": SidecarYaml,
|
||||
ExtJson: SidecarJson,
|
||||
ExtTxt: SidecarText,
|
||||
".nfo": SidecarInfo,
|
||||
|
||||
25
pkg/fs/yaml.go
Normal file
25
pkg/fs/yaml.go
Normal 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
65
pkg/fs/yaml_test.go
Normal 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)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user