Skip to content

Commit

Permalink
fix: missing remapping logics
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeuoly committed Oct 25, 2024
1 parent 6adfe92 commit 4a06bbd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
27 changes: 16 additions & 11 deletions internal/core/plugin_manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 9 additions & 13 deletions internal/service/plugin_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}

Expand Down

0 comments on commit 4a06bbd

Please sign in to comment.