pkgsynapse.pp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. {$mode objfpc}
  2. {$h+}
  3. unit pkgsynapse;
  4. interface
  5. uses Classes,pkgdownload;
  6. Type
  7. TSynapseDownloader = Class(TBaseDownloader)
  8. Protected
  9. Procedure FTPDownload(Const URL : String; Dest : TStream); override;
  10. Procedure HTTPDownload(Const URL : String; Dest : TStream); override;
  11. end;
  12. implementation
  13. uses sysutils,uriparser,httpsend,ftpsend,pkgmessages;
  14. Procedure TSynapseDownloader.FTPDownload(Const URL : String; Dest : TStream);
  15. Var
  16. URI : TURI;
  17. FN : String;
  18. F : TFileStream;
  19. begin
  20. // Download in temporary file.
  21. FN:=GetTempFileName();
  22. try
  23. URI:=ParseURI(URL);
  24. with TFTPSend.Create do
  25. try
  26. if URI.UserName <> '' then
  27. begin
  28. Username := URI.UserName;
  29. Password := URI.Password;
  30. end;
  31. TargetHost := URI.Host;
  32. if (URI.Port<>0) then
  33. TargetPort := IntToStr(URI.Port);
  34. if not Login then
  35. Error(SErrLoginFailed);
  36. DirectFileName := FN;
  37. DirectFile:=True;
  38. If (URI.Path<>'') then
  39. if not ChangeWorkingDir(URI.Path) then
  40. Error(SErrCWDFailed,[URI.PATH]);
  41. BinaryMode:=True;
  42. If Not RetrieveFile(URI.Document, False) then
  43. Error(SErrGETFailed,[URI.Document]);
  44. Logout;
  45. finally
  46. Free;
  47. end;
  48. F:=TFileStream.Create(FN,fmOpenRead);
  49. Try
  50. Dest.CopyFrom(F,0);
  51. Finally
  52. F.Free;
  53. end;
  54. finally
  55. // Delete temporary file.
  56. If FileExists(FN) then
  57. DeleteFile(FN);
  58. end;
  59. end;
  60. Procedure TSynapseDownloader.HTTPDownload(Const URL : String; Dest : TStream);
  61. begin
  62. If Not HttpGetBinary(URL,Dest) then
  63. Error(SErrHTTPGetFailed);
  64. end;
  65. end.