Newsgroups : Borland : borland.public.delphi.internet.winsock : 2008 Jan : INDY 10 - Enhance TIdFTPServer.DoOnRenameFile

www.cryer.info
Managed Newsgroup Archive

INDY 10 - Enhance TIdFTPServer.DoOnRenameFile

Subject:INDY 10 - Enhance TIdFTPServer.DoOnRenameFile
Posted by:"SoftVision" (softvisi..@softvision.sk)
Date:Thu, 24 Jan 2008 03:46:36

unit IdFTPBaseFileSystem;

TIdFTPBaseFileSystem = class(TIdBaseComponent)
...
public
     procedure RenameFile(AContext : TIdFTPServerContextBase; const
ARenameFromFile, ARenameToFile: string); virtual; abstract;

added ARenameFromFile

then correct >

unit IdFTPServer;
...
procedure TIdFTPServer.DoOnRenameFile(ASender: TIdFTPServerContext;
   const ARenameFromFile, ARenameToFile: string);
begin
   if Assigned(FFTPFileSystem) then begin
     FFTPFileSystem.RenameFile(ASender, ARenameFromFile, ARenameToFile);
   end else if Assigned(FOnRenameFile) then begin
     FOnRenameFile(ASender, ARenameFromFile, ARenameToFile);
   end;
end;

after this modifications you can implement new class that serves local
file system :

   TStdFileSystem = class(TIdFTPBaseFileSystem)
   private
     FRootPath:string;
     procedure SetRootPath(const Value: string);
   protected
     procedure InitComponent; override;
   public
     procedure ChangeDir(AContext : TIdFTPServerContextBase; var
VDirectory: string); override;
     procedure GetFileSize(AContext : TIdFTPServerContextBase; const
AFilename: string; var VFileSize: Int64); override;
     procedure GetFileDate(AContext : TIdFTPServerContextBase; const
AFilename: string; var VFileDate: TDateTime); override;
     procedure ListDirectory(AContext : TIdFTPServerContextBase; const
APath: string; ADirectoryListing: TIdFTPListOutput; const ACmd,
ASwitches : String); override;
     procedure RenameFile(AContext : TIdFTPServerContextBase; const
ARenameFromFile, ARenameToFile: string); override;
     procedure DeleteFile(AContext : TIdFTPServerContextBase; const
APathName: string); override;
     procedure RetrieveFile(AContext : TIdFTPServerContextBase; const
AFileName: string; var VStream: TStream); override;
     procedure StoreFile(AContext : TIdFTPServerContextBase; const
AFileName: string; AAppend: Boolean; var VStream: TStream); override;
     procedure MakeDirectory(AContext : TIdFTPServerContextBase; var
VDirectory: string); override;
     procedure RemoveDirectory(AContext : TIdFTPServerContextBase; var
VDirectory: string); override;
     procedure SetModifiedFileDate(AContext : TIdFTPServerContextBase;
const AFileName: String; var VDateTime: TDateTime); override;
     procedure GetCRCCalcStream(AContext : TIdFTPServerContextBase;
const AFileName: string; var VStream : TStream); override;
     procedure CombineFiles(AContext : TIdFTPServerContextBase; const
ATargetFileName: string; AParts: TStrings); override;
     property RootPath:string read FRootPath write SetRootPath;
   end;

{ TStdFileSystem }

procedure TStdFileSystem.InitComponent;
begin
   inherited InitComponent;
   RootPath:='C:';
end;

procedure TStdFileSystem.SetRootPath(const Value: string);
begin
   FRootPath := PurePath(Value);
end;

procedure TStdFileSystem.CombineFiles(AContext: TIdFTPServerContextBase;
const ATargetFileName: string; AParts: TStrings);
begin
   FileHand.CombineFiles(ATargetFileName, AParts);
end;

procedure TStdFileSystem.DeleteFile(AContext: TIdFTPServerContextBase;
const APathName: string);
begin
   SysUtils.DeleteFile(ReplaceChars(RootPath+APathName));
end;

procedure TStdFileSystem.GetCRCCalcStream(AContext:
TIdFTPServerContextBase; const AFileName: string; var VStream: TStream);
begin
   VStream :=
TFileStream.Create(ReplaceChars(RootPath+AFilename),fmOpenRead);
end;

procedure TStdFileSystem.GetFileDate(AContext: TIdFTPServerContextBase;
const AFilename: string; var VFileDate: TDateTime);
begin
   VFileDate:=FileDateTime(ReplaceChars(RootPath+AFilename));
end;

procedure TStdFileSystem.GetFileSize(AContext: TIdFTPServerContextBase;
const AFilename: string; var VFileSize: Int64);
begin
   VFileSize:=FileGetSize(ReplaceChars(RootPath+AFilename));
end;

procedure TStdFileSystem.ChangeDir(AContext: TIdFTPServerContextBase;
var VDirectory: string);
begin
   AContext.CurrentDir := VDirectory;
end;

procedure TStdFileSystem.ListDirectory(AContext: TIdFTPServerContextBase;
   const APath: string; ADirectoryListing: TIdFTPListOutput; const ACmd,
   ASwitches: String);
var
  LFTPItem :TIdFTPListItem;
  SR : TSearchRec;
  SRI : Integer;
begin
   SRI := FindFirst(RootPath + ReplaceChars(APath) + '\*.*', faAnyFile -
faHidden - faSysFile, SR);
   While SRI = 0 do
   begin
     LFTPItem := ADirectoryListing.Add;
     LFTPItem.FileName := SR.Name;
     LFTPItem.Size := SR.Size;
     LFTPItem.ModifiedDate := FileDateToDateTime(SR.Time);
     if SR.Attr = faDirectory then
      LFTPItem.ItemType   := ditDirectory
     else
      LFTPItem.ItemType   := ditFile;
     SRI := FindNext(SR);
   end;
   FindClose(SR);
   SetCurrentDir(RootPath + ReplaceChars(APath) + '\..');
end;

procedure TStdFileSystem.MakeDirectory(AContext:
TIdFTPServerContextBase; var VDirectory: string);
begin
   if not ForceDirectories(ReplaceChars(RootPath + VDirectory)) then
   begin
     Raise Exception.Create('Unable to create directory');
   end;
end;

procedure
TStdFileSystem.RemoveDirectory(AContext:TIdFTPServerContextBase; var
VDirectory: string);
begin
   RemoveTree(ReplaceChars(RootPath + VDirectory));
end;

procedure TStdFileSystem.RenameFile(AContext: TIdFTPServerContextBase;
const ARenameFromFile, ARenameToFile: string);
begin
   SysUtils.RenameFile(ReplaceChars(RootPath+ARenameFromFile),
ReplaceChars(RootPath+ARenameToFile));
end;

procedure TStdFileSystem.RetrieveFile(AContext: TIdFTPServerContextBase;
   const AFileName: string; var VStream: TStream);
begin
   VStream :=
TFileStream.Create(ReplaceChars(RootPath+AFilename),fmOpenRead);
end;

procedure
TStdFileSystem.SetModifiedFileDate(AContext:TIdFTPServerContextBase;
const AFileName: String; var VDateTime: TDateTime);
begin
   SetFileDateTime(ReplaceChars(RootPath+AFilename), VDateTime);
end;

procedure TStdFileSystem.StoreFile(AContext: TIdFTPServerContextBase;
   const AFileName: string; AAppend: Boolean; var VStream: TStream);
begin
  if not Aappend then
    VStream := TFileStream.Create(ReplaceChars(RootPath+AFilename),fmCreate)
  else
    VStream :=
TFileStream.Create(ReplaceChars(RootPath+AFilename),fmOpenWrite)
end;

Replies:

www.cryer.info
Managed Newsgroup Archive