{*******************************************************} { } { Responsive Software http://www.responsive.co.nz } { } { Copyright (c) 2003-2006 Responsive Software Limited } { } {*******************************************************} unit AttachmentCacheManagerUnit; interface type TAttachmentCacheManager = class function GlobalFileContentsFileName (AttachmentId : int64) : string; function WorkstationFileContentsFileName (AttachmentId : int64) : string; public function GetFileContents (AttachmentId : int64) : string; procedure SaveFileContents (AttachmentId : int64; FileContents : string); procedure DeleteFileContents (AttachmentId : int64); procedure SaveWorkstationFileContents (AttachmentId : int64; FileContents : string); end; implementation uses Classes, SysUtils, Controls, Forms, GeneralUtilities, Globals; function TAttachmentCacheManager.GlobalFileContentsFileName (AttachmentId : int64) : string; begin Result := AppendBackslash(GlobalConfiguration.AttachmentDirectory) + IntToStr(AttachmentId); end; function TAttachmentCacheManager.WorkstationFileContentsFileName (AttachmentId : int64) : string; begin Result := AppendBackslash(WorkstationConfiguration.AttachmentDirectory) + IntToStr(AttachmentId); end; function TAttachmentCacheManager.GetFileContents (AttachmentId : int64) : string; var WorkstationFileAge : integer; GlobalFileAge : integer; GetGlobalCopy : boolean; Cursor : TCursor; begin GlobalFileAge := 0; if ServerMode then Result := GeneralUtilities.GetFileContents(GlobalFileContentsFileName(AttachmentId)) else begin GetGlobalCopy := true; // check to see if we can use the cached copy if WorkstationConfiguration.CacheAttachments then begin // check to see if there is a copy in the cache and if so check that // the date/time stamp matches the global copy if FileExists(WorkstationFileContentsFileName(AttachmentId)) then begin WorkstationFileAge := FileAge(WorkstationFileContentsFileName(AttachmentId)); if ClientMode then GlobalFileAge := ClientCommunicator.GetAttachmentFileAge(AttachmentId) else GlobalFileAge := FileAge(GlobalFileContentsFileName(AttachmentId)); if WorkstationFileAge = GlobalFileAge then GetGlobalCopy := false; end; end; if GetGlobalCopy then begin if ClientMode then begin Cursor := Screen.Cursor; Screen.Cursor := crHourGlass; try Result := ClientCommunicator.GetAttachmentFileContents(AttachmentId); finally Screen.Cursor := Cursor; end; end else Result := GeneralUtilities.GetFileContents(GlobalFileContentsFileName(AttachmentId)); // save copy in cache if required if WorkstationConfiguration.CacheAttachments then begin if GlobalFileAge = 0 then begin if ClientMode then GlobalFileAge := ClientCommunicator.GetAttachmentFileAge(AttachmentId) else GlobalFileAge := FileAge(GlobalFileContentsFileName(AttachmentId)); end; // save the contents in the cache giving it the same date/time stamp as the global copy GeneralUtilities.SaveFileContents(WorkstationFileContentsFileName(AttachmentId),Result,GlobalFileAge); end; end else Result := GeneralUtilities.GetFileContents(WorkstationFileContentsFileName(AttachmentId)); end; end; procedure TAttachmentCacheManager.SaveWorkstationFileContents (AttachmentId : int64; FileContents : string); var FileAge : integer; begin // this function is called in client mode to make sure cache is updated if not ClientMode then raise Exception.Create('SaveWorkstationFileContents called in non-client mode'); // don't do anything if not caching if not WorkstationConfiguration.CacheAttachments then Exit; // get date/time stamp from global copy FileAge := ClientCommunicator.GetAttachmentFileAge(AttachmentId); GeneralUtilities.SaveFileContents(WorkstationFileContentsFileName(AttachmentId),FileContents,FileAge); end; procedure TAttachmentCacheManager.SaveFileContents (AttachmentId : int64; FileContents : string); var FileAge : integer; begin // this function should never get called in client mode if ClientMode then raise Exception.Create('SaveFileContents called in client mode'); FileAge := DateTimeToFileDate(Now); // save contents {$WARNINGS OFF} FileSetAttr(GlobalFileContentsFileName(AttachmentId),0); GeneralUtilities.SaveFileContents(GlobalFileContentsFileName(AttachmentId),FileContents,FileAge); FileSetAttr(GlobalFileContentsFileName(AttachmentId),faReadOnly); {$WARNINGS ON} // also save in cache if required giving it the same date/time stamp if StandardMode and WorkstationConfiguration.CacheAttachments then GeneralUtilities.SaveFileContents(WorkstationFileContentsFileName(AttachmentId),FileContents,FileAge); end; procedure TAttachmentCacheManager.DeleteFileContents (AttachmentId : int64); begin // this function should never get called in client mode if ClientMode then raise Exception.Create('DeleteFileContents called in client mode'); // delete contents SysUtils.DeleteFile(GlobalFileContentsFileName(AttachmentId)); // delete from cache also if StandardMode and WorkstationConfiguration.CacheAttachments then SysUtils.DeleteFile(WorkstationFileContentsFileName(AttachmentId)); end; end.