IdAttachmentFile.pas 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.4 28.09.2004 21:04:44 Andreas Hausladen
  18. Delphi 5 does not have a Owner property in TCollection
  19. Rev 1.3 24.08.2004 18:01:42 Andreas Hausladen
  20. Added AttachmentBlocked property to TIdAttachmentFile.
  21. Rev 1.2 2004.02.03 5:44:50 PM czhower
  22. Name changes
  23. Rev 1.1 5/9/2003 10:27:20 AM BGooijen
  24. Attachment is now opened in fmShareDenyWrite mode
  25. Rev 1.0 11/14/2002 02:12:42 PM JPMugaas
  26. }
  27. unit IdAttachmentFile;
  28. interface
  29. {$i IdCompilerDefines.inc}
  30. uses
  31. Classes,
  32. IdAttachment,
  33. IdMessageParts;
  34. type
  35. TIdAttachmentFile = class(TIdAttachment)
  36. protected
  37. FTempFileStream: TFileStream;
  38. FStoredPathName: String;
  39. FFileIsTempFile: Boolean;
  40. FAttachmentBlocked: Boolean;
  41. public
  42. constructor Create(Collection: TIdMessageParts; const AFileName: String = ''); reintroduce;
  43. destructor Destroy; override;
  44. function OpenLoadStream: TStream; override;
  45. procedure CloseLoadStream; override;
  46. function PrepareTempStream: TStream; override;
  47. procedure FinishTempStream; override;
  48. procedure SaveToFile(const FileName: String); override;
  49. property FileIsTempFile: Boolean read FFileIsTempFile write FFileIsTempFile;
  50. property StoredPathName: String read FStoredPathName write FStoredPathName;
  51. property AttachmentBlocked: Boolean read FAttachmentBlocked;
  52. end;
  53. implementation
  54. uses
  55. {$IFDEF USE_VCL_POSIX}
  56. Posix.Unistd,
  57. {$ENDIF}
  58. {$IFDEF KYLIXCOMPAT}
  59. Libc,
  60. {$ENDIF}
  61. //facilitate inlining only.
  62. {$IFDEF USE_INLINE}
  63. {$IFDEF WINDOWS}
  64. Windows,
  65. {$ENDIF}
  66. {$IFDEF DOTNET}
  67. System.IO,
  68. {$ENDIF}
  69. {$ENDIF}
  70. IdGlobal, IdGlobalProtocols, IdException, IdResourceStringsProtocols,
  71. IdMessage, SysUtils;
  72. { TIdAttachmentFile }
  73. procedure TIdAttachmentFile.CloseLoadStream;
  74. begin
  75. FreeAndNil(FTempFileStream);
  76. end;
  77. constructor TIdAttachmentFile.Create(Collection: TIdMessageParts; const AFileName: String = '');
  78. begin
  79. inherited Create(Collection);
  80. FFilename := ExtractFileName(AFilename);
  81. FTempFileStream := nil;
  82. FStoredPathName := AFileName;
  83. FFileIsTempFile := False;
  84. if FFilename <> '' then begin
  85. ContentType := GetMimeTypeFromFile(FFilename);
  86. end;
  87. end;
  88. destructor TIdAttachmentFile.Destroy;
  89. begin
  90. if FileIsTempFile then begin
  91. SysUtils.DeleteFile(StoredPathName);
  92. end;
  93. inherited Destroy;
  94. end;
  95. procedure TIdAttachmentFile.FinishTempStream;
  96. var
  97. LMsg: TIdMessage;
  98. begin
  99. FreeAndNil(FTempFileStream);
  100. // An on access virus scanner meight delete/block the temporary file.
  101. FAttachmentBlocked := not FileExists(StoredPathName);
  102. if FAttachmentBlocked then begin
  103. LMsg := TIdMessage(OwnerMessage);
  104. if Assigned(LMsg) and (not LMsg.ExceptionOnBlockedAttachments) then begin
  105. Exit;
  106. end;
  107. raise EIdMessageCannotLoad.CreateFmt(RSTIdMessageErrorAttachmentBlocked, [StoredPathName]);
  108. end;
  109. end;
  110. function TIdAttachmentFile.OpenLoadStream: TStream;
  111. begin
  112. FTempFileStream := TIdReadFileExclusiveStream.Create(StoredPathName);
  113. Result := FTempFileStream;
  114. end;
  115. function TIdAttachmentFile.PrepareTempStream: TStream;
  116. var
  117. LMsg: TIdMessage;
  118. begin
  119. LMsg := TIdMessage(OwnerMessage);
  120. if Assigned(LMsg) then begin
  121. FStoredPathName := MakeTempFilename(LMsg.AttachmentTempDirectory);
  122. end else begin
  123. FStoredPathName := MakeTempFilename;
  124. end;
  125. FTempFileStream := TIdFileCreateStream.Create(FStoredPathName);
  126. FFileIsTempFile := True;
  127. Result := FTempFileStream;
  128. end;
  129. procedure TIdAttachmentFile.SaveToFile(const FileName: String);
  130. begin
  131. if not CopyFileTo(StoredPathname, FileName) then begin
  132. raise EIdException.Create(RSTIdMessageErrorSavingAttachment); // TODO: create a new Exception class for this
  133. end;
  134. end;
  135. initialization
  136. // MtW: Shouldn't be neccessary??
  137. // RegisterClass(TIdAttachmentFile);
  138. end.