-
Notifications
You must be signed in to change notification settings - Fork 5
/
jamfproapi_icon.go
100 lines (81 loc) · 2.83 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
91
92
93
94
95
96
97
98
99
100
// 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"
// ResponseUploadIcon is the response structure for uploading icons.
type ResponseUploadIcon struct {
URL string `json:"url"`
ID int `json:"id"`
}
// 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
// Construct the files map
files := map[string]string{
"file": filePath, // 'file' is the form field name for the file uploads from jamf docs
}
// Initialize the response struct
var uploadResponse ResponseUploadIcon
// Call DoMultipartRequest with the method, endpoint, files, and the response struct
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 the response struct pointer
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 {
// Construct the endpoint with query parameters
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)
// Initialize an empty struct since we are interested in the response body (the file content)
var placeholder struct{}
// Call DoRequest with the method, endpoint, nil for body since this is a GET request, and the placeholder for the response 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()
// Check for successful status code
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf("received non-success status code: %d", resp.StatusCode)
}
// Optionally, check the Content-Type of the response here
// Create the file to write to
file, err := os.Create(savePath)
if err != nil {
return fmt.Errorf("failed to create file: %v", err)
}
defer file.Close()
// Write the response body to file
_, err = io.Copy(file, resp.Body)
if err != nil {
return fmt.Errorf("failed to write file: %v", err)
}
// Ensure the file is written to disk
err = file.Sync()
if err != nil {
return fmt.Errorf("failed to sync file: %v", err)
}
return nil
}