Browse Source

change save files

master
godo 10 months ago
parent
commit
40788c92ed
  1. 6
      frontend/src/stores/localchat.ts
  2. 1
      godo/cmd/main.go
  3. 331
      godo/localchat/file.go
  4. 90
      godo/localchat/getfiles.go

6
frontend/src/stores/localchat.ts

@ -57,7 +57,11 @@ export const useLocalChatStore = defineStore('localChatStore', () => {
else if (msg.type === "fileSending"){ else if (msg.type === "fileSending"){
addText(msg) addText(msg)
} }
else if( msg.type === "fileCannel" || msg.type === "fileAccessed") { else if( msg.type === "fileCannel") {
changeMsg(msg)
}
else if( msg.type === "fileAccessed") {
msg.message = msg.message.msgId
changeMsg(msg) changeMsg(msg)
} }
}) })

1
godo/cmd/main.go

@ -106,6 +106,7 @@ func OsStart() {
localchatRouter.HandleFunc("/applyfile", localchat.HandlerApplySendFile).Methods(http.MethodPost) localchatRouter.HandleFunc("/applyfile", localchat.HandlerApplySendFile).Methods(http.MethodPost)
localchatRouter.HandleFunc("/cannelfile", localchat.HandlerCannelFile).Methods(http.MethodPost) localchatRouter.HandleFunc("/cannelfile", localchat.HandlerCannelFile).Methods(http.MethodPost)
localchatRouter.HandleFunc("/accessfile", localchat.HandlerAccessFile).Methods(http.MethodPost) localchatRouter.HandleFunc("/accessfile", localchat.HandlerAccessFile).Methods(http.MethodPost)
localchatRouter.HandleFunc("/getfiles", localchat.HandleGetFiles).Methods(http.MethodPost)
localchatRouter.HandleFunc("/sendimage", localchat.HandlerSendImg).Methods(http.MethodPost) localchatRouter.HandleFunc("/sendimage", localchat.HandlerSendImg).Methods(http.MethodPost)
localchatRouter.HandleFunc("/viewimage", localchat.HandleViewImg).Methods(http.MethodGet) localchatRouter.HandleFunc("/viewimage", localchat.HandleViewImg).Methods(http.MethodGet)
localchatRouter.HandleFunc("/setting", localchat.HandleAddr).Methods(http.MethodPost) localchatRouter.HandleFunc("/setting", localchat.HandleAddr).Methods(http.MethodPost)

331
godo/localchat/file.go

@ -24,26 +24,18 @@
package localchat package localchat
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt"
"godo/libs" "godo/libs"
"io"
"log"
"net/http" "net/http"
"os" "os"
"path/filepath"
"time" "time"
) )
const (
GlobalfileSize = 768 // 每个数据包的大小
)
type FileChunk struct {
ChunkIndex int `json:"chunk_index"`
Data []byte `json:"data"`
Checksum uint32 `json:"checksum"`
Timestamp time.Time `json:"timestamp"`
Filename string `json:"filename"`
Filesize int64 `json:"filesize"`
}
func HandlerApplySendFile(w http.ResponseWriter, r *http.Request) { func HandlerApplySendFile(w http.ResponseWriter, r *http.Request) {
var msg UdpMessage var msg UdpMessage
decoder := json.NewDecoder(r.Body) decoder := json.NewDecoder(r.Body)
@ -106,216 +98,151 @@ func HandlerAccessFile(w http.ResponseWriter, r *http.Request) {
} }
libs.SuccessMsg(w, msg.Message, "接收文件中") libs.SuccessMsg(w, msg.Message, "接收文件中")
} }
func downloadFiles(msg UdpMessage) error { func downloadFiles(msg UdpMessage) 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 nil resp, err := http.Post(postUrl, "application/json", bytes.NewBuffer(postData))
} if err != nil {
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)
}
// func HandlerSendFile(msg UdpMessage) { // 接收文件的目录
// toIp := msg.IP baseDir, err := libs.GetOsDir()
// hostname, err := os.Hostname() if err != nil {
// if err != nil { log.Printf("Failed to get OS directory: %v", err)
// log.Printf("HandleMessage error: %v", err) return fmt.Errorf("failed to get OS directory")
// return }
// }
// msg.Hostname = hostname
// msg.Time = time.Now()
// msg.Type = "file"
// basePath, err := libs.GetOsDir()
// if err != nil {
// log.Printf("GetOsDir error: %v", err)
// return
// }
// paths, ok := msg.Message.([]string)
// if !ok {
// log.Printf("invalid message type")
// return
// }
// for _, p := range paths {
// filePath := filepath.Join(basePath, p)
// // 处理单个文件或整个文件夹
// if fileInfo, err := os.Stat(filePath); err == nil {
// if fileInfo.IsDir() {
// handleDirectory(filePath, toIp, msg)
// } else {
// handleFile(filePath, toIp, msg)
// }
// } else {
// continue
// }
// }
// msg.Type = "fileSended"
// msg.Message = ""
// msg.Time = time.Now()
// SendToIP(msg)
// }
// func handleFile(filePath string, toIp string, message UdpMessage) { resPath := filepath.Join("C", "Users", "Reciv", time.Now().Format("2006-01-02"))
// // 打开文件 receiveDir := filepath.Join(baseDir, resPath)
// file, err := os.Open(filePath) if !libs.PathExists(receiveDir) {
// if err != nil { err := os.MkdirAll(receiveDir, 0755)
// log.Fatalf("Failed to open file: %v", err) if err != nil {
// } log.Printf("Failed to create receive directory: %v", err)
// defer file.Close() return fmt.Errorf("failed to create receive directory")
}
}
// // 获取文件大小 err = saveFiles(resp.Body, receiveDir)
// fileInfo, err := file.Stat() if err != nil {
// if err != nil { return fmt.Errorf("failed to save files: %v", err)
// log.Fatalf("Failed to get file info: %v", err) }
// }
// fileSize := fileInfo.Size()
// // 计算需要发送的数据包数量 fmt.Println("Files downloaded successfully")
// numChunks := fileSize / GlobalfileSize return nil
// if fileSize%GlobalfileSize != 0 { }
// numChunks++
// }
// // 发送文件
// SendFile(file, int(numChunks), toIp, fileSize, message)
// }
// func handleDirectory(dirPath string, toIp string, message UdpMessage) { func saveFiles(reader io.Reader, saveDir string) error {
// err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error { body, err := io.ReadAll(reader)
// if err != nil { if err != nil {
// return err return fmt.Errorf("failed to read response body: %v", err)
// } }
// if !info.IsDir() {
// handleFile(path, toIp, message)
// }
// return nil
// })
// if err != nil {
// log.Fatalf("Failed to walk directory: %v", err)
// }
// }
// func SendFile(file *os.File, numChunks int, toIp string, fSize int64, message UdpMessage) {
// var wg sync.WaitGroup
// // 逐块读取文件并发送 var fileList FileList
// for i := 0; i < numChunks; i++ { err = json.Unmarshal(body, &fileList)
// wg.Add(1) if err != nil {
// go func(index int) { return fmt.Errorf("failed to unmarshal file list: %v", err)
// defer wg.Done() }
// idStr := message.Type + "@" + message.Hostname + "@" + filepath.Base(file.Name()) + "@" + fmt.Sprintf("%d", fSize) + "@"
// chunkData := make([]byte, GlobalfileSize)
// n, err := file.Read(chunkData[:])
// if err != nil && err != io.EOF {
// log.Fatalf("Failed to read file chunk: %v", err)
// }
// sendBinaryData(idStr, chunkData[:n], toIp)
// fmt.Printf("发送文件块 %d 到 %s 成功\n", index, toIp)
// }(i)
// }
// wg.Wait() for _, filePath := range fileList.Files {
// } err := saveFileOrFolder(filePath, saveDir)
// func sendBinaryData(idStr string, data []byte, toIp string) { if err != nil {
// port := "56780" return fmt.Errorf("failed to serve file or folder: %v", err)
// addr, err := net.ResolveUDPAddr("udp4", fmt.Sprintf("%s:%s", toIp, port)) }
// if err != nil { }
// log.Fatalf("Failed to resolve UDP address: %v", err)
// }
// conn, err := net.DialUDP("udp4", nil, addr) return nil
// if err != nil { }
// log.Fatalf("Failed to dial UDP address: %v", err)
// }
// defer conn.Close()
// // 添加长度前缀 func saveFileOrFolder(filePath string, saveDir string) error {
// identifier := []byte(idStr) file, err := os.Open(filePath)
// lengthPrefix := make([]byte, 4) if err != nil {
// binary.BigEndian.PutUint32(lengthPrefix, uint32(len(data))) return fmt.Errorf("failed to open file or folder: %v", err)
}
defer file.Close()
// // 发送长度前缀和数据 fileInfo, err := file.Stat()
// _, err = conn.Write(append(append(identifier, lengthPrefix...), data...)) if err != nil {
// if err != nil { return fmt.Errorf("failed to stat file or folder: %v", err)
// log.Printf("Failed to write data: %v", err) }
// }
// }
// var fileLocks = make(map[string]*sync.Mutex) if fileInfo.IsDir() {
return saveFolder(fileInfo.Name(), filePath, saveDir)
}
// func ReceiveFiles(parts []string) (string, error) { return saveFile(filePath, saveDir)
// fileName := parts[2] }
// fileSize, err := strconv.ParseInt(parts[3], 10, 64)
// if err != nil {
// return "", fmt.Errorf("failed to parse file size: %v", err)
// }
// // 创建接收文件的目录 func saveFolder(folderName string, folderPath string, saveDir string) error {
// baseDir, err := libs.GetOsDir() localFolderPath := filepath.Join(saveDir, folderName)
// if err != nil { err := os.MkdirAll(localFolderPath, 0755)
// log.Printf("Failed to get OS directory: %v", err) if err != nil {
// return "", fmt.Errorf("failed to get OS directory") return fmt.Errorf("failed to create directory: %v", err)
// } }
// resPath := filepath.Join("C", "Users", "Reciv", time.Now().Format("2006-01-02")) files, err := os.ReadDir(folderPath)
// receiveDir := filepath.Join(baseDir, resPath) if err != nil {
// if !libs.PathExists(receiveDir) { return fmt.Errorf("failed to read directory: %v", err)
// 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")
// }
// }
// // 确定文件路径 for _, file := range files {
// filePath := filepath.Join(receiveDir, fileName) subPath := filepath.Join(folderPath, file.Name())
subSavePath := filepath.Join(localFolderPath, file.Name())
if file.IsDir() {
err := saveFolder(file.Name(), subPath, localFolderPath)
if err != nil {
return fmt.Errorf("failed to save folder: %v", err)
}
} else {
err := saveFile(subPath, subSavePath)
if err != nil {
return fmt.Errorf("failed to save file: %v", err)
}
}
}
// // 如果文件不存在,则创建新文件 return nil
// if _, err := os.Stat(filePath); os.IsNotExist(err) { }
// file, err := os.Create(filePath)
// if err != nil {
// log.Printf("Failed to create file: %v", err)
// return "", fmt.Errorf("failed to create file")
// }
// defer file.Close()
// }
// // 锁定文件 func saveFile(filePath string, saveDir string) error {
// lock, ok := fileLocks[fileName] file, err := os.Open(filePath)
// if !ok { if err != nil {
// lock = &sync.Mutex{} return fmt.Errorf("failed to open file: %v", err)
// fileLocks[fileName] = lock }
// } defer file.Close()
// lock.Lock()
// defer lock.Unlock()
// file, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) fileInfo, err := file.Stat()
// if err != nil { if err != nil {
// log.Printf("Failed to open file: %v", err) return fmt.Errorf("failed to stat file: %v", err)
// return "", fmt.Errorf("failed to open file") }
// }
// defer file.Close()
// // 提取实际数据 localFilePath := filepath.Join(saveDir, fileInfo.Name())
// data := strings.Join(parts[4:], "") err = os.MkdirAll(filepath.Dir(localFilePath), 0755)
// if len(data) < 1 { if err != nil {
// return "", fmt.Errorf("empty data") return fmt.Errorf("failed to create directory: %v", err)
// } }
// n, err := file.Write([]byte(data)) out, err := os.Create(localFilePath)
// if err != nil { if err != nil {
// log.Printf("Failed to write data to file: %v", err) return fmt.Errorf("failed to create file: %v", err)
// return "", fmt.Errorf("failed to write data to file") }
// } defer out.Close()
// if n != len(data) {
// log.Printf("Incomplete write: wrote %d bytes, expected %d bytes", n, len(data))
// return "", fmt.Errorf("incomplete write")
// }
// fileInfo, err := os.Stat(filePath) _, err = io.Copy(out, file)
// if err != nil { if err != nil {
// log.Printf("Failed to stat file: %v", err) return fmt.Errorf("failed to copy file: %v", err)
// return "", fmt.Errorf("failed to stat file") }
// }
// if fileInfo.Size() == fileSize { return nil
// fmt.Println("文件接收完成且大小一致") }
// return filePath, nil
// } else {
// fmt.Printf("文件大小不一致,发送大小为%d,接收大小为%d\n", fileSize, fileInfo.Size())
// return "", fmt.Errorf("file size mismatch")
// }
// }

90
godo/localchat/getfiles.go

@ -0,0 +1,90 @@
package localchat
import (
"encoding/json"
"fmt"
"godo/libs"
"log"
"net/http"
"os"
"path/filepath"
)
type FileList struct {
Files []string `json:"files"`
}
func HandleGetFiles(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Only POST method is allowed", http.StatusMethodNotAllowed)
return
}
var fileList FileList
err := json.NewDecoder(r.Body).Decode(&fileList)
if err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
defer r.Body.Close()
baseDir, err := libs.GetOsDir()
if err != nil {
log.Printf("Failed to get OS directory: %v", err)
return
}
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)
return
}
}
fmt.Fprintf(w, "Files served successfully")
}
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)
}
defer file.Close()
fileInfo, err := file.Stat()
if err != nil {
return fmt.Errorf("failed to stat file: %v", err)
}
if fileInfo.IsDir() {
return serveDirectory(w, r, filePath)
}
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
}
Loading…
Cancel
Save