refactor(*): 平台架构 - 拆分后端领域模块并同步插件协议与模型能力
This commit is contained in:
1 parent
916043fb5a
commit
6e5cbc8b05
644 files changed
+62731
-2320
No files matched your search
@@ -0,0 +1,108 @@
|
||||
package assets
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func DocumentReferences(raw string, resourceIDs map[string]struct{}) bool {
|
||||
return len(DocumentReferencedIDs(raw, resourceIDs)) > 0
|
||||
}
|
||||
|
||||
func DocumentReferencedIDs(raw string, resourceIDs map[string]struct{}) map[string]struct{} {
|
||||
matched := map[string]struct{}{}
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" || len(resourceIDs) == 0 {
|
||||
return matched
|
||||
}
|
||||
found := map[string]struct{}{}
|
||||
var value any
|
||||
if err := json.Unmarshal([]byte(raw), &value); err == nil {
|
||||
if scalar, ok := value.(string); ok {
|
||||
if resourceID := ResourceID(scalar); resourceID != "" {
|
||||
found[resourceID] = struct{}{}
|
||||
}
|
||||
} else {
|
||||
walkReferenceDocument(value, "", found)
|
||||
}
|
||||
} else if resourceID := ResourceID(raw); resourceID != "" {
|
||||
found[resourceID] = struct{}{}
|
||||
}
|
||||
for resourceID := range found {
|
||||
if _, exists := resourceIDs[resourceID]; exists {
|
||||
matched[resourceID] = struct{}{}
|
||||
}
|
||||
}
|
||||
return matched
|
||||
}
|
||||
|
||||
func CollectOwnedDocumentReferences(raw string, resourceIDs map[string]struct{}) error {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return nil
|
||||
}
|
||||
var value any
|
||||
if err := json.Unmarshal([]byte(raw), &value); err != nil {
|
||||
return err
|
||||
}
|
||||
if scalar, ok := value.(string); ok {
|
||||
if resourceID := ResourceID(scalar); resourceID != "" {
|
||||
resourceIDs[resourceID] = struct{}{}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
walkReferenceDocument(value, "", resourceIDs)
|
||||
return nil
|
||||
}
|
||||
|
||||
func SortedIDs(values map[string]struct{}) []string {
|
||||
result := make([]string, 0, len(values))
|
||||
for value := range values {
|
||||
result = append(result, value)
|
||||
}
|
||||
sort.Strings(result)
|
||||
return result
|
||||
}
|
||||
|
||||
func walkReferenceDocument(value any, parentKey string, resourceIDs map[string]struct{}) {
|
||||
switch item := value.(type) {
|
||||
case map[string]any:
|
||||
for key, child := range item {
|
||||
walkReferenceDocument(child, key, resourceIDs)
|
||||
}
|
||||
case []any:
|
||||
for _, child := range item {
|
||||
walkReferenceDocument(child, parentKey, resourceIDs)
|
||||
}
|
||||
case string:
|
||||
if isResourceLocatorField(parentKey) {
|
||||
if resourceID := ResourceID(item); resourceID != "" {
|
||||
resourceIDs[resourceID] = struct{}{}
|
||||
}
|
||||
}
|
||||
if isBareResourceIDField(parentKey) {
|
||||
if resourceID := ValidID(item); resourceID != "" {
|
||||
resourceIDs[resourceID] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func isBareResourceIDField(field string) bool {
|
||||
switch field {
|
||||
case "resourceId", "resourceIds", "sampleResourceId", "referenceResourceId", "referenceResourceIds":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func isResourceLocatorField(field string) bool {
|
||||
switch field {
|
||||
case "storageKey", "content", "url", "dataUrl", "coverUrl", "imageUrl", "videoUrl", "audioUrl", "referenceUrl", "referenceUrls", "artifactRef", "providerArtifactRef":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package assets
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"infinite-canvas/backend/internal/model"
|
||||
)
|
||||
|
||||
type ResourceStream struct {
|
||||
Resource *model.Resource
|
||||
Body io.ReadCloser
|
||||
StatusCode int
|
||||
ContentLength int64
|
||||
ContentRange string
|
||||
AcceptRanges string
|
||||
}
|
||||
|
||||
type ResourceDeliveryOptions struct {
|
||||
ForceDirect bool
|
||||
ForceProxy bool
|
||||
}
|
||||
|
||||
type ResourceDelivery struct {
|
||||
Resource *model.Resource
|
||||
Stream *ResourceStream
|
||||
RedirectURL string
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package assets
|
||||
|
||||
import "strings"
|
||||
|
||||
func FileURL(id string) string {
|
||||
return "/api/resources/" + id + "/file"
|
||||
}
|
||||
|
||||
func IsFileURL(value string) bool {
|
||||
return IDFromFileURL(value) != ""
|
||||
}
|
||||
|
||||
func IDFromFileURL(value string) string {
|
||||
const prefix = "/api/resources/"
|
||||
value = strings.TrimSpace(value)
|
||||
index := strings.Index(value, prefix)
|
||||
if index < 0 {
|
||||
return ""
|
||||
}
|
||||
remainder := value[index+len(prefix):]
|
||||
if remainder == "" {
|
||||
return ""
|
||||
}
|
||||
if end := strings.IndexAny(remainder, "/?#"); end >= 0 {
|
||||
remainder = remainder[:end]
|
||||
}
|
||||
return remainder
|
||||
}
|
||||
|
||||
func ResourceID(value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
if strings.HasPrefix(value, "resource:") {
|
||||
return ValidID(strings.TrimPrefix(value, "resource:"))
|
||||
}
|
||||
return ValidID(IDFromFileURL(value))
|
||||
}
|
||||
|
||||
func ValidID(value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" || len(value) > 80 {
|
||||
return ""
|
||||
}
|
||||
for _, char := range value {
|
||||
if (char < 'a' || char > 'z') && (char < 'A' || char > 'Z') && (char < '0' || char > '9') && char != '-' && char != '_' {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
return value
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package assets
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestIDFromFileURL(t *testing.T) {
|
||||
cases := []struct {
|
||||
in string
|
||||
want string
|
||||
}{
|
||||
{"/api/resources/abc_123/file", "abc_123"},
|
||||
{"https://host.example/api/resources/abc_123/file?proxy=1", "abc_123"},
|
||||
{"/api/projects/abc_123", ""},
|
||||
{"", ""},
|
||||
}
|
||||
for _, item := range cases {
|
||||
if got := IDFromFileURL(item.in); got != item.want {
|
||||
t.Fatalf("IDFromFileURL(%q) = %q, want %q", item.in, got, item.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceID(t *testing.T) {
|
||||
if got := ResourceID("resource:abc_123"); got != "abc_123" {
|
||||
t.Fatalf("ResourceID(resource:) = %q", got)
|
||||
}
|
||||
if got := ResourceID("/api/resources/abc_123/file"); got != "abc_123" {
|
||||
t.Fatalf("ResourceID(file url) = %q", got)
|
||||
}
|
||||
if got := ResourceID("resource:../secret"); got != "" {
|
||||
t.Fatalf("ResourceID accepted traversal: %q", got)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user