fs.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package decoder
  2. import (
  3. "bufio"
  4. "errors"
  5. "io"
  6. "io/fs"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "github.com/langgenius/dify-plugin-daemon/internal/types/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. // dify_ignores is a map[string][]string, the key is the directory, the value is a list of files to ignore
  50. dify_ignores := make(map[string][]string)
  51. return filepath.Walk(d.root, func(path string, info fs.FileInfo, err error) error {
  52. // trim the first directory path
  53. path = strings.TrimPrefix(path, d.root)
  54. // trim / from the beginning
  55. path = strings.TrimPrefix(path, "/")
  56. p := filepath.Dir(path)
  57. if info.IsDir() {
  58. // try read the .difyignore file if it's the first time to walk this directory
  59. if _, ok := dify_ignores[p]; !ok {
  60. dify_ignores[p] = make([]string, 0)
  61. // read the .difyignore file if it exists
  62. ignore_file_path := filepath.Join(d.root, p, ".difyignore")
  63. if _, err := os.Stat(ignore_file_path); err == nil {
  64. ignore_file, err := os.Open(ignore_file_path)
  65. if err != nil {
  66. return err
  67. }
  68. scanner := bufio.NewScanner(ignore_file)
  69. for scanner.Scan() {
  70. line := scanner.Text()
  71. if strings.HasPrefix(line, "#") {
  72. continue
  73. }
  74. dify_ignores[p] = append(dify_ignores[p], line)
  75. }
  76. ignore_file.Close()
  77. }
  78. }
  79. return nil
  80. }
  81. current_ignore_files := dify_ignores[p]
  82. for _, ignore_file := range current_ignore_files {
  83. // skip if match
  84. matched, err := filepath.Match(ignore_file, info.Name())
  85. if err != nil {
  86. return err
  87. }
  88. if matched {
  89. return nil
  90. }
  91. }
  92. if path == "" {
  93. return nil
  94. }
  95. if err != nil {
  96. return err
  97. }
  98. return fn(info.Name(), p)
  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. rel_path, err := filepath.Rel(d.root, path)
  120. if err != nil {
  121. return err
  122. }
  123. files = append(files, rel_path)
  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. }