pkgwget.pp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. {
  2. This file is part of the fppkg package manager
  3. Copyright (c) 1999-2022 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {$mode objfpc}
  11. {$h+}
  12. unit pkgwget;
  13. interface
  14. uses Classes,pkgdownload,pkgoptions,fprepos;
  15. Type
  16. { TWGetDownloader }
  17. TWGetDownloader = Class(TBaseDownloader)
  18. Private
  19. FWGet : String;
  20. Protected
  21. Constructor Create(AOwner: TComponent); override;
  22. function WGetDownload(Const URL : String; Dest : TStream): Boolean; virtual;
  23. function FTPDownload(Const URL : String; Dest : TStream): Boolean; override;
  24. function HTTPDownload(Const URL : String; Dest : TStream): Boolean; override;
  25. Public
  26. Property WGet : String Read FWGet Write FWGet;
  27. end;
  28. implementation
  29. uses
  30. sysutils,process,
  31. pkgglobals,
  32. pkgmessages;
  33. Constructor TWGetDownloader.Create(AOwner: TComponent);
  34. begin
  35. Inherited;
  36. wget:='wget';
  37. end;
  38. function TWGetDownloader.WGetDownload(Const URL: String; Dest: TStream): Boolean;
  39. Var
  40. Buffer : Array[0..4096] of byte;
  41. Count : Integer;
  42. begin
  43. Result := False;
  44. With TProcess.Create(Self) do
  45. try
  46. CommandLine:=WGet+' -q --output-document=- '+url;
  47. Options:=[poUsePipes,poNoConsole];
  48. Execute;
  49. While Running do
  50. begin
  51. Count:=Output.Read(Buffer,SizeOf(Buffer));
  52. If (Count>0) then
  53. Dest.WriteBuffer(Buffer,Count);
  54. end;
  55. If (ExitStatus<>0) then
  56. Error(SErrDownloadFailed,['WGET',URL,Format('exit status %d',[ExitStatus])])
  57. else
  58. Result := True;
  59. finally
  60. Free;
  61. end;
  62. end;
  63. function TWGetDownloader.FTPDownload(Const URL: String; Dest: TStream): Boolean;
  64. begin
  65. Result := WGetDownload(URL,Dest);
  66. end;
  67. function TWGetDownloader.HTTPDownload(Const URL: String; Dest: TStream): Boolean;
  68. begin
  69. Result := WGetDownload(URL,Dest);
  70. end;
  71. initialization
  72. RegisterDownloader('wget',TWGetDownloader);
  73. end.