This repository has been archived by the owner on Mar 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathfiles.go
173 lines (145 loc) · 3.54 KB
/
files.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
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
166
167
168
169
170
171
172
173
package slack
import (
"bytes"
"encoding/json"
"errors"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
)
// API files.upload: Uploads or creates a file.
func (sl *Slack) FilesUpload(opt *FilesUploadOpt) (file *UploadedFile, err error) {
req, err := sl.createFilesUploadRequest(opt)
if err != nil {
return
}
body, err := sl.DoRequest(req)
if err != nil {
return
}
res := new(FilesUploadAPIResponse)
err = json.Unmarshal(body, res)
if err != nil {
return
}
if res.Ok {
file = &res.File
} else {
err = errors.New(res.Error)
}
return
}
// API files.info: Retrieves information about a specified uploaded file.
func (sl *Slack) FindFile(id string) (file *UploadedFile, err error) {
uv := sl.urlValues()
uv.Add("file", id)
body, err := sl.GetRequest(filesInfoApiEndpoint, uv)
if err != nil {
return
}
res := new(FilesUploadAPIResponse)
err = json.Unmarshal(body, res)
if err != nil {
return
}
if res.Ok {
file = &res.File
} else {
err = errors.New("File not found")
}
return
}
// option type for `files.upload` api
type FilesUploadOpt struct {
Content string
Filepath string
Filetype string
Filename string
Title string
InitialComment string
Channels []string
}
// response of `files.upload` api
type FilesUploadAPIResponse struct {
Ok bool `json:"ok"`
Error string `json:"error"`
File UploadedFile `json:"file"`
}
type UploadedFile struct {
ID string `json:"id"`
Title string `json:"title"`
Name string `json:"name"`
MimeType string `json:"mimetype"`
FileType string `json:"filetype"`
User string `json:"user"`
PrivateUrl string `json:"url_private"`
PrivateDownloadUrl string `json:"url_private_download"`
Permalink string `json:"permalink"`
PublicPermalink string `json:"permalink_public"`
}
func (sl *Slack) createFilesUploadRequest(opt *FilesUploadOpt) (*http.Request, error) {
var body io.Reader
uv := sl.urlValues()
if opt == nil {
return nil, errors.New("`opt *FilesUploadOpt` argument must be specified.")
}
contentType := "application/x-www-form-urlencoded"
if opt.Filetype != "" {
uv.Add("filetype", opt.Filetype)
}
if opt.Filename != "" {
uv.Add("filename", opt.Filename)
}
if opt.Title != "" {
uv.Add("title", opt.Title)
}
if opt.InitialComment != "" {
uv.Add("initial_comment", opt.InitialComment)
}
if len(opt.Channels) != 0 {
uv.Add("channels", strings.Join(opt.Channels, ","))
}
if opt.Filepath != "" {
var err error
body, contentType, err = createFileParam("file", opt.Filepath)
if err != nil {
return nil, err
}
} else if opt.Content != "" {
body = strings.NewReader(url.Values{"content": []string{opt.Content}}.Encode())
}
req, err := http.NewRequest("POST", apiBaseUrl+filesUploadApiEndpoint, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
req.URL.RawQuery = (*uv).Encode()
return req, nil
}
func createFileParam(param, path string) (*bytes.Buffer, string, error) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
defer writer.Close()
p, err := filepath.Abs(path)
if err != nil {
return nil, "", err
}
file, err := os.Open(p)
if err != nil {
return nil, "", err
}
defer file.Close()
part, err := writer.CreateFormFile(param, filepath.Base(path))
if err != nil {
return nil, "", err
}
_, err = io.Copy(part, file)
if err != nil {
return nil, "", err
}
return body, writer.FormDataContentType(), nil
}