pkgwget.pp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. {$mode objfpc}
  2. {$h+}
  3. unit pkgwget;
  4. interface
  5. uses Classes,pkgdownload,pkgoptions,fprepos;
  6. Type
  7. { TWGetDownloader }
  8. TWGetDownloader = Class(TBaseDownloader)
  9. Private
  10. FWGet : String;
  11. Protected
  12. Constructor Create(AOwner: TComponent); override;
  13. function WGetDownload(Const URL : String; Dest : TStream): Boolean; virtual;
  14. function FTPDownload(Const URL : String; Dest : TStream): Boolean; override;
  15. function HTTPDownload(Const URL : String; Dest : TStream): Boolean; override;
  16. Public
  17. Property WGet : String Read FWGet Write FWGet;
  18. end;
  19. implementation
  20. uses
  21. sysutils,process,
  22. pkgglobals,
  23. pkgmessages;
  24. Constructor TWGetDownloader.Create(AOwner: TComponent);
  25. begin
  26. Inherited;
  27. wget:='wget';
  28. end;
  29. function TWGetDownloader.WGetDownload(Const URL: String; Dest: TStream): Boolean;
  30. Var
  31. Buffer : Array[0..4096] of byte;
  32. Count : Integer;
  33. begin
  34. Result := False;
  35. With TProcess.Create(Self) do
  36. try
  37. CommandLine:=WGet+' -q --output-document=- '+url;
  38. Options:=[poUsePipes,poNoConsole];
  39. Execute;
  40. While Running do
  41. begin
  42. Count:=Output.Read(Buffer,SizeOf(Buffer));
  43. If (Count>0) then
  44. Dest.WriteBuffer(Buffer,Count);
  45. end;
  46. If (ExitStatus<>0) then
  47. Error(SErrDownloadFailed,['WGET',URL,Format('exit status %d',[ExitStatus])])
  48. else
  49. Result := True;
  50. finally
  51. Free;
  52. end;
  53. end;
  54. function TWGetDownloader.FTPDownload(Const URL: String; Dest: TStream): Boolean;
  55. begin
  56. Result := WGetDownload(URL,Dest);
  57. end;
  58. function TWGetDownloader.HTTPDownload(Const URL: String; Dest: TStream): Boolean;
  59. begin
  60. Result := WGetDownload(URL,Dest);
  61. end;
  62. initialization
  63. RegisterDownloader('wget',TWGetDownloader);
  64. end.