mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-16 21:23:44 +08:00
✨ feat: Add image upload
This commit is contained in:
90
common/storage/drives/sm.go
Normal file
90
common/storage/drives/sm.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package drives
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"one-api/common/requester"
|
||||
)
|
||||
|
||||
var smUploadURL = "https://sm.ms/api/v2/upload"
|
||||
|
||||
type SMUpload struct {
|
||||
Secret string
|
||||
}
|
||||
|
||||
type SMData struct {
|
||||
URL string `json:"url"`
|
||||
// FileID int `json:"file_id"`
|
||||
// Width int `json:"width"`
|
||||
// Height int `json:"height"`
|
||||
// Filename string `json:"filename"`
|
||||
// Storename string `json:"storename"`
|
||||
// Size int `json:"size"`
|
||||
// Path string `json:"path"`
|
||||
// Hash string `json:"hash"`
|
||||
// Delete string `json:"delete"`
|
||||
// Page string `json:"page"`
|
||||
}
|
||||
|
||||
type SMResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data SMData `json:"data"`
|
||||
RequestID string `json:"RequestId"`
|
||||
}
|
||||
|
||||
func NewSMUpload(secret string) *SMUpload {
|
||||
return &SMUpload{
|
||||
Secret: secret,
|
||||
}
|
||||
}
|
||||
|
||||
func (sm *SMUpload) Name() string {
|
||||
return "SM.MS"
|
||||
}
|
||||
|
||||
func (sm *SMUpload) Upload(data []byte, fileName string) (string, error) {
|
||||
client := requester.NewHTTPRequester("", nil)
|
||||
|
||||
var formBody bytes.Buffer
|
||||
builder := client.CreateFormBuilder(&formBody)
|
||||
|
||||
err := builder.CreateFormFileReader("smfile", bytes.NewReader(data), fileName)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("creating form file: %w", err)
|
||||
}
|
||||
builder.WriteField("format", "json")
|
||||
|
||||
headers := map[string]string{
|
||||
"Content-type": "application/json",
|
||||
"Authorization": sm.Secret,
|
||||
}
|
||||
|
||||
req, err := client.NewRequest(
|
||||
http.MethodPost,
|
||||
smUploadURL,
|
||||
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()
|
||||
|
||||
smResponse := &SMResponse{}
|
||||
_, errWithCode := client.SendRequest(req, smResponse, false)
|
||||
if errWithCode != nil {
|
||||
return "", fmt.Errorf("%s", errWithCode.Message)
|
||||
}
|
||||
|
||||
if !smResponse.Success {
|
||||
return "", fmt.Errorf("upload failed: %s", smResponse.Message)
|
||||
}
|
||||
|
||||
return smResponse.Data.URL, nil
|
||||
}
|
||||
25
common/storage/storage.go
Normal file
25
common/storage/storage.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"one-api/common/storage/drives"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Storage struct {
|
||||
drives map[string]StorageDrive
|
||||
}
|
||||
|
||||
func InitStorage() {
|
||||
InitSMStorage()
|
||||
}
|
||||
|
||||
func InitSMStorage() {
|
||||
smSecret := viper.GetString("storage.smms.secret")
|
||||
if smSecret == "" {
|
||||
return
|
||||
}
|
||||
|
||||
smUpload := drives.NewSMUpload(smSecret)
|
||||
AddStorageDrive(smUpload)
|
||||
}
|
||||
36
common/storage/storageDrive.go
Normal file
36
common/storage/storageDrive.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package storage
|
||||
|
||||
var storageDrives = New()
|
||||
|
||||
type StorageDrive interface {
|
||||
Upload(data []byte, fileName string) (string, error)
|
||||
Name() string
|
||||
}
|
||||
|
||||
func New() *Storage {
|
||||
storageDrive := &Storage{
|
||||
drives: make(map[string]StorageDrive, 0),
|
||||
}
|
||||
|
||||
return storageDrive
|
||||
}
|
||||
|
||||
func AddStorageDrive(drives ...StorageDrive) {
|
||||
storageDrives.addDrives(drives...)
|
||||
}
|
||||
|
||||
func (s *Storage) addDrives(drives ...StorageDrive) {
|
||||
for _, d := range drives {
|
||||
s.addDrive(d)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Storage) addDrive(drive StorageDrive) {
|
||||
if drive != nil {
|
||||
driveName := drive.Name()
|
||||
if _, ok := s.drives[driveName]; ok {
|
||||
return
|
||||
}
|
||||
s.drives[driveName] = drive
|
||||
}
|
||||
}
|
||||
50
common/storage/storage_test.go
Normal file
50
common/storage/storage_test.go
Normal file
File diff suppressed because one or more lines are too long
34
common/storage/upload.go
Normal file
34
common/storage/upload.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"one-api/common"
|
||||
)
|
||||
|
||||
func (s *Storage) Upload(ctx context.Context, data []byte, fileName string) string {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
|
||||
for driveName, drive := range s.drives {
|
||||
if drive == nil {
|
||||
continue
|
||||
}
|
||||
url, err := drive.Upload(data, fileName)
|
||||
if err != nil {
|
||||
common.LogError(ctx, fmt.Sprintf("%s err: %s", driveName, err.Error()))
|
||||
} else {
|
||||
return url
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func Upload(data []byte, fileName string) string {
|
||||
//lint:ignore SA1029 reason: 需要使用该类型作为错误处理
|
||||
ctx := context.WithValue(context.Background(), common.RequestIdKey, "Upload")
|
||||
|
||||
return storageDrives.Upload(ctx, data, fileName)
|
||||
}
|
||||
Reference in New Issue
Block a user