From 81db2b2ec624afa2c125fee6cdeebc2b44db081e Mon Sep 17 00:00:00 2001 From: godo Date: Tue, 10 Sep 2024 17:54:07 +0800 Subject: [PATCH] change msg --- .../src/components/localchat/ChatContent.vue | 10 ++--- frontend/src/stores/localchat.ts | 10 +++++ godo/localchat/filedown.go | 41 +++++++++++-------- godo/localchat/filemsg.go | 8 +++- 4 files changed, 46 insertions(+), 23 deletions(-) diff --git a/frontend/src/components/localchat/ChatContent.vue b/frontend/src/components/localchat/ChatContent.vue index 97cd7eb..1ee192e 100644 --- a/frontend/src/components/localchat/ChatContent.vue +++ b/frontend/src/components/localchat/ChatContent.vue @@ -95,8 +95,8 @@ async function scroll({ scrollTop }: { scrollTop: number }) { 对方发送文件 -
-
{{ el }}
+
+
查看文件
diff --git a/frontend/src/stores/localchat.ts b/frontend/src/stores/localchat.ts index 6e977ea..6ca9c41 100644 --- a/frontend/src/stores/localchat.ts +++ b/frontend/src/stores/localchat.ts @@ -430,6 +430,16 @@ export const useLocalChatStore = defineStore('localChatStore', () => { //console.log(coms) notifyError("确认失败!") } else { + const res = await coms.json() + if(res.code === 0){ + item.content.path = res.data.path + item.content.status = 'accessed' + //console.log(item) + await db.update('chatmsg', res.data.msg.msgId, toRaw(item)) + await getMsgList() + }else{ + notifyError(res.message) + } // item.content.status = 'accessing' // //console.log(item) // await db.update('chatmsg', item.id, toRaw(item)) diff --git a/godo/localchat/filedown.go b/godo/localchat/filedown.go index d9ba98b..01c891c 100644 --- a/godo/localchat/filedown.go +++ b/godo/localchat/filedown.go @@ -38,38 +38,38 @@ import ( "time" ) -func downloadFiles(msg UdpMessage) error { +func downloadFiles(msg UdpMessage) (string, error) { postUrl := fmt.Sprintf("http://%s:56780/localchat/getfiles", msg.IP) postData, err := json.Marshal(msg.Message) if err != nil { - return fmt.Errorf("failed to marshal post data: %v", err) + return "", fmt.Errorf("failed to marshal post data: %v", err) } log.Printf("Sending POST request to %s with data: %s", postUrl, string(postData)) resp, err := http.Post(postUrl, "application/json", bytes.NewBuffer(postData)) if err != nil { - return fmt.Errorf("failed to make POST request: %v", err) + return "", fmt.Errorf("failed to make POST request: %v", err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { body, _ := io.ReadAll(resp.Body) - return fmt.Errorf("server returned status code: %v, body: %s", resp.StatusCode, body) + return "", fmt.Errorf("server returned status code: %v, body: %s", resp.StatusCode, body) } - + path, err := handleResponse(resp.Body, msg.IP) // 处理响应中的文件 - if err := handleResponse(resp.Body, msg.IP); err != nil { + if err != nil { log.Fatalf("Failed to handle response: %v", err) } fmt.Println("Files downloaded successfully") - return nil + return path, nil } -func handleResponse(reader io.Reader, ip string) error { +func handleResponse(reader io.Reader, ip string) (string, error) { // 接收文件的目录 baseDir, err := libs.GetOsDir() if err != nil { log.Printf("Failed to get OS directory: %v", err) - return fmt.Errorf("failed to get OS directory") + return "", fmt.Errorf("failed to get OS directory") } resPath := filepath.Join("C", "Users", "Reciv", time.Now().Format("2006-01-02")) @@ -78,27 +78,36 @@ func handleResponse(reader io.Reader, ip string) error { err := os.MkdirAll(receiveDir, 0755) if err != nil { log.Printf("Failed to create receive directory: %v", err) - return fmt.Errorf("failed to create receive directory") + return "", fmt.Errorf("failed to create receive directory") + } + } + timestamp := time.Now().Format("15-04-05") + revPath := filepath.Join(receiveDir, timestamp) + if !libs.PathExists(revPath) { + err := os.MkdirAll(revPath, 0755) + if err != nil { + log.Printf("Failed to create receive directory: %v", err) + return "", fmt.Errorf("failed to create receive directory") } } body, err := io.ReadAll(reader) if err != nil { - return fmt.Errorf("failed to read response body: %v", err) + return "", fmt.Errorf("failed to read response body: %v", err) } - log.Printf("Received file list: %v", string(body)) + //log.Printf("Received file list: %v", string(body)) // 解析文件列表 var fileList []FileItem if err := json.Unmarshal(body, &fileList); err != nil { - return fmt.Errorf("failed to unmarshal file list: %v", err) + return "", fmt.Errorf("failed to unmarshal file list: %v", err) } - log.Printf("Received file list: %v", fileList) + //log.Printf("Received file list: %v", fileList) for _, file := range fileList { if runtime.GOOS != "windows" && strings.Contains(file.WritePath, "\\") { file.WritePath = strings.ReplaceAll(file.WritePath, "\\", "/") } - checkpath := filepath.Join(receiveDir, file.WritePath) + checkpath := filepath.Join(revPath, file.WritePath) if !libs.PathExists(checkpath) { os.MkdirAll(checkpath, 0755) @@ -109,7 +118,7 @@ func handleResponse(reader io.Reader, ip string) error { } } - return nil + return revPath, nil } // downloadFile 下载单个文件 diff --git a/godo/localchat/filemsg.go b/godo/localchat/filemsg.go index 2a55b12..767a276 100644 --- a/godo/localchat/filemsg.go +++ b/godo/localchat/filemsg.go @@ -86,10 +86,14 @@ func HandlerAccessFile(w http.ResponseWriter, r *http.Request) { msg.Time = time.Now() msg.Type = "fileAccessed" SendToIP(msg) - err = downloadFiles(msg) + path, err := downloadFiles(msg) if err != nil { libs.ErrorMsg(w, "HandleMessage error") return } - libs.SuccessMsg(w, msg.Message, "接收文件中") + res := map[string]interface{}{ + "path": path, + "msg": msg.Message, + } + libs.SuccessMsg(w, res, "接收文件成功") }