@@ -2,10 +2,15 @@ package main
2
2
3
3
import (
4
4
"fmt"
5
+ "io"
5
6
"log"
7
+ "path/filepath"
6
8
7
9
"github.com/spf13/cobra"
8
10
toast "gopkg.in/toast.v1"
11
+
12
+ "net/http"
13
+ "os"
9
14
)
10
15
11
16
// Overridden via ldflags
@@ -36,6 +41,21 @@ func main() {
36
41
_ = cmd .Usage ()
37
42
return
38
43
}
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
+
39
59
notification := & toast.Notification {
40
60
AppID : appID ,
41
61
Title : category ,
@@ -62,6 +82,33 @@ func main() {
62
82
_ = rootCmd .Execute ()
63
83
}
64
84
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
+
65
112
// TODO - explore mapping icons: https://wiki.ubuntu.com/NotificationDevelopmentGuidelines#How_do_I_get_these_slick_icons
66
113
// https://docs.microsoft.com/en-us/uwp/api/windows.ui.notifications.toastnotification?view=winrt-19041
67
114
// https://docs.microsoft.com/en-us/uwp/schemas/tiles/toastschema/schema-root
0 commit comments