diff --git a/frontend/src/stores/localchat.ts b/frontend/src/stores/localchat.ts index 9e01476..6e977ea 100644 --- a/frontend/src/stores/localchat.ts +++ b/frontend/src/stores/localchat.ts @@ -430,10 +430,10 @@ export const useLocalChatStore = defineStore('localChatStore', () => { //console.log(coms) notifyError("确认失败!") } else { - item.content.status = 'accessing' - //console.log(item) - await db.update('chatmsg', item.id, toRaw(item)) - await getMsgList() + // item.content.status = 'accessing' + // //console.log(item) + // await db.update('chatmsg', item.id, toRaw(item)) + // await getMsgList() //notifySuccess("确认成功!") } } diff --git a/godo/localchat/filedown.go b/godo/localchat/filedown.go index 2c554e3..559a1d8 100644 --- a/godo/localchat/filedown.go +++ b/godo/localchat/filedown.go @@ -42,7 +42,7 @@ func downloadFiles(msg UdpMessage) error { if err != nil { 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) @@ -84,12 +84,12 @@ func handleResponse(reader io.Reader, saveDir string) error { if err != nil { return fmt.Errorf("failed to read response body: %v", err) } - + log.Printf("Received file list: %v", string(body)) var fileList FileList if err := json.Unmarshal(body, &fileList); err != nil { return fmt.Errorf("failed to unmarshal file list: %v", err) } - + log.Printf("Received file list: %v", fileList.Files) for _, filePath := range fileList.Files { err := saveFileOrFolder(filePath, saveDir) if err != nil { diff --git a/godo/localchat/getfiles.go b/godo/localchat/getfiles.go index 8a41b17..6df006f 100644 --- a/godo/localchat/getfiles.go +++ b/godo/localchat/getfiles.go @@ -28,25 +28,56 @@ func HandleGetFiles(w http.ResponseWriter, r *http.Request) { } log.Printf("Received file list: %v", fileList) defer r.Body.Close() + baseDir, err := libs.GetOsDir() if err != nil { log.Printf("Failed to get OS directory: %v", err) return } + + // 用于存储文件列表 + var files []string + for _, filePath := range fileList.Files { fp := filepath.Join(baseDir, filePath) - err := ServeFile(w, r, fp) - if err != nil { - http.Error(w, fmt.Sprintf("Failed to serve file: %v", err), http.StatusInternalServerError) + if err := serveDirectory(w, r, fp, &files); err != nil { + http.Error(w, fmt.Sprintf("Failed to serve directory: %v", err), http.StatusInternalServerError) return } } - fmt.Fprintf(w, "Files served successfully") + // 将文件列表编码为 JSON 并返回 + jsonData, err := json.Marshal(files) + if err != nil { + http.Error(w, "Failed to marshal file list", http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.Write(jsonData) } -func ServeFile(w http.ResponseWriter, r *http.Request, filePath string) error { +func serveDirectory(w http.ResponseWriter, r *http.Request, dirPath string, files *[]string) error { + filesInDir, err := os.ReadDir(dirPath) + if err != nil { + return fmt.Errorf("failed to read directory: %v", err) + } + + for _, f := range filesInDir { + filePath := filepath.Join(dirPath, f.Name()) + if f.IsDir() { + if err := serveDirectory(w, r, filePath, files); err != nil { + return err + } + } else { + *files = append(*files, filePath) + } + } + + return nil +} +func ServeFile(w http.ResponseWriter, r *http.Request, filePath string) error { file, err := os.Open(filePath) if err != nil { return fmt.Errorf("failed to open file: %v", err) @@ -59,33 +90,9 @@ func ServeFile(w http.ResponseWriter, r *http.Request, filePath string) error { } if fileInfo.IsDir() { - return serveDirectory(w, r, filePath) + return serveDirectory(w, r, filePath, nil) } http.ServeContent(w, r, fileInfo.Name(), fileInfo.ModTime(), file) return nil } - -func serveDirectory(w http.ResponseWriter, r *http.Request, dirPath string) error { - files, err := os.ReadDir(dirPath) - if err != nil { - return fmt.Errorf("failed to read directory: %v", err) - } - - for _, f := range files { - filePath := filepath.Join(dirPath, f.Name()) - if f.IsDir() { - err := serveDirectory(w, r, filePath) - if err != nil { - return err - } - } else { - err := ServeFile(w, r, filePath) - if err != nil { - return err - } - } - } - - return nil -}