summaryrefslogtreecommitdiff
path: root/protocols/Facebook
diff options
context:
space:
mode:
authorGeorge Hazan <george.hazan@gmail.com>2024-01-10 12:11:16 +0300
committerGeorge Hazan <george.hazan@gmail.com>2024-01-10 12:11:16 +0300
commit730c132f369842cd219388905cf981c2e90f98b3 (patch)
treee347546e613854070cfd3da32eb0e2e8d30b3b91 /protocols/Facebook
parente688d75e8db7616d7e6d6fb3ed3c892e4fbe8a97 (diff)
code cleaning
Diffstat (limited to 'protocols/Facebook')
-rw-r--r--protocols/Facebook/src/avatars.cpp36
-rw-r--r--protocols/Facebook/src/server.cpp16
2 files changed, 19 insertions, 33 deletions
diff --git a/protocols/Facebook/src/avatars.cpp b/protocols/Facebook/src/avatars.cpp
index b395da7805..453c78f0f8 100644
--- a/protocols/Facebook/src/avatars.cpp
+++ b/protocols/Facebook/src/avatars.cpp
@@ -49,36 +49,30 @@ void __cdecl FacebookProto::AvatarsUpdate(void *)
delSetting(cc, "UpdateNeeded");
req.m_szUrl.Format("https://graph.facebook.com/%s/picture?%s", getMStringA(cc, DBKEY_ID).c_str(), szParams.c_str());
- NLHR_PTR pReply(Netlib_HttpTransaction(m_hNetlibUser, &req));
- if (pReply == nullptr) {
- debugLogA("Failed to retrieve avatar from url: %s", req.m_szUrl.c_str());
- continue;
- }
-
PROTO_AVATAR_INFORMATION ai;
ai.hContact = cc;
ai.format = PA_FORMAT_UNKNOWN;
GetAvatarFilename(cc, ai.filename);
- bool bSuccess = false;
- if (pReply->resultCode == 200 && !pReply->body.IsEmpty()) {
+ NLHR_PTR pReply(Netlib_DownloadFile(m_hNetlibUser, &req, ai.filename));
+ if (pReply == nullptr) {
+ debugLogA("Failed to retrieve avatar from url: %s", req.m_szUrl.c_str());
+ continue;
+ }
+
+ if (pReply->resultCode == 200) {
if (auto *pszHdr = pReply->FindHeader("Content-Type"))
ai.format = ProtoGetAvatarFormatByMimeType(pszHdr);
- if (ai.format != PA_FORMAT_UNKNOWN) {
- FILE *fout = _wfopen(ai.filename, L"wb");
- if (fout) {
- fwrite(pReply->body, 1, pReply->body.GetLength(), fout);
- fclose(fout);
- bSuccess = true;
- }
- else debugLogA("Error saving avatar to file %S", ai.filename);
- }
- else debugLogA("unknown avatar mime type");
- }
- else debugLogA("Error %d reading avatar from url: %s", pReply->resultCode, req.m_szUrl.c_str());
+ if (ai.format == PA_FORMAT_UNKNOWN)
+ debugLogA("unknown avatar mime type");
- ProtoBroadcastAck(cc, ACKTYPE_AVATAR, bSuccess ? ACKRESULT_SUCCESS : ACKRESULT_FAILED, &ai);
+ ProtoBroadcastAck(cc, ACKTYPE_AVATAR, ACKRESULT_SUCCESS, &ai);
+ }
+ else {
+ debugLogA("Error %d reading avatar from url: %s", pReply->resultCode, req.m_szUrl.c_str());
+ ProtoBroadcastAck(cc, ACKTYPE_AVATAR, ACKRESULT_FAILED, &ai);
+ }
}
}
diff --git a/protocols/Facebook/src/server.cpp b/protocols/Facebook/src/server.cpp
index 63b292a77e..14f71fc032 100644
--- a/protocols/Facebook/src/server.cpp
+++ b/protocols/Facebook/src/server.cpp
@@ -737,7 +737,8 @@ void FacebookProto::OnPublishPrivateMessage(const JSONNode &root)
CreateDirectoryTreeW(wszPath);
bool bSuccess = false;
- CMStringW wszFileName(FORMAT, L"%s\\STK{%S}.png", wszPath.c_str(), stickerId.c_str());
+ MFilePath wszFileName;
+ wszFileName.Format(L"%s\\STK{%S}.png", wszPath.c_str(), stickerId.c_str());
uint32_t dwAttrib = GetFileAttributesW(wszFileName);
if (dwAttrib == INVALID_FILE_ATTRIBUTES) {
wszFileName.Format(L"%s\\STK{%S}.webp", wszPath.c_str(), stickerId.c_str());
@@ -753,24 +754,15 @@ void FacebookProto::OnPublishPrivateMessage(const JSONNode &root)
JsonReply reply(ExecuteRequest(pReq));
if (!reply.error()) {
for (auto &sticker : reply.data()) {
- // std::string szUrl = sticker["animated_image"]["uri"].as_string();
- // if (szUrl.empty())
- // szUrl = sticker["thread_image"]["uri"].as_string();
- // else
- // wszFileName.Format(L"%s\\STK{%S}.webp", wszPath.c_str(), stickerId.c_str());
std::string szUrl = sticker["thread_image"]["uri"].as_string();
MHttpRequest req(REQUEST_GET);
req.flags = NLHRF_NODUMP | NLHRF_SSL | NLHRF_HTTP11 | NLHRF_REDIRECT;
req.m_szUrl = szUrl.c_str();
- MHttpResponse *pReply = Netlib_HttpTransaction(m_hNetlibUser, &req);
- if (pReply != nullptr && pReply->resultCode == 200 && !pReply->body.IsEmpty()) {
+ NLHR_PTR pReply(Netlib_DownloadFile(m_hNetlibUser, &req, wszFileName));
+ if (pReply != nullptr && pReply->resultCode == 200)
bSuccess = true;
- FILE *out = _wfopen(wszFileName, L"wb");
- fwrite(pReply->body, 1, pReply->body.GetLength(), out);
- fclose(out);
- }
}
}
}