From 09a49cfe0925494f7fd516657df7fe327c7370dd Mon Sep 17 00:00:00 2001 From: George Hazan Date: Wed, 3 Jan 2024 19:15:44 +0300 Subject: =?UTF-8?q?fixes=20#3946=20(=D0=9F=D1=80=D0=BE=D0=B3=D1=80=D0=B5?= =?UTF-8?q?=D1=81=D1=81=20=D1=81=D0=BA=D0=B0=D1=87=D0=B8=D0=B2=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BA=D1=80=D1=83=D0=BF=D0=BD=D1=8B=D1=85=20?= =?UTF-8?q?=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- protocols/Telegram/src/avatars.cpp | 176 +++++++++++++++++++++---------------- protocols/Telegram/src/proto.h | 2 +- 2 files changed, 99 insertions(+), 79 deletions(-) (limited to 'protocols/Telegram/src') diff --git a/protocols/Telegram/src/avatars.cpp b/protocols/Telegram/src/avatars.cpp index 9da6068cce..932e63ca43 100644 --- a/protocols/Telegram/src/avatars.cpp +++ b/protocols/Telegram/src/avatars.cpp @@ -94,7 +94,7 @@ void CTelegramProto::OnGetFileInfo(td::ClientManager::Response &response, void * ft->m_fileId = pFile->id_; ft->m_uniqueId = pFile->remote_->unique_id_.c_str(); - SendQuery(new TD::downloadFile(pFile->id_, 10, 0, 0, true)); + SendQuery(new TD::downloadFile(pFile->id_, 10, 0, 0, false)); } void CTelegramProto::OnGetFileLink(td::ClientManager::Response &response) @@ -161,7 +161,7 @@ void CTelegramProto::OnSendOfflineFile(DB::EventInfo &dbei, DB::FILE_BLOB &blob, ///////////////////////////////////////////////////////////////////////////////////////// -TG_FILE_REQUEST* CTelegramProto::PopFile(const char *pszUniqueId) +TG_FILE_REQUEST* CTelegramProto::FindFile(const char *pszUniqueId) { mir_cslock lck(m_csFiles); @@ -177,99 +177,119 @@ TG_FILE_REQUEST* CTelegramProto::PopFile(const char *pszUniqueId) void CTelegramProto::ProcessFile(TD::updateFile *pObj) { - if (auto *pFile = pObj->file_.get()) { - if (!pFile->local_->is_downloading_completed_) - return; - - Utf2T wszExistingFile(pFile->local_->path_.c_str()); - - if (auto *F = PopFile(pFile->remote_->unique_id_.c_str())) { - if (F->m_type == F->AVATAR) { - CMStringW wszFullName = F->m_destPath; - if (!wszFullName.IsEmpty()) - wszFullName += L"\\"; - wszFullName += F->m_fileName; - - if (F->m_fileName.Right(5).MakeLower() == L".webp") { - if (auto *pImage = FreeImage_LoadU(FIF_WEBP, wszExistingFile)) { - wszFullName.Truncate(wszFullName.GetLength() - 5); - wszFullName += L".png"; - FreeImage_SaveU(FIF_PNG, pImage, wszFullName); - FreeImage_Unload(pImage); - } - } - else MoveFileW(wszExistingFile, wszFullName); - - SmileyAdd_LoadContactSmileys(SMADD_FILE, m_szModuleName, wszFullName); + auto *pFile = pObj->file_.get(); + if (pFile == nullptr) + return; - mir_cslock lck(m_csFiles); - m_arFiles.remove(F); + if (!pFile->local_->is_downloading_completed_) { + if (auto *F = FindFile(pFile->remote_->unique_id_.c_str())) { + if (F->m_type != F->AVATAR && F->ofd) { + DBVARIANT dbv = { DBVT_DWORD }; + dbv.dVal = pFile->local_->downloaded_size_; + db_event_setJson(F->ofd->hDbEvent, "ft", &dbv); } - else { // FILE, PICTURE, VIDEO, VOICE - if (pFile->local_->is_downloading_completed_) { - if (F->ofd) { - DBVARIANT dbv = { DBVT_DWORD }; - dbv.dVal = pFile->local_->downloaded_size_; - db_event_setJson(F->ofd->hDbEvent, "ft", &dbv); - db_event_setJson(F->ofd->hDbEvent, "fs", &dbv); - - CMStringW wszFullName(F->ofd->wszPath); - - auto *pSlash = strrchr(pFile->local_->path_.c_str(), '\\'); - if (!pSlash) - pSlash = pFile->local_->path_.c_str(); - else - pSlash++; - - dbv.type = DBVT_UTF8; - dbv.pszVal = (char *)pSlash; - db_event_setJson(F->ofd->hDbEvent, "f", &dbv); - - wszFullName.Truncate(wszFullName.ReverseFind('\\') + 1); - wszFullName.Append(Utf2T(pSlash)); - F->ofd->ResetFileName(wszFullName); // resulting ofd->wszPath may differ from wszFullName - - MoveFileW(wszExistingFile, F->ofd->wszPath); - F->ofd->Finish(); - } + } + return; + } - mir_cslock lck(m_csFiles); - m_arFiles.remove(F); + Utf2T wszExistingFile(pFile->local_->path_.c_str()); + + if (auto *F = FindFile(pFile->remote_->unique_id_.c_str())) { + if (F->m_type == F->AVATAR) { + CMStringW wszFullName = F->m_destPath; + if (!wszFullName.IsEmpty()) + wszFullName += L"\\"; + wszFullName += F->m_fileName; + + if (F->m_fileName.Right(5).MakeLower() == L".webp") { + if (auto *pImage = FreeImage_LoadU(FIF_WEBP, wszExistingFile)) { + wszFullName.Truncate(wszFullName.GetLength() - 5); + wszFullName += L".png"; + FreeImage_SaveU(FIF_PNG, pImage, wszFullName); + FreeImage_Unload(pImage); } } + else MoveFileW(wszExistingFile, wszFullName); + + SmileyAdd_LoadContactSmileys(SMADD_FILE, m_szModuleName, wszFullName); + mir_cslock lck(m_csFiles); + m_arFiles.remove(F); delete F; - return; } + else { // FILE, PICTURE, VIDEO, VOICE + if (F->ofd == nullptr) + return; - for (auto &it : m_arOwnMsg) { - if (it->tmpFileId == pFile->id_) { - if (!pFile->remote_->id_.empty()) { - if (auto hDbEvent = db_event_getById(m_szModuleName, it->szMsgId)) { - DBVARIANT dbv = { DBVT_UTF8 }; - dbv.pszVal = (char *)pFile->remote_->id_.c_str(); - db_event_setJson(hDbEvent, "u", &dbv); + DBVARIANT dbv = { DBVT_DWORD }; + dbv.dVal = pFile->local_->downloaded_size_; + db_event_setJson(F->ofd->hDbEvent, "ft", &dbv); + + CMStringW wszFullName(F->ofd->wszPath); + int idxSlash = wszFullName.ReverseFind('\\') + 1; + if (wszFullName.Find('.', idxSlash) == -1) { + auto *pSlash = strrchr(pFile->local_->path_.c_str(), '\\'); + if (!pSlash) + pSlash = pFile->local_->path_.c_str(); + else + pSlash++; + + if (strchr(pSlash, '.')) { + dbv.type = DBVT_UTF8; + dbv.pszVal = (char *)pSlash; + db_event_setJson(F->ofd->hDbEvent, "f", &dbv); + + wszFullName.Truncate(idxSlash); + wszFullName.Append(Utf2T(pSlash)); + F->ofd->ResetFileName(wszFullName); // resulting ofd->wszPath may differ from wszFullName + } + else { + int iFormat = ProtoGetAvatarFileFormat(wszExistingFile); + if (iFormat != PA_FORMAT_UNKNOWN) { + wszFullName.AppendChar('.'); + wszFullName.Append(ProtoGetAvatarExtension(iFormat)); + F->ofd->ResetFileName(wszFullName); } } - return; } + + MoveFileW(wszExistingFile, F->ofd->wszPath); + F->ofd->Finish(); + + mir_cslock lck(m_csFiles); + m_arFiles.remove(F); + delete F; } + return; + } - for (auto &it : m_arUsers) { - if (it->szAvatarHash == pFile->remote_->unique_id_.c_str()) { - PROTO_AVATAR_INFORMATION pai; - pai.hContact = it->hContact; - pai.format = ProtoGetAvatarFileFormat(wszExistingFile); - setByte(pai.hContact, DBKEY_AVATAR_TYPE, pai.format); + for (auto &it : m_arOwnMsg) { + if (it->tmpFileId == pFile->id_) { + if (!pFile->remote_->id_.empty()) { + if (auto hDbEvent = db_event_getById(m_szModuleName, it->szMsgId)) { + DBVARIANT dbv = { DBVT_UTF8 }; + dbv.pszVal = (char *)pFile->remote_->id_.c_str(); + db_event_setJson(hDbEvent, "u", &dbv); + } + } + return; + } + } - CMStringW wszAvatarPath(GetAvatarFilename(it->hContact)); - wcsncpy_s(pai.filename, wszAvatarPath, _TRUNCATE); + for (auto &it : m_arUsers) { + if (it->szAvatarHash == pFile->remote_->unique_id_.c_str()) { + PROTO_AVATAR_INFORMATION pai; + pai.hContact = it->hContact; + pai.format = ProtoGetAvatarFileFormat(wszExistingFile); + setByte(pai.hContact, DBKEY_AVATAR_TYPE, pai.format); - MoveFileW(wszExistingFile, wszAvatarPath); + CMStringW wszAvatarPath(GetAvatarFilename(it->hContact)); + wcsncpy_s(pai.filename, wszAvatarPath, _TRUNCATE); - ProtoBroadcastAck(it->hContact, ACKTYPE_AVATAR, ACKRESULT_SUCCESS, &pai); - break; - } + MoveFileW(wszExistingFile, wszAvatarPath); + + ProtoBroadcastAck(it->hContact, ACKTYPE_AVATAR, ACKRESULT_SUCCESS, &pai); + break; } } } diff --git a/protocols/Telegram/src/proto.h b/protocols/Telegram/src/proto.h index a4f8520abf..020b6d740a 100644 --- a/protocols/Telegram/src/proto.h +++ b/protocols/Telegram/src/proto.h @@ -210,7 +210,7 @@ class CTelegramProto : public PROTO mir_cs m_csFiles; LIST m_arFiles; - TG_FILE_REQUEST* PopFile(const char *pszUniqueId); + TG_FILE_REQUEST* FindFile(const char *pszUniqueId); static INT_PTR CALLBACK EnterEmail(void *param); static INT_PTR CALLBACK EnterEmailCode(void *param); -- cgit v1.2.3