feat: add imgur image upload

This commit is contained in:
Martial BE 2024-04-18 18:51:09 +08:00
parent cfa68df4aa
commit b20659dfcc
No known key found for this signature in database
GPG Key ID: D06C32DF0EDB9084
5 changed files with 109 additions and 2 deletions

View File

@ -0,0 +1,77 @@
package drives
import (
"bytes"
"fmt"
"net/http"
"one-api/common/requester"
)
var imgurUploadURL = "https://api.imgur.com/3/image"
type ImgurUpload struct {
ClientId string
}
func NewImgurUpload(clientId string) *ImgurUpload {
return &ImgurUpload{
ClientId: clientId,
}
}
type ImgurResponse struct {
Status int `json:"status"`
Success bool `json:"success"`
Data ImgurData `json:"data,omitempty"`
}
type ImgurData struct {
Link string `json:"link"`
}
func (i *ImgurUpload) Name() string {
return "Imgur"
}
func (i *ImgurUpload) Upload(data []byte, fileName string) (string, error) {
client := requester.NewHTTPRequester("", nil)
var formBody bytes.Buffer
builder := client.CreateFormBuilder(&formBody)
err := builder.CreateFormFileReader("image", bytes.NewReader(data), fileName)
if err != nil {
return "", fmt.Errorf("creating form file: %w", err)
}
builder.Close()
headers := map[string]string{
"Authorization": "Client-ID " + i.ClientId,
}
req, err := client.NewRequest(
http.MethodPost,
imgurUploadURL,
client.WithBody(&formBody),
client.WithHeader(headers),
client.WithContentType(builder.FormDataContentType()))
req.ContentLength = int64(formBody.Len())
if err != nil {
return "", fmt.Errorf("new request failed: %w", err)
}
defer req.Body.Close()
imgurResponse := &ImgurResponse{}
_, errWithCode := client.SendRequest(req, imgurResponse, false)
if errWithCode != nil {
return "", fmt.Errorf("%s", errWithCode.Message)
}
if !imgurResponse.Success {
return "", fmt.Errorf("upload failed Status: %d", imgurResponse.Status)
}
return imgurResponse.Data.Link, nil
}

View File

@ -31,7 +31,7 @@ type SMResponse struct {
Success bool `json:"success"`
Code string `json:"code"`
Message string `json:"message"`
Data SMData `json:"data"`
Data SMData `json:"data,omitempty"`
RequestID string `json:"RequestId"`
}
@ -56,6 +56,7 @@ func (sm *SMUpload) Upload(data []byte, fileName string) (string, error) {
return "", fmt.Errorf("creating form file: %w", err)
}
builder.WriteField("format", "json")
builder.Close()
headers := map[string]string{
"Content-type": "application/json",

View File

@ -11,6 +11,7 @@ type Storage struct {
}
func InitStorage() {
InitImgurStorage()
InitSMStorage()
}
@ -23,3 +24,13 @@ func InitSMStorage() {
smUpload := drives.NewSMUpload(smSecret)
AddStorageDrive(smUpload)
}
func InitImgurStorage() {
imgurClientId := viper.GetString("storage.imgur.client_id")
if imgurClientId == "" {
return
}
imgurUpload := drives.NewImgurUpload(imgurClientId)
AddStorageDrive(imgurUpload)
}

View File

@ -38,6 +38,22 @@ func TestSMMSUpload(t *testing.T) {
assert.Nil(t, err)
}
func TestImgurUpload(t *testing.T) {
InitConfig()
imgurClientId := viper.GetString("storage.imgur.client_id")
imgurUpload := drives.NewImgurUpload(imgurClientId)
image, err := base64.StdEncoding.DecodeString(testImageB64)
if err != nil {
fmt.Println(err)
}
url, err := imgurUpload.Upload(image, common.GetUUID()+".png")
fmt.Println(url)
fmt.Println(err)
assert.Nil(t, err)
}
// func TestSMMSUploadError(t *testing.T) {
// InitConfig()
// access_token := viper.GetString("notify.dingtalk.token")

View File

@ -63,4 +63,6 @@ notify: # 通知设置, 配置了几个通知方式,就会同时发送几次
chat_id: "" # 你的 Telegram chat_id
storage: # 存储设置 (可选,主要用于图片生成有些供应商不提供url只能返回base64图片设置后可以正常返回url格式的图片生成)
smms: # sm.ms 图床设置
secret: "" # 你的 sm.ms API 密钥
secret: "" # 你的 sm.ms API 密钥
imgur:
client_id: "" # 你的 imgur client_id