From fd2053e14cd4990a0a84984b6871146a589d4699 Mon Sep 17 00:00:00 2001 From: 1-6 Date: Tue, 25 Jul 2023 14:48:49 +0800 Subject: [PATCH] x --- core/api_bucket.go | 9 +++++++- core/cdle.go | 8 +++---- core/grpc_download.go | 51 +++++++++++++++++++++++++++++-------------- core/grpc_plugins.go | 6 ++++- core/plugin_core.go | 3 ++- 5 files changed, 54 insertions(+), 23 deletions(-) diff --git a/core/api_bucket.go b/core/api_bucket.go index ed68d87..429e875 100644 --- a/core/api_bucket.go +++ b/core/api_bucket.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "net/http" "os" + "path/filepath" "strconv" "strings" "time" @@ -195,7 +196,13 @@ func init() { ar := strings.SplitN(bk, ".", 2) if len(ar) == 2 { if vv, ok := plugins_id.Load(ar[1]); ok { - os.WriteFile(vv.(string), []byte(fmt.Sprint(v)), 0755) + filename := vv.(string) + vs := fmt.Sprint(v) + if vs == "" { + os.RemoveAll(filepath.Dir(filename)) + } else { + os.WriteFile(filename, []byte(vs), 0755) + } continue } msg, changed, err := SetBucketKeyValue(MakeBucket(ar[0]), ar[1], v) diff --git a/core/cdle.go b/core/cdle.go index c1adbdb..7742bf8 100644 --- a/core/cdle.go +++ b/core/cdle.go @@ -21,10 +21,10 @@ func init() { { Admin: true, Rules: []string{"identify sublink [地址] [组织]"}, - ImType: &common.Filter{ - BlackMode: true, - Items: []string{"pgm"}, - }, + // ImType: &common.Filter{ + // BlackMode: true, + // Items: []string{"pgm"}, + // }, Handle: func(s common.Sender, _ func(vm *goja.Runtime)) interface{} { address := s.Get(0) organization := s.Get(1) diff --git a/core/grpc_download.go b/core/grpc_download.go index 95f03cd..eec906f 100644 --- a/core/grpc_download.go +++ b/core/grpc_download.go @@ -82,31 +82,50 @@ func init() { func unzip(filename string, perm fs.FileMode) error { zipFile, err := zip.OpenReader(filename) - dir := filepath.Dir(filename) - // fmt.Println(filename, err) if err != nil { return err } defer zipFile.Close() + for _, file := range zipFile.File { // 忽略以 "__MACOSX/" 开头的文件 if strings.HasPrefix(file.Name, "__MACOSX/") { continue } - zipFile, err := file.Open() - if err != nil { - return err - } - defer zipFile.Close() - localFile, err := os.OpenFile(dir+"/"+file.Name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm) - if err != nil { - return err - } - defer localFile.Close() - _, err = io.Copy(localFile, zipFile) - if err != nil { - return err + + path := filepath.Join(filepath.Dir(filename), file.Name) + if file.FileInfo().IsDir() { + // 如果是目录则创建目录 + err = os.MkdirAll(path, perm) + if err != nil { + return err + } + } else { + // 创建文件的父目录 + err = os.MkdirAll(filepath.Dir(path), perm) + if err != nil { + return err + } + + // 创建文件并解压缩数据 + zipFile, err := file.Open() + if err != nil { + return err + } + defer zipFile.Close() + + localFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm) + if err != nil { + return err + } + defer localFile.Close() + + _, err = io.Copy(localFile, zipFile) + if err != nil { + return err + } } } - return err + + return nil } diff --git a/core/grpc_plugins.go b/core/grpc_plugins.go index 0b846f6..7977f69 100644 --- a/core/grpc_plugins.go +++ b/core/grpc_plugins.go @@ -190,10 +190,14 @@ func AddNodePlugin(path, name string) error { if err != nil { return err } + script := string(data) + if script == "" { + return nil + } uuid := nameUuid(name) plugins_id.Store(uuid, path) // fmt.Println("add,", uuid, name) - f, cbs := pluginParse(string(data), uuid) + f, cbs := pluginParse(script, uuid) f.Suffix = ".js" f.Type = "typescript" f.Handle = func(s common.Sender, f func(vm *goja.Runtime)) interface{} { diff --git a/core/plugin_core.go b/core/plugin_core.go index 83490d0..2ca98bd 100644 --- a/core/plugin_core.go +++ b/core/plugin_core.go @@ -149,10 +149,11 @@ func initPlugins() { Error: err, } } + fmt.Println(zipfile) defer os.Remove(zipfile) if err := unzip(zipfile, 0755); err != nil { return &storage.Final{ - Error: errors.New("安装异常!"), + Error: errors.New("安装异常!" + err.Error()), } } return &storage.Final{