shithub: hell

Download patch

ref: 7dee6e81cb521229d588749d3d2d2732512a87a5
parent: 76fdddacbf04d3d6fc6d30ab9e8ead9017281558
author: penny <penny@limitedideas.org>
date: Wed Oct 1 19:41:42 EDT 2025

/reload command and fix /download

--- a/commands.go
+++ b/commands.go
@@ -4,7 +4,7 @@
 	"strings"
 )
 
-var commands = []string{"examine", "reply", "like", "thread", "open", "prev", "download", "dm", "rt", "parent", "children", "rm", "mark", "unmark", "account", "import", "pause", "resume", "url", "fpost", "ufpost", "edit", "notice", "stats", "next", "view", "bookmarks", "follow", "unfollow", "likes", "help"}
+var commands = []string{"examine", "reply", "like", "thread", "open", "prev", "download", "dm", "rt", "parent", "children", "rm", "mark", "unmark", "account", "import", "pause", "resume", "url", "fpost", "ufpost", "edit", "notice", "stats", "next", "view", "bookmarks", "follow", "unfollow", "likes", "help", "reload"}
 
 func processInput(input string) (command string, arguments string, found bool) {
 
--- a/config.go
+++ b/config.go
@@ -150,6 +150,7 @@
 		}
 	}
 	prefs.Save_Location = expandDir(prefs.Save_Location)
+	config.AccountConfigs[0].Preferences = *prefs
 	return config.AccountConfigs[0], configpath, nil
 }
 
--- a/filehandler.go
+++ b/filehandler.go
@@ -8,6 +8,7 @@
 	"os"
 	"os/exec"
 	"path"
+	"path/filepath"
 
 	"codeberg.org/penny64/hellclient-go-mastodon"
 	"github.com/google/shlex"
@@ -44,38 +45,51 @@
 
 }
 
-func savePostImages(target *mastodon.Status, savedir string) (err error) {
-	files, pathnames, err := downloadPostImages(target)
+func savePostImages(target *mastodon.Status, savedir string) error {
+	files, _, err := downloadPostImages(target)
 	if err != nil {
-		fmt.Printf("Failed to download post images:%v", err)
-		return
+		fmt.Printf("Failed to download post images:%v\n", err)
+		return err
 	}
-	for file := range pathnames {
-		defer os.Remove(pathnames[file])
+	if len(files) == 0 {
+		return nil
 	}
-	var download *os.File
-	for file := range files {
-		download, err = os.Create(savedir + "/" + path.Base(files[file].Name()))
-		filereader, _ := os.Open(files[file].Name())
-		if _, err = io.Copy(download, filereader); err != nil {
-			fmt.Println("Failed to save %v\n", path.Base(files[file].Name()))
+
+	for _, file := range files {
+		defer file.Close()
+		destPath := filepath.Join(savedir, path.Base(file.Name()))
+		download, err := os.Create(destPath)
+		if err != nil {
+			fmt.Printf("Failed to create destination file %s: %s\n", destPath, err)
 			continue
 		}
-		fmt.Printf("Downloaded: %v\n", download.Name())
 
+		_, err = io.Copy(download, file)
+		download.Close()
+
+		if err != nil {
+			fmt.Printf("Failed to save %s: %s\n", destPath, err)
+			continue
+		}
+
+		fmt.Printf("Downloaded: %v\n", destPath)
 	}
-	return err
+
+	return nil
 }
 
-func downloadPostImages(target *mastodon.Status) (files []*os.File, pathnames []string, err error) {
+func downloadPostImages(target *mastodon.Status) ([]*os.File, []string, error) {
 
 	if len(target.MediaAttachments) == 0 {
-		fmt.Printf("Status has no attachments.\n")
-		return
+		fmt.Println("Status has no attachments.")
+		return nil, nil, nil
 	}
 
-	for media := range target.MediaAttachments {
-		err, mediafile := downloadImage(target.MediaAttachments[media].URL)
+	var files []*os.File
+	var pathnames []string
+
+	for _, media := range target.MediaAttachments {
+		err, mediafile := downloadImage(media.URL)
 		if err != nil {
 			fmt.Printf("Media download error: %v\n", err)
 			continue
@@ -82,49 +96,44 @@
 		}
 		files = append(files, mediafile)
 		pathnames = append(pathnames, mediafile.Name())
+	}
 
+	if len(files) == 0 && len(target.MediaAttachments) > 0 {
+		return nil, nil, fmt.Errorf("failed to download any attachments")
 	}
-	return
+
+	return files, pathnames, nil
 }
 
-func downloadImage(target string) (err error, file *os.File) {
+func downloadImage(target string) (error, *os.File) {
 	response, err := http.Get(target)
 	if err != nil {
-		return
+		return err, nil
 	}
+	defer response.Body.Close()
 
-	parsedurl, err := url.Parse(target)
+	if response.StatusCode != http.StatusOK {
+		return fmt.Errorf("bad status: %s", response.Status), nil
+	}
 
+	parsedurl, err := url.Parse(target)
 	if err != nil {
-		return
+		return err, nil
 	}
 
 	filename := path.Base(parsedurl.Path)
 
+	file, err := os.CreateTemp("", "*"+filename)
 	if err != nil {
-		return
+		return err, nil
 	}
 
-	file, err = os.CreateTemp("", "*"+filename)
-	if err != nil {
-		return
-	}
-
-	if response.StatusCode != http.StatusOK {
-		fmt.Printf("HTTP Error: %v\n", response.StatusCode)
-		return
-	}
-
 	_, err = io.Copy(file, response.Body)
 	if err != nil {
-		return
+		file.Close()
+		os.Remove(file.Name())
+		return err, nil
 	}
 
-	if err != nil {
-		return
-	}
-
-	defer response.Body.Close()
-	return
-
+	return nil, file
 }
--- a/help.go
+++ b/help.go
@@ -46,6 +46,7 @@
   /follow [index] [account]    Follow account by post index or name.
   /unfollow [index] [account]  Unfollow account by post index or name.
   /likes                       Display favorited status page.
+  /reload                      Reload configuration file.
 `
 return fmt.Sprintf(helpstring, configpath)
 }
--- a/main.go
+++ b/main.go
@@ -105,6 +105,15 @@
 			case "help":
 				fmt.Println(hyphenate(helpString(hc.configPath)))
 				return
+			case "reload":
+				account, _, err := loadConfig()
+				if err != nil {
+					fmt.Printf("Error reloading config: %s\n")
+					return
+				}
+				fmt.Println("Successfully reloaded preferences\n")
+				hc.preferences = &account.Preferences
+				return
 			case "stats":
 				hc.stats.slock.Lock()
 				var sb strings.Builder
--