Refactoring

This commit is contained in:
Changhua 2025-02-06 07:02:19 +08:00
parent d4707997ba
commit 730f51344c
2 changed files with 22 additions and 19 deletions

View File

@ -81,14 +81,14 @@ std::string decrypt_image(const fs::path &src, const fs::path &dst_dir)
uint8_t key = 0x00; uint8_t key = 0x00;
auto ext = detect_image_extension(buffer[0], buffer[1], key); auto ext = detect_image_extension(buffer[0], buffer[1], key);
if (!ext) { if (!ext.empty()) {
LOG_ERROR("无法检测文件类型."); LOG_ERROR("无法检测文件类型.");
return ""; return "";
} }
std::for_each(buffer.begin(), buffer.end(), [key](char &c) { c ^= key; }); std::for_each(buffer.begin(), buffer.end(), [key](char &c) { c ^= key; });
fs::path dst_path = dst_dir / (src.stem().string() + *ext); fs::path dst_path = dst_dir / (src.stem().string() + ext);
if (!fs::exists(dst_dir)) fs::create_directories(dst_dir); if (!fs::exists(dst_dir)) fs::create_directories(dst_dir);
std::ofstream out(dst_path, std::ios::binary); std::ofstream out(dst_path, std::ios::binary);
@ -163,7 +163,7 @@ int download_attachment(uint64_t id, const fs::path &thumb, const fs::path &extr
} }
if (db::get_local_id_and_dbidx(id, &localId, &dbIdx) != 0) { if (db::get_local_id_and_dbidx(id, &localId, &dbIdx) != 0) {
LOG_ERROR("Failed to get localId, Please check id: {}", to_string(id)); LOG_ERROR("获取 localId 失败, 请检查消息 id: {} 是否正确", to_string(id));
return status; return status;
} }
@ -181,7 +181,7 @@ int download_attachment(uint64_t id, const fs::path &thumb, const fs::path &extr
char *buff = (char *)HeapAlloc(GetProcessHeap(), 0, 0x460); char *buff = (char *)HeapAlloc(GetProcessHeap(), 0, 0x460);
if (buff == nullptr) { if (buff == nullptr) {
LOG_ERROR("Failed to allocate memory."); LOG_ERROR("申请内存失败.");
return status; return status;
} }
@ -191,9 +191,7 @@ int download_attachment(uint64_t id, const fs::path &thumb, const fs::path &extr
QWORD type = util::get_qword(reinterpret_cast<QWORD>(buff) + 0x38); QWORD type = util::get_qword(reinterpret_cast<QWORD>(buff) + 0x38);
string save_path = ""; fs::path save_path, thumb_path;
string thumb_path = "";
switch (type) { switch (type) {
case 0x03: { // Image: extra case 0x03: { // Image: extra
save_path = extra; save_path = extra;
@ -210,20 +208,21 @@ int download_attachment(uint64_t id, const fs::path &thumb, const fs::path &extr
break; break;
} }
default: default:
break; LOG_ERROR("不支持的文件类型: {}", type);
return -2;
} }
if (fs::exists(save_path)) { // 不重复下载。TODO: 通过文件大小来判断 if (fs::exists(save_path)) { // 不重复下载。TODO: 通过文件大小来判断
return 0; return 0;
} }
LOG_DEBUG("path: {}", save_path); LOG_DEBUG("保存路径: {}", save_path.string());
// 创建父目录,由于路径来源于微信,不做检查 // 创建父目录,由于路径来源于微信,不做检查
fs::create_directory(fs::path(save_path).parent_path()); fs::create_directory(save_path.parent_path());
int temp = 1; int temp = 1;
auto wx_save_path = util::new_wx_string(save_path); auto wx_save_path = util::new_wx_string(save_path.string());
auto wx_thumb_path = util::new_wx_string(thumb_path); auto wx_thumb_path = util::new_wx_string(thumb_path.string());
memcpy(&buff[0x280], wx_thumb_path.get(), sizeof(WxString)); memcpy(&buff[0x280], wx_thumb_path.get(), sizeof(WxString));
memcpy(&buff[0x2A0], wx_save_path.get(), sizeof(WxString)); memcpy(&buff[0x2A0], wx_save_path.get(), sizeof(WxString));
@ -245,7 +244,7 @@ std::string get_audio(uint64_t id, const fs::path &dir)
auto silk = db::get_audio_data(id); auto silk = db::get_audio_data(id);
if (silk.empty()) { if (silk.empty()) {
LOG_ERROR("Empty audio data."); LOG_ERROR("没有获取到语音数据.");
return ""; return "";
} }
@ -262,8 +261,8 @@ std::string get_pcm_audio(uint64_t id, const fs::path &dir, int32_t sr)
auto silk = db::get_audio_data(id); auto silk = db::get_audio_data(id);
if (silk.empty()) { if (silk.empty()) {
LOG_ERROR("Empty audio data."); LOG_ERROR("没有获取到语音数据.");
return std::nullopt; return "";
} }
std::vector<uint8_t> pcm; std::vector<uint8_t> pcm;
@ -271,8 +270,8 @@ std::string get_pcm_audio(uint64_t id, const fs::path &dir, int32_t sr)
std::ofstream out(pcmpath, std::ios::binary); std::ofstream out(pcmpath, std::ios::binary);
if (!out) { if (!out) {
LOG_ERROR("Failed to write file: {}", pcmpath.string()); LOG_ERROR("创建文件失败: {}", pcmpath.string());
return std::nullopt; return "";
} }
out.write(reinterpret_cast<char *>(pcm.data()), pcm.size()); out.write(reinterpret_cast<char *>(pcm.data()), pcm.size());
@ -339,7 +338,7 @@ std::string get_login_url()
LPVOID targetAddress = reinterpret_cast<LPBYTE>(g_WeChatWinDllAddr) + OS_LOGIN_QR_CODE; LPVOID targetAddress = reinterpret_cast<LPBYTE>(g_WeChatWinDllAddr) + OS_LOGIN_QR_CODE;
char *dataPtr = *reinterpret_cast<char **>(targetAddress); char *dataPtr = *reinterpret_cast<char **>(targetAddress);
if (!dataPtr) { if (!dataPtr) {
LOG_ERROR("Failed to get login URL."); LOG_ERROR("获取二维码失败.");
return ""; return "";
} }
return "http://weixin.qq.com/x/" + std::string(dataPtr, 22); return "http://weixin.qq.com/x/" + std::string(dataPtr, 22);
@ -350,4 +349,4 @@ int receive_transfer(const std::string &wxid, const std::string &transferid, con
// 别想了,这个不实现了 // 别想了,这个不实现了
return -1; return -1;
} }
} // namespace misc } // namespace misc

View File

@ -6,6 +6,8 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "wcf.pb.h"
#include "pb_types.h" #include "pb_types.h"
namespace misc namespace misc
@ -26,6 +28,7 @@ OcrResult_t get_ocr_result(const std::filesystem::path &path);
int receive_transfer(const std::string &wxid, const std::string &transferid, const std::string &transactionid); int receive_transfer(const std::string &wxid, const std::string &transferid, const std::string &transactionid);
// RPC // RPC
// clang-format off
bool rpc_is_logged_in(uint8_t *out, size_t *len); bool rpc_is_logged_in(uint8_t *out, size_t *len);
bool rpc_get_audio(uint64_t id, const std::filesystem::path &dir, uint8_t *out, size_t *len); bool rpc_get_audio(uint64_t id, const std::filesystem::path &dir, uint8_t *out, size_t *len);
bool rpc_get_pcm_audio(uint64_t id, const std::filesystem::path &dir, int32_t sr, uint8_t *out, size_t *len); bool rpc_get_pcm_audio(uint64_t id, const std::filesystem::path &dir, int32_t sr, uint8_t *out, size_t *len);
@ -36,4 +39,5 @@ bool rpc_download_attachment(uint64_t id, const std::filesystem::path &thumb, co
bool rpc_revoke_message(uint64_t id, uint8_t *out, size_t *len); bool rpc_revoke_message(uint64_t id, uint8_t *out, size_t *len);
bool rpc_get_ocr_result(const std::filesystem::path &path, uint8_t *out, size_t *len); bool rpc_get_ocr_result(const std::filesystem::path &path, uint8_t *out, size_t *len);
bool rpc_receive_transfer(const std::string &wxid, const std::string &transferid, const std::string &transactionid, uint8_t *out, size_t *len); bool rpc_receive_transfer(const std::string &wxid, const std::string &transferid, const std::string &transactionid, uint8_t *out, size_t *len);
// clang-format on
} // namespace misc } // namespace misc