-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload_api.go
168 lines (139 loc) · 4.33 KB
/
upload_api.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
package images
import (
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strings"
"github.com/hoshinonyaruko/gensokyo-discord/config"
)
// 将base64图片通过lotus转换成url
func UploadBase64ImageToServer(base64Image string) (string, error) {
// 根据serverPort确定协议
protocol := "http"
serverPort := config.GetPortValue()
if serverPort == "443" {
protocol = "https"
}
if config.GetLotusValue() {
serverDir := config.GetServer_dir()
url := fmt.Sprintf("%s://%s:%s/uploadpic", protocol, serverDir, serverPort)
resp, err := postImageToServer(base64Image, url)
if err != nil {
return "", err
}
return resp, nil
}
serverDir := config.GetServer_dir()
// 当端口是443时,使用HTTP和444端口
if serverPort == "443" {
protocol = "http"
serverPort = "444"
}
if isPublicAddress(serverDir) {
url := fmt.Sprintf("%s://127.0.0.1:%s/uploadpic", protocol, serverPort)
resp, err := postImageToServer(base64Image, url)
if err != nil {
return "", err
}
return resp, nil
}
return "", errors.New("local server uses a private address; image upload failed")
}
// 将base64语音通过lotus转换成url
func UploadBase64RecordToServer(base64Image string) (string, error) {
// 根据serverPort确定协议
protocol := "http"
serverPort := config.GetPortValue()
if serverPort == "443" {
protocol = "https"
}
if config.GetLotusValue() {
serverDir := config.GetServer_dir()
url := fmt.Sprintf("%s://%s:%s/uploadrecord", protocol, serverDir, serverPort)
resp, err := postRecordToServer(base64Image, url)
if err != nil {
return "", err
}
return resp, nil
}
serverDir := config.GetServer_dir()
// 当端口是443时,使用HTTP和444端口
if serverPort == "443" {
protocol = "http"
serverPort = "444"
}
if isPublicAddress(serverDir) {
url := fmt.Sprintf("%s://127.0.0.1:%s/uploadrecord", protocol, serverPort)
resp, err := postRecordToServer(base64Image, url)
if err != nil {
return "", err
}
return resp, nil
}
return "", errors.New("local server uses a private address; image record failed")
}
// 请求图床api(图床就是lolus为false的gensokyo)
func postImageToServer(base64Image, targetURL string) (string, error) {
data := url.Values{}
data.Set("base64Image", base64Image) // 修改字段名以与服务器匹配
resp, err := http.PostForm(targetURL, data)
if err != nil {
return "", fmt.Errorf("failed to send request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("error response from server: %s", resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response body: %v", err)
}
var responseMap map[string]interface{}
if err := json.Unmarshal(body, &responseMap); err != nil {
return "", fmt.Errorf("failed to unmarshal response: %v", err)
}
if value, ok := responseMap["url"]; ok {
return fmt.Sprintf("%v", value), nil
}
return "", fmt.Errorf("URL not found in response")
}
// 请求语音床api(图床就是lolus为false的gensokyo)
func postRecordToServer(base64Image, targetURL string) (string, error) {
data := url.Values{}
data.Set("base64Record", base64Image) // 修改字段名以与服务器匹配
resp, err := http.PostForm(targetURL, data)
if err != nil {
return "", fmt.Errorf("failed to send request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("error response from server: %s", resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response body: %v", err)
}
var responseMap map[string]interface{}
if err := json.Unmarshal(body, &responseMap); err != nil {
return "", fmt.Errorf("failed to unmarshal response: %v", err)
}
if value, ok := responseMap["url"]; ok {
return fmt.Sprintf("%v", value), nil
}
return "", fmt.Errorf("URL not found in response")
}
// 判断是否公网ip 填写域名也会被认为是公网,但需要用户自己确保域名正确解析到gensokyo所在的ip地址
func isPublicAddress(addr string) bool {
if strings.Contains(addr, "localhost") || strings.HasPrefix(addr, "127.") || strings.HasPrefix(addr, "192.168.") {
return false
}
if net.ParseIP(addr) != nil {
return true
}
// If it's not a recognized IP address format, consider it a domain name (public).
return true
}