local.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package bundle_packager
  2. import (
  3. "bytes"
  4. "io"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  9. "github.com/langgenius/dify-plugin-daemon/pkg/entities/bundle_entities"
  10. )
  11. type LocalBundlePackager struct {
  12. GenericBundlePackager
  13. path string
  14. }
  15. func NewLocalBundlePackager(path string) (BundlePackager, error) {
  16. // try read manifest file
  17. manifestFile, err := os.Open(filepath.Join(path, "manifest.yaml"))
  18. if err != nil {
  19. return nil, err
  20. }
  21. defer manifestFile.Close()
  22. manifestBytes, err := io.ReadAll(manifestFile)
  23. if err != nil {
  24. return nil, err
  25. }
  26. bundle, err := parser.UnmarshalYamlBytes[bundle_entities.Bundle](manifestBytes)
  27. if err != nil {
  28. return nil, err
  29. }
  30. // collect files starts with README
  31. readmeFiles, err := filepath.Glob(filepath.Join(path, "README*"))
  32. if err != nil {
  33. return nil, err
  34. }
  35. extraFiles := make(map[string]*bytes.Buffer)
  36. for _, readmeFile := range readmeFiles {
  37. // trim the path
  38. buffer, err := os.ReadFile(readmeFile)
  39. if err != nil {
  40. return nil, err
  41. }
  42. readmeFile = strings.TrimPrefix(readmeFile, filepath.Clean(path)+"/")
  43. extraFiles[readmeFile] = bytes.NewBuffer(buffer)
  44. }
  45. packager := &LocalBundlePackager{
  46. GenericBundlePackager: *NewGenericBundlePackager(&bundle, extraFiles),
  47. path: path,
  48. }
  49. // walk through the path and load the assets
  50. err = filepath.Walk(filepath.Join(path, "_assets"), func(filePath string, info os.FileInfo, err error) error {
  51. if err != nil {
  52. return err
  53. }
  54. if info.IsDir() {
  55. return nil
  56. }
  57. assetBytes, err := os.ReadFile(filePath)
  58. if err != nil {
  59. return err
  60. }
  61. prefix := filepath.Join(path, "_assets")
  62. assetName := strings.TrimPrefix(filePath, prefix)
  63. // trim slash
  64. assetName = strings.Trim(assetName, "/")
  65. packager.assets[assetName] = bytes.NewBuffer(assetBytes)
  66. return nil
  67. })
  68. if err != nil {
  69. return nil, err
  70. }
  71. return packager, nil
  72. }
  73. func (p *LocalBundlePackager) Save() error {
  74. // save the assets
  75. for name, asset := range p.assets {
  76. err := os.WriteFile(filepath.Join(p.path, "_assets", name), asset.Bytes(), 0644)
  77. if err != nil {
  78. return err
  79. }
  80. }
  81. // save the manifest file
  82. manifestBytes := parser.MarshalYamlBytes(p.bundle)
  83. err := os.WriteFile(filepath.Join(p.path, "manifest.yaml"), manifestBytes, 0644)
  84. if err != nil {
  85. return err
  86. }
  87. return nil
  88. }
  89. func (p *LocalBundlePackager) ReadFile(path string) ([]byte, error) {
  90. return os.ReadFile(filepath.Join(p.path, path))
  91. }