实现 MinIO 对象存储图片流水线(可迁移 OSS)
- 新增 S3 兼容 Storage 抽象与 MinIO 实现,compose 启动 MinIO - 上传接口:校验 → 缩放 → WebP → 主图/缩略图入库 - 消息图片 content 改为对象 URL,拒绝 base64 - 工作台/访客端改为先上传再发消息
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// MemoryStorage 测试用内存存储
|
||||
type MemoryStorage struct {
|
||||
mu sync.RWMutex
|
||||
data map[string][]byte
|
||||
base string
|
||||
}
|
||||
|
||||
func NewMemory(publicBase string) *MemoryStorage {
|
||||
return &MemoryStorage{
|
||||
data: make(map[string][]byte),
|
||||
base: NormalizePublicBase(publicBase),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MemoryStorage) Put(_ context.Context, key string, r io.Reader, _ int64, _ string) (string, error) {
|
||||
b, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
m.mu.Lock()
|
||||
m.data[key] = b
|
||||
m.mu.Unlock()
|
||||
return m.base + "/" + key, nil
|
||||
}
|
||||
|
||||
func (m *MemoryStorage) Delete(_ context.Context, key string) error {
|
||||
m.mu.Lock()
|
||||
delete(m.data, key)
|
||||
m.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MemoryStorage) PublicBase() string { return m.base }
|
||||
|
||||
func (m *MemoryStorage) EnsureBucket(context.Context) error { return nil }
|
||||
|
||||
func (m *MemoryStorage) Get(key string) ([]byte, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
b, ok := m.data[key]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("not found")
|
||||
}
|
||||
return bytes.Clone(b), nil
|
||||
}
|
||||
Reference in New Issue
Block a user