shithub: hell

Download patch

ref: 7723acd54c4cc96d2784a1c6e1067f0b47fb81ca
parent: f27bbb07a94c0d6117d16532024a16abeb9381e1
author: penny <penny@limitedideas.org>
date: Fri Aug 1 14:20:53 EDT 2025

Image previews

--- /dev/null
+++ b/.gitignore
@@ -1,0 +1,4 @@
+
+config.json
+
+.DS_Store
--- /dev/null
+++ b/filehandler.go
@@ -1,0 +1,71 @@
+package main
+
+import (
+	"path"
+	"net/url"
+	"net/http"
+	"os"
+	"io"
+	"github.com/mattn/go-mastodon"
+	"fmt"
+	"strings"
+	"os/exec"
+)
+
+func downloadPostImages(target *mastodon.Status, commandstring string) (err error){
+	var files []*os.File
+	var pathnames []string
+
+	for media := range target.MediaAttachments {
+		err, mediafile := downloadImage(target.MediaAttachments[media].URL)
+		if(err != nil) {
+			fmt.Printf("Media download error:%v\n", err)
+		}
+		defer os.Remove(mediafile.Name())
+		files = append(files, mediafile)
+		pathnames = append(pathnames, mediafile.Name())
+	}
+	//pathargument := strings.Join(pathnames," ")
+	
+	args := strings.Fields(commandstring)
+	var command string
+	
+	if(len(args) > 0) {
+		command = args[0]
+		args = args[1:]
+	}
+	
+	args = append(args, pathnames...)
+	
+	cmd := exec.Command(command, args...)
+	err = cmd.Run()
+	return
+}
+
+func downloadImage(target string) (err error, file *os.File) {
+	response, err := http.Get(target)
+	parsedurl, err := url.Parse(target)
+	filename := path.Base(parsedurl.Path)
+	
+	file, err = os.CreateTemp("","*"+filename)
+	if err != nil {
+		return
+	}
+	
+	if response.StatusCode != http.StatusOK {
+    	return
+ 	}
+ 	
+ 	_, err = io.Copy(file, response.Body)
+  	if err != nil  {
+    	return
+  	}
+	
+	if err != nil {
+		return
+	}
+	
+	defer response.Body.Close()
+	return
+	
+}
\ No newline at end of file
--- a/format.go
+++ b/format.go
@@ -18,7 +18,7 @@
 func hyphenateline(input string, width int) (string, string) {
 
 	if strings.HasPrefix(input, "\n") {
-		return "\n", input[1:]
+		return "", input[1:]
 	}
 
 	begin, _, _ := strings.Cut(input, "\n")
--- a/main.go
+++ b/main.go
@@ -138,9 +138,11 @@
 			cmd := exec.Command("open", url, "-a", "Eldritch Café")
 			cmd.Run()
 		case "preview":
-			url := fmt.Sprintf("%v/statuses/%v", client.Config.Server, postItem.ID)
-			cmd := exec.Command("open", url)
-			cmd.Run()
+			err := downloadPostImages(postItem, "qlmanage -p")
+			if(err != nil) {
+				fmt.Printf("Image preview failed: %v\n", err)
+			}
+
 		case "reply":
 			if currentUser.ID == postItem.Account.ID {
 				recentpost, err = postReply(content, account, *client, postItem.Visibility, postItem.ID)
--