-
Notifications
You must be signed in to change notification settings - Fork 168
/
anonfiles_util.go
133 lines (126 loc) · 2.94 KB
/
anonfiles_util.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
/*
* @Author: SpenserCai
* @Date: 2023-02-24 16:36:01
* @version:
* @LastEditors: SpenserCai
* @LastEditTime: 2023-02-25 13:07:29
* @Description: file content
*/
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"github.com/mitchellh/mapstructure"
)
type AnonFileError struct {
Message string `json:"message"`
Type string `json:"type"`
Code int `json:"code"`
}
type AnonFileUploadData struct {
File struct {
Url struct {
Full string `json:"full"`
Short string `json:"short"`
} `json:"url"`
Metadata struct {
Id string `json:"id"`
Name string `json:"name"`
Size struct {
Bytes int `json:"bytes"`
Readable string `json:"readable"`
} `json:"size"`
} `json:"metadata"`
} `json:"file"`
}
// AnonFiles的基础返回结构 Status是bool类型的,Data是对象类型的
type AnonFilesBaseResponse struct {
Status bool `json:"status"`
Data map[string]interface{} `json:"data"`
Error AnonFileError `json:"error"`
}
func AnonFilesUpload(filePath string) (string, error) {
apiUrl := fmt.Sprintf("https://api.anonfiles.com/upload?token=%s", ANONFILES_TOKEN)
// curl -F "file=@filePath" url 转成go代码并且使用代理
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer file.Close()
// 创建一个缓冲区
buf := new(bytes.Buffer)
// 创建一个multipart writer
writer := multipart.NewWriter(buf)
// 创建一个form-data
formFile, err := writer.CreateFormFile("file", strings.Split(filePath, "\\")[len(strings.Split(filePath, "\\"))-1])
if err != nil {
return "", err
}
// 将文件写入到form-data
_, err = io.Copy(formFile, file)
if err != nil {
return "", err
}
// 关闭writer
err = writer.Close()
if err != nil {
return "", err
}
// 创建一个http请求,并使用代理socks5://127.0.0.1:LOCAL_PROXY_PORT
req, err := http.NewRequest("POST", apiUrl, buf)
if err != nil {
return "", err
}
// 设置请求头
req.Header.Set("Content-Type", writer.FormDataContentType())
// 创建一个http client
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(&url.URL{
Scheme: "socks5",
Host: "127.0.0.1:" + strconv.Itoa(LOCAL_PROXY_PORT),
}),
},
}
// 发送请求
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
// 读取响应
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
// 解析响应
var response AnonFilesBaseResponse
err = json.Unmarshal(body, &response)
if err != nil {
return "", err
}
// 判断是否成功
if response.Status {
// 将data转成AnonFileUploadData
var data AnonFileUploadData
err = mapstructure.Decode(response.Data, &data)
if err != nil {
return "", err
}
// 返回短链接
return data.File.Url.Short, nil
} else {
// 失败
return "", errors.New(response.Error.Message)
}
}