checksum.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package plugin
  2. import (
  3. "os"
  4. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  5. "github.com/langgenius/dify-plugin-daemon/pkg/plugin_packager/decoder"
  6. )
  7. func CalculateChecksum(pluginPath string) {
  8. var pluginDecoder decoder.PluginDecoder
  9. if stat, err := os.Stat(pluginPath); err == nil {
  10. if stat.IsDir() {
  11. pluginDecoder, err = decoder.NewFSPluginDecoder(pluginPath)
  12. if err != nil {
  13. log.Error("failed to create plugin decoder, plugin path: %s, error: %v", pluginPath, err)
  14. return
  15. }
  16. } else {
  17. bytes, err := os.ReadFile(pluginPath)
  18. if err != nil {
  19. log.Error("failed to read plugin file, plugin path: %s, error: %v", pluginPath, err)
  20. return
  21. }
  22. pluginDecoder, err = decoder.NewZipPluginDecoder(bytes)
  23. if err != nil {
  24. log.Error("failed to create plugin decoder, plugin path: %s, error: %v", pluginPath, err)
  25. return
  26. }
  27. }
  28. } else {
  29. log.Error("failed to get plugin file info, plugin path: %s, error: %v", pluginPath, err)
  30. return
  31. }
  32. checksum, err := pluginDecoder.Checksum()
  33. if err != nil {
  34. log.Error("failed to calculate checksum, plugin path: %s, error: %v", pluginPath, err)
  35. return
  36. }
  37. log.Info("plugin checksum: %s", checksum)
  38. }