Files
common-server-public/archive/bbs-go/server/internal/pkg/uploader/uploader.go
12600k-rog-d4 8f4d399cfd 20251209
2025-12-09 20:33:57 +08:00

65 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package uploader
import (
"strings"
"sync"
"github.com/mlogclub/simple/common/strs"
"github.com/mlogclub/simple/common/urls"
"bbs-go/internal/pkg/config"
)
type uploader interface {
PutImage(data []byte, contentType string) (string, error)
PutObject(key string, data []byte, contentType string) (string, error)
CopyImage(originUrl string) (string, error)
}
var (
aliyun = &aliyunOssUploader{
once: sync.Once{},
bucket: nil,
}
local = &localUploader{}
)
func PutImage(data []byte, contentType string) (string, error) {
return getUploader().PutImage(data, contentType)
}
func PutObject(key string, data []byte, contentType string) (string, error) {
return getUploader().PutObject(key, data, contentType)
}
func CopyImage(url string) (string, error) {
u1 := urls.ParseUrl(url).GetURL()
u2 := urls.ParseUrl(config.Instance.BaseUrl).GetURL()
// 本站host不下载
if u1.Host == u2.Host {
return url, nil
}
return getUploader().CopyImage(url)
}
func getUploader() uploader {
if IsEnabledOss() {
return aliyun
} else {
return local
}
}
// IsEnabledOss 是否启用阿里云oss
func IsEnabledOss() bool {
enable := config.Instance.Uploader.Enable
return strs.EqualsIgnoreCase(enable, "aliyun") || strs.EqualsIgnoreCase(enable, "oss") ||
strs.EqualsIgnoreCase(enable, "aliyunOss")
}
// IsOssImageUrl 是否是存放在阿里云oss中的图片
func IsOssImageUrl(url string) bool {
host := urls.ParseUrl(config.Instance.Uploader.AliyunOss.Host).GetURL().Host
return strings.Contains(url, host)
}