Skip to content

Commit a149983

Browse files
committed
hacking the ability to display icons from the internet
1 parent 8c965de commit a149983

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

main.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ package main
22

33
import (
44
"fmt"
5+
"io"
56
"log"
7+
"path/filepath"
68

79
"github.com/spf13/cobra"
810
toast "gopkg.in/toast.v1"
11+
12+
"net/http"
13+
"os"
914
)
1015

1116
// Overridden via ldflags
@@ -36,6 +41,21 @@ func main() {
3641
_ = cmd.Usage()
3742
return
3843
}
44+
45+
if len(icon) > 0 && (icon[:7] == "http://" || icon[:8] == "https://") {
46+
tmpFolder := os.TempDir()
47+
48+
err := DownloadFile(icon, filepath.Join(tmpFolder, "wsl-notify-send-icon-tmp.png"))
49+
if err != nil {
50+
log.Fatalln(err)
51+
icon = ""
52+
} else {
53+
// had to comment this out because the toast wasn't getting invoked before the file was removed
54+
// defer os.Remove("wsl-notify-send-icon-tmp.png")
55+
icon = filepath.Join(tmpFolder, "wsl-notify-send-icon-tmp.png")
56+
}
57+
}
58+
3959
notification := &toast.Notification{
4060
AppID: appID,
4161
Title: category,
@@ -62,6 +82,33 @@ func main() {
6282
_ = rootCmd.Execute()
6383
}
6484

85+
// DownloadFile will download a url and store it in local filepath.
86+
// It writes to the destination file as it downloads it, without
87+
// loading the entire file into memory.
88+
func DownloadFile(url string, filepath string) error {
89+
// Create the file
90+
out, err := os.Create(filepath)
91+
if err != nil {
92+
return err
93+
}
94+
defer out.Close()
95+
96+
// Get the data
97+
resp, err := http.Get(url)
98+
if err != nil {
99+
return err
100+
}
101+
defer resp.Body.Close()
102+
103+
// Write the body to file
104+
_, err = io.Copy(out, resp.Body)
105+
if err != nil {
106+
return err
107+
}
108+
109+
return nil
110+
}
111+
65112
// TODO - explore mapping icons: https://wiki.ubuntu.com/NotificationDevelopmentGuidelines#How_do_I_get_these_slick_icons
66113
// https://docs.microsoft.com/en-us/uwp/api/windows.ui.notifications.toastnotification?view=winrt-19041
67114
// https://docs.microsoft.com/en-us/uwp/schemas/tiles/toastschema/schema-root

0 commit comments

Comments
 (0)