|
@ -26,6 +26,7 @@ package localchat |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
"encoding/json" |
|
|
"encoding/json" |
|
|
|
|
|
"fmt" |
|
|
"godo/libs" |
|
|
"godo/libs" |
|
|
"io" |
|
|
"io" |
|
|
"log" |
|
|
"log" |
|
@ -33,6 +34,8 @@ import ( |
|
|
"net/url" |
|
|
"net/url" |
|
|
"os" |
|
|
"os" |
|
|
"path/filepath" |
|
|
"path/filepath" |
|
|
|
|
|
"strconv" |
|
|
|
|
|
"strings" |
|
|
"time" |
|
|
"time" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
@ -52,7 +55,6 @@ func HandlerSendImg(w http.ResponseWriter, r *http.Request) { |
|
|
msg.Hostname = hostname |
|
|
msg.Hostname = hostname |
|
|
msg.Time = time.Now() |
|
|
msg.Time = time.Now() |
|
|
msg.Type = "image" |
|
|
msg.Type = "image" |
|
|
toIp := msg.IP |
|
|
|
|
|
basePath, err := libs.GetOsDir() |
|
|
basePath, err := libs.GetOsDir() |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Printf("GetOsDir error: %v", err) |
|
|
log.Printf("GetOsDir error: %v", err) |
|
@ -66,7 +68,7 @@ func HandlerSendImg(w http.ResponseWriter, r *http.Request) { |
|
|
libs.ErrorMsg(w, "HandleMessage message error") |
|
|
libs.ErrorMsg(w, "HandleMessage message error") |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
sendPath := []string{} |
|
|
for _, v := range paths { |
|
|
for _, v := range paths { |
|
|
p, ok := v.(string) |
|
|
p, ok := v.(string) |
|
|
if !ok { |
|
|
if !ok { |
|
@ -76,74 +78,111 @@ func HandlerSendImg(w http.ResponseWriter, r *http.Request) { |
|
|
// 处理多张图片
|
|
|
// 处理多张图片
|
|
|
if fileInfo, err := os.Stat(filePath); err == nil { |
|
|
if fileInfo, err := os.Stat(filePath); err == nil { |
|
|
if !fileInfo.IsDir() { |
|
|
if !fileInfo.IsDir() { |
|
|
handleFile(filePath, toIp, msg) |
|
|
//handleFile(filePath, toIp, msg)
|
|
|
|
|
|
sendPath = append(sendPath, p) |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
continue |
|
|
continue |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
msg.Message = sendPath |
|
|
|
|
|
SendToIP(msg) |
|
|
libs.SuccessMsg(w, nil, "图片发送成功") |
|
|
libs.SuccessMsg(w, nil, "图片发送成功") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// func ReceiveImg(msg UdpMessage) (string, error) {
|
|
|
func ReceiveImg(msg UdpMessage) ([]string, error) { |
|
|
// chunk := msg.Message.(FileChunk)
|
|
|
res := []string{} |
|
|
|
|
|
baseDir, err := libs.GetOsDir() |
|
|
// // 验证校验和
|
|
|
if err != nil { |
|
|
// calculatedChecksum := calculateChecksum(chunk.Data)
|
|
|
log.Printf("Failed to get OS directory: %v", err) |
|
|
// if calculatedChecksum != chunk.Checksum {
|
|
|
return res, err |
|
|
// fmt.Printf("Checksum mismatch for image from %s\n", msg.IP)
|
|
|
} |
|
|
// return "", fmt.Errorf("checksum mismatch")
|
|
|
|
|
|
// }
|
|
|
// 创建接收文件的目录
|
|
|
|
|
|
resPath := filepath.Join("C", "Users", "Reciv", time.Now().Format("2006-01-02")) |
|
|
// baseDir, err := libs.GetOsDir()
|
|
|
receiveDir := filepath.Join(baseDir, resPath) |
|
|
// if err != nil {
|
|
|
if !libs.PathExists(receiveDir) { |
|
|
// log.Printf("Failed to get OS directory: %v", err)
|
|
|
err := os.MkdirAll(receiveDir, 0755) |
|
|
// return "", err
|
|
|
if err != nil { |
|
|
// }
|
|
|
log.Printf("Failed to create receive directory: %v", err) |
|
|
|
|
|
return res, err |
|
|
// // 创建接收文件的目录
|
|
|
} |
|
|
// resPath := filepath.Join("C", "Users", "Reciv", time.Now().Format("2006-01-02"))
|
|
|
} |
|
|
// receiveDir := filepath.Join(baseDir, resPath)
|
|
|
paths, ok := msg.Message.([]interface{}) |
|
|
// if !libs.PathExists(receiveDir) {
|
|
|
//log.Printf("paths: %v", paths)
|
|
|
// err := os.MkdirAll(receiveDir, 0755)
|
|
|
if !ok { |
|
|
// if err != nil {
|
|
|
return res, fmt.Errorf("HandleMessage message error") |
|
|
// log.Printf("Failed to create receive directory: %v", err)
|
|
|
} |
|
|
// return "", err
|
|
|
var savedPaths []string |
|
|
// }
|
|
|
for _, v := range paths { |
|
|
// }
|
|
|
p, ok := v.(string) |
|
|
|
|
|
if !ok { |
|
|
// // 确定文件路径
|
|
|
continue |
|
|
// filePath := filepath.Join(receiveDir, chunk.Filename)
|
|
|
} |
|
|
|
|
|
imgUrl := fmt.Sprintf("http://%s/viewimg?img=%s", msg.IP, url.QueryEscape(p)) |
|
|
// // 如果文件不存在,则创建新文件
|
|
|
resp, err := http.Get(imgUrl) |
|
|
// if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
|
|
if err != nil { |
|
|
// file, err := os.Create(filePath)
|
|
|
log.Printf("Failed to download image from URL %s: %v", imgUrl, err) |
|
|
// if err != nil {
|
|
|
continue |
|
|
// log.Printf("Failed to create file: %v", err)
|
|
|
} |
|
|
// return "", err
|
|
|
defer resp.Body.Close() |
|
|
// }
|
|
|
|
|
|
// defer file.Close()
|
|
|
if resp.StatusCode != http.StatusOK { |
|
|
// }
|
|
|
log.Printf("Failed to download image from URL %s: %v", imgUrl, resp.Status) |
|
|
|
|
|
continue |
|
|
// // 打开或追加到现有文件
|
|
|
} |
|
|
// file, err := os.OpenFile(filePath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
// 生成随机文件名
|
|
|
// if err != nil {
|
|
|
// 生成随机文件名并保留扩展名
|
|
|
// log.Printf("Failed to open file: %v", err)
|
|
|
fileName, err := generateRandomFileNameWithExtension(p) |
|
|
// return "", err
|
|
|
if err != nil { |
|
|
// }
|
|
|
log.Printf("Failed to generate random file name: %v", err) |
|
|
// defer file.Close()
|
|
|
continue |
|
|
|
|
|
} |
|
|
// // 写入数据
|
|
|
filePath := filepath.Join(receiveDir, fileName) |
|
|
// _, err = file.Write(chunk.Data)
|
|
|
// 保存图片
|
|
|
// if err != nil {
|
|
|
err = saveImage(resp.Body, filePath) |
|
|
// log.Printf("Failed to write data to file: %v", err)
|
|
|
if err != nil { |
|
|
// return "", err
|
|
|
log.Printf("Failed to save image to %s: %v", filePath, err) |
|
|
// }
|
|
|
continue |
|
|
|
|
|
} |
|
|
// fmt.Printf("接收到图片 %s 从 %s 成功\n", filePath, msg.IP)
|
|
|
savedPaths = append(savedPaths, filePath) |
|
|
// resFilePath := filepath.Join(resPath, chunk.Filename)
|
|
|
|
|
|
// return resFilePath, nil
|
|
|
} |
|
|
// }
|
|
|
if len(savedPaths) > 0 { |
|
|
|
|
|
return savedPaths, nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return res, fmt.Errorf("no images were saved") |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 生成随机文件名并保留扩展名
|
|
|
|
|
|
func generateRandomFileNameWithExtension(originalFileName string) (string, error) { |
|
|
|
|
|
fileNameWithoutExt, fileExt := filepath.Split(originalFileName) |
|
|
|
|
|
fileExt = strings.TrimPrefix(fileExt, ".") |
|
|
|
|
|
if fileExt == "" { |
|
|
|
|
|
fileExt = "png" |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
randomFileName := fmt.Sprintf("%s_%s.%s", fileNameWithoutExt, strconv.FormatInt(time.Now().UnixNano(), 10), fileExt) |
|
|
|
|
|
return randomFileName, nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 保存图片到本地文件
|
|
|
|
|
|
func saveImage(reader io.Reader, filePath string) error { |
|
|
|
|
|
file, err := os.Create(filePath) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return fmt.Errorf("failed to create file: %v", err) |
|
|
|
|
|
} |
|
|
|
|
|
defer file.Close() |
|
|
|
|
|
|
|
|
|
|
|
_, err = io.Copy(file, reader) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return fmt.Errorf("failed to write image data to file: %v", err) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
} |
|
|
func HandleViewImg(w http.ResponseWriter, r *http.Request) { |
|
|
func HandleViewImg(w http.ResponseWriter, r *http.Request) { |
|
|
img := r.URL.Query().Get("img") |
|
|
img := r.URL.Query().Get("img") |
|
|
if img == "" { |
|
|
if img == "" { |
|
|