fs.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package decoder
  2. import (
  3. "errors"
  4. "io"
  5. "io/fs"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "github.com/go-git/go-git/plumbing/format/gitignore"
  10. "github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities"
  11. )
  12. var (
  13. ErrNotDir = errors.New("not a directory")
  14. )
  15. type FSPluginDecoder struct {
  16. PluginDecoder
  17. PluginDecoderHelper
  18. // root directory of the plugin
  19. root string
  20. fs fs.FS
  21. }
  22. func NewFSPluginDecoder(root string) (*FSPluginDecoder, error) {
  23. decoder := &FSPluginDecoder{
  24. root: root,
  25. }
  26. err := decoder.Open()
  27. if err != nil {
  28. return nil, err
  29. }
  30. // read the manifest file
  31. if _, err := decoder.Manifest(); err != nil {
  32. return nil, err
  33. }
  34. return decoder, nil
  35. }
  36. func (d *FSPluginDecoder) Open() error {
  37. d.fs = os.DirFS(d.root)
  38. // try to stat the root directory
  39. s, err := os.Stat(d.root)
  40. if err != nil {
  41. return err
  42. }
  43. if !s.IsDir() {
  44. return ErrNotDir
  45. }
  46. return nil
  47. }
  48. func (d *FSPluginDecoder) Walk(fn func(filename string, dir string) error) error {
  49. // read .difyignore file
  50. ignorePatterns := []gitignore.Pattern{}
  51. // Try .difyignore first, fallback to .gitignore if not found
  52. ignoreBytes, err := d.ReadFile(".difyignore")
  53. if err != nil {
  54. ignoreBytes, err = d.ReadFile(".gitignore")
  55. }
  56. if err == nil {
  57. ignoreLines := strings.Split(string(ignoreBytes), "\n")
  58. for _, line := range ignoreLines {
  59. line = strings.TrimSpace(line)
  60. if line == "" || strings.HasPrefix(line, "#") {
  61. continue
  62. }
  63. ignorePatterns = append(ignorePatterns, gitignore.ParsePattern(line, nil))
  64. }
  65. }
  66. return filepath.WalkDir(d.root, func(path string, info fs.DirEntry, err error) error {
  67. if err != nil {
  68. return err
  69. }
  70. // get relative path from root
  71. relPath, err := filepath.Rel(d.root, path)
  72. if err != nil {
  73. return err
  74. }
  75. // skip root directory
  76. if relPath == "." {
  77. return nil
  78. }
  79. // check if path matches any ignore pattern
  80. pathParts := strings.Split(relPath, string(filepath.Separator))
  81. for _, pattern := range ignorePatterns {
  82. if result := pattern.Match(pathParts, info.IsDir()); result == gitignore.Exclude {
  83. if info.IsDir() {
  84. return filepath.SkipDir
  85. }
  86. return nil
  87. }
  88. }
  89. // get directory path relative to root
  90. dir := filepath.Dir(relPath)
  91. if dir == "." {
  92. dir = ""
  93. }
  94. // skip if the path is a directory
  95. if info.IsDir() {
  96. return nil
  97. }
  98. return fn(info.Name(), dir)
  99. })
  100. }
  101. func (d *FSPluginDecoder) Close() error {
  102. return nil
  103. }
  104. func (d *FSPluginDecoder) Stat(filename string) (fs.FileInfo, error) {
  105. return os.Stat(filepath.Join(d.root, filename))
  106. }
  107. func (d *FSPluginDecoder) ReadFile(filename string) ([]byte, error) {
  108. return os.ReadFile(filepath.Join(d.root, filename))
  109. }
  110. func (d *FSPluginDecoder) ReadDir(dirname string) ([]string, error) {
  111. var files []string
  112. err := filepath.WalkDir(
  113. filepath.Join(d.root, dirname),
  114. func(path string, info fs.DirEntry, err error) error {
  115. if err != nil {
  116. return err
  117. }
  118. if !info.IsDir() {
  119. relPath, err := filepath.Rel(d.root, path)
  120. if err != nil {
  121. return err
  122. }
  123. files = append(files, relPath)
  124. }
  125. return nil
  126. },
  127. )
  128. if err != nil {
  129. return nil, err
  130. }
  131. return files, nil
  132. }
  133. func (d *FSPluginDecoder) FileReader(filename string) (io.ReadCloser, error) {
  134. return os.Open(filepath.Join(d.root, filename))
  135. }
  136. func (d *FSPluginDecoder) Signature() (string, error) {
  137. return "", nil
  138. }
  139. func (d *FSPluginDecoder) CreateTime() (int64, error) {
  140. return 0, nil
  141. }
  142. func (d *FSPluginDecoder) Manifest() (plugin_entities.PluginDeclaration, error) {
  143. return d.PluginDecoderHelper.Manifest(d)
  144. }
  145. func (d *FSPluginDecoder) Assets() (map[string][]byte, error) {
  146. return d.PluginDecoderHelper.Assets(d)
  147. }
  148. func (d *FSPluginDecoder) Checksum() (string, error) {
  149. return d.PluginDecoderHelper.Checksum(d)
  150. }
  151. func (d *FSPluginDecoder) UniqueIdentity() (plugin_entities.PluginUniqueIdentifier, error) {
  152. return d.PluginDecoderHelper.UniqueIdentity(d)
  153. }
  154. func (d *FSPluginDecoder) CheckAssetsValid() error {
  155. return d.PluginDecoderHelper.CheckAssetsValid(d)
  156. }