-
Notifications
You must be signed in to change notification settings - Fork 5
/
jamfproapi_icon.go
90 lines (71 loc) · 2.13 KB
/
jamfproapi_icon.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// jamfproapi_upload_icon.go
// Jamf Pro Api - Upload Icon
// api reference: https://developer.jamf.com/jamf-pro/reference/post_v1-icon
// This endpoint uploads an icon and returns its URL and ID.
package jamfpro
import (
"fmt"
"io"
"net/url"
"os"
)
const uriUploadIcon = "/api/v1/icon"
// Response
// ResponseUploadIcon is the response structure for uploading icons.
type ResponseUploadIcon struct {
URL string `json:"url"`
ID int `json:"id"`
}
// CRUD
// UploadIcon uploads an icon file to Jamf Pro and returns the icon URL and ID.
func (c *Client) UploadIcon(filePath string) (*ResponseUploadIcon, error) {
endpoint := uriUploadIcon
files := map[string]string{
"file": filePath,
}
var uploadResponse ResponseUploadIcon
resp, err := c.HTTP.DoMultipartRequest("POST", endpoint, nil, files, &uploadResponse)
if err != nil {
return nil, fmt.Errorf("failed to upload icon: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &uploadResponse, nil
}
// DownloadIcon downloads an icon by its ID from Jamf Pro and saves it to the specified file path.
// The icon is saved to the path provided in the 'savePath' parameter.
func (c *Client) DownloadIcon(iconID int, savePath string, res string, scale string) error {
params := url.Values{}
if res != "" {
params.Add("res", res)
}
if scale != "" {
params.Add("scale", scale)
}
queryString := params.Encode()
endpoint := fmt.Sprintf("%s/download/%d?%s", uriUploadIcon, iconID, queryString)
var placeholder struct{}
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &placeholder)
if err != nil {
return fmt.Errorf("failed to download icon: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf("received non-success status code: %d", resp.StatusCode)
}
file, err := os.Create(savePath)
if err != nil {
return fmt.Errorf("failed to create file: %v", err)
}
defer file.Close()
_, err = io.Copy(file, resp.Body)
if err != nil {
return fmt.Errorf("failed to write file: %v", err)
}
err = file.Sync()
if err != nil {
return fmt.Errorf("failed to sync file: %v", err)
}
return nil
}