Skip to content

Commit

Permalink
Use real file extensions for exported config files (#145)
Browse files Browse the repository at this point in the history
(cherry picked from commit f342a37)
  • Loading branch information
koho committed May 13, 2024
1 parent dd0e3cb commit 14acaab
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
8 changes: 8 additions & 0 deletions pkg/config/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,14 @@ func (conf *ClientConfig) CountStart() int {
return len(lo.Filter(conf.Proxies, func(proxy *Proxy, i int) bool { return !proxy.Disabled }))
}

func (conf *ClientConfig) Ext() string {
if conf.LegacyFormat {
return ".ini"
} else {
return ".toml"
}
}

// NewProxyFromIni creates a proxy object from ini section
func NewProxyFromIni(name string, section *ini.Section) (*Proxy, error) {
proxy := NewDefaultProxyConfig(name)
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type Config interface {
GetSTUNServer() string
// Copy creates a new copy of this config.
Copy(all bool) Config
// Ext is the file extension of this config.
Ext() string
}

type AutoDelete struct {
Expand Down
12 changes: 6 additions & 6 deletions pkg/util/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func ReadFileLines(path string) ([]string, error) {
}

// ZipFiles compresses the given file list to a zip file
func ZipFiles(filename string, files []string) error {
func ZipFiles(filename string, files map[string]string) error {
newZipFile, err := os.Create(filename)
if err != nil {
return err
Expand All @@ -116,16 +116,16 @@ func ZipFiles(filename string, files []string) error {
defer zipWriter.Close()

// Add files to zip
for _, file := range files {
if err = addFileToZip(zipWriter, file); err != nil {
for src, dst := range files {
if err = addFileToZip(zipWriter, src, dst); err != nil {
return err
}
}
return nil
}

func addFileToZip(zipWriter *zip.Writer, filename string) error {
fileToZip, err := os.Open(filename)
func addFileToZip(zipWriter *zip.Writer, src, dst string) error {
fileToZip, err := os.Open(src)
if err != nil {
return err
}
Expand All @@ -140,7 +140,7 @@ func addFileToZip(zipWriter *zip.Writer, filename string) error {
if err != nil {
return err
}
header.Name = filepath.Base(filename)
header.Name = filepath.Base(dst)

// Change to deflate to gain better compression
header.Method = zip.Deflate
Expand Down
26 changes: 11 additions & 15 deletions ui/confview.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,12 @@ func (cv *ConfView) onURLImport() {
showError(err, cv.Form())
continue
}
if err = os.WriteFile(newPath, item.Data, 0666); err != nil {
cfg := NewConf(newPath, conf)
if err = cfg.Save(); err != nil {
showError(err, cv.Form())
continue
}
addConf(NewConf(newPath, conf))
addConf(cfg)
imported++
}
}
Expand Down Expand Up @@ -367,17 +368,17 @@ func (cv *ConfView) ImportFiles(files []string) {
i18n.Sprintf("Another config already exists with the name \"%s\".", baseName))
continue
}
// Verify config before copying file
conf, err := config.UnmarshalClientConf(path)
if err != nil {
showError(err, cv.Form())
continue
}
if _, err = util.CopyFile(path, newPath); err != nil {
cfg := NewConf(newPath, conf)
if err = cfg.Save(); err != nil {
showError(err, cv.Form())
continue
}
addConf(NewConf(newPath, conf))
addConf(cfg)
imported++
}
}
Expand All @@ -400,15 +401,11 @@ func (cv *ConfView) importZip(path string, data []byte, rename bool) (total, imp
if err != nil {
return err
}
fw, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, file.Mode())
if err != nil {
return err
}
defer fw.Close()
if _, err = fw.Write(src); err != nil {
cfg := NewConf(dst, conf)
if err = cfg.Save(); err != nil {
return err
}
addConf(NewConf(dst, conf))
addConf(cfg)
return nil
}
var zr *zip.Reader
Expand Down Expand Up @@ -534,9 +531,8 @@ func (cv *ConfView) onExport() {
if !strings.HasSuffix(dlg.FilePath, ".zip") {
dlg.FilePath += ".zip"
}

files := lo.Map(confList, func(conf *Conf, i int) string {
return conf.Path
files := lo.SliceToMap(confList, func(conf *Conf) (string, string) {
return conf.Path, conf.Name + conf.Data.Ext()
})
if err := util.ZipFiles(dlg.FilePath, files); err != nil {
showError(err, cv.Form())
Expand Down

0 comments on commit 14acaab

Please sign in to comment.