From 4a06bbda09dc1ec2fa369800927cb3c5214bc04d Mon Sep 17 00:00:00 2001 From: Yeuoly Date: Fri, 25 Oct 2024 18:54:03 +0800 Subject: [PATCH] fix: missing remapping logics --- internal/core/plugin_manager/manager.go | 27 +++++++++++++++---------- internal/service/plugin_decoder.go | 22 +++++++++----------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/internal/core/plugin_manager/manager.go b/internal/core/plugin_manager/manager.go index 6ffb329..b8edb65 100644 --- a/internal/core/plugin_manager/manager.go +++ b/internal/core/plugin_manager/manager.go @@ -138,61 +138,66 @@ func (p *PluginManager) BackwardsInvocation() dify_invocation.BackwardsInvocatio return p.backwardsInvocation } -func (p *PluginManager) SavePackage(plugin_unique_identifier plugin_entities.PluginUniqueIdentifier, pkg []byte) error { +func (p *PluginManager) SavePackage(plugin_unique_identifier plugin_entities.PluginUniqueIdentifier, pkg []byte) ( + *plugin_entities.PluginDeclaration, error, +) { // save to storage pkg_path := filepath.Join(p.packageCachePath, plugin_unique_identifier.String()) pkg_dir := filepath.Dir(pkg_path) if err := os.MkdirAll(pkg_dir, 0755); err != nil { - return err + return nil, err } if err := os.WriteFile(pkg_path, pkg, 0644); err != nil { - return err + return nil, err } // try to decode the package package_decoder, err := decoder.NewZipPluginDecoder(pkg) if err != nil { - return err + return nil, err } // get the declaration declaration, err := package_decoder.Manifest() if err != nil { - return err + return nil, err } // get the assets assets, err := package_decoder.Assets() if err != nil { - return err + return nil, err } // remap the assets _, err = p.mediaManager.RemapAssets(&declaration, assets) if err != nil { - return err + return nil, err } unique_identifier, err := package_decoder.UniqueIdentity() if err != nil { - return err + return nil, err } // create plugin if not exists if _, err := db.GetOne[models.PluginDeclaration]( db.Equal("plugin_unique_identifier", unique_identifier.String()), ); err == db.ErrDatabaseNotFound { - return db.Create(&models.PluginDeclaration{ + err = db.Create(&models.PluginDeclaration{ PluginUniqueIdentifier: unique_identifier.String(), PluginID: unique_identifier.PluginID(), Declaration: declaration, }) + if err != nil { + return nil, err + } } else if err != nil { - return err + return nil, err } - return nil + return &declaration, nil } func (p *PluginManager) GetPackage(plugin_unique_identifier plugin_entities.PluginUniqueIdentifier) ([]byte, error) { diff --git a/internal/service/plugin_decoder.go b/internal/service/plugin_decoder.go index df8a579..c450b6d 100644 --- a/internal/service/plugin_decoder.go +++ b/internal/service/plugin_decoder.go @@ -31,32 +31,28 @@ func UploadPluginFromPkg( return entities.NewErrorResponse(-500, err.Error()) } - manifest, err := decoder.Manifest() + plugin_unique_identifier, err := decoder.UniqueIdentity() + if err != nil { + return entities.NewErrorResponse(-500, err.Error()) + } + + manager := plugin_manager.Manager() + declaration, err := manager.SavePackage(plugin_unique_identifier, plugin_file) if err != nil { return entities.NewErrorResponse(-500, err.Error()) } if config.ForceVerifyingSignature || verify_signature { - if !manifest.Verified { + if !declaration.Verified { return entities.NewErrorResponse(-500, errors.Join(err, errors.New( "plugin verification has been enabled, and the plugin you want to install has a bad signature", )).Error()) } } - plugin_unique_identifier, err := decoder.UniqueIdentity() - if err != nil { - return entities.NewErrorResponse(-500, err.Error()) - } - - manager := plugin_manager.Manager() - if err := manager.SavePackage(plugin_unique_identifier, plugin_file); err != nil { - return entities.NewErrorResponse(-500, err.Error()) - } - return entities.NewSuccessResponse(map[string]any{ "unique_identifier": plugin_unique_identifier, - "manifest": manifest, + "manifest": declaration, }) }