-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathicon.go
More file actions
165 lines (138 loc) · 4.28 KB
/
icon.go
File metadata and controls
165 lines (138 loc) · 4.28 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2022-2026 Salesforce, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package api
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/textproto"
"net/url"
"runtime"
"github.com/opentracing/opentracing-go"
"github.com/slackapi/slack-cli/internal/image"
"github.com/slackapi/slack-cli/internal/slackcontext"
"github.com/slackapi/slack-cli/internal/slackerror"
"github.com/spf13/afero"
)
const (
appIconMethod = "apps.hosted.icon"
// AppIconSetMethod is the API method for setting app icons for non-hosted apps.
AppIconSetMethod = "apps.icon.set"
)
// IconResult details to be saved
type IconResult struct {
}
type iconResponse struct {
extendedBaseResponse
IconResult
}
// Icon updates a Slack App's icon
func (c *Client) Icon(ctx context.Context, fs afero.Fs, token, appID, iconFilePath string) (IconResult, error) {
return c.uploadIcon(ctx, fs, token, appID, iconFilePath, appIconMethod, "file")
}
// IconSet sets a Slack App's icon using the apps.icon.set API method.
func (c *Client) IconSet(ctx context.Context, fs afero.Fs, token, appID, iconFilePath string) (IconResult, error) {
return c.uploadIcon(ctx, fs, token, appID, iconFilePath, AppIconSetMethod, "icon")
}
// uploadIcon uploads an icon to the given API method.
func (c *Client) uploadIcon(ctx context.Context, fs afero.Fs, token, appID, iconFilePath, apiMethod, fileFieldName string) (IconResult, error) {
var (
iconBytes []byte
err error
span opentracing.Span
)
span, ctx = opentracing.StartSpanFromContext(ctx, "apiclient.Icon")
defer span.Finish()
if iconFilePath == "" || appID == "" {
return IconResult{}, slackerror.New("missing required args")
}
icon, err := fs.Open(iconFilePath)
if err != nil {
return IconResult{}, err
}
defer icon.Close()
iconBytes, err = image.CropResizeImageRatioFromFileToBytes(fs, iconFilePath, 512, 1, 1)
if err != nil {
return IconResult{}, err
}
iconStat, err := icon.Stat()
if err != nil {
return IconResult{}, err
}
var body = new(bytes.Buffer)
var writer = multipart.NewWriter(body)
var part io.Writer
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`, fileFieldName, iconStat.Name()))
h.Set("Content-Type", http.DetectContentType(iconBytes))
part, err = writer.CreatePart(h)
if err != nil {
return IconResult{}, err
}
_, err = part.Write(iconBytes)
if err != nil {
return IconResult{}, err
}
err = writer.WriteField("app_id", appID)
if err != nil {
return IconResult{}, err
}
// close the writer after the body is formed
writer.Close()
var sURL *url.URL
sURL, err = url.Parse(c.host + "/api/" + apiMethod)
if err != nil {
return IconResult{}, err
}
span.SetTag("request_url", sURL)
var request *http.Request
request, err = http.NewRequestWithContext(ctx, "POST", sURL.String(), body)
if err != nil {
return IconResult{}, err
}
request.Header.Add("Content-Type", writer.FormDataContentType())
request.Header.Add("Authorization", "Bearer "+token)
cliVersion, err := slackcontext.Version(ctx)
if err != nil {
return IconResult{}, err
}
var userAgent = fmt.Sprintf("slack-cli/%s (os: %s)", cliVersion, runtime.GOOS)
request.Header.Add("User-Agent", userAgent)
resp, err := c.httpClient.Do(request)
if err != nil {
return IconResult{}, err
}
defer resp.Body.Close()
span.SetTag("status_code", resp.StatusCode)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return IconResult{}, err
}
var result iconResponse
err = json.Unmarshal(respBody, &result)
if err != nil {
return IconResult{}, err
}
span.SetTag("ok", result.Ok)
if !result.Ok {
span.SetTag("error", result.Error)
return IconResult{}, fmt.Errorf("%s error: %s", sURL.String(), result.Error)
}
// return result
return IconResult{}, nil
}