From 513c5f177e4f02e93d5e1e34b444400d968c4247 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sat, 22 Feb 2025 20:09:31 +0200 Subject: =?UTF-8?q?fixes=20#4875=20(Telegram:=20=D0=9C=D0=B8=D1=80=D0=B0?= =?UTF-8?q?=D0=BD=D0=B4=D0=B0=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B0=D0=B5?= =?UTF-8?q?=D1=82,=20=D1=87=D1=82=D0=BE=20=D1=84=D0=B0=D0=B9=D0=BB=20?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=B4=D0=B0=D0=BD=20=D0=B2=20=D1=82?= =?UTF-8?q?=D0=BE=20=D0=B2=D1=80=D0=B5=D0=BC=D1=8F,=20=D0=BA=D0=B0=D0=BA?= =?UTF-8?q?=20=D0=BE=D0=BD=20=D0=B5=D1=89=D1=91=20=D0=B7=D0=B0=D0=B3=D1=80?= =?UTF-8?q?=D1=83=D0=B6=D0=B0=D0=B5=D1=82=D1=81=D1=8F)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- protocols/Telegram/src/avatars.cpp | 128 ++++++++++++++++++++++--------------- protocols/Telegram/src/proto.cpp | 29 +++++---- protocols/Telegram/src/proto.h | 1 + 3 files changed, 96 insertions(+), 62 deletions(-) (limited to 'protocols/Telegram/src') diff --git a/protocols/Telegram/src/avatars.cpp b/protocols/Telegram/src/avatars.cpp index 761776ce3e..237bbc627e 100644 --- a/protocols/Telegram/src/avatars.cpp +++ b/protocols/Telegram/src/avatars.cpp @@ -191,6 +191,17 @@ TG_FILE_REQUEST* CTelegramProto::FindFile(const char *pszUniqueId) return nullptr; } +TG_FILE_REQUEST* CTelegramProto::FindFile(int id) +{ + mir_cslock lck(m_csFiles); + + for (auto &it : m_arFiles) + if (it->m_fileId == id) + return it; + + return nullptr; +} + ///////////////////////////////////////////////////////////////////////////////////////// // Extracts a photo/avatar to a file @@ -227,30 +238,39 @@ void CTelegramProto::ProcessFile(TD::updateFile *pObj) return; if (!pLocal->is_downloading_completed_) { - if (auto *F = FindFile(pRemote->unique_id_.c_str())) { - if (F->m_type != F->AVATAR && F->ofd) { - DBVARIANT dbv = { DBVT_DWORD }; - dbv.dVal = pLocal->downloaded_size_; - db_event_setJson(F->ofd->hDbEvent, "ft", &dbv); - } + auto *ft = FindFile(pRemote->unique_id_.c_str()); + if (ft && ft->m_type != ft->AVATAR && ft->ofd) { + DBVARIANT dbv = { DBVT_DWORD }; + dbv.dVal = pLocal->downloaded_size_; + db_event_setJson(ft->ofd->hDbEvent, "ft", &dbv); } return; } // file upload is not completed, skip it - if (pRemote->is_uploading_active_) + if (pRemote->is_uploading_active_) { + auto *ft = FindFile(pFile->id_); + if (ft) { + PROTOFILETRANSFERSTATUS fts = {}; + fts.hContact = ft->m_hContact; + fts.totalFiles = 1; + fts.totalBytes = fts.currentFileSize = pFile->size_; + fts.totalProgress = fts.currentFileProgress = pFile->remote_->uploaded_size_; + ProtoBroadcastAck((UINT_PTR)ft->m_hContact, ACKTYPE_FILE, ACKRESULT_DATA, ft, (LPARAM)&fts); + } return; + } Utf2T wszExistingFile(pLocal->path_.c_str()); - if (auto *F = FindFile(pRemote->unique_id_.c_str())) { - if (F->m_type == F->AVATAR) { - CMStringW wszFullName = F->m_destPath; + if (auto *ft = FindFile(pRemote->unique_id_.c_str())) { + if (ft->m_type == ft->AVATAR) { + CMStringW wszFullName = ft->m_destPath; if (!wszFullName.IsEmpty()) wszFullName += L"\\"; - wszFullName += F->m_fileName; + wszFullName += ft->m_fileName; - if (F->m_fileName.Right(5).MakeLower() == L".webp") { + if (ft->m_fileName.Right(5).MakeLower() == L".webp") { if (auto *pImage = FreeImage_LoadU(FIF_WEBP, wszExistingFile)) { wszFullName.Truncate(wszFullName.GetLength() - 5); wszFullName += L".png"; @@ -258,7 +278,7 @@ void CTelegramProto::ProcessFile(TD::updateFile *pObj) FreeImage_Unload(pImage); } } - else if (F->m_fileName.Right(4).MakeLower() == L".tga") { + else if (ft->m_fileName.Right(4).MakeLower() == L".tga") { if (auto *pImage = FreeImage_LoadU(FIF_TARGA, wszExistingFile)) { wszFullName.Truncate(wszFullName.GetLength() - 5); wszFullName += L".png"; @@ -268,54 +288,53 @@ void CTelegramProto::ProcessFile(TD::updateFile *pObj) } else MoveFileW(wszExistingFile, wszFullName); - if (F->m_isSmiley) + if (ft->m_isSmiley) SmileyAdd_LoadContactSmileys(SMADD_FILE, m_szModuleName, wszFullName); else NS_NotifyFileReady(wszFullName); } else { // FILE, PICTURE, VIDEO, VOICE - if (F->ofd == nullptr) - return; - - DBVARIANT dbv = { DBVT_DWORD }; - dbv.dVal = pLocal->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(pLocal->path_.c_str(), '\\'); - if (!pSlash) - pSlash = pLocal->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); + if (ft->ofd) { + DBVARIANT dbv = { DBVT_DWORD }; + dbv.dVal = pLocal->downloaded_size_; + db_event_setJson(ft->ofd->hDbEvent, "ft", &dbv); + + CMStringW wszFullName(ft->ofd->wszPath); + int idxSlash = wszFullName.ReverseFind('\\') + 1; + if (wszFullName.Find('.', idxSlash) == -1) { + auto *pSlash = strrchr(pLocal->path_.c_str(), '\\'); + if (!pSlash) + pSlash = pLocal->path_.c_str(); + else + pSlash++; + + if (strchr(pSlash, '.')) { + dbv.type = DBVT_UTF8; + dbv.pszVal = (char *)pSlash; + db_event_setJson(ft->ofd->hDbEvent, "f", &dbv); + + wszFullName.Truncate(idxSlash); + wszFullName.Append(Utf2T(pSlash)); + ft->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)); + ft->ofd->ResetFileName(wszFullName); + } } } - } - MoveFileW(wszExistingFile, F->ofd->wszPath); - F->ofd->Finish(); + MoveFileW(wszExistingFile, ft->ofd->wszPath); + ft->ofd->Finish(); + } } mir_cslock lck(m_csFiles); - m_arFiles.remove(F); - delete F; + m_arFiles.remove(ft); + delete ft; return; } @@ -331,6 +350,15 @@ void CTelegramProto::ProcessFile(TD::updateFile *pObj) db_event_delivered(it->hContact, hDbEvent); } } + + if (auto *ft = FindFile(pFile->id_)) { + // file being uploaded is finished + ProtoBroadcastAck(ft->m_hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ft); + + mir_cslock lck(m_csFiles); + m_arFiles.remove(ft); + delete ft; + } return; } } diff --git a/protocols/Telegram/src/proto.cpp b/protocols/Telegram/src/proto.cpp index 40f7f166d6..627b00c228 100644 --- a/protocols/Telegram/src/proto.cpp +++ b/protocols/Telegram/src/proto.cpp @@ -473,22 +473,30 @@ void CTelegramProto::ProcessFileMessage(TG_FILE_REQUEST *ft, const TD::message * dbei.flags = DBEF_SENT | DBEF_UTF; dbei.iTimestamp = time(0); - TG_FILE_REQUEST localft(TG_FILE_REQUEST::FILE, 0, 0); - localft.m_fileName = Utf2T(pFile->local_->path_.c_str()); - localft.m_fileSize = pFile->size_; - localft.m_uniqueId = szMsgId; - localft.m_szUserId = szUserId; - - DB::FILE_BLOB blob(localft.m_fileName, ft->m_wszDescr); - OnSendOfflineFile(dbei, blob, &localft); + auto *localft = new TG_FILE_REQUEST(TG_FILE_REQUEST::FILE, 0, 0); + localft->m_hContact = ft->m_hContact; + localft->m_fileName = Utf2T(pFile->local_->path_.c_str()); + localft->m_fileSize = pFile->size_; + localft->m_uniqueId = szMsgId; + localft->m_szUserId = szUserId; + localft->m_fileId = pFile->id_; + + DB::FILE_BLOB blob(localft->m_fileName, ft->m_wszDescr); + OnSendOfflineFile(dbei, blob, localft); blob.write(dbei); db_event_add(ft->m_hContact, &dbei); + + mir_cslock lck(m_csFiles); + m_arFiles.insert(localft); } else { ft->m_szUserId = szUserId; ft->m_uniqueId = szMsgId; - ProtoBroadcastAck(ft->m_hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ft); + ft->m_fileId = pFile->id_; + + mir_cslock lck(m_csFiles); + m_arFiles.insert(ft); } } } @@ -499,7 +507,6 @@ void CTelegramProto::OnSendFile(td::ClientManager::Response &response, void *pUs if (response.object->get_id() == TD::message::ID) { ProcessFileMessage(ft, (TD::message *)response.object.get(), false); - ProtoBroadcastAck(ft->m_hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ft); } else if (response.object->get_id() == TD::messages::ID) { int i = 0; @@ -509,8 +516,6 @@ void CTelegramProto::OnSendFile(td::ClientManager::Response &response, void *pUs i++; } } - - delete ft; } HANDLE CTelegramProto::SendFile(MCONTACT hContact, const wchar_t *szDescription, wchar_t **ppszFiles) diff --git a/protocols/Telegram/src/proto.h b/protocols/Telegram/src/proto.h index d9a2e647b6..4125db0bb0 100644 --- a/protocols/Telegram/src/proto.h +++ b/protocols/Telegram/src/proto.h @@ -220,6 +220,7 @@ class CTelegramProto : public PROTO mir_cs m_csFiles; LIST m_arFiles; TG_FILE_REQUEST* FindFile(const char *pszUniqueId); + TG_FILE_REQUEST* FindFile(int id); static INT_PTR CALLBACK EnterEmail(void *param); static INT_PTR CALLBACK EnterEmailCode(void *param); -- cgit v1.2.3