41 lines
1.1 KiB
Go
41 lines
1.1 KiB
Go
package session
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"github.com/Tencent/WeKnora/internal/types"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Only call with the server-saved uploads or owned temporary documents prepared
|
||
|
|
// by prepareQARequest. Text and client-supplied URLs are not ownership evidence.
|
||
|
|
func (h *Handler) bindUploadedMessageResources(ctx context.Context, message *types.Message) error {
|
||
|
|
if h.resourceCatalog == nil || message == nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
tenantID, _ := types.TenantIDFromContext(ctx)
|
||
|
|
refs := make(map[string]struct{})
|
||
|
|
for _, image := range message.Images {
|
||
|
|
refs[image.URL] = struct{}{}
|
||
|
|
}
|
||
|
|
for _, attachment := range message.Attachments {
|
||
|
|
refs[attachment.URL] = struct{}{}
|
||
|
|
}
|
||
|
|
for ref := range refs {
|
||
|
|
if _, ok := types.ParseResourcePath(ref); !ok {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
resource, err := h.resourceCatalog.Resolve(ctx, ref)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if resource == nil || resource.TenantID != tenantID || message.ID == "" {
|
||
|
|
return fmt.Errorf("invalid uploaded message resource")
|
||
|
|
}
|
||
|
|
if err := h.resourceCatalog.Bind(ctx, ref, types.ResourceOwnerMessage, message.ID, types.ResourceRelationAttachment); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|