Skip to content

Commit

Permalink
Merge pull request #26 from uncledu/master
Browse files Browse the repository at this point in the history
feat: 发送图片,支持发送网络图片
  • Loading branch information
lich0821 authored Aug 1, 2024
2 parents 58f13c8 + a6c6fb8 commit cd55941
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ winapi = { version = "0.3", features = [
] }
tauri = { version = "1.6.1", features = ["api-all", "system-tray"] }
local-ip-address = "0.6.1"
uuid = { version = "1.2", features = ["v4"] }

[features]
# this feature is used for production builds or when `devPath` points to the filesystem
Expand Down
87 changes: 84 additions & 3 deletions src-tauri/src/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ use warp::{
http::Uri,
hyper::{Response, StatusCode},
path::{FullPath, Tail},
Filter, Rejection, Reply,
Filter, Rejection, Reply
};

use reqwest::get;
use std::fs::File;
use std::io::{copy, Cursor};
use std::path::PathBuf;
use serde_json::json;
use log::{debug, error};
use uuid::Uuid;
use tokio::fs;
use tokio::io::AsyncWriteExt;
use crate::wcferry::{
wcf::{
AttachMsg, AudioMsg, DbNames, DbQuery, DbTable, DbTables, DecPath, ForwardMsg, MemberMgmt,
Expand Down Expand Up @@ -458,7 +466,80 @@ pub async fn send_text(text: TextMsg, wechat: Arc<Mutex<WeChat>>) -> Result<Json
)
)]
pub async fn send_image(image: PathMsg, wechat: Arc<Mutex<WeChat>>) -> Result<Json, Infallible> {
wechat_api_handler!(wechat, WeChat::send_image, image, "发送图片消息")
// 记录图片内容
debug!("收到图片消息:\n{:?}", image);

let mut image_path = PathBuf::from(image.path.clone());

// 检查是否是网络路径
if image.path.starts_with("http") {
// 下载图片
debug!("开始下载图片\n");
let response = match get(&image.path).await {
Ok(res) => res,
Err(e) => {
debug!("下载图片失败: {:?}", e);
return Ok(warp::reply::json(&json!({"error": "下载图片失败"})));
}
};
// 确认状态码
debug!("响应状态码: {:?}", response.status());
if response.status().is_success() {
debug!("下载图片成功\n");
let content_type = response.headers().get("content-type").and_then(|val| val.to_str().ok()).unwrap_or("image/png");
let extension = match content_type {
"image/jpeg" => "jpg",
"image/png" => "png",
_ => "png", // 默认使用png
};

// 使用 UUID 生成唯一的文件名
let unique_filename = Uuid::new_v4().to_string();
let local_image_path = PathBuf::from(format!("C:\\images\\{}.{}", unique_filename, extension));

// 确保目录存在
if let Err(e) = fs::create_dir_all(local_image_path.parent().unwrap()).await {
debug!("创建目录失败: {:?}", e);
return Ok(warp::reply::json(&json!({"error": "创建目录失败"})));
}
let mut file = match File::create(&local_image_path) {
Ok(f) => f,
Err(e) => {
debug!("创建文件失败: {:?}", e);
return Ok(warp::reply::json(&json!({"error": "创建文件失败"})));
}
};
debug!("创建图片文件成功,开始获取图片内容做保存\n");
// 获取图片内容并保存到文件
let bytes = match response.bytes().await {
Ok(b) => b,
Err(e) => {
debug!("读取图片内容失败: {:?}", e);
return Ok(warp::reply::json(&json!({"error": "读取图片内容失败"})));
}
};
debug!("读取图片内容成功,开始保存图片内容\n");
let mut cursor = Cursor::new(bytes);
if let Err(e) = copy(&mut cursor, &mut file) {
debug!("保存图片失败: {:?}", e);
return Ok(warp::reply::json(&json!({"error": "保存图片失败"})));
}
debug!("保存图片内容成功, {:?}\n", local_image_path);
image_path = PathBuf::from(local_image_path);

} else {
error!("下载图片失败,状态码: {:?}", response.status());
return Ok(warp::reply::json(&json!({"error": "下载图片失败"})));
}
}

// 更新 image 的路径
let updated_image = PathMsg {
path: image_path.to_string_lossy().to_string(),
receiver: image.receiver,
};

wechat_api_handler!(wechat, WeChat::send_image, updated_image, "发送图片消息")
}

/// 发送文件
Expand Down

0 comments on commit cd55941

Please sign in to comment.