feat: add download directory placeholders, category support (#1188)

This commit is contained in:
Copilot authored and GitHub committed 2025-11-27 18:39:08 +08:00
1 parent 9d9f1caaaa
commit 49b4d99b5e
31 files changed
+1204 -118

No files matched your search

+26
View File
@@ -8,6 +8,7 @@ import (
syspath "path"
"path/filepath"
"strings"
"time"
)
func Dir(path string) string {
@@ -199,3 +200,28 @@ func ReplaceInvalidFilename(path string) string {
}
return path
}
// ReplacePathPlaceholders replaces date placeholders in a path with actual values
// Supported placeholders:
// - %year% - Current year (e.g., 2025)
// - %month% - Current month (01-12)
// - %day% - Current day (01-31)
// - %date% - Full date format (2025-01-01)
func ReplacePathPlaceholders(path string) string {
if path == "" {
return ""
}
now := time.Now()
year := fmt.Sprintf("%d", now.Year())
month := fmt.Sprintf("%02d", now.Month())
day := fmt.Sprintf("%02d", now.Day())
date := fmt.Sprintf("%s-%s-%s", year, month, day)
path = strings.ReplaceAll(path, "%year%", year)
path = strings.ReplaceAll(path, "%month%", month)
path = strings.ReplaceAll(path, "%day%", day)
path = strings.ReplaceAll(path, "%date%", date)
return path
}
+75
View File
@@ -1,8 +1,11 @@
package util
import (
"fmt"
"os"
"strings"
"testing"
"time"
)
func TestDir(t *testing.T) {
@@ -240,3 +243,75 @@ func TestReplaceInvalidFilename(t *testing.T) {
})
}
}
func TestReplacePathPlaceholders(t *testing.T) {
now := time.Now()
year := fmt.Sprintf("%d", now.Year())
month := fmt.Sprintf("%02d", now.Month())
day := fmt.Sprintf("%02d", now.Day())
date := fmt.Sprintf("%s-%s-%s", year, month, day)
tests := []struct {
name string
path string
want string
}{
{
name: "empty path",
path: "",
want: "",
},
{
name: "no placeholders",
path: "/home/user/Downloads",
want: "/home/user/Downloads",
},
{
name: "year placeholder",
path: "/Downloads/%year%",
want: "/Downloads/" + year,
},
{
name: "month placeholder",
path: "/Downloads/%month%",
want: "/Downloads/" + month,
},
{
name: "day placeholder",
path: "/Downloads/%day%",
want: "/Downloads/" + day,
},
{
name: "date placeholder",
path: "/Downloads/%date%",
want: "/Downloads/" + date,
},
{
name: "multiple placeholders",
path: "/Downloads/%year%-%month%",
want: "/Downloads/" + year + "-" + month,
},
{
name: "mixed path with placeholders",
path: "/home/user/Downloads/%year%/%month%/%day%",
want: "/home/user/Downloads/" + year + "/" + month + "/" + day,
},
{
name: "windows style path",
path: "D:\\Downloads\\%year%-%month%",
want: "D:\\Downloads\\" + year + "-" + month,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ReplacePathPlaceholders(tt.path)
if !strings.Contains(got, year) && tt.path != "" && strings.Contains(tt.path, "%year%") {
t.Errorf("ReplacePathPlaceholders() = %v, want containing year %v", got, year)
}
if got != tt.want {
t.Errorf("ReplacePathPlaceholders() = %v, want %v", got, tt.want)
}
})
}
}