44406b72db
Moteva-style AI design workbench replica. Home prompt creates a project; the project page provides an infinite canvas with node drag, zoom/pan, a design chat panel, history replay, image asset upload, and project save/regenerate. - Backend: go-zero, DDD layering, sqlc/pgx, optional PostgreSQL; memory or Redis cache; asynq job queue; MinIO/S3/R2/OSS object storage; sky-valley/pi agent runtime adapter. - Frontend: Next.js App Router + Vite artifact build, TypeScript, i18n, shadcn/ui components, auth (OTP/Turnstile/Google/WeChat). - Deploy: Docker Compose and k3s manifests. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
103 lines
2.4 KiB
Go
103 lines
2.4 KiB
Go
package objectstorage
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/url"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"img_infinite_canvas/internal/domain/design"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type MemoryStorage struct {
|
|
bucket string
|
|
publicBase string
|
|
expiresIn int64
|
|
}
|
|
|
|
func NewMemoryStorage(bucket string, publicBase string, expiresIn int64) *MemoryStorage {
|
|
if bucket == "" {
|
|
bucket = "memory-assets"
|
|
}
|
|
if expiresIn <= 0 {
|
|
expiresIn = 900
|
|
}
|
|
return &MemoryStorage{
|
|
bucket: bucket,
|
|
publicBase: strings.TrimRight(publicBase, "/"),
|
|
expiresIn: expiresIn,
|
|
}
|
|
}
|
|
|
|
func (s *MemoryStorage) CreateUpload(ctx context.Context, req design.AssetUploadRequest) (design.AssetUpload, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return design.AssetUpload{}, err
|
|
}
|
|
|
|
objectKey := objectKey(req.FileName)
|
|
publicURL := fmt.Sprintf("/memory-assets/%s", escapeObjectPath(objectKey))
|
|
if s.publicBase != "" {
|
|
publicURL = publicObjectURL(s.publicBase, objectKey)
|
|
}
|
|
|
|
return design.AssetUpload{
|
|
Driver: "memory",
|
|
Bucket: s.bucket,
|
|
ObjectKey: objectKey,
|
|
UploadURL: publicURL + "?upload=1",
|
|
PublicURL: publicURL,
|
|
Headers: map[string]string{
|
|
"Content-Type": req.ContentType,
|
|
},
|
|
ExpiresIn: s.expiresIn,
|
|
}, nil
|
|
}
|
|
|
|
func (s *MemoryStorage) PutObject(ctx context.Context, req design.AssetObjectRequest) (design.AssetObject, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return design.AssetObject{}, err
|
|
}
|
|
|
|
objectKey := objectKey(req.FileName)
|
|
publicURL := fmt.Sprintf("/memory-assets/%s", escapeObjectPath(objectKey))
|
|
if s.publicBase != "" {
|
|
publicURL = publicObjectURL(s.publicBase, objectKey)
|
|
}
|
|
|
|
return design.AssetObject{
|
|
Driver: "memory",
|
|
Bucket: s.bucket,
|
|
ObjectKey: objectKey,
|
|
PublicURL: publicURL,
|
|
}, nil
|
|
}
|
|
|
|
func (s *MemoryStorage) DeleteObject(ctx context.Context, publicURL string) error {
|
|
return ctx.Err()
|
|
}
|
|
|
|
func objectKey(fileName string) string {
|
|
ext := filepath.Ext(fileName)
|
|
return time.Now().UTC().Format("2006/01/02") + "/" + uuid.NewString() + ext
|
|
}
|
|
|
|
func publicObjectURL(publicBase string, objectKey string) string {
|
|
publicBase = strings.TrimRight(publicBase, "/")
|
|
if publicBase == "" {
|
|
return ""
|
|
}
|
|
return publicBase + "/" + escapeObjectPath(objectKey)
|
|
}
|
|
|
|
func escapeObjectPath(objectKey string) string {
|
|
parts := strings.Split(objectKey, "/")
|
|
for index, part := range parts {
|
|
parts[index] = url.PathEscape(part)
|
|
}
|
|
return strings.Join(parts, "/")
|
|
}
|