소스 검색

fix: avoid saving package before decoding package successfully

Yeuoly 8 달 전
부모
커밋
d455acb919
3개의 변경된 파일23개의 추가작업 그리고 42개의 파일을 삭제
  1. 7 7
      internal/core/plugin_manager/manager.go
  2. 11 10
      internal/core/plugin_manager/media_manager/assets.go
  3. 5 25
      internal/service/plugin_decoder.go

+ 7 - 7
internal/core/plugin_manager/manager.go

@@ -157,12 +157,6 @@ func (p *PluginManager) BackwardsInvocation() dify_invocation.BackwardsInvocatio
 func (p *PluginManager) SavePackage(plugin_unique_identifier plugin_entities.PluginUniqueIdentifier, pkg []byte) (
 	*plugin_entities.PluginDeclaration, error,
 ) {
-	// save to storage
-	err := p.packageBucket.Save(plugin_unique_identifier.String(), pkg)
-	if err != nil {
-		return nil, err
-	}
-
 	// try to decode the package
 	packageDecoder, err := decoder.NewZipPluginDecoder(pkg)
 	if err != nil {
@@ -184,7 +178,7 @@ func (p *PluginManager) SavePackage(plugin_unique_identifier plugin_entities.Plu
 	// remap the assets
 	_, err = p.mediaBucket.RemapAssets(&declaration, assets)
 	if err != nil {
-		return nil, err
+		return nil, errors.Join(err, fmt.Errorf("failed to remap assets"))
 	}
 
 	uniqueIdentifier, err := packageDecoder.UniqueIdentity()
@@ -192,6 +186,12 @@ func (p *PluginManager) SavePackage(plugin_unique_identifier plugin_entities.Plu
 		return nil, err
 	}
 
+	// save to storage
+	err = p.packageBucket.Save(plugin_unique_identifier.String(), pkg)
+	if err != nil {
+		return nil, err
+	}
+
 	// create plugin if not exists
 	if _, err := db.GetOne[models.PluginDeclaration](
 		db.Equal("plugin_unique_identifier", uniqueIdentifier.String()),

+ 11 - 10
internal/core/plugin_manager/media_manager/assets.go

@@ -1,6 +1,7 @@
 package media_manager
 
 import (
+	"errors"
 	"fmt"
 
 	"github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
@@ -37,28 +38,28 @@ func (m *MediaBucket) RemapAssets(declaration *plugin_entities.PluginDeclaration
 			if declaration.Model.IconSmall.EnUS != "" {
 				declaration.Model.IconSmall.EnUS, err = remap(declaration.Model.IconSmall.EnUS)
 				if err != nil {
-					return nil, err
+					return nil, errors.Join(err, fmt.Errorf("failed to remap model icon small en_US"))
 				}
 			}
 
 			if declaration.Model.IconSmall.ZhHans != "" {
 				declaration.Model.IconSmall.ZhHans, err = remap(declaration.Model.IconSmall.ZhHans)
 				if err != nil {
-					return nil, err
+					return nil, errors.Join(err, fmt.Errorf("failed to remap model icon small zh_Hans"))
 				}
 			}
 
 			if declaration.Model.IconSmall.JaJp != "" {
 				declaration.Model.IconSmall.JaJp, err = remap(declaration.Model.IconSmall.JaJp)
 				if err != nil {
-					return nil, err
+					return nil, errors.Join(err, fmt.Errorf("failed to remap model icon small ja_JP"))
 				}
 			}
 
 			if declaration.Model.IconSmall.PtBr != "" {
 				declaration.Model.IconSmall.PtBr, err = remap(declaration.Model.IconSmall.PtBr)
 				if err != nil {
-					return nil, err
+					return nil, errors.Join(err, fmt.Errorf("failed to remap model icon small pt_BR"))
 				}
 			}
 		}
@@ -67,28 +68,28 @@ func (m *MediaBucket) RemapAssets(declaration *plugin_entities.PluginDeclaration
 			if declaration.Model.IconLarge.EnUS != "" {
 				declaration.Model.IconLarge.EnUS, err = remap(declaration.Model.IconLarge.EnUS)
 				if err != nil {
-					return nil, err
+					return nil, errors.Join(err, fmt.Errorf("failed to remap model icon large en_US"))
 				}
 			}
 
 			if declaration.Model.IconLarge.ZhHans != "" {
 				declaration.Model.IconLarge.ZhHans, err = remap(declaration.Model.IconLarge.ZhHans)
 				if err != nil {
-					return nil, err
+					return nil, errors.Join(err, fmt.Errorf("failed to remap model icon large zh_Hans"))
 				}
 			}
 
 			if declaration.Model.IconLarge.JaJp != "" {
 				declaration.Model.IconLarge.JaJp, err = remap(declaration.Model.IconLarge.JaJp)
 				if err != nil {
-					return nil, err
+					return nil, errors.Join(err, fmt.Errorf("failed to remap model icon large ja_JP"))
 				}
 			}
 
 			if declaration.Model.IconLarge.PtBr != "" {
 				declaration.Model.IconLarge.PtBr, err = remap(declaration.Model.IconLarge.PtBr)
 				if err != nil {
-					return nil, err
+					return nil, errors.Join(err, fmt.Errorf("failed to remap model icon large pt_BR"))
 				}
 			}
 		}
@@ -98,7 +99,7 @@ func (m *MediaBucket) RemapAssets(declaration *plugin_entities.PluginDeclaration
 		if declaration.Tool.Identity.Icon != "" {
 			declaration.Tool.Identity.Icon, err = remap(declaration.Tool.Identity.Icon)
 			if err != nil {
-				return nil, err
+				return nil, errors.Join(err, fmt.Errorf("failed to remap tool icon"))
 			}
 		}
 	}
@@ -106,7 +107,7 @@ func (m *MediaBucket) RemapAssets(declaration *plugin_entities.PluginDeclaration
 	if declaration.Icon != "" {
 		declaration.Icon, err = remap(declaration.Icon)
 		if err != nil {
-			return nil, err
+			return nil, errors.Join(err, fmt.Errorf("failed to remap plugin icon"))
 		}
 	}
 

+ 5 - 25
internal/service/plugin_decoder.go

@@ -13,7 +13,7 @@ import (
 	"github.com/langgenius/dify-plugin-daemon/internal/types/entities"
 	"github.com/langgenius/dify-plugin-daemon/internal/types/entities/bundle_entities"
 	"github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
-	"github.com/langgenius/dify-plugin-daemon/internal/utils/cache"
+	"github.com/langgenius/dify-plugin-daemon/internal/utils/cache/helper"
 )
 
 func UploadPluginPkg(
@@ -41,7 +41,7 @@ func UploadPluginPkg(
 	manager := plugin_manager.Manager()
 	declaration, err := manager.SavePackage(pluginUniqueIdentifier, pluginFile)
 	if err != nil {
-		return entities.NewErrorResponse(-500, err.Error())
+		return entities.NewErrorResponse(-500, errors.Join(err, errors.New("failed to save package")).Error())
 	}
 
 	if config.ForceVerifyingSignature || verify_signature {
@@ -159,29 +159,9 @@ func FetchPluginManifest(
 	tenant_id string,
 	pluginUniqueIdentifier plugin_entities.PluginUniqueIdentifier,
 ) *entities.Response {
-	type ManifestCache struct {
-		Declaration plugin_entities.PluginDeclaration `json:"declaration"`
-	}
-
-	pluginManifestCache, err := cache.AutoGetWithGetter(pluginUniqueIdentifier.String(), func() (*ManifestCache, error) {
-		manager := plugin_manager.Manager()
-		pkg, err := manager.GetPackage(pluginUniqueIdentifier)
-		if err != nil {
-			return nil, err
-		}
-
-		decoder, err := decoder.NewZipPluginDecoder(pkg)
-		if err != nil {
-			return nil, err
-		}
-
-		manifest, err := decoder.Manifest()
-		if err != nil {
-			return nil, err
-		}
-
-		return &ManifestCache{Declaration: manifest}, nil
-	})
+	pluginManifestCache, err := helper.CombinedGetPluginDeclaration(
+		pluginUniqueIdentifier, tenant_id, plugin_entities.PLUGIN_RUNTIME_TYPE_LOCAL,
+	)
 
 	if err != nil {
 		return entities.NewErrorResponse(-500, err.Error())