From f6e565a84860b1d494ad9abd6bc2410b77739e0b Mon Sep 17 00:00:00 2001 From: yuan_rh <545873205@qq.com> Date: Fri, 20 Nov 2020 16:11:23 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=E6=B7=BB=E5=8A=A0=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E6=89=AB=E6=8F=8F=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handle/handle.go | 78 ++++++++++++++++++++++++++++++++++++++++-- main.go | 1 + websocket/websocket.go | 38 ++++++++++++++++++++ 3 files changed, 115 insertions(+), 2 deletions(-) diff --git a/handle/handle.go b/handle/handle.go index 91539b2..019b24c 100644 --- a/handle/handle.go +++ b/handle/handle.go @@ -11,6 +11,7 @@ import ( "log" "os" "os/exec" + "path" "path/filepath" "strconv" "strings" @@ -477,14 +478,87 @@ func copyAndCapture(w io.Writer, r io.Reader, progress chan string) ([]byte, err } /** -运行变更生效 +单个文件信息 + */ +type simpleFileInfo struct { + Name string `json:"name" ` + Extension string `json:"extension"` + RelativePath string `json:"relativePath"` + AbsolutePath string `json:"absolutePath"` +} + +var gobalFolderFileMap = make(map[string] *simpleFileInfo) +var gobalRelativePath string +/** +获取指定目录或文件的文件信息,如果是目录递归获取文件信息 @param id 文件id */ -func NotifyFileChange(id string, changeType int){ +func GetFolderFileInfo(conn *websocket.Conn,absolutePath string) error{ + + fileInfo,err :=os.Stat(absolutePath) + if err!=nil{ + log.Println(err) + return err + } + + log.Println(filepath.Dir(absolutePath)) + //单个文件处理 + + if !fileInfo.IsDir() { + simpleFileInfo := new(simpleFileInfo) + simpleFileInfo.Name=fileInfo.Name() + simpleFileInfo.Extension=path.Ext(absolutePath) + simpleFileInfo.RelativePath="" + simpleFileInfo.AbsolutePath=absolutePath + gobalFolderFileMap[absolutePath]=simpleFileInfo + bytes,err :=json.Marshal(gobalFolderFileMap) + if err != nil { + log.Println(err) + return err + } + if err := conn.WriteMessage(websocket.TextMessage, bytes); err != nil { + log.Println(err) + return err + } + return nil + } + + //文件目录处理 + gobalRelativePath = filepath.Dir(absolutePath)+"\\" + err =filepath.Walk(absolutePath, myWalkfunc) + + if err != nil { + log.Println(err) + return err + } + bytes,err :=json.Marshal(gobalFolderFileMap) + if err != nil { + log.Println(err) + return err + } + if err := conn.WriteMessage(websocket.TextMessage, bytes); err != nil { + log.Println(err) + return err + } + + return nil } +func myWalkfunc(path string, info os.FileInfo, err error) error { + if info.IsDir()==false{ + simpleFileInfo := new(simpleFileInfo) + simpleFileInfo.Name=info.Name() + simpleFileInfo.Extension=filepath.Ext(path) + simpleFileInfo.RelativePath=strings.Replace(path,gobalRelativePath,"",1) + simpleFileInfo.AbsolutePath=path + gobalFolderFileMap[path]=simpleFileInfo + return nil + } + + return nil +} /** 获取本地文件列表 diff --git a/main.go b/main.go index 733c18c..759dece 100644 --- a/main.go +++ b/main.go @@ -45,6 +45,7 @@ func main() { http.HandleFunc("/subscriptionFileChange", websocket.SubscriptionFileChangeHandler) http.HandleFunc("/download", websocket.DownloadHandler) http.HandleFunc("/init", websocket.InitLocalWorkSpaceHandler) + http.HandleFunc("/getFolderFileInfo", websocket.GetFolderFileInfoHandler) //服务端启动 log.Println("服务启动成功,监听端口7777,等待连接。") diff --git a/websocket/websocket.go b/websocket/websocket.go index 66eda1b..51d8afc 100644 --- a/websocket/websocket.go +++ b/websocket/websocket.go @@ -133,6 +133,44 @@ ERR: conn.Close() } +func GetFolderFileInfoHandler(w http.ResponseWriter, r *http.Request) { + //w.Write([]byte("hello")) + //收到http请求(upgrade),完成websocket协议转换 + //在应答的header中放上upgrade:websoket + var ( + conn *websocket.Conn + err error + //msgType int + data []byte + ) + if conn, err = upgrader.Upgrade(w, r, nil); err !=nil { + //报错了,直接返回底层的websocket链接就会终断掉 + return + } + //得到了websocket.Conn长连接的对象,实现数据的收发 + for { + //Text(json), Binary + //if _, data, err = conn.ReadMessage(); err != nil { + if _, data, err = conn.ReadMessage(); err != nil { + //报错关闭websocket + goto ERR + } + //发送数据,判断返回值是否报错 + log.Println("param GetFolderFileInfo:"+string(data)) + + err := handle.GetFolderFileInfo(conn,string(data)) + if err!=nil{ + log.Println(err) + goto ERR + } + + goto ERR + } + //error的标签 +ERR: + conn.Close() +} + func SubscriptionFileChangeHandler(w http.ResponseWriter, r *http.Request){ //w.Write([]byte("hello")) //收到http请求(upgrade),完成websocket协议转换 From c4ef2a1f6764274db3983d8349ebbac2a9705f08 Mon Sep 17 00:00:00 2001 From: yuan_rh <545873205@qq.com> Date: Mon, 23 Nov 2020 16:42:32 +0800 Subject: [PATCH 2/2] feat: --- handle/handle.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/handle/handle.go b/handle/handle.go index 019b24c..6205b1b 100644 --- a/handle/handle.go +++ b/handle/handle.go @@ -487,7 +487,7 @@ type simpleFileInfo struct { AbsolutePath string `json:"absolutePath"` } -var gobalFolderFileMap = make(map[string] *simpleFileInfo) +var gobalFolderFileMap map[string] *simpleFileInfo var gobalRelativePath string /** 获取指定目录或文件的文件信息,如果是目录递归获取文件信息 @@ -495,6 +495,7 @@ var gobalRelativePath string */ func GetFolderFileInfo(conn *websocket.Conn,absolutePath string) error{ + gobalFolderFileMap = make(map[string] *simpleFileInfo) fileInfo,err :=os.Stat(absolutePath) if err!=nil{ log.Println(err) @@ -551,7 +552,7 @@ func myWalkfunc(path string, info os.FileInfo, err error) error { simpleFileInfo := new(simpleFileInfo) simpleFileInfo.Name=info.Name() simpleFileInfo.Extension=filepath.Ext(path) - simpleFileInfo.RelativePath=strings.Replace(path,gobalRelativePath,"",1) + simpleFileInfo.RelativePath=filepath.Dir(strings.Replace(path,gobalRelativePath,"",1)) simpleFileInfo.AbsolutePath=path gobalFolderFileMap[path]=simpleFileInfo return nil