Skip to content

Commit

Permalink
优化图片接收,不下载图片了
Browse files Browse the repository at this point in the history
  • Loading branch information
super1207 committed Aug 6, 2024
1 parent 389c4ba commit c45af0d
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 233 deletions.
2 changes: 1 addition & 1 deletion src/MiraiCQ/MiraiCQ/MiraiCQ.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@
</ImportGroup>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\curl-vc140-static-32_64.7.53.0\build\native\curl-vc140-static-32_64.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\curl-vc140-static-32_64.7.53.0\build\native\curl-vc140-static-32_64.targets'))" />
<Error Condition="!Exists('..\packages\openssl-vc140-static-32_64.1.1.0\build\native\openssl-vc140-static-32_64.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\openssl-vc140-static-32_64.1.1.0\build\native\openssl-vc140-static-32_64.targets'))" />
Expand Down
108 changes: 76 additions & 32 deletions src/MiraiCQ/MiraiCQ/center/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,37 @@ std::string Center::CQ_getRecordV2(int auth_code, const char* file, const char*
return std::string();
}

static bool is_img_file_exist(const std::string& cqimg_file) {
if (cqimg_file.length() < 6) {
return false;
}
std::string base = cqimg_file.substr(0, cqimg_file.length() - 6);
return (PathTool::is_file_exist(base + ".png")
|| PathTool::is_file_exist(base + ".jpg")
|| PathTool::is_file_exist(base + ".gif")
|| PathTool::is_file_exist(base + ".bmp"));
}

static std::string get_img_file_savepath(const std::string& cqimg_file) {
if (cqimg_file.length() < 6) {
return "";
}
std::string base = cqimg_file.substr(0, cqimg_file.length() - 6);
if (PathTool::is_file_exist(base + ".png")) {
return base + ".png";
}
if (PathTool::is_file_exist(base + ".jpg")) {
return base + ".jpg";
}
if (PathTool::is_file_exist(base + ".gif")) {
return base + ".gif";
}
if (PathTool::is_file_exist(base + ".bmp")) {
return base + ".bmp";
}
return "";
}

std::string Center::CQ_getImage(int auth_code, const char* file)
{

Expand All @@ -1289,13 +1320,16 @@ std::string Center::CQ_getImage(int auth_code, const char* file)
return "";
}
auto img_dir = PathTool::get_exe_dir() + "data\\image\\";
auto cqimg_path = img_dir + file + ".cqimg";
if (!PathTool::is_file_exist(cqimg_path))
auto cqimg_file = img_dir + file;
if (!StrTool::end_with(cqimg_file, ".cqimg")) {
return "";
}
if (!PathTool::is_file_exist(cqimg_file))
{
//无cqimg文件,无法获取图片
return "";
}
auto url = StrTool::get_str_from_ini(cqimg_path, "image", "url", "");
auto url = StrTool::get_str_from_ini(cqimg_file, "image", "url", "");
if (url == "")
{
//没有读取到url
Expand All @@ -1306,6 +1340,12 @@ std::string Center::CQ_getImage(int auth_code, const char* file)
static std::set<std::string> downing_set;
bool is_downloading = false;

std::string save_path = get_img_file_savepath(cqimg_file);

if (save_path != "") {
return save_path;
}

/* 检查是否正在其他插件中被下载 */
{
std::lock_guard<std::mutex> lock(mx);
Expand All @@ -1321,30 +1361,38 @@ std::string Center::CQ_getImage(int auth_code, const char* file)
is_downloading = true;
}
}

/* 没有正在下载,说明该自己下载 */
if (!is_downloading)
{
/* 如果图片已经存在,就不用真正下载了 */
if (!PathTool::is_file_exist(img_dir + file))
{
try
{
/* 下载图片 */
if (ImgTool::download_img(url, img_dir + file + ".tmp"))
{
PathTool::rename(img_dir + file + ".tmp", img_dir + file);
if (is_downloading == false) {
/* 下载图片 */
std::string image_content;
if (!ImgTool::download_img(url, image_content)) {
MiraiLog::get_instance()->add_debug_log("Center", "图片下载失败");
}
else {
ImgTool::ImgInfo img_info = ImgTool::parse_img(image_content);
if (img_info.type == "") {
MiraiLog::get_instance()->add_debug_log("Center", "图片解析失败");
}
else {
std::string base = cqimg_file.substr(0, cqimg_file.length() - 6);
std::string save_path_t = base + "." + img_info.type;
try {
std::ofstream out_file;
out_file.open(save_path_t, std::ios::binary);
if (!out_file.is_open())
{
MiraiLog::get_instance()->add_debug_log("Center", "图片保存失败");
}
else {
out_file.write(image_content.data(), image_content.size());
out_file.close();
}
}
else
catch (const std::exception&)
{
MiraiLog::get_instance()->add_debug_log("Center", "图片下载失败");
MiraiLog::get_instance()->add_debug_log("Center", "图片保存失败");
}
save_path = save_path_t;
}
catch (const std::exception&)
{
MiraiLog::get_instance()->add_debug_log("Center", "图片下载失败");
}

}
/* 图片下载完成(包括失败),删除正在下载的标记 */
std::lock_guard<std::mutex> lock(mx);
Expand All @@ -1362,17 +1410,13 @@ std::string Center::CQ_getImage(int auth_code, const char* file)
}
}
/* 睡眠一段时间 */
TimeTool::sleep(0);
TimeTool::sleep(1);
}

/* 检查是否下载成功 */
if (!PathTool::is_file_exist(img_dir + file))
{
//没有下载成功
return "";
}
/* 下载成功,返回路径 */
return img_dir + file;
/* 获得下载后的文件路径 */
save_path = get_img_file_savepath(cqimg_file);

return save_path;
}


Expand Down
75 changes: 15 additions & 60 deletions src/MiraiCQ/MiraiCQ/center/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,78 +428,33 @@ static bool deal_json_array(Json::Value & json_arr)
if (type_str == "image")
{
Json::Value dat_json = node.get("data", Json::Value());
std::string md5_str;
std::string url;
std::string file_str = StrTool::get_str_from_json(dat_json, "file", "");
bool is_qq = false;
if (is_qq_url(dat_json)) {
is_qq = true;
md5_str = get_md5_from_file_str(file_str);
if (md5_str == "") {
md5_str = get_md5_from_imgurl(StrTool::get_str_from_json(dat_json, "url", ""));
}
if (md5_str == "") {
md5_str = get_md5_from_imgurl(StrTool::get_str_from_json(dat_json, "file", ""));
}
url = "https://gchat.qpic.cn/gchatpic_new/0/0-0-" + md5_str + "/0?term=2";
}
else {
url = StrTool::get_str_from_json(dat_json, "url", "");
}

if (md5_str == "" && is_qq == true)
{
MiraiLog::get_instance()->add_warning_log("Center", "无法从file字段获取qq图片的md5");
node = Json::Value();
continue;
}

if (url == "") {
MiraiLog::get_instance()->add_warning_log("Center", "无法构造图片url");
node = Json::Value();
continue;
}

/* 获得图片信息需要下载一部分图片 */
ImgTool::ImgInfo info;

/* 这里进行两次尝试,增大成功概率 */
if (!ImgTool::get_info(url, info, is_qq) && !ImgTool::get_info(url, info, is_qq))
{
MiraiLog::get_instance()->add_warning_log("Center", "无法从url中获取图片信息:" + url);
/*
即使无法获得图片信息,也需要写入url,md5等信息到cqimg文件
node = Json::Value();
continue;
*/
/* 无法获得图片信息,则类型使用image,之后若使用`CQ_getImage`获取图片,可能会没有正确的后缀名 */
info.height = info.width = info.size = 0;
info.type = "image";
}
std::string file_str = StrTool::get_str_from_json(dat_json, "url", "");

if (md5_str != "") {
info.md5_str = md5_str;
if (file_str == "") {
file_str = StrTool::get_str_from_json(dat_json, "file", "");
}

if (info.md5_str == "")
{
MiraiLog::get_instance()->add_warning_log("Center", "无法从file字段获取图片的md5???");
if (file_str.rfind("http://") != 0 && file_str.rfind("https://") != 0) {
MiraiLog::get_instance()->add_warning_log("Center", "无法获取图片的url");
node = Json::Value();
continue;
}

std::string cqimg_name = info.md5_str + "." + info.type;
// 得不到图片的信息了,不管啦
std::string md5_str = StrTool::toupper(md5(file_str));
std::string cqimg_name = md5_str + ".cqimg";

/* 创建目录 */
std::string exe_dir = PathTool::get_exe_dir();
PathTool::create_dir(exe_dir + "data");
PathTool::create_dir(exe_dir + "data\\image");
std::string cqimg_path = exe_dir + "data\\image\\" + cqimg_name + ".cqimg";
std::string cqimg_path = exe_dir + "data\\image\\" + cqimg_name;
/* 此处将图片信息直接写入cqimg文件即可 */
WritePrivateProfileStringA("image", "md5", info.md5_str.c_str(), cqimg_path.c_str());
WritePrivateProfileStringA("image", "width", std::to_string(info.width).c_str(), cqimg_path.c_str());
WritePrivateProfileStringA("image", "height", std::to_string(info.height).c_str(), cqimg_path.c_str());
WritePrivateProfileStringA("image", "size", std::to_string(info.size).c_str(), cqimg_path.c_str());
WritePrivateProfileStringA("image", "url", url.c_str(), cqimg_path.c_str());
//WritePrivateProfileStringA("image", "md5", info.md5_str.c_str(), cqimg_path.c_str());
//WritePrivateProfileStringA("image", "width", std::to_string(info.width).c_str(), cqimg_path.c_str());
//WritePrivateProfileStringA("image", "height", std::to_string(info.height).c_str(), cqimg_path.c_str());
//WritePrivateProfileStringA("image", "size", std::to_string(info.size).c_str(), cqimg_path.c_str());
WritePrivateProfileStringA("image", "url", file_str.c_str(), cqimg_path.c_str());
WritePrivateProfileStringA("image", "addtime", std::to_string(time(0)).c_str(), cqimg_path.c_str());
WritePrivateProfileStringA("notice", "致开发者", "由于消息服务器的更新,本文件中的 url 已被弃用,并将在未来被移除,请勿直接读取本文件。请更新至最新版 SDK,并使用 Api(CQ_getImage, 接收图片)读取本图片。", cqimg_path.c_str());
if (!PathTool::is_file_exist(cqimg_path))
Expand Down
6 changes: 3 additions & 3 deletions src/MiraiCQ/MiraiCQ/mainprocess/mainprocess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static void login_dlg_cb(Fl_Widget* o, void* p) {
static bool login_dlg()
{
LOGIN_INFO login_info;
Fl_Window win(300, 180, "MiraiCQ V2.4.4");
Fl_Window win(300, 180, "MiraiCQ V2.4.5");
win.begin();
login_info.ws_url = Config::get_instance()->get_ws_url();
login_info.access_token = Config::get_instance()->get_access_token();
Expand Down Expand Up @@ -451,7 +451,7 @@ static void ex_btn_cb(Fl_Widget* o, void* p)
static void plus_dlg()
{
//fl_register_images();
std::string str1 = StrTool::to_utf8("MiraiCQ插件管理V2.4.4 " + Config::get_instance()->get_name());
std::string str1 = StrTool::to_utf8("MiraiCQ插件管理V2.4.5 " + Config::get_instance()->get_name());
Fl_Double_Window win(508, 400, str1.c_str());
win.color(fl_rgb_color(0, 255, 255));
win.size_range(500, 400, 500, 400);
Expand Down Expand Up @@ -638,7 +638,7 @@ void mainprocess()
hide_all_window();
}

MiraiLog::get_instance()->add_info_log("VERSION", "V2.4.2");
MiraiLog::get_instance()->add_info_log("VERSION", "V2.4.5");
MiraiLog::get_instance()->add_info_log("CORE", "开源地址:https://github.com/super1207/MiraiCQ");

// 释放CQP.dll
Expand Down
Loading

0 comments on commit c45af0d

Please sign in to comment.