forked from go-lark/lark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_upload.go
149 lines (138 loc) · 3.35 KB
/
api_upload.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
package lark
// Import from Lark API Go demo
// with adaption to go-lark frame
import (
"bytes"
"fmt"
"image"
"image/jpeg"
"io"
"mime/multipart"
"net/http"
"os"
)
const (
uploadImageURL = "/open-apis/im/v1/images"
uploadFileURL = "/open-apis/im/v1/files"
)
// UploadImageResponse .
type UploadImageResponse struct {
BaseResponse
Data struct {
ImageKey string `json:"image_key"`
} `json:"data"`
}
// UploadFileRequest .
type UploadFileRequest struct {
FileType string `json:"-"`
FileName string `json:"-"`
Duration int `json:"-"`
Path string `json:"-"`
Reader io.Reader `json:"-"`
}
// UploadFileResponse .
type UploadFileResponse struct {
BaseResponse
Data struct {
FileKey string `json:"file_key"`
} `json:"data"`
}
// UploadImage uploads image to Lark server
func (bot Bot) UploadImage(path string) (*UploadImageResponse, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
writer.WriteField("image_type", "message")
part, err := writer.CreateFormFile("image", path)
if err != nil {
return nil, err
}
_, err = io.Copy(part, file)
if err != nil {
return nil, err
}
err = writer.Close()
if err != nil {
return nil, err
}
var respData UploadImageResponse
header := make(http.Header)
header.Set("Content-Type", writer.FormDataContentType())
err = bot.DoAPIRequest("POST", "UploadImage", uploadImageURL, header, true, body, &respData)
if err != nil {
return nil, err
}
return &respData, err
}
// UploadImageObject uploads image to Lark server
func (bot Bot) UploadImageObject(img image.Image) (*UploadImageResponse, error) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
writer.WriteField("image_type", "message")
part, err := writer.CreateFormFile("image", "temp_image")
if err != nil {
return nil, err
}
err = jpeg.Encode(part, img, nil)
if err != nil {
return nil, err
}
err = writer.Close()
if err != nil {
return nil, err
}
var respData UploadImageResponse
header := make(http.Header)
header.Set("Content-Type", writer.FormDataContentType())
err = bot.DoAPIRequest("POST", "UploadImage", uploadImageURL, header, true, body, &respData)
if err != nil {
return nil, err
}
return &respData, err
}
// UploadFile uploads file to Lark server
func (bot Bot) UploadFile(req UploadFileRequest) (*UploadFileResponse, error) {
var content io.Reader
if req.Reader == nil {
file, err := os.Open(req.Path)
if err != nil {
return nil, err
}
defer file.Close()
content = file
} else {
content = req.Reader
req.Path = req.FileName
}
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
writer.WriteField("file_type", req.FileType)
writer.WriteField("file_name", req.FileName)
if req.FileType == "mp4" && req.Duration > 0 {
writer.WriteField("duration", fmt.Sprintf("%d", req.Duration))
}
part, err := writer.CreateFormFile("file", req.Path)
if err != nil {
return nil, err
}
_, err = io.Copy(part, content)
if err != nil {
return nil, err
}
err = writer.Close()
if err != nil {
return nil, err
}
var respData UploadFileResponse
header := make(http.Header)
header.Set("Content-Type", writer.FormDataContentType())
err = bot.DoAPIRequest("POST", "UploadFile", uploadFileURL, header, true, body, &respData)
if err != nil {
return nil, err
}
return &respData, err
}