UploadFiles.pas 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. unit UploadFiles;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5. Dialogs, JvExControls, JvComponent, JvLabel, StdCtrls, ComCtrls, Winsock,
  6. Misc;
  7. type
  8. TfrmUploadFiles = class(TForm)
  9. anmFileTransfer: TAnimate;
  10. pgbFileTransfer: TProgressBar;
  11. Label1: TLabel;
  12. jvlblSource: TJvLabel;
  13. Label2: TLabel;
  14. jvlblTarget: TJvLabel;
  15. Label3: TLabel;
  16. jvlblSize: TJvLabel;
  17. Label4: TLabel;
  18. jvlblHost: TJvLabel;
  19. btnCancel: TButton;
  20. private
  21. { Private declarations }
  22. public
  23. { Public declarations }
  24. procedure Transfer(pSock: TSocket; pLuaProject: TLuaEditProject; hModule: Cardinal);
  25. end;
  26. var
  27. frmUploadFiles: TfrmUploadFiles;
  28. implementation
  29. {$R *.dfm}
  30. procedure TfrmUploadFiles.Transfer(pSock: TSocket; pLuaProject: TLuaEditProject; hModule: Cardinal);
  31. var
  32. x, iFileCount: Integer;
  33. pLuaUnit: TLuaEditUnit;
  34. SearchRec: TSearchRec;
  35. SizeInBytes: Cardinal;
  36. Ptr: TFarProc;
  37. pSendFile: TSendFile;
  38. pSocketSend: TSocketSend;
  39. CurrentPath, FileName, ErrMsg: String;
  40. begin
  41. // Retrieve the 'SocketSend' function from plugin dll
  42. Ptr := GetProcAddress(hModule, 'SocketSend');
  43. pSocketSend := TSocketSend(Ptr);
  44. Ptr := nil;
  45. // Retrieve the 'SendFile' function from plugin dll
  46. Ptr := GetProcAddress(hModule, 'SendFile');
  47. pSendFile := TSendFile(Ptr);
  48. Ptr := nil;
  49. // Send quantity of files to transfer
  50. if pLuaProject.sInitializer <> '' then
  51. iFileCount := pLuaProject.lstUnits.Count + 1
  52. else
  53. iFileCount := pLuaProject.lstUnits.Count;
  54. if not pSocketSend(pSock, iFileCount, SizeOf(iFileCount), 0, 'Fail to send quantity of files to transfer.') then
  55. Exit;
  56. // Initialize stuff...
  57. anmFileTransfer.Play(1, anmFileTransfer.FrameCount, 0);
  58. pgbFileTransfer.Max := iFileCount;
  59. // Send all files under currently active project
  60. for x := 0 to iFileCount - 1 do
  61. begin
  62. if x < pLuaProject.lstUnits.Count then
  63. begin
  64. // Sending units under current project
  65. pLuaUnit := TLuaEditUnit(pLuaProject.lstUnits[x]);
  66. FindFirst(pLuaUnit.Path, faAnyFile, SearchRec);
  67. SizeInBytes := SearchRec.Size;
  68. jvlblSource.Caption := pLuaUnit.Path;
  69. jvlblTarget.Caption := pLuaProject.sRemoteDirectory + pLuaUnit.Name;
  70. jvlblSize.Caption := GetFileSizeStr(SizeInBytes);
  71. jvlblHost.Caption := pLuaProject.sRemoteIP;
  72. FindClose(SearchRec);
  73. pgbFileTransfer.StepIt;
  74. Application.ProcessMessages;
  75. CurrentPath := pLuaUnit.Path;
  76. FileName := pLuaProject.sRemoteDirectory + pLuaUnit.Name;
  77. ErrMsg := 'Fail to send file: ' + pLuaUnit.Path;
  78. end
  79. else if pLuaProject.sInitializer <> '' then
  80. begin
  81. // Sending project's initializer
  82. FindFirst(pLuaProject.sInitializer, faAnyFile, SearchRec);
  83. SizeInBytes := SearchRec.Size;
  84. jvlblSource.Caption := pLuaProject.sInitializer;
  85. jvlblTarget.Caption := pLuaProject.sRemoteDirectory + ExtractFileName(pLuaProject.sInitializer);
  86. jvlblSize.Caption := GetFileSizeStr(SizeInBytes);
  87. jvlblHost.Caption := pLuaProject.sRemoteIP;
  88. FindClose(SearchRec);
  89. pgbFileTransfer.StepIt;
  90. Application.ProcessMessages;
  91. CurrentPath := pLuaProject.sInitializer;
  92. FileName := pLuaProject.sRemoteDirectory + ExtractFileName(pLuaProject.sInitializer);
  93. ErrMsg := 'Fail to send file: ' + pLuaProject.sInitializer;
  94. end;
  95. // Send current file...
  96. if not pSendFile(pSock, PChar(CurrentPath), PChar(FileName), PChar(ErrMsg)) then
  97. Exit;
  98. end;
  99. // Uninitialize stuff...
  100. anmFileTransfer.Stop;
  101. end;
  102. end.