Skip to content

Commit b3c68cb

Browse files
authored
add packaged file info when plugin package larger than max size (#312)
1 parent 2cd64ad commit b3c68cb

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

cmd/commandline/plugin/package.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
var (
12-
MaxPluginPackageSize = int64(52428800) // 50MB
12+
MaxPluginPackageSize = int64(50 * 1024 * 1024) // 50 MB
1313
)
1414

1515
func PackagePlugin(inputPath string, outputPath string) {
@@ -24,7 +24,7 @@ func PackagePlugin(inputPath string, outputPath string) {
2424
zipFile, err := packager.Pack(MaxPluginPackageSize)
2525

2626
if err != nil {
27-
log.Error("failed to package plugin %v", err)
27+
log.Error("failed to package plugin: %v", err)
2828
os.Exit(1)
2929
return
3030
}

pkg/plugin_packager/packager/packager.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import (
44
"archive/zip"
55
"bytes"
66
"errors"
7+
"fmt"
78
"path/filepath"
8-
"strconv"
9+
"sort"
910
"strings"
1011

1112
"github.com/langgenius/dify-plugin-daemon/pkg/plugin_packager/decoder"
@@ -34,16 +35,32 @@ func (p *Packager) Pack(maxSize int64) ([]byte, error) {
3435

3536
totalSize := int64(0)
3637

38+
var files []FileInfoWithPath
39+
3740
err = p.decoder.Walk(func(filename, dir string) error {
3841
fullPath := filepath.Join(dir, filename)
3942
file, err := p.decoder.ReadFile(fullPath)
4043
if err != nil {
4144
return err
4245
}
43-
44-
totalSize += int64(len(file))
46+
fileSize := int64(len(file))
47+
files = append(files, FileInfoWithPath{Path: fullPath, Size: fileSize})
48+
totalSize += fileSize
4549
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)
4764
}
4865

4966
// 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) {
7491

7592
return zipBuffer.Bytes(), nil
7693
}
94+
95+
type FileInfoWithPath struct {
96+
Path string
97+
Size int64
98+
}

0 commit comments

Comments
 (0)