@@ -22,6 +22,7 @@ import (
2222 "encoding/hex"
2323 "fmt"
2424 "io"
25+ "os"
2526
2627 "github.com/arduino/go-paths-helper"
2728 "github.com/codeclysm/extract/v4"
@@ -108,7 +109,7 @@ func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb Downlo
108109 defer download .Close ()
109110
110111 // Download the zip
111- temp , err := paths . MkTempDir ( "" , "flasher-updater -" )
112+ temp , err := GetTempDir ( "download -" )
112113 if err != nil {
113114 return nil , "" , fmt .Errorf ("could not create temporary download directory: %w" , err )
114115 }
@@ -159,3 +160,21 @@ func ExtractImage(archive, temp *paths.Path) error {
159160 }
160161 return nil
161162}
163+
164+ // GetTempDir returns a temporary directory inside the user's cache directory.
165+ // The caller is responsible for removing the directory when no longer needed.
166+ func GetTempDir (prefix string ) (* paths.Path , error ) {
167+ userCacheDir , err := os .UserCacheDir ()
168+ if err != nil {
169+ return nil , fmt .Errorf ("could not get user's cache directory: %w" , err )
170+ }
171+
172+ cacheDir := paths .New (userCacheDir , "arduino-flasher-cli" )
173+ _ = cacheDir .MkdirAll ()
174+
175+ temp , err := paths .MkTempDir (cacheDir .String (), prefix )
176+ if err != nil {
177+ return nil , fmt .Errorf ("could not create .cache directory: %w" , err )
178+ }
179+ return temp , nil
180+ }
0 commit comments