-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathmain.go
73 lines (59 loc) · 1.32 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package wallpaper
import (
"errors"
"io"
"net/http"
"os"
"path/filepath"
)
type Mode int
const (
Center Mode = iota
Crop
Fit
Span
Stretch
Tile
)
// Desktop contains the current desktop environment on Linux.
// Empty string on all other operating systems.
var Desktop = os.Getenv("XDG_CURRENT_DESKTOP")
// DesktopSession is used by LXDE on Linux.
var DesktopSession = os.Getenv("DESKTOP_SESSION")
// ErrUnsupportedDE is thrown when Desktop is not a supported desktop environment.
var ErrUnsupportedDE = errors.New("your desktop environment is not supported")
func downloadImage(url string) (string, error) {
res, err := http.Get(url)
if err != nil {
return "", err
}
defer res.Body.Close()
if res.StatusCode < 200 || res.StatusCode >= 300 {
return "", errors.New("non-200 status code")
}
cacheDir, err := getCacheDir()
if err != nil {
return "", err
}
file, err := os.Create(filepath.Join(cacheDir, "wallpaper"))
if err != nil {
return "", err
}
_, err = io.Copy(file, res.Body)
if err != nil {
return "", err
}
err = file.Close()
if err != nil {
return "", err
}
return file.Name(), nil
}
// SetFromURL downloads the image to a cache directory and calls SetFromFile.
func SetFromURL(url string) error {
file, err := downloadImage(url)
if err != nil {
return err
}
return SetFromFile(file)
}