diff --git a/cmd/commandline/plugin/package.go b/cmd/commandline/plugin/package.go index c36a7b751..f5a4fe1ae 100644 --- a/cmd/commandline/plugin/package.go +++ b/cmd/commandline/plugin/package.go @@ -9,7 +9,7 @@ import ( ) var ( - MaxPluginPackageSize = int64(52428800) // 50MB + MaxPluginPackageSize = int64(50 * 1024 * 1024) // 50 MB ) func PackagePlugin(inputPath string, outputPath string) { @@ -24,7 +24,7 @@ func PackagePlugin(inputPath string, outputPath string) { zipFile, err := packager.Pack(MaxPluginPackageSize) if err != nil { - log.Error("failed to package plugin %v", err) + log.Error("failed to package plugin: %v", err) os.Exit(1) return } diff --git a/pkg/plugin_packager/packager/packager.go b/pkg/plugin_packager/packager/packager.go index 10837f48a..ef010cebb 100644 --- a/pkg/plugin_packager/packager/packager.go +++ b/pkg/plugin_packager/packager/packager.go @@ -4,8 +4,9 @@ import ( "archive/zip" "bytes" "errors" + "fmt" "path/filepath" - "strconv" + "sort" "strings" "github.com/langgenius/dify-plugin-daemon/pkg/plugin_packager/decoder" @@ -34,16 +35,32 @@ func (p *Packager) Pack(maxSize int64) ([]byte, error) { totalSize := int64(0) + var files []FileInfoWithPath + err = p.decoder.Walk(func(filename, dir string) error { fullPath := filepath.Join(dir, filename) file, err := p.decoder.ReadFile(fullPath) if err != nil { return err } - - totalSize += int64(len(file)) + fileSize := int64(len(file)) + files = append(files, FileInfoWithPath{Path: fullPath, Size: fileSize}) + totalSize += fileSize if totalSize > maxSize { - return errors.New("plugin package size is too large, please ensure the uncompressed size is less than " + strconv.FormatInt(maxSize, 10) + " bytes") + sort.Slice(files, func(i, j int) bool { + return files[i].Size > files[j].Size + }) + fileTop5Info := "" + top := 5 + if len(files) < 5 { + top = len(files) + } + for i := 0; i < top; i++ { + fileTop5Info += fmt.Sprintf("%d. name: %s, size: %d bytes\n", i+1, files[i].Path, files[i].Size) + } + errMsg := fmt.Sprintf("Plugin package size is too large. Please ensure the uncompressed size is less than %d bytes.\nPackaged file info:\n%s", + maxSize, fileTop5Info) + return errors.New(errMsg) } // 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) { return zipBuffer.Bytes(), nil } + +type FileInfoWithPath struct { + Path string + Size int64 +}