IdAttachment.pas 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.8 2/8/05 6:02:10 PM RLebeau
  18. Try that again...
  19. Rev 1.7 2/8/05 6:00:02 PM RLebeau
  20. Updated SaveToFile() to call SaveToStream()
  21. Rev 1.6 6/16/2004 2:10:48 PM EHill
  22. Added SaveToStream method for TIdAttachment
  23. Rev 1.5 2004.03.03 10:30:46 AM czhower
  24. Removed warning.
  25. Rev 1.4 2/24/04 1:23:58 PM RLebeau
  26. Bug fix for SaveToFile() using the wrong Size
  27. Rev 1.3 2004.02.03 5:44:50 PM czhower
  28. Name changes
  29. Rev 1.2 10/17/03 12:07:28 PM RLebeau
  30. Updated Assign() to copy all available header values rather than select ones.
  31. Rev 1.1 10/16/2003 10:55:24 PM DSiders
  32. Added localization comments.
  33. Rev 1.0 11/14/2002 02:12:36 PM JPMugaas
  34. }
  35. unit IdAttachment;
  36. interface
  37. {$i IdCompilerDefines.inc}
  38. uses
  39. Classes,
  40. IdMessageParts;
  41. type
  42. TIdAttachment = class(TIdMessagePart)
  43. public
  44. // here the methods you have to override...
  45. // for open handling
  46. // works like this:
  47. // 1) you create an attachment - and do whatever it takes to put data in it
  48. // 2) you send the message
  49. // 3) this will be called - first OpenLoadStream, to get a stream
  50. // 4) when the message is fully encoded, CloseLoadStream is called
  51. // to close the stream. The Attachment implementation decides what to do
  52. function OpenLoadStream: TStream; virtual; abstract;
  53. procedure CloseLoadStream; virtual; abstract;
  54. // for save handling
  55. // works like this:
  56. // 1) new attachment is created
  57. // 2) PrepareTempStream is called
  58. // 3) stuff is loaded
  59. // 4) FinishTempStream is called of the newly created attachment
  60. function PrepareTempStream: TStream; virtual; abstract;
  61. procedure FinishTempStream; virtual; abstract;
  62. procedure LoadFromFile(const FileName: String); virtual;
  63. procedure LoadFromStream(AStream: TStream); virtual;
  64. procedure SaveToFile(const FileName: String); virtual;
  65. procedure SaveToStream(AStream: TStream); virtual;
  66. class function PartType: TIdMessagePartType; override;
  67. end;
  68. TIdAttachmentClass = class of TIdAttachment;
  69. implementation
  70. uses
  71. IdGlobal, IdGlobalProtocols, IdCoderHeader,
  72. SysUtils;
  73. { TIdAttachment }
  74. class function TIdAttachment.PartType: TIdMessagePartType;
  75. begin
  76. Result := mptAttachment;
  77. end;
  78. procedure TIdAttachment.LoadFromFile(const FileName: String);
  79. var
  80. LStrm: TIdReadFileExclusiveStream;
  81. begin
  82. LStrm := TIdReadFileExclusiveStream.Create(FileName); try
  83. LoadFromStream(LStrm);
  84. finally
  85. FreeAndNil(LStrm);
  86. end;
  87. end;
  88. procedure TIdAttachment.LoadFromStream(AStream: TStream);
  89. var
  90. LStrm: TStream;
  91. begin
  92. LStrm := PrepareTempStream;
  93. try
  94. // TODO: use AStream.Size-AStream.Position instead of 0, and don't call
  95. // CopyFrom() if (AStream.Size-AStream.Position) is <= 0. Passing 0 to
  96. // CopyFrom() tells it to seek AStream to Position=0 and then copy the
  97. // entire stream, which is fine for the stream provided by LoadFromFile(),
  98. // but may not always be desirable for user-provided streams...
  99. LStrm.CopyFrom(AStream, 0);
  100. finally
  101. FinishTempStream;
  102. end;
  103. end;
  104. procedure TIdAttachment.SaveToFile(const FileName: String);
  105. var
  106. LStrm: TIdFileCreateStream;
  107. begin
  108. LStrm := TIdFileCreateStream.Create(FileName); try
  109. SaveToStream(LStrm);
  110. finally
  111. FreeAndNil(LStrm);
  112. end;
  113. end;
  114. procedure TIdAttachment.SaveToStream(AStream: TStream);
  115. var
  116. LStrm: TStream;
  117. begin
  118. LStrm := OpenLoadStream;
  119. try
  120. AStream.CopyFrom(LStrm, 0);
  121. finally
  122. CloseLoadStream;
  123. end;
  124. end;
  125. end.