perf(files): 优化私有文件鉴权与高成本查询隔离

This commit is contained in:
yml
2026-11-20 20:12:00 +08:00
parent e01a01c1aa
commit 9d542af51f
26 changed files with 2068 additions and 42 deletions
+138
View File
@@ -0,0 +1,138 @@
package model
import (
"encoding/json"
"strings"
"time"
"hfb_sys/backend/internal/filekey"
"gorm.io/datatypes"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
const (
FileResourceGameAccount = "game_account"
FileResourceChatConversation = "chat_conversation"
FileResourceOrder = "order"
FileResourcePaymentAccount = "user_payment_account"
FileResourceWithdrawal = "withdrawal"
FileResourceChatQrCode = "chat_qrcode"
FileResourceOperatingExpense = "operating_expense"
FileResourceManualDisbursement = "manual_disbursement"
FileScopeListingAsset = "listing_asset"
FileScopeChatParticipant = "chat_participant"
FileScopeOrderParticipant = "order_participant"
FileScopeOwner = "owner"
FileScopeQrCodeParticipant = "chat_qrcode_participant"
FileScopeQrCodeTenant = "chat_qrcode_tenant"
FileScopePlatformSuperAdmin = "platform_super_admin"
)
type FileObjectBinding struct {
ID uint64 `gorm:"primaryKey" json:"id"`
TenantID uint64 `gorm:"not null;default:0;index:idx_file_object_binding_key,priority:2" json:"tenant_id"`
CanonicalKey string `gorm:"size:512;not null;uniqueIndex:uk_file_object_binding,priority:1;index:idx_file_object_binding_key,priority:1" json:"canonical_key"`
ResourceType string `gorm:"size:32;not null;uniqueIndex:uk_file_object_binding,priority:2;index:idx_file_object_binding_resource,priority:1" json:"resource_type"`
ResourceID uint64 `gorm:"not null;uniqueIndex:uk_file_object_binding,priority:3;index:idx_file_object_binding_resource,priority:2" json:"resource_id"`
AccessScope string `gorm:"size:32;not null;uniqueIndex:uk_file_object_binding,priority:4" json:"access_scope"`
OwnerUserID uint64 `gorm:"not null;default:0" json:"owner_user_id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
func (FileObjectBinding) TableName() string {
return "file_object_bindings"
}
func UpsertFileBindings(tx *gorm.DB, tenantID uint64, resourceType string, resourceID uint64, accessScope string, ownerUserID uint64, rawKeys []string) error {
if tx == nil || resourceID == 0 || len(rawKeys) == 0 {
return nil
}
rows := make([]FileObjectBinding, 0, len(rawKeys))
seen := make(map[string]struct{}, len(rawKeys))
for _, raw := range rawKeys {
canonical := filekey.Canonical(raw)
if canonical == "" {
continue
}
key := canonical + "\x00" + resourceType + "\x00" + accessScope
if _, ok := seen[key]; ok {
continue
}
seen[key] = struct{}{}
rows = append(rows, FileObjectBinding{
TenantID: tenantID,
CanonicalKey: canonical,
ResourceType: resourceType,
ResourceID: resourceID,
AccessScope: accessScope,
OwnerUserID: ownerUserID,
})
}
if len(rows) == 0 {
return nil
}
return tx.Clauses(clause.OnConflict{
Columns: []clause.Column{
{Name: "canonical_key"},
{Name: "resource_type"},
{Name: "resource_id"},
{Name: "access_scope"},
},
DoUpdates: clause.AssignmentColumns([]string{"tenant_id", "owner_user_id", "updated_at"}),
}).Create(&rows).Error
}
func UpsertJSONFileBindings(tx *gorm.DB, tenantID uint64, resourceType string, resourceID uint64, accessScope string, ownerUserID uint64, raw datatypes.JSON) error {
var keys []string
if len(raw) > 0 {
if err := json.Unmarshal(raw, &keys); err != nil {
return nil
}
}
return UpsertFileBindings(tx, tenantID, resourceType, resourceID, accessScope, ownerUserID, keys)
}
func UpsertScalarFileBinding(tx *gorm.DB, tenantID uint64, resourceType string, resourceID uint64, accessScope string, ownerUserID uint64, raw string) error {
if strings.TrimSpace(raw) == "" {
return nil
}
return UpsertFileBindings(tx, tenantID, resourceType, resourceID, accessScope, ownerUserID, []string{raw})
}
func (m *GameAccount) AfterSave(tx *gorm.DB) error {
return UpsertJSONFileBindings(tx, m.SourceTenantID, FileResourceGameAccount, m.ID, FileScopeListingAsset, m.OwnerID, m.ScreenshotURLS)
}
func (m *ChatMessage) AfterSave(tx *gorm.DB) error {
return UpsertJSONFileBindings(tx, 0, FileResourceChatConversation, m.ConversationID, FileScopeChatParticipant, 0, m.AttachmentURLS)
}
func (m *HandoffRecord) AfterSave(tx *gorm.DB) error {
return UpsertJSONFileBindings(tx, 0, FileResourceOrder, m.OrderID, FileScopeOrderParticipant, 0, m.AttachmentURLS)
}
func (m *Dispute) AfterSave(tx *gorm.DB) error {
return UpsertJSONFileBindings(tx, 0, FileResourceOrder, m.OrderID, FileScopeOrderParticipant, 0, m.EvidenceURLS)
}
func (m *OrderCheckout) AfterSave(tx *gorm.DB) error {
return UpsertJSONFileBindings(tx, 0, FileResourceOrder, m.OrderID, FileScopeOrderParticipant, 0, m.EvidenceURLS)
}
func (m *UserPaymentAccount) AfterSave(tx *gorm.DB) error {
return UpsertJSONFileBindings(tx, m.TenantID, FileResourcePaymentAccount, m.ID, FileScopeOwner, m.UserID, m.CertificateURLs)
}
func (m *WithdrawalRequest) AfterSave(tx *gorm.DB) error {
return UpsertScalarFileBinding(tx, m.TenantID, FileResourceWithdrawal, m.ID, FileScopeOwner, m.UserID, m.PaymentProofURL)
}
func (m *ChatQrCode) AfterSave(tx *gorm.DB) error {
if m.ConversationID != nil {
return UpsertScalarFileBinding(tx, m.TenantID, FileResourceChatConversation, *m.ConversationID, FileScopeQrCodeParticipant, 0, m.ImageURL)
}
return UpsertScalarFileBinding(tx, m.TenantID, FileResourceChatQrCode, m.ID, FileScopeQrCodeTenant, 0, m.ImageURL)
}