Skip to content

Commit 39b5c11

Browse files
committed
avoid tmp icon downloaded file collisions
1 parent a149983 commit 39b5c11

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

main.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"fmt"
55
"io"
66
"log"
7+
"math/rand"
78
"path/filepath"
9+
"strconv"
810

911
"github.com/spf13/cobra"
1012
toast "gopkg.in/toast.v1"
@@ -42,17 +44,20 @@ func main() {
4244
return
4345
}
4446

47+
// only use 100 random file names to avoid too much garbage since this application closes before the windows executable completes
48+
random := strconv.Itoa(rand.Intn(100) + 1)
49+
4550
if len(icon) > 0 && (icon[:7] == "http://" || icon[:8] == "https://") {
4651
tmpFolder := os.TempDir()
4752

48-
err := DownloadFile(icon, filepath.Join(tmpFolder, "wsl-notify-send-icon-tmp.png"))
53+
err := DownloadFile(icon, filepath.Join(tmpFolder, "wsl-notify-send-icon-tmp"+random+".png"))
4954
if err != nil {
5055
log.Fatalln(err)
5156
icon = ""
5257
} else {
5358
// 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")
59+
// defer os.Remove("wsl-notify-send-icon-tmp"+random+".png")
60+
icon = filepath.Join(tmpFolder, "wsl-notify-send-icon-tmp"+random+".png")
5661
}
5762
}
5863

0 commit comments

Comments
 (0)