Add config option to change prefix for Content-Disposition filename. Defaults to backup.

This commit is contained in:
eleksir
2026-03-27 09:50:51 +03:00
parent 607922be19
commit 565e40c9be
9 changed files with 53 additions and 6 deletions

View File

@@ -128,6 +128,13 @@ func LoadConfig(path string) error {
return errors.New("Option password is not set")
}
// FilenamePrefix: default.
if C.FilenamePrefix == "" {
C.FilenamePrefix = "backup"
log.Warnf("Config option filename_prefix is not set, fallback to %s", C.FilenamePrefix)
}
// Compile exclude patterns.
excludePatterns = make([]*regexp.Regexp, 0, len(C.ExcludePatterns))

View File

@@ -170,6 +170,27 @@ func TestLoadConfigNoHTTPS(t *testing.T) {
}
}
// TestLoadConfigFilenamePrefixCustom tests custom filename prefix.
func TestLoadConfigFilenamePrefixCustom(t *testing.T) {
config := `{
"user": "test",
"password": "test",
"directories": ["../../test_data/test1/foo"],
"nohttps": true,
"filename_prefix": "mybackup"
}`
tmpFile := writeTempConfig(t, config)
err := LoadConfig(tmpFile)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if C.FilenamePrefix != "mybackup" {
t.Errorf("C.FilenamePrefix should be mybackup, got %s", C.FilenamePrefix)
}
}
// TestLoadConfigDefaultValues tests default values are set correctly.
func TestLoadConfigDefaultValues(t *testing.T) {
config := `{
@@ -201,6 +222,10 @@ func TestLoadConfigDefaultValues(t *testing.T) {
if C.LogLevel != "info" {
t.Errorf("C.LogLevel should default to info, got %s", C.LogLevel)
}
if C.FilenamePrefix != "backup" {
t.Errorf("C.FilenamePrefix should default to backup, got %s", C.FilenamePrefix)
}
}
// writeTempConfig is a helper to write a temp config file for testing.

View File

@@ -82,7 +82,7 @@ func NewServer(configPath string) (*http.Server, error) {
}()
timestamp := time.Now().Format("20060102-150405")
filename := fmt.Sprintf("backup-%s.tar.gz", timestamp)
filename := fmt.Sprintf("%s-%s.tar.gz", C.FilenamePrefix, timestamp)
w.Header().Set("Content-Disposition", "attachment; filename="+filename)
w.Header().Set("Content-Type", "application/octet-stream")

View File

@@ -17,6 +17,7 @@ type (
BackupTimeout int `json:"backup_timeout"` // Timeout in minutes for backup streaming.
CompressionLevel int `json:"compression_level"` // Gzip compression level (1-9, default 9).
ExcludePatterns []string `json:"exclude_patterns"` // Regex patterns to exclude from backup.
FilenamePrefix string `json:"filename_prefix"` // Prefix for backup filename (default: "backup").
}
)