FileSend.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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;
  102. if(CTextConvert::ConvertToUTF8(csFile, dest))
  103. {
  104. strncpy(Info.m_cDesc, dest, sizeof(Info.m_cDesc));
  105. Info.m_cDesc[sizeof(Info.m_cDesc)-1] = 0;
  106. }
  107. Info.m_lParameter1 = (long)file.GetLength();
  108. if(m_Send.SendCSendData(Info, MyEnums::DATA_START))
  109. {
  110. long lReadBytes = 0;
  111. BOOL bError = FALSE;
  112. CMd5 md5;
  113. md5.MD5Init();
  114. BOOL calcMd5 = CGetSetOptions::GetCheckMd5OnFileTransfers();
  115. DWORD d = GetTickCount();
  116. do
  117. {
  118. lReadBytes = file.Read(pBuffer, CHUNK_WRITE_SIZE);
  119. if(m_Send.SendExactSize(pBuffer, lReadBytes, false) == FALSE)
  120. {
  121. LogSendRecieveInfo("Error sending SendExactSize in SendFile");
  122. bError = TRUE;
  123. break;
  124. }
  125. if (calcMd5)
  126. {
  127. //md5.MD5Update((unsigned char *)pBuffer, lReadBytes);
  128. }
  129. }while(lReadBytes >= CHUNK_WRITE_SIZE);
  130. DWORD end = GetTickCount() - d;
  131. if(bError == FALSE)
  132. {
  133. Info.m_lParameter1 = 0;
  134. Info.m_lParameter2 = 0;
  135. FILETIME creationTime;
  136. FILETIME lastAccessTime;
  137. FILETIME lastWriteTime;
  138. if (GetFileTime(file, &creationTime, &lastAccessTime, &lastWriteTime))
  139. {
  140. Info.m_lParameter1 = lastWriteTime.dwLowDateTime;
  141. Info.m_lParameter2 = lastWriteTime.dwHighDateTime;
  142. }
  143. CStringA csMd5 = md5.MD5FinalToString();
  144. strncpy(Info.m_md5, csMd5, sizeof(Info.m_md5));
  145. LogSendRecieveInfo(StrF(_T("Sending data_end for file: %s, md5: %s"), csFile, CTextConvert::MultiByteToUnicodeString(csMd5)));
  146. if(m_Send.SendCSendData(Info, MyEnums::DATA_END))
  147. bRet = TRUE;
  148. }
  149. }
  150. }
  151. else
  152. {
  153. TCHAR szError[100];
  154. ex.GetErrorMessage(szError, 100);
  155. LogSendRecieveInfo(StrF(_T("Error opening file in Send file, error: %s"), szError));
  156. }
  157. }
  158. catch(CFileException *e)
  159. {
  160. TCHAR szError[100];
  161. e->GetErrorMessage(szError, 100);
  162. LogSendRecieveInfo(StrF(_T("Exception - Error in Send file, error: %s"), szError));
  163. }
  164. delete []pBuffer;
  165. pBuffer = NULL;
  166. return bRet;
  167. }