FileSend.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // FileSend.cpp: implementation of the CFileSend class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "cp_main.h"
  6. #include "FileSend.h"
  7. #include "Server.h"
  8. #include "shared/TextConvert.h"
  9. #include "Md5.h"
  10. #include <shlwapi.h>
  11. #ifdef _DEBUG
  12. #undef THIS_FILE
  13. static char THIS_FILE[]=__FILE__;
  14. #define new DEBUG_NEW
  15. #endif
  16. //using namespace nsPath;
  17. CFileSend::CFileSend()
  18. {
  19. }
  20. CFileSend::~CFileSend()
  21. {
  22. }
  23. BOOL CFileSend::SendClientFiles(SOCKET sock, CClipList *pClipList)
  24. {
  25. if(!pClipList || pClipList->GetCount() <= 0)
  26. {
  27. LogSendRecieveInfo("::ERROR SendClientFiles called either pClipList was null or empty");
  28. return FALSE;
  29. }
  30. m_Send.SetSocket(sock);
  31. CSendInfo Info;
  32. BOOL bRet = FALSE;
  33. CStringArray CopyFiles;
  34. CClipFormat *pFormat = GetCF_HDROP_Data(pClipList);
  35. if(pFormat)
  36. {
  37. HDROP drop = (HDROP)GlobalLock(pFormat->m_hgData);
  38. int nNumFiles = DragQueryFile(drop, -1, NULL, 0);
  39. TCHAR file[MAX_PATH];
  40. for(int nFile = 0; nFile < nNumFiles; nFile++)
  41. {
  42. if(DragQueryFile(drop, nFile, file, sizeof(file)) > 0)
  43. {
  44. if(PathIsDirectory(file) == FALSE)
  45. {
  46. CopyFiles.Add(file);
  47. }
  48. }
  49. }
  50. GlobalUnlock(pFormat->m_hgData);
  51. }
  52. Info.m_lParameter1 = (long)CopyFiles.GetSize();
  53. if(Info.m_lParameter1 > 0)
  54. {
  55. if(m_Send.SendCSendData(Info, MyEnums::START))
  56. {
  57. for(int nFile = 0; nFile < Info.m_lParameter1; nFile++)
  58. {
  59. SendFile(CopyFiles[nFile]);
  60. }
  61. }
  62. }
  63. if(m_Send.SendCSendData(Info, MyEnums::END))
  64. bRet = TRUE;
  65. return bRet;
  66. }
  67. CClipFormat* CFileSend::GetCF_HDROP_Data(CClipList *pClipList)
  68. {
  69. CClip* pClip;
  70. CClipFormat* pCF;
  71. POSITION pos;
  72. pos = pClipList->GetHeadPosition();
  73. while(pos)
  74. {
  75. pClip = pClipList->GetNext(pos);
  76. if(pClip)
  77. {
  78. pCF = pClip->m_Formats.FindFormat(CF_HDROP);
  79. if(pCF)
  80. return pCF;
  81. }
  82. }
  83. return NULL;
  84. }
  85. BOOL CFileSend::SendFile(CString csFile)
  86. {
  87. CFile file;
  88. BOOL bRet = FALSE;
  89. CSendInfo Info;
  90. char *pBuffer = new char[CHUNK_WRITE_SIZE];
  91. if(pBuffer == NULL)
  92. {
  93. LogSendRecieveInfo("Error creating buffer to send file over in");
  94. return FALSE;
  95. }
  96. try
  97. {
  98. CFileException ex;
  99. if(file.Open(csFile, CFile::modeRead|CFile::typeBinary|CFile::shareDenyNone, &ex))
  100. {
  101. CStringA dest = CTextConvert::UnicodeToUTF8(csFile);
  102. strncpy(Info.m_cDesc, dest, sizeof(Info.m_cDesc));
  103. Info.m_cDesc[sizeof(Info.m_cDesc)-1] = 0;
  104. Info.m_lParameter1 = (long)file.GetLength();
  105. if(m_Send.SendCSendData(Info, MyEnums::DATA_START))
  106. {
  107. long lReadBytes = 0;
  108. BOOL bError = FALSE;
  109. CMd5 md5;
  110. md5.MD5Init();
  111. BOOL calcMd5 = CGetSetOptions::GetCheckMd5OnFileTransfers();
  112. DWORD d = GetTickCount();
  113. do
  114. {
  115. lReadBytes = file.Read(pBuffer, CHUNK_WRITE_SIZE);
  116. if(m_Send.SendExactSize(pBuffer, lReadBytes, false) == FALSE)
  117. {
  118. LogSendRecieveInfo("Error sending SendExactSize in SendFile");
  119. bError = TRUE;
  120. break;
  121. }
  122. if (calcMd5)
  123. {
  124. md5.MD5Update((unsigned char *)pBuffer, lReadBytes);
  125. }
  126. }while(lReadBytes >= CHUNK_WRITE_SIZE);
  127. DWORD end = GetTickCount() - d;
  128. if(bError == FALSE)
  129. {
  130. Info.m_lParameter1 = 0;
  131. Info.m_lParameter2 = 0;
  132. FILETIME creationTime;
  133. FILETIME lastAccessTime;
  134. FILETIME lastWriteTime;
  135. if (GetFileTime(file, &creationTime, &lastAccessTime, &lastWriteTime))
  136. {
  137. Info.m_lParameter1 = lastWriteTime.dwLowDateTime;
  138. Info.m_lParameter2 = lastWriteTime.dwHighDateTime;
  139. }
  140. CStringA csMd5 = md5.MD5FinalToString();
  141. strncpy(Info.m_md5, csMd5, sizeof(Info.m_md5));
  142. LogSendRecieveInfo(StrF(_T("Sending data_end for file: %s, md5: %s"), csFile, CTextConvert::AnsiToUnicode(csMd5)));
  143. if(m_Send.SendCSendData(Info, MyEnums::DATA_END))
  144. bRet = TRUE;
  145. }
  146. }
  147. }
  148. else
  149. {
  150. TCHAR szError[100];
  151. ex.GetErrorMessage(szError, 100);
  152. LogSendRecieveInfo(StrF(_T("Error opening file in Send file, error: %s"), szError));
  153. }
  154. }
  155. catch(CFileException *e)
  156. {
  157. TCHAR szError[100];
  158. e->GetErrorMessage(szError, 100);
  159. LogSendRecieveInfo(StrF(_T("Exception - Error in Send file, error: %s"), szError));
  160. }
  161. delete []pBuffer;
  162. pBuffer = NULL;
  163. return bRet;
  164. }