From b998fa9e7cf6378a5e45a58fbad37ec505159681 Mon Sep 17 00:00:00 2001 From: sloumdrone Date: Sat, 2 Nov 2019 20:18:21 -0700 Subject: [PATCH 1/3] Adds suffixing for file writes --- client.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/client.go b/client.go index 452dad1..599bade 100644 --- a/client.go +++ b/client.go @@ -529,6 +529,14 @@ func (c *client) saveFile(u Url, name string) { } savePath := filepath.Join(c.Options["savelocation"], name) + suffix := 1 + _, fileErr := os.Stat(savePath) + for fileErr == nil { + fn := fmt.Sprintf("%s.%d", name, suffix) + savePath = filepath.Join(c.Options["savelocation"], fn) + _, fileErr = os.Stat(savePath) + suffix++ + } err = ioutil.WriteFile(savePath, file, 0644) if err != nil { c.SetMessage("Error writing file: "+err.Error(), true) @@ -546,6 +554,14 @@ func (c *client) saveFileFromData(d, name string) { c.DrawMessage() savePath := filepath.Join(c.Options["savelocation"], name) + suffix := 1 + _, fileErr := os.Stat(savePath) + for fileErr == nil { + fn := fmt.Sprintf("%s.%d", name, suffix) + savePath = filepath.Join(c.Options["savelocation"], fn) + _, fileErr = os.Stat(savePath) + suffix++ + } err := ioutil.WriteFile(savePath, data, 0644) if err != nil { c.SetMessage("Error writing file: "+err.Error(), true) From 3a96792cf3bb54127630df8e3814239a8209bc02 Mon Sep 17 00:00:00 2001 From: sloumdrone Date: Sat, 2 Nov 2019 22:14:10 -0700 Subject: [PATCH 2/3] Moved repeated logic into function --- client.go | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/client.go b/client.go index 599bade..b2d6e80 100644 --- a/client.go +++ b/client.go @@ -528,18 +528,12 @@ func (c *client) saveFile(u Url, name string) { return } - savePath := filepath.Join(c.Options["savelocation"], name) - suffix := 1 - _, fileErr := os.Stat(savePath) - for fileErr == nil { - fn := fmt.Sprintf("%s.%d", name, suffix) - savePath = filepath.Join(c.Options["savelocation"], fn) - _, fileErr = os.Stat(savePath) - suffix++ - } + // We are ignoring the error here since WriteFile will + // generate the same error, and will handle the messaging + savePath, _ := findAvailableFileName(c.Options["savelocation"], name) err = ioutil.WriteFile(savePath, file, 0644) if err != nil { - c.SetMessage("Error writing file: "+err.Error(), true) + c.SetMessage("Error writing file: "+err.Error()+savePath, true) c.DrawMessage() return } @@ -553,18 +547,12 @@ func (c *client) saveFileFromData(d, name string) { c.SetMessage(fmt.Sprintf("Saving %s ...", name), false) c.DrawMessage() - savePath := filepath.Join(c.Options["savelocation"], name) - suffix := 1 - _, fileErr := os.Stat(savePath) - for fileErr == nil { - fn := fmt.Sprintf("%s.%d", name, suffix) - savePath = filepath.Join(c.Options["savelocation"], fn) - _, fileErr = os.Stat(savePath) - suffix++ - } + // We are ignoring the error here since WriteFile will + // generate the same error, and will handle the messaging + savePath, _ := findAvailableFileName(c.Options["savelocation"], name) err := ioutil.WriteFile(savePath, data, 0644) if err != nil { - c.SetMessage("Error writing file: "+err.Error(), true) + c.SetMessage("Error writing file: "+err.Error()+savePath, true) c.DrawMessage() return } @@ -1081,3 +1069,21 @@ func MakeClient(name string) *client { c := client{0, 0, defaultOptions, "", false, MakePages(), MakeBookmarks(), MakeHeadbar(name), MakeFootbar(), gemini.MakeTofuDigest()} return &c } + +func findAvailableFileName(fpath, fname string) (string, error) { + savePath := filepath.Join(fpath, fname) + _, fileErr := os.Stat(savePath) + + for suffix := 1; fileErr == nil; suffix++ { + fn := fmt.Sprintf("%s.%d", fname, suffix) + savePath = filepath.Join(fpath, fn) + _, fileErr = os.Stat(savePath) + + if !os.IsNotExist(fileErr) && fileErr != nil { + return savePath, fileErr + } + } + + return savePath, nil +} + From eef9ba6e813ac7deb5e03eb86c83e9c7f815e7c0 Mon Sep 17 00:00:00 2001 From: sloumdrone Date: Sat, 2 Nov 2019 22:15:31 -0700 Subject: [PATCH 3/3] Removes debugging code --- client.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index b2d6e80..df55ad2 100644 --- a/client.go +++ b/client.go @@ -533,7 +533,7 @@ func (c *client) saveFile(u Url, name string) { savePath, _ := findAvailableFileName(c.Options["savelocation"], name) err = ioutil.WriteFile(savePath, file, 0644) if err != nil { - c.SetMessage("Error writing file: "+err.Error()+savePath, true) + c.SetMessage("Error writing file: "+err.Error(), true) c.DrawMessage() return } @@ -552,7 +552,7 @@ func (c *client) saveFileFromData(d, name string) { savePath, _ := findAvailableFileName(c.Options["savelocation"], name) err := ioutil.WriteFile(savePath, data, 0644) if err != nil { - c.SetMessage("Error writing file: "+err.Error()+savePath, true) + c.SetMessage("Error writing file: "+err.Error(), true) c.DrawMessage() return }