Skip to content

Commit c36e66c

Browse files
committed
Config: Add fs.ExtYml file extension const for transitioning to ".yaml"
Signed-off-by: Michael Mayer <[email protected]>
1 parent dbf0fa6 commit c36e66c

File tree

9 files changed

+103
-8
lines changed

9 files changed

+103
-8
lines changed

internal/api/photos.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func GetPhotoYaml(router *gin.RouterGroup) {
206206
}
207207

208208
if c.Query("download") != "" {
209-
AddDownloadHeader(c, clean.UID(c.Param("uid"))+fs.ExtYaml)
209+
AddDownloadHeader(c, clean.UID(c.Param("uid"))+fs.ExtYml)
210210
}
211211

212212
c.Data(http.StatusOK, "text/x-yaml; charset=utf-8", data)

internal/config/config_vision.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import (
99
)
1010

1111
// VisionYaml returns the vision config YAML filename.
12+
//
13+
// TODO: Call fs.YamlFilePath to use ".yaml" extension for new YAML files, unless a .yml" file already exists.
14+
//
15+
// return fs.YamlFilePath("vision", c.ConfigPath(), c.options.VisionYaml)
1216
func (c *Config) VisionYaml() string {
1317
if c.options.VisionYaml != "" {
1418
return fs.Abs(c.options.VisionYaml)

internal/entity/album_yaml.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (m *Album) YamlFileName(backupPath string) (absolute, relative string, err
6868
return "", "", fmt.Errorf("album uid is empty")
6969
}
7070

71-
relative = filepath.Join(m.AlbumType, m.AlbumUID+fs.ExtYaml)
71+
relative = filepath.Join(m.AlbumType, m.AlbumUID+fs.ExtYml)
7272

7373
if backupPath == "" {
7474
return "", relative, fmt.Errorf("backup path is empty")

internal/entity/photo_yaml.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ func (m *Photo) SaveAsYaml(fileName string) error {
6767

6868
// YamlFileName returns both the absolute file path and the relative name for the YAML sidecar file, e.g. for logging.
6969
func (m *Photo) YamlFileName(originalsPath, sidecarPath string) (absolute, relative string, err error) {
70-
absolute, err = fs.FileName(filepath.Join(originalsPath, m.PhotoPath, m.PhotoName), sidecarPath, originalsPath, fs.ExtYaml)
71-
relative = filepath.Join(m.PhotoPath, m.PhotoName) + fs.ExtYaml
70+
absolute, err = fs.FileName(filepath.Join(originalsPath, m.PhotoPath, m.PhotoName), sidecarPath, originalsPath, fs.ExtYml)
71+
relative = filepath.Join(m.PhotoPath, m.PhotoName) + fs.ExtYml
7272

7373
return absolute, relative, err
7474
}

internal/server/webdav.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func WebDAVFileName(request *http.Request, router *gin.RouterGroup, conf *config
169169

170170
// WebDAVSetFavoriteFlag adds the favorite flag to files uploaded via WebDAV.
171171
func WebDAVSetFavoriteFlag(fileName string) {
172-
yamlName := fs.AbsPrefix(fileName, false) + fs.ExtYaml
172+
yamlName := fs.AbsPrefix(fileName, false) + fs.ExtYml
173173

174174
// Abort if YAML file already exists to avoid overwriting metadata.
175175
if fs.FileExists(yamlName) {

pkg/fs/file_ext.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ const (
4444
ExtMp4 = ".mp4"
4545
ExtMov = ".mov"
4646
ExtQT = ".qt"
47-
ExtYaml = ".yml"
47+
ExtYml = ".yml"
48+
ExtYaml = ".yaml"
4849
ExtJson = ".json"
4950
ExtXml = ".xml"
5051
ExtXMP = ".xmp"

pkg/fs/file_exts.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ var Extensions = FileExtensions{
8585
ExtXMP: SidecarXMP,
8686
".aae": SidecarAppleXml,
8787
ExtXml: SidecarXml,
88-
ExtYaml: SidecarYaml, // .yml
89-
".yaml": SidecarYaml,
88+
ExtYaml: SidecarYaml, // .yaml
89+
".yml": SidecarYaml,
9090
ExtJson: SidecarJson,
9191
ExtTxt: SidecarText,
9292
".nfo": SidecarInfo,

pkg/fs/yaml.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package fs
2+
3+
import (
4+
"path/filepath"
5+
)
6+
7+
// YamlFilePath returns the appropriate YAML file name to use. This can be either
8+
// the absolute path of the custom file name passed as the first argument, the default
9+
// name with a ".yml" extension if it already exists, or the default name with a ".yaml"
10+
// extension if a ".yml" file does not exist. This facilitates the transition from ".yml"
11+
// to the new default YAML file extension, ".yaml".
12+
func YamlFilePath(yamlName, yamlDir, customFileName string) string {
13+
// Return custom file name with absolute path.
14+
if customFileName != "" {
15+
return Abs(customFileName)
16+
}
17+
18+
// If the file already exists, return the file path with the legacy "*.yml" extension.
19+
if filePathYml := filepath.Join(yamlDir, yamlName+ExtYml); FileExists(filePathYml) {
20+
return filePathYml
21+
}
22+
23+
// Return file path with ".yaml" extension.
24+
return filepath.Join(yamlDir, yamlName+ExtYaml)
25+
}

pkg/fs/yaml_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package fs
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
// Tests for YamlFilePath in yaml.go using subtests.
12+
func TestYamlFilePath(t *testing.T) {
13+
t.Run("CustomPath", func(t *testing.T) {
14+
tmp := t.TempDir()
15+
rel := filepath.Join(tmp, "custom", "config.yaml")
16+
// Do not create the file; function should simply return Abs(customFileName).
17+
expected := Abs(rel)
18+
got := YamlFilePath("", "", rel)
19+
assert.Equal(t, expected, got)
20+
})
21+
22+
t.Run("PreferYmlIfExists", func(t *testing.T) {
23+
dir := t.TempDir()
24+
name := "app-config"
25+
26+
// Create .yml file
27+
ymlPath := filepath.Join(dir, name+ExtYml)
28+
err := os.WriteFile(ymlPath, []byte("foo: bar\n"), 0o644)
29+
if err != nil {
30+
t.Fatalf("write %s: %v", ymlPath, err)
31+
}
32+
33+
got := YamlFilePath(name, dir, "")
34+
assert.Equal(t, ymlPath, got)
35+
})
36+
37+
t.Run("DefaultYamlWhenYmlMissing", func(t *testing.T) {
38+
dir := t.TempDir()
39+
name := "settings"
40+
41+
// Ensure .yml does not exist; do not create it.
42+
expected := filepath.Join(dir, name+ExtYaml)
43+
got := YamlFilePath(name, dir, "")
44+
assert.Equal(t, expected, got)
45+
})
46+
47+
t.Run("BothExistReturnsYml", func(t *testing.T) {
48+
dir := t.TempDir()
49+
name := "prefs"
50+
51+
// Create both files
52+
ymlPath := filepath.Join(dir, name+ExtYml)
53+
yamlPath := filepath.Join(dir, name+ExtYaml)
54+
55+
if err := os.WriteFile(ymlPath, []byte("a: 1\n"), 0o644); err != nil {
56+
t.Fatalf("write %s: %v", ymlPath, err)
57+
}
58+
if err := os.WriteFile(yamlPath, []byte("a: 2\n"), 0o644); err != nil {
59+
t.Fatalf("write %s: %v", yamlPath, err)
60+
}
61+
62+
got := YamlFilePath(name, dir, "")
63+
assert.Equal(t, ymlPath, got)
64+
})
65+
}

0 commit comments

Comments
 (0)