Initial commit: img-infinite-canvas AI design workbench MVP
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>
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
package objectstorage
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"img_infinite_canvas/internal/domain/design"
|
||||
|
||||
"github.com/aliyun/aliyun-oss-go-sdk/oss"
|
||||
)
|
||||
|
||||
type AliyunOptions struct {
|
||||
Bucket string
|
||||
Endpoint string
|
||||
AccessKey string
|
||||
SecretKey string
|
||||
PublicBase string
|
||||
ExpiresIn int64
|
||||
}
|
||||
|
||||
type AliyunStorage struct {
|
||||
bucketName string
|
||||
publicBase string
|
||||
expiresIn int64
|
||||
bucket *oss.Bucket
|
||||
}
|
||||
|
||||
func NewAliyunStorage(ctx context.Context, opts AliyunOptions) (*AliyunStorage, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
client, err := oss.New(opts.Endpoint, opts.AccessKey, opts.SecretKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
exists, err := client.IsBucketExist(opts.Bucket)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exists {
|
||||
if err := client.CreateBucket(opts.Bucket); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
bucket, err := client.Bucket(opts.Bucket)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if opts.ExpiresIn <= 0 {
|
||||
opts.ExpiresIn = 900
|
||||
}
|
||||
return &AliyunStorage{
|
||||
bucketName: opts.Bucket,
|
||||
publicBase: strings.TrimRight(opts.PublicBase, "/"),
|
||||
expiresIn: opts.ExpiresIn,
|
||||
bucket: bucket,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *AliyunStorage) CreateUpload(ctx context.Context, req design.AssetUploadRequest) (design.AssetUpload, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return design.AssetUpload{}, err
|
||||
}
|
||||
|
||||
objectKey := objectKey(req.FileName)
|
||||
signedURL, err := s.bucket.SignURL(objectKey, oss.HTTPPut, s.expiresIn, oss.ContentType(req.ContentType))
|
||||
if err != nil {
|
||||
return design.AssetUpload{}, err
|
||||
}
|
||||
|
||||
publicURL := signedURL
|
||||
if s.publicBase != "" {
|
||||
publicURL = publicObjectURL(s.publicBase, objectKey)
|
||||
}
|
||||
|
||||
return design.AssetUpload{
|
||||
Driver: "aliyun",
|
||||
Bucket: s.bucketName,
|
||||
ObjectKey: objectKey,
|
||||
UploadURL: signedURL,
|
||||
PublicURL: publicURL,
|
||||
Headers: map[string]string{
|
||||
"Content-Type": req.ContentType,
|
||||
},
|
||||
ExpiresIn: s.expiresIn,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *AliyunStorage) PutObject(ctx context.Context, req design.AssetObjectRequest) (design.AssetObject, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return design.AssetObject{}, err
|
||||
}
|
||||
|
||||
objectKey := objectKey(req.FileName)
|
||||
if err := s.bucket.PutObject(objectKey, bytes.NewReader(req.Data), oss.ContentType(req.ContentType)); err != nil {
|
||||
return design.AssetObject{}, err
|
||||
}
|
||||
|
||||
publicURL := publicObjectURL(s.publicBase, objectKey)
|
||||
|
||||
return design.AssetObject{
|
||||
Driver: "aliyun",
|
||||
Bucket: s.bucketName,
|
||||
ObjectKey: objectKey,
|
||||
PublicURL: publicURL,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *AliyunStorage) DeleteObject(ctx context.Context, publicURL string) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
objectKey, ok := objectKeyFromPublicURL(s.publicBase, publicURL)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return s.bucket.DeleteObject(objectKey)
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
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, "/")
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
package objectstorage
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"img_infinite_canvas/internal/domain/design"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
type S3Options struct {
|
||||
Driver string
|
||||
Bucket string
|
||||
Endpoint string
|
||||
Region string
|
||||
AccessKey string
|
||||
SecretKey string
|
||||
PublicBase string
|
||||
UseSSL bool
|
||||
ExpiresIn int64
|
||||
}
|
||||
|
||||
type S3Storage struct {
|
||||
driver string
|
||||
bucket string
|
||||
publicBase string
|
||||
expiresIn int64
|
||||
client *minio.Client
|
||||
}
|
||||
|
||||
func NewS3Storage(ctx context.Context, opts S3Options) (*S3Storage, error) {
|
||||
client, err := minio.New(opts.Endpoint, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(opts.AccessKey, opts.SecretKey, ""),
|
||||
Secure: opts.UseSSL,
|
||||
Region: opts.Region,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
exists, err := client.BucketExists(ctx, opts.Bucket)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exists {
|
||||
if err := client.MakeBucket(ctx, opts.Bucket, minio.MakeBucketOptions{Region: opts.Region}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if strings.EqualFold(opts.Driver, "minio") && strings.TrimSpace(opts.PublicBase) != "" {
|
||||
if err := setMinioPublicReadPolicy(ctx, client, opts.Bucket); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if opts.ExpiresIn <= 0 {
|
||||
opts.ExpiresIn = 900
|
||||
}
|
||||
return &S3Storage{
|
||||
driver: opts.Driver,
|
||||
bucket: opts.Bucket,
|
||||
publicBase: strings.TrimRight(opts.PublicBase, "/"),
|
||||
expiresIn: opts.ExpiresIn,
|
||||
client: client,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func setMinioPublicReadPolicy(ctx context.Context, client *minio.Client, bucket string) error {
|
||||
policy := fmt.Sprintf(`{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Principal": {"AWS": ["*"]},
|
||||
"Action": ["s3:GetObject"],
|
||||
"Resource": ["arn:aws:s3:::%s/*"]
|
||||
}
|
||||
]
|
||||
}`, bucket)
|
||||
return client.SetBucketPolicy(ctx, bucket, policy)
|
||||
}
|
||||
|
||||
func (s *S3Storage) CreateUpload(ctx context.Context, req design.AssetUploadRequest) (design.AssetUpload, error) {
|
||||
objectKey := objectKey(req.FileName)
|
||||
headers := map[string]string{"Content-Type": req.ContentType}
|
||||
uploadURL, err := s.client.PresignedPutObject(ctx, s.bucket, objectKey, time.Duration(s.expiresIn)*time.Second)
|
||||
if err != nil {
|
||||
return design.AssetUpload{}, err
|
||||
}
|
||||
|
||||
publicURL := uploadURL.String()
|
||||
if s.publicBase != "" {
|
||||
publicURL = publicObjectURL(s.publicBase, objectKey)
|
||||
}
|
||||
|
||||
return design.AssetUpload{
|
||||
Driver: s.driver,
|
||||
Bucket: s.bucket,
|
||||
ObjectKey: objectKey,
|
||||
UploadURL: uploadURL.String(),
|
||||
PublicURL: publicURL,
|
||||
Headers: headers,
|
||||
ExpiresIn: s.expiresIn,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *S3Storage) PutObject(ctx context.Context, req design.AssetObjectRequest) (design.AssetObject, error) {
|
||||
objectKey := objectKey(req.FileName)
|
||||
if _, err := s.client.PutObject(ctx, s.bucket, objectKey, bytes.NewReader(req.Data), int64(len(req.Data)), minio.PutObjectOptions{
|
||||
ContentType: req.ContentType,
|
||||
}); err != nil {
|
||||
return design.AssetObject{}, err
|
||||
}
|
||||
|
||||
publicURL := publicObjectURL(s.publicBase, objectKey)
|
||||
|
||||
return design.AssetObject{
|
||||
Driver: s.driver,
|
||||
Bucket: s.bucket,
|
||||
ObjectKey: objectKey,
|
||||
PublicURL: publicURL,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *S3Storage) DeleteObject(ctx context.Context, publicURL string) error {
|
||||
objectKey, ok := objectKeyFromPublicURL(s.publicBase, publicURL)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return s.client.RemoveObject(ctx, s.bucket, objectKey, minio.RemoveObjectOptions{})
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package objectstorage
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func objectKeyFromPublicURL(publicBase string, value string) (string, bool) {
|
||||
value = strings.TrimSpace(value)
|
||||
publicBase = strings.TrimRight(strings.TrimSpace(publicBase), "/")
|
||||
if value == "" || publicBase == "" {
|
||||
return "", false
|
||||
}
|
||||
|
||||
if strings.HasPrefix(value, publicBase+"/") {
|
||||
return unescapeObjectKey(strings.TrimPrefix(strings.Split(value, "?")[0], publicBase+"/"))
|
||||
}
|
||||
|
||||
parsedValue, err := url.Parse(value)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
parsedBase, err := url.Parse(publicBase)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
basePath := strings.TrimRight(parsedBase.EscapedPath(), "/")
|
||||
valuePath := parsedValue.EscapedPath()
|
||||
if basePath == "" || !strings.HasPrefix(valuePath, basePath+"/") {
|
||||
return "", false
|
||||
}
|
||||
return unescapeObjectKey(strings.TrimPrefix(valuePath, basePath+"/"))
|
||||
}
|
||||
|
||||
func unescapeObjectKey(value string) (string, bool) {
|
||||
value = strings.TrimLeft(strings.TrimSpace(value), "/")
|
||||
if value == "" {
|
||||
return "", false
|
||||
}
|
||||
objectKey, err := url.PathUnescape(value)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
return objectKey, objectKey != ""
|
||||
}
|
||||
Reference in New Issue
Block a user