|
4 | 4 | "archive/zip"
|
5 | 5 | "bytes"
|
6 | 6 | "errors"
|
| 7 | + "fmt" |
7 | 8 | "path/filepath"
|
8 |
| - "strconv" |
| 9 | + "sort" |
9 | 10 | "strings"
|
10 | 11 |
|
11 | 12 | "github.com/langgenius/dify-plugin-daemon/pkg/plugin_packager/decoder"
|
@@ -34,16 +35,32 @@ func (p *Packager) Pack(maxSize int64) ([]byte, error) {
|
34 | 35 |
|
35 | 36 | totalSize := int64(0)
|
36 | 37 |
|
| 38 | + var files []FileInfoWithPath |
| 39 | + |
37 | 40 | err = p.decoder.Walk(func(filename, dir string) error {
|
38 | 41 | fullPath := filepath.Join(dir, filename)
|
39 | 42 | file, err := p.decoder.ReadFile(fullPath)
|
40 | 43 | if err != nil {
|
41 | 44 | return err
|
42 | 45 | }
|
43 |
| - |
44 |
| - totalSize += int64(len(file)) |
| 46 | + fileSize := int64(len(file)) |
| 47 | + files = append(files, FileInfoWithPath{Path: fullPath, Size: fileSize}) |
| 48 | + totalSize += fileSize |
45 | 49 | if totalSize > maxSize {
|
46 |
| - return errors.New("plugin package size is too large, please ensure the uncompressed size is less than " + strconv.FormatInt(maxSize, 10) + " bytes") |
| 50 | + sort.Slice(files, func(i, j int) bool { |
| 51 | + return files[i].Size > files[j].Size |
| 52 | + }) |
| 53 | + fileTop5Info := "" |
| 54 | + top := 5 |
| 55 | + if len(files) < 5 { |
| 56 | + top = len(files) |
| 57 | + } |
| 58 | + for i := 0; i < top; i++ { |
| 59 | + fileTop5Info += fmt.Sprintf("%d. name: %s, size: %d bytes\n", i+1, files[i].Path, files[i].Size) |
| 60 | + } |
| 61 | + errMsg := fmt.Sprintf("Plugin package size is too large. Please ensure the uncompressed size is less than %d bytes.\nPackaged file info:\n%s", |
| 62 | + maxSize, fileTop5Info) |
| 63 | + return errors.New(errMsg) |
47 | 64 | }
|
48 | 65 |
|
49 | 66 | // ISSUES: Windows path separator is \, but zip requires /, to avoid this we just simply replace all \ with / for now
|
@@ -74,3 +91,8 @@ func (p *Packager) Pack(maxSize int64) ([]byte, error) {
|
74 | 91 |
|
75 | 92 | return zipBuffer.Bytes(), nil
|
76 | 93 | }
|
| 94 | + |
| 95 | +type FileInfoWithPath struct { |
| 96 | + Path string |
| 97 | + Size int64 |
| 98 | +} |
0 commit comments