TeamShareAdapter.pas 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. unit TeamShareAdapter;
  2. interface
  3. uses
  4. GroupConfig, RealICQClient, Classes, StrUtils, SysUtils, Dialogs, RealICQModel;
  5. type
  6. TTeamShareAdapter = class
  7. private
  8. class procedure SendUploadedEventToServer(AJson: string; ARealICQClient: TRealICQClient);
  9. public
  10. class procedure UploadFile(ATeamID, AFileName: string; ATeamForm: TComponent; ARealICQClient: TRealICQClient; IsNeedNotify: Boolean); overload;
  11. class function GetTheFileSize(AFileName: String): Int64;
  12. class function GetShareURL(ATeamID, ALoginName, ADisplayName, AIsAdmin: string): string;
  13. class procedure UploadedNotifyToMembers(ASender, AMembers, AFileMd5, AFileName: string; AFileSize: Integer; ARealICQClient: TRealICQClient);
  14. end;
  15. implementation
  16. uses
  17. MainFrm, Windows, TalkingFrm, HTTPApp, RealICQUtils, LoggerImport, superobject,
  18. md5, UploadOrDownloadFileMission, TransmitDirection, Forms;
  19. const
  20. DOWN_URL: string = 'http://%s:%d/home/down/%s?%s';
  21. UP_URL: string = 'http://%s:%d/Home/Create?use=%s&groupid=%s&displayname=%s';
  22. SHARE_URL: string = 'http://%s:%d/home/index?loginname=%s&teamid=%s&displayname=%s&isAdmin=%s';
  23. OLD_SHARE_URL: string = '%s/share/Default.aspx?TeamID=%s&LoginName=%s&password=%s&tick=%s';// + IntToStr(GetTickCount)
  24. NOTIFY_JSON: string = '{sender:"", receivers:[], sendtime:"", url:"", fileSize:""}';
  25. { TTeamShareAdapter }
  26. class function TTeamShareAdapter.GetShareURL(ATeamID, ALoginName, ADisplayName,
  27. AIsAdmin: string): string;
  28. var
  29. AConfig: TGroupShareConfig;
  30. begin
  31. AConfig := TGroupShareConfig.GetConfig;
  32. if AConfig.GroupShareVersion = gsvNew then
  33. Result := Format(SHARE_URL, [AConfig.IP, AConfig.Port, ALoginName, ATeamID, HttpEncode(AnsiToUtf8(ADisplayName)), AIsAdmin])
  34. else if AConfig.GroupShareVersion = gsvOld then
  35. Result := Format(OLD_SHARE_URL, [AConfig.URL, ATeamID, ALoginName, MD5En(MainForm.RealICQClient.Password), IntToStr(GetTickCount)]);
  36. end;
  37. class function TTeamShareAdapter.GetTheFileSize(AFileName: String): Int64;
  38. var
  39. FileHandle: THandle;
  40. MapHandle: THandle;
  41. ViewPointer: pointer;
  42. begin
  43. Result := 0;
  44. FileHandle := CreateFile(pChar(AFileName), GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE,
  45. nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL or FILE_FLAG_SEQUENTIAL_SCAN, 0);
  46. if FileHandle <> INVALID_HANDLE_VALUE then try
  47. MapHandle := CreateFileMapping(FileHandle, nil, PAGE_READONLY, 0, 0, nil);
  48. if MapHandle <> 0 then try
  49. ViewPointer := MapViewOfFile(MapHandle, FILE_MAP_READ, 0, 0, 0);
  50. if ViewPointer <> nil then try
  51. Result := GetFileSize(FileHandle, nil);
  52. finally
  53. UnmapViewOfFile(ViewPointer);
  54. end;
  55. finally
  56. CloseHandle(MapHandle);
  57. end;
  58. finally
  59. CloseHandle(FileHandle);
  60. end;
  61. end;
  62. class procedure TTeamShareAdapter.SendUploadedEventToServer(AJson: string;
  63. ARealICQClient: TRealICQClient);
  64. var
  65. nIndex,AJsonLength,IIDLength,
  66. BufferLength: SmallInt;
  67. SendBuffer: Array of Byte;
  68. begin
  69. try
  70. AJsonLength := Length(AJson);
  71. BufferLength := 5 + Length(AJson);
  72. SetLength(SendBuffer, BufferLength);
  73. nIndex := 0;
  74. //填充 (1)协议类型(0xD3) 1byte
  75. SendBuffer[nIndex] := $D3;
  76. Inc(nIndex, 1);
  77. //填充 (2)消息总长度 2byte
  78. CopyMemory(@SendBuffer[nIndex], @BufferLength, 2);
  79. Inc(nIndex, 2);
  80. //填充 (2)JSON总长度 2byte
  81. CopyMemory(@SendBuffer[nIndex], @AJsonLength, 2);
  82. Inc(nIndex, 2);
  83. //填充(4)JSON 动态长度
  84. CopyMemory(@SendBuffer[nIndex], PChar(AJson), AJsonLength);
  85. ARealICQClient.TCPClient.SendBuffer(SendBuffer[0], BufferLength);
  86. except
  87. on E: Exception do
  88. Log(E.Message, 'TTeamShareAdapter.SendUploadedEventToServer('+AJson+')');
  89. end;
  90. end;
  91. //sender:"%s", receivers:"[%s]", sendtime:"%f", url:"%s", fileSize
  92. class procedure TTeamShareAdapter.UploadedNotifyToMembers(ASender, AMembers,
  93. AFileMd5, AFileName: string; AFileSize: Integer; ARealICQClient: TRealICQClient);
  94. var
  95. AJson,ADownUrl: string;
  96. AReceivers: TStringList;
  97. AConfig: TGroupShareConfig;
  98. iLoop, iMax: Integer;
  99. jo :ISuperObject;
  100. begin
  101. AConfig := TGroupShareConfig.GetConfig;
  102. if AConfig.GroupShareVersion <> gsvNew then
  103. Exit;
  104. AReceivers := SplitString(AMembers, Chr(10));
  105. ADownUrl := Format(DOWN_URL,
  106. [
  107. AConfig.IP,
  108. AConfig.Port,
  109. AFileMd5,
  110. AFileName
  111. ]);
  112. jo := SO(NOTIFY_JSON);
  113. jo.S['sender'] := ASender;
  114. jo.D['sendtime'] := Now;
  115. jo.S['url'] := ADownUrl;
  116. jo.I['fileSize'] := AFileSize;
  117. iMax := 10;
  118. for iLoop := 0 to AReceivers.Count - 1 do
  119. begin
  120. if iLoop >= iMax then
  121. begin
  122. AJson := jo.AsJSon(False,False);
  123. SendUploadedEventToServer(AJson, ARealICQClient);
  124. Jo.A['receivers'].Clear;
  125. Inc(iMax, 10);
  126. end;
  127. jo.A['receivers'].Add(TSuperObject.Create(AReceivers[iLoop]))
  128. end;
  129. if jo.A['receivers'].Length > 0 then
  130. begin
  131. AJson := jo.AsJSon(False,False);
  132. SendUploadedEventToServer(AJson, ARealICQClient);
  133. end;
  134. AReceivers.Free;
  135. end;
  136. class procedure TTeamShareAdapter.UploadFile(ATeamID, AFileName: string;
  137. ATeamForm: TComponent; ARealICQClient: TRealICQClient; IsNeedNotify: Boolean);
  138. var
  139. AConfig: TGroupShareConfig;
  140. AUpUrl: String;
  141. AFileSize:int64;
  142. strMissionID,
  143. js: string;
  144. begin
  145. AConfig := TGroupShareConfig.GetConfig;
  146. if (not ARealICQClient.Connected) or ( not ARealICQClient.Logined) then
  147. Exit;
  148. if IsNeedNotify and not (MessageBox(0, '您确认要群发该文件吗?', '提示', MB_OKCANCEL + MB_ICONQUESTION) = ID_OK) then
  149. Exit;
  150. if DirectoryExists(AFileName) then
  151. begin
  152. MessageBox(0, PChar('您上传的是目录,请压缩后上传!'), '提示', MB_ICONINFORMATION);
  153. Exit;
  154. end;
  155. if FileExists(AFileName) then
  156. begin
  157. TTalkingForm(ATeamForm).spbTeamNetWorkDisk.OnClick(Nil);
  158. AFileSize := GetTheFileSize(AFileName);
  159. if AFileSize=0 then
  160. begin
  161. MessageBox(0, PChar('发送文件大小不允许超过 ' + IntToStr(20) + 'M !'), '提示', MB_ICONINFORMATION);
  162. Exit;
  163. end;
  164. if AFileSize >200 * 1024 * 1024 then
  165. begin
  166. MessageBox(0, PChar('发送文件大小不允许超过 ' + IntToStr(20) + 'M !'), '提示', MB_ICONINFORMATION);
  167. Exit;
  168. end;
  169. end
  170. else
  171. begin
  172. MessageBox(0, PChar('发送文件不存在!'), '提示', MB_ICONINFORMATION);
  173. Exit;
  174. end;
  175. if AConfig.GroupShareVersion = gsvNew then
  176. begin
  177. try
  178. AUpUrl := Format(UP_URL,
  179. [
  180. AConfig.IP,
  181. AConfig.Port,
  182. ARealICQClient.LoginName,
  183. ATeamID,
  184. HttpEncode(AnsiToUtf8(ARealICQClient.Me.DisplayName))]);
  185. TTalkingForm(ATeamForm).spbUploadTeamFileProcess.Visible := True;
  186. TTalkingForm(ATeamForm).TeamUpLoadFile.ThreadDownFile(AUpUrl,AFileName, AFileSize, IsNeedNotify);
  187. except
  188. on E: Exception do
  189. MessageBox(0, PChar(E.Message), '发送文件时出错', MB_ICONINFORMATION);
  190. end;
  191. end
  192. else if AConfig.GroupShareVersion = gsvOld then
  193. begin
  194. try
  195. strMissionID := '1|' + IntToStr(GetTickCount) + ',' + ATeamID + ',' + MainForm.RealICQClient.LoginName;
  196. js := format('ReadyToUpload("%s", "%s", %d)', [strMissionID, ReplaceStr(AFileName, '\', '\\'), GetTheFileSize(AFileName)]);
  197. (ATeamForm as TTalkingForm)
  198. .WebBrowserForTeamDisk
  199. .OleObject
  200. .Document
  201. .parentWindow
  202. .execScript(js, 'JavaScript');
  203. except
  204. on E: Exception do MessageBox(0, PChar(E.Message), '上传文件时出错', MB_ICONINFORMATION);
  205. end;
  206. end;
  207. end;
  208. end.