Browse Source

feat: checksum

Yeuoly 10 months ago
parent
commit
3abfa02471
2 changed files with 53 additions and 2 deletions
  1. 49 0
      cmd/commandline/plugin.go
  2. 4 2
      internal/core/plugin_packager/packager/packager.go

+ 49 - 0
cmd/commandline/plugin.go

@@ -5,6 +5,7 @@ import (
 	"os"
 
 	init_pkg "github.com/langgenius/dify-plugin-daemon/cmd/commandline/init"
+	"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/checksum"
 	"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/decoder"
 	"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/packager"
 	"github.com/langgenius/dify-plugin-daemon/internal/utils/log"
@@ -58,6 +59,53 @@ var (
 		},
 	}
 
+	pluginChecksumCommand = &cobra.Command{
+		Use:   "checksum plugin_path",
+		Short: "Checksum",
+		Long:  "Calculate the checksum of the plugin, you need specify the plugin path or .difypkg file path",
+		Run: func(cmd *cobra.Command, args []string) {
+			if len(args) < 1 {
+				fmt.Println("Error: plugin_path is required")
+				return
+			}
+
+			plugin_path := args[0]
+			var plugin_decoder decoder.PluginDecoder
+			if stat, err := os.Stat(plugin_path); err == nil {
+				if stat.IsDir() {
+					plugin_decoder, err = decoder.NewFSPluginDecoder(plugin_path)
+					if err != nil {
+						log.Error("failed to create plugin decoder, plugin path: %s, error: %v", plugin_path, err)
+						return
+					}
+				} else {
+					bytes, err := os.ReadFile(plugin_path)
+					if err != nil {
+						log.Error("failed to read plugin file, plugin path: %s, error: %v", plugin_path, err)
+						return
+					}
+
+					plugin_decoder, err = decoder.NewZipPluginDecoder(bytes)
+					if err != nil {
+						log.Error("failed to create plugin decoder, plugin path: %s, error: %v", plugin_path, err)
+						return
+					}
+				}
+			} else {
+				log.Error("failed to get plugin file info, plugin path: %s, error: %v", plugin_path, err)
+				return
+			}
+
+			checksum, err := checksum.CalculateChecksum(plugin_decoder)
+			if err != nil {
+				log.Error("failed to calculate checksum, plugin path: %s, error: %v", plugin_path, err)
+				return
+			}
+
+			log.Info("plugin checksum: %s", checksum)
+		},
+	}
+
 	pluginPermissionCommand = &cobra.Command{
 		Use:   "permission",
 		Short: "Permission",
@@ -91,6 +139,7 @@ endpoint				- allow plugin to register endpoint`,
 func init() {
 	pluginCommand.AddCommand(pluginInitCommand)
 	pluginCommand.AddCommand(pluginPackageCommand)
+	pluginCommand.AddCommand(pluginChecksumCommand)
 	pluginCommand.AddCommand(pluginPermissionCommand)
 	pluginPermissionCommand.AddCommand(pluginPermissionAddCommand)
 	pluginPermissionCommand.AddCommand(pluginPermissionDropCommand)

+ 4 - 2
internal/core/plugin_packager/packager/packager.go

@@ -3,6 +3,7 @@ package packager
 import (
 	"archive/zip"
 	"bytes"
+	"path/filepath"
 
 	"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/decoder"
 )
@@ -29,12 +30,13 @@ func (p *Packager) Pack() ([]byte, error) {
 	zip_writer := zip.NewWriter(zip_buffer)
 
 	err = p.decoder.Walk(func(filename, dir string) error {
-		file, err := p.decoder.ReadFile(filename)
+		full_path := filepath.Join(dir, filename)
+		file, err := p.decoder.ReadFile(full_path)
 		if err != nil {
 			return err
 		}
 
-		zip_file, err := zip_writer.Create(filename)
+		zip_file, err := zip_writer.Create(full_path)
 		if err != nil {
 			return err
 		}