From f9596f63637cbde11388e126b0ff600814db441f Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Mon, 11 Sep 2023 21:03:53 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BB=E5=8F=96CSV=E6=96=87=E4=BB=B6?= =?UTF-8?q?=EF=BC=8C=E8=BD=AC=E6=8D=A2map=E6=95=B0=E6=8D=AE=E3=80=82?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0scp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/core/file/csv.go | 43 +++++++++++++++++++++++++++++++++++++++ lib/core/file/ssh.go | 22 ++++++++++++++++++++ lib/core/utils/ctx/ctx.go | 32 +++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 lib/core/file/ssh.go diff --git a/lib/core/file/csv.go b/lib/core/file/csv.go index ad7763f5..f8bcce90 100644 --- a/lib/core/file/csv.go +++ b/lib/core/file/csv.go @@ -4,6 +4,7 @@ import ( "encoding/csv" "os" "path/filepath" + "strings" "ems.agt/lib/log" ) @@ -41,3 +42,45 @@ func WriterCSVFile(data [][]string, filePath string) error { } return nil } + +// 读取CSV文件,转换map数据 +func ReadCSVFile(filePath string) []map[string]string { + // 打开 CSV 文件 + file, err := os.Open(filePath) + if err != nil { + log.Fatal("无法打开 CSV 文件:", err) + } + defer file.Close() + + // 创建 CSV Reader + reader := csv.NewReader(file) + + // 读取 CSV 头部行 + header, err := reader.Read() + if err != nil { + log.Fatal("无法读取 CSV 头部行:", err) + } + + // 创建 map 存储 CSV 数据 + arr := make([]map[string]string, 0) + + // 遍历 CSV 数据行 + for { + // 读取一行数据 + record, err := reader.Read() + if err != nil { + // 到达文件末尾或遇到错误时退出循环 + break + } + + // 将 CSV 数据插入到 map 中 + data := make(map[string]string) + for i, value := range record { + key := strings.ToLower(header[i]) + data[key] = value + } + arr = append(arr, data) + } + + return arr +} diff --git a/lib/core/file/ssh.go b/lib/core/file/ssh.go new file mode 100644 index 00000000..d5d11e80 --- /dev/null +++ b/lib/core/file/ssh.go @@ -0,0 +1,22 @@ +package file + +import ( + "fmt" + "os/exec" + + "ems.agt/lib/core/conf" +) + +// 网元NE 文件复制到远程文件夹 +func FileNeSCP(neIp, filePath, dstPath string) error { + usernameNe := conf.Get("ne.user").(string) + // scp /path/to/local/file.txt user@remote-server:/path/to/remote/directory/ + dstDir := fmt.Sprintf("%s@%s:%s", usernameNe, neIp, dstPath) + cmd := exec.Command("scp", "-r", filePath, dstDir) + out, err := cmd.CombinedOutput() + if err != nil { + return err + } + fmt.Println(out) + return nil +} diff --git a/lib/core/utils/ctx/ctx.go b/lib/core/utils/ctx/ctx.go index 59a5f2e4..f800d7a4 100644 --- a/lib/core/utils/ctx/ctx.go +++ b/lib/core/utils/ctx/ctx.go @@ -6,6 +6,8 @@ import ( "io" "net/http" "net/url" + "os" + "path/filepath" "ems.agt/lib/core/vo" "github.com/gorilla/mux" @@ -67,6 +69,36 @@ func FileAttachment(w http.ResponseWriter, r *http.Request, filepath, filename s http.ServeFile(w, r, filepath) } +// 将文件上传保存到指定目录 +// file, handler, err := r.FormFile("file") +// SaveUploadedFile uploads the form file to specific dst. +func SaveUploadedFile(r *http.Request, dst string) error { + // 解析请求中的文件 + _, handler, err := r.FormFile("file") + if err != nil { + return err + } + + src, err := handler.Open() + if err != nil { + return err + } + defer src.Close() + + if err = os.MkdirAll(filepath.Dir(dst), 0750); err != nil { + return err + } + + out, err := os.Create(dst) + if err != nil { + return err + } + defer out.Close() + + _, err = io.Copy(out, src) + return err +} + /// ==== 登录用户信息, 通过中间件后预置入 // 定义自定义类型作为键