diff options
-rw-r--r-- | plugins/PackUpdater/Src/Utils.cpp | 14 | ||||
-rw-r--r-- | plugins/SmileyAdd/src/download.cpp | 31 | ||||
-rw-r--r-- | protocols/Facebook/src/avatars.cpp | 36 | ||||
-rw-r--r-- | protocols/Facebook/src/server.cpp | 16 | ||||
-rw-r--r-- | protocols/Gadu-Gadu/src/avatar.cpp | 41 | ||||
-rw-r--r-- | protocols/NewsAggregator/Src/Utils.cpp | 29 | ||||
-rw-r--r-- | protocols/Twitter/src/connection.cpp | 11 | ||||
-rw-r--r-- | protocols/Twitter/src/utility.cpp | 29 | ||||
-rw-r--r-- | protocols/Twitter/src/utility.h | 2 | ||||
-rw-r--r-- | protocols/VKontakte/src/misc.cpp | 14 | ||||
-rw-r--r-- | protocols/WhatsApp/src/avatars.cpp | 23 |
11 files changed, 62 insertions, 184 deletions
diff --git a/plugins/PackUpdater/Src/Utils.cpp b/plugins/PackUpdater/Src/Utils.cpp index 7c3212388e..17b9f100f1 100644 --- a/plugins/PackUpdater/Src/Utils.cpp +++ b/plugins/PackUpdater/Src/Utils.cpp @@ -116,17 +116,9 @@ BOOL DownloadFile(LPCTSTR tszURL, LPCTSTR tszLocal) nlhr.AddHeader("Pragma", "no-cache");
bool ret = false;
- NLHR_PTR pReply(Netlib_HttpTransaction(hNetlibUser, &nlhr));
- if (pReply) {
- if (200 == pReply->resultCode && !pReply->body.IsEmpty()) {
- HANDLE hFile = CreateFile(tszLocal, GENERIC_READ | GENERIC_WRITE, NULL, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
- DWORD dwBytes;
- WriteFile(hFile, pReply->body, pReply->body.GetLength(), &dwBytes, nullptr);
- ret = true;
- if (hFile)
- CloseHandle(hFile);
- }
- }
+ NLHR_PTR pReply(Netlib_DownloadFile(hNetlibUser, &nlhr, tszLocal));
+ if (pReply && pReply->resultCode == 200)
+ ret = true;
DlgDld = ret;
return ret;
diff --git a/plugins/SmileyAdd/src/download.cpp b/plugins/SmileyAdd/src/download.cpp index 0695876acc..978e00c72f 100644 --- a/plugins/SmileyAdd/src/download.cpp +++ b/plugins/SmileyAdd/src/download.cpp @@ -39,7 +39,7 @@ static LIST<QueueElem> dlQueue(10); static wchar_t g_wszCachePath[MAX_PATH];
static bool threadRunning;
-bool InternetDownloadFile(const char *szUrl, char *szDest, HNETLIBCONN &hHttpDwnl)
+static bool InternetDownloadFile(const char *szUrl, const wchar_t *szDest, HNETLIBCONN &hHttpDwnl)
{
int result = 0xBADBAD;
@@ -53,33 +53,22 @@ bool InternetDownloadFile(const char *szUrl, char *szDest, HNETLIBCONN &hHttpDwn nlhr.AddHeader("User-Agent", NETLIB_USER_AGENT);
nlhr.AddHeader("Connection", "close");
+ CreatePathToFileW(szDest);
+
while (result == 0xBADBAD) {
// download the page
- NLHR_PTR nlhrReply(Netlib_HttpTransaction(hNetlibUser, &nlhr));
+ NLHR_PTR nlhrReply(Netlib_DownloadFile(hNetlibUser, &nlhr, szDest));
if (nlhrReply) {
hHttpDwnl = nlhrReply->nlc;
// if the recieved code is 200 OK
- if (nlhrReply->resultCode == 200) {
- char *delim = strrchr(szDest, '\\');
- if (delim) *delim = '\0';
- CreateDirectoryTree(szDest);
- if (delim) *delim = '\\';
- int res = -1;
- int fh = _open(szDest, _O_BINARY | _O_WRONLY | _O_CREAT, _S_IREAD | _S_IWRITE);
- if (fh != -1) {
- res = _write(fh, nlhrReply->body, nlhrReply->body.GetLength());
- _close(fh);
- }
- if (res < 0)
- remove(szDest);
- else
- result = 0;
- }
- else result = 1;
+ if (nlhrReply->resultCode == 200)
+ result = 0;
+ else
+ result = 1;
}
else {
+ // retry
hHttpDwnl = nullptr;
- result = 1;
}
}
@@ -109,7 +98,7 @@ void __cdecl SmileyDownloadThread(void*) }
if (_waccess(pItem->fname.c_str(), 0) != 0) {
- InternetDownloadFile(_T2A(pItem->url.c_str()), _T2A(pItem->fname.c_str()), hHttpDwnl);
+ InternetDownloadFile(_T2A(pItem->url.c_str()), pItem->fname, hHttpDwnl);
CMStringW fname(pItem->fname);
if (pItem->needext) {
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);
- }
}
}
}
diff --git a/protocols/Gadu-Gadu/src/avatar.cpp b/protocols/Gadu-Gadu/src/avatar.cpp index 0bfce1c00c..edd53dad01 100644 --- a/protocols/Gadu-Gadu/src/avatar.cpp +++ b/protocols/Gadu-Gadu/src/avatar.cpp @@ -218,47 +218,24 @@ void __cdecl GaduProto::avatarrequestthread(void*) gg_LeaveCriticalSection(&avatar_mutex, "avatarrequestthread", 4, 1, "avatar_mutex", 1);
debugLogA("avatarrequestthread() new avatar_transfers item for url=%s.", data->szAvatarURL);
- int result = 0;
-
PROTO_AVATAR_INFORMATION ai = { 0 };
ai.hContact = data->hContact;
ai.format = getByte(ai.hContact, GG_KEY_AVATARTYPE, GG_KEYDEF_AVATARTYPE);
+ getAvatarFilename(ai.hContact, ai.filename, _countof(ai.filename));
MHttpRequest req(REQUEST_GET);
req.m_szUrl = data->szAvatarURL;
req.flags = NLHRF_NODUMP | NLHRF_HTTP11 | NLHRF_REDIRECT;
- NLHR_PTR resp(Netlib_HttpTransaction(m_hNetlibUser, &req));
- if (resp) {
- if (resp->resultCode == 200 && !resp->body.IsEmpty()) {
- int file_fd;
-
- int avatarType = PA_FORMAT_UNKNOWN;
- if (strncmp(resp->body, "\xFF\xD8", 2) == 0) avatarType = PA_FORMAT_JPEG;
- if (strncmp(resp->body, "\x47\x49\x46\x38", 4) == 0) avatarType = PA_FORMAT_GIF;
- if (strncmp(resp->body, "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", 8) == 0) avatarType = PA_FORMAT_PNG;
- setByte(data->hContact, GG_KEY_AVATARTYPE, (uint8_t)avatarType);
-
- getAvatarFilename(ai.hContact, ai.filename, _countof(ai.filename));
- file_fd = _wopen(ai.filename, _O_WRONLY | _O_TRUNC | _O_BINARY | _O_CREAT, _S_IREAD | _S_IWRITE);
- if (file_fd != -1) {
- _write(file_fd, resp->body, resp->body.GetLength());
- _close(file_fd);
- result = 1;
- debugLogW(L"avatarrequestthread() new avatar_transfers item. Saved data to file=%s.", ai.filename);
- }
- else {
- debugLogW(L"avatarrequestthread(): _wopen file %s error. errno=%d: %s", ai.filename, errno, ws_strerror(errno));
- wchar_t error[512];
- mir_snwprintf(error, TranslateT("Cannot create avatar file. ERROR: %d: %s\n%s"), errno, ws_strerror(errno), ai.filename);
- showpopup(m_tszUserName, error, GG_POPUP_ERROR);
- }
- }
- else debugLogA("avatarrequestthread(): Invalid response code from HTTP request");
+ NLHR_PTR resp(Netlib_DownloadFile(m_hNetlibUser, &req, ai.filename));
+ if (resp && resp->resultCode == 200) {
+ setByte(data->hContact, GG_KEY_AVATARTYPE, (uint8_t)ProtoGetBufferFormat(resp->body));
+ ProtoBroadcastAck(ai.hContact, ACKTYPE_AVATAR, ACKRESULT_SUCCESS, &ai);
+ }
+ else {
+ debugLogA("avatarrequestthread(): No response from HTTP request");
+ ProtoBroadcastAck(ai.hContact, ACKTYPE_AVATAR, ACKRESULT_FAILED, &ai);
}
- else debugLogA("avatarrequestthread(): No response from HTTP request");
-
- ProtoBroadcastAck(ai.hContact, ACKTYPE_AVATAR, result ? ACKRESULT_SUCCESS : ACKRESULT_FAILED, &ai);
if (!ai.hContact)
ReportSelfAvatarChanged();
diff --git a/protocols/NewsAggregator/Src/Utils.cpp b/protocols/NewsAggregator/Src/Utils.cpp index 60aec00266..8c760996aa 100644 --- a/protocols/NewsAggregator/Src/Utils.cpp +++ b/protocols/NewsAggregator/Src/Utils.cpp @@ -212,33 +212,8 @@ bool DownloadFile(LPCTSTR tszURL, LPCTSTR tszLocal) nlhr.AddHeader("Cache-Control", "no-cache"); nlhr.AddHeader("Pragma", "no-cache"); - NLHR_PTR pReply(Netlib_HttpTransaction(hNetlibUser, &nlhr)); - if (pReply) { - if (200 == pReply->resultCode && !pReply->body.IsEmpty()) { - const char *date = pReply->FindHeader("Last-Modified"); - const char *size = pReply->FindHeader("Content-Length"); - if (date != nullptr && size != nullptr) { - struct _stat buf; - int fh = _wopen(tszLocal, _O_RDONLY); - if (fh != -1) { - _fstat(fh, &buf); - _close(fh); - - time_t modtime = DateToUnixTime(date, 0); - time_t filemodtime = mktime(localtime(&buf.st_atime)); - if (modtime <= filemodtime || buf.st_size == atoi(size)) - return false; - } - } - - DWORD dwBytes; - HANDLE hFile = CreateFile(tszLocal, GENERIC_READ | GENERIC_WRITE, NULL, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); - WriteFile(hFile, pReply->body, pReply->body.GetLength(), &dwBytes, nullptr); - if (hFile) - CloseHandle(hFile); - } - } - return true; + NLHR_PTR pReply(Netlib_DownloadFile(hNetlibUser, &nlhr, tszLocal)); + return (pReply && 200 == pReply->resultCode); } typedef HRESULT(MarkupCallback)(IHTMLDocument3 *, BSTR &message); diff --git a/protocols/Twitter/src/connection.cpp b/protocols/Twitter/src/connection.cpp index 3cf4673d27..438572eb7e 100644 --- a/protocols/Twitter/src/connection.cpp +++ b/protocols/Twitter/src/connection.cpp @@ -164,7 +164,9 @@ void CTwitterProto::UpdateAvatarWorker(void *p) return;
CMStringA ext = data->url.Mid(data->url.ReverseFind('.')); // finds the filetype of the avatar
- CMStringW filename(FORMAT, L"%s\\%S%S", GetAvatarFolder().c_str(), username.c_str(), ext.c_str()); // local filename and path
+
+ MFilePath filename; // local filename and path
+ filename.Format(L"%s\\%S%S", GetAvatarFolder().c_str(), username.c_str(), ext.c_str());
PROTO_AVATAR_INFORMATION ai = { 0 };
ai.hContact = data->hContact;
@@ -185,7 +187,12 @@ void CTwitterProto::UpdateAvatarWorker(void *p) return;
}
- if (save_url(hAvatarNetlib_, data->url, filename)) {
+ MHttpRequest req(REQUEST_GET);
+ req.flags = NLHRF_HTTP11 | NLHRF_REDIRECT;
+ req.m_szUrl = data->url;
+
+ NLHR_PTR resp(Netlib_DownloadFile(hAvatarNetlib_, &req, filename));
+ if (resp && resp->resultCode == 200) {
setString(data->hContact, TWITTER_KEY_AV_URL, data->url.c_str());
ProtoBroadcastAck(data->hContact, ACKTYPE_AVATAR, ACKRESULT_SUCCESS, &ai);
}
diff --git a/protocols/Twitter/src/utility.cpp b/protocols/Twitter/src/utility.cpp index c46c146390..09e764f5f6 100644 --- a/protocols/Twitter/src/utility.cpp +++ b/protocols/Twitter/src/utility.cpp @@ -66,32 +66,3 @@ void CTwitterProto::ShowPopup(const char *text, int Error) }
PUAddPopupW(&popup);
}
-
-/////////////////////////////////////////////////////////////////////////////////////////
-
-bool save_url(HNETLIBUSER hNetlib, const CMStringA &url, const CMStringW &filename)
-{
- MHttpRequest req(REQUEST_GET);
- req.flags = NLHRF_HTTP11 | NLHRF_REDIRECT;
- req.m_szUrl = const_cast<char*>(url.c_str());
-
- NLHR_PTR resp(Netlib_HttpTransaction(hNetlib, &req));
- if (!resp)
- return false;
-
- if (resp->resultCode != 200)
- return false;
-
- // Create folder if necessary
- if (CreatePathToFileW(filename) != ERROR_SUCCESS)
- return false;
-
- // Write to file
- if (FILE *f = _wfopen(filename, L"wb")) {
- fwrite(resp->body, 1, resp->body.GetLength(), f);
- fclose(f);
- }
- else return false;
-
- return true;
-}
diff --git a/protocols/Twitter/src/utility.h b/protocols/Twitter/src/utility.h index 3a7e32a0e1..aa53df8dc0 100644 --- a/protocols/Twitter/src/utility.h +++ b/protocols/Twitter/src/utility.h @@ -24,5 +24,3 @@ StringPairs ParseQueryString(const CMStringA &queryString); void Split(const CMStringA &str, OBJLIST<CMStringA> &out, char sep, bool includeEmpty = false);
void htmlEntitiesDecode(CMStringA &context);
-
-bool save_url(HNETLIBUSER hNetlib,const CMStringA &url,const CMStringW &filename);
diff --git a/protocols/VKontakte/src/misc.cpp b/protocols/VKontakte/src/misc.cpp index 3f15bcac16..e76f8787d9 100644 --- a/protocols/VKontakte/src/misc.cpp +++ b/protocols/VKontakte/src/misc.cpp @@ -1465,21 +1465,17 @@ CMStringW CVkProto::GetAttachmentDescr(const JSONNode &jnAttachments, BBCSupport CreateDirectoryTreeW(wszPath);
bool bSuccess = false;
- CMStringW wszFileName(FORMAT, L"%s\\[sticker-%d].png", wszPath.c_str(), iStickerId);
+ MFilePath wszFileName;
+ wszFileName.Format(L"%s\\[sticker-%d].png", wszPath.c_str(), iStickerId);
if (GetFileAttributesW(wszFileName) == INVALID_FILE_ATTRIBUTES) {
- T2Utf szUrl(wszUrl);
MHttpRequest req(REQUEST_GET);
req.flags = NLHRF_NODUMP | NLHRF_SSL | NLHRF_HTTP11 | NLHRF_REDIRECT;
- req.m_szUrl = szUrl.get();
+ req.m_szUrl = T2Utf(wszUrl).get();
- 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 && pReply->resultCode == 200)
bSuccess = true;
- FILE* out = _wfopen(wszFileName, L"wb");
- fwrite(pReply->body, 1, pReply->body.GetLength(), out);
- fclose(out);
- }
}
else bSuccess = true;
diff --git a/protocols/WhatsApp/src/avatars.cpp b/protocols/WhatsApp/src/avatars.cpp index 32e57706a4..f1b0709c34 100644 --- a/protocols/WhatsApp/src/avatars.cpp +++ b/protocols/WhatsApp/src/avatars.cpp @@ -135,7 +135,7 @@ bool CMPlugin::SaveFile(const char *pszUrl, PROTO_AVATAR_INFORMATION &ai) req.m_szUrl = pszUrl;
req.nlc = hAvatarConn;
- NLHR_PTR pReply(Netlib_HttpTransaction(hAvatarUser, &req));
+ NLHR_PTR pReply(Netlib_DownloadFile(hAvatarUser, &req, ai.filename));
if (pReply == nullptr) {
hAvatarConn = nullptr;
debugLogA("Failed to retrieve avatar from url: %s", pszUrl);
@@ -144,23 +144,10 @@ bool CMPlugin::SaveFile(const char *pszUrl, PROTO_AVATAR_INFORMATION &ai) hAvatarConn = pReply->nlc;
- bool bSuccess = false;
- if (pReply->resultCode == 200 && !pReply->body.IsEmpty()) {
- 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");
+ if (pReply->resultCode != 200) {
+ debugLogA("Error %d reading avatar from url: %s", pReply->resultCode, pszUrl);
+ return false;
}
- else debugLogA("Error %d reading avatar from url: %s", pReply->resultCode, pszUrl);
- return bSuccess;
+ return true;
}
|