Re: FTPServer List of Files Does Not Send to Client. Why ?

Giganews Newsgroups
Subject: Re: FTPServer List of Files Does Not Send to Client. Why ?
Posted by:  Edward (eduglobo20…@globo.com)
Date: Mon, 27 Mar 2006

OK

Gambit Thank you very much i will update the full code in

http://sourceforge.net/projects/builderindyex

Here is the full code.

Just on last think:

On the function:
void __fastcall TForm1::IdFTPServer1MakeDirectory(TIdFTPServerThread
*ASender, AnsiString &VDirectory)

    ASender->Reply->SetReply(521, AnsiQuotedStr(VDirectory, '"') +
"directory already exists");

    Reply is not a member of 'TIdFTPServerThread'

Thanks

Edward

/*
//-------------------------------------------------------------------------------------------------------------------------------------------------------
//
BOF
//-------------------------------------------------------------------------------------------------------------------------------------------------------

  PROGRAM          : FTPSERVER Indy Component 9.0.50 for C++Builder - This
program is a part of Big Bible of Indy.

  License          : GNU General Public License (GPL)
  MainFile          : Unit1.cpp
  Release Date      : 27-03-2006
  Last Updates      :

  Version          : 1.0.0.0
  Programmer        : Edward                          [
eduglobo20…@gmail.com ]
  O.S.              : Windows XP
  Thansk To        : Remy Lebeau (TeamB) - Gambit
  WebSite          :http://sourceforge.net/projects/builderindyex -
BigBible of Indy
  Comment          : Set in your client, the port 10000 and Emulate System
to UNIX

//--------------------------------------------------------------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------------------------------------------------------------
*/
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//-------------------------------------------------------------------------------------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "IdFTPServer"
#pragma resource "*.dfm"
//-------------------------------------------------------------------------------------------------------------------------------------------------------
TForm1 *Form1;
//-------------------------------------------------------------------------------------------------------------------------------------------------------
AnsiString DirCorrente;
//-------------------------------------------------------------------------------------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------
class TRichEditLog : public TIdSync
{
  private:
    AnsiString FStr;
  public:
    __fastcall TRichEditLog(const AnsiString &AStr):TIdSync()
    {
      FStr = TimeToStr(Time()) + ":> " + AStr;
    }
    virtual void __fastcall DoSynchronize()
    {
      Form1->RichEditLogFtpServer->Lines->Add(FStr);
    }
    static void __fastcall Add(const AnsiString &AStr)
    {
      TRichEditLog *Sync = new TRichEditLog(AStr);
      try
      {
        Sync->Synchronize();
      }
      __finally
      {
        delete Sync;
      }
    }
};
//-------------------------------------------------------------------------------------------------------------------------------------------------------
void __fastcall TForm1::ButtonActiveClick(TObject *Sender)
{
  IdFTPServer1->Active = false;
  IdFTPServer1->Bindings->Clear();
  IdFTPServer1->Bindings->Add();
  IdFTPServer1->Bindings->Items[0]->Port = EditPort->Text.ToInt();
  IdFTPServer1->Bindings->Items[0]->IP = EditIP->Text;
  IdFTPServer1->DefaultDataPort = EditDataPort->Text.ToInt();
  IdFTPServer1->DefaultPort = EditPort->Text.ToInt();
  IdFTPServer1->Active = true;
  TRichEditLog::Add(TimeToStr(Time()) + ":> FTP SERVER Active." +
AnsiString(" DefaultPort: ") +  IdFTPServer1->DefaultPort + AnsiString("
DefaultDataPort: ") +  IdFTPServer1->DefaultDataPort);
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------
void __fastcall TForm1::IdFTPServer1AfterUserLogin(TIdFTPServerThread
*ASender)
{
  ASender->HomeDir = DirCorrente + AnsiString("\\");
  ASender->CurrentDir = "/";
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------
void __fastcall TForm1::IdFTPServer1Connect(TIdPeerThread *AThread)
{
  TRichEditLog::Add(TimeToStr(Time()) + ":> Conect");
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------
void __fastcall TForm1::IdFTPServer1Disconnect(TIdPeerThread *AThread)
{
  TRichEditLog::Add(TimeToStr(Time()) + ":> Disconect");
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------
AnsiString __fastcall TForm1::FTPToLocal(const AnsiString &ABase, const
AnsiString &APath)
{
  if( APath != "" )
  {
    AnsiString StrAux = StringReplace(APath, "/", "\\" ,
    TReplaceFlags() << rfReplaceAll );
    StrAux = StringReplace(StrAux, "\\\\", "\\", TReplaceFlags() <<
rfReplaceAll );
    if( StrAux[1] == '\\' )
    {
      StrAux.Delete(1, 1);
      if( (StrAux.Length() > 2) && (StrAux[2] == ':') && (StrAux[3] ==
'\\') )
        return StrAux;
      return ABase + StrAux;
    }
    return StrAux;
  }
  return ABase;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------
void __fastcall TForm1::IdFTPServer1MakeDirectory(TIdFTPServerThread
*ASender, AnsiString &VDirectory)
{
  AnsiString Dir = FTPToLocal(ASender->HomeDir, VDirectory);

  if( DirectoryExists(Dir) )
  {
    TRichEditLog::Add(TimeToStr(Time()) + ":> Dir create : " + Dir);
//    ASender->Reply->SetReply(521, AnsiQuotedStr(VDirectory, '"') +
"directory already exists");
//    ASender->SendReply();
  }

  if( !CreateDir(Dir) )
  {
    TRichEditLog::Add(TimeToStr(Time()) + ":> Error on create Dir: " + Dir);
  //  ASender->Reply->SetReply(550, AnsiQuotedStr(VDirectory, '"') +
"directory not created");
  // ASender->SendReply();
  }
  VDirectory = LocalToFTP(VDirectory,Dir);
  TRichEditLog::Add(TimeToStr(Time()) + ":> Diretório Criado: "
+VDirectory );
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------
void __fastcall TForm1::IdFTPServer1StoreFile(TIdFTPServerThread *ASender,
const AnsiString AFileName, bool AAppend, TStream *&VStream)
{
  AnsiString File = FTPToLocal(ASender->HomeDir, AFileName);
  WORD mode;

  if( AAppend )
    mode = (fmOpenReadWrite | fmShareDenyWrite);
  else
    mode = fmCreate;

  try
  {
    VStream = new TFileStream(File, mode);
    TRichEditLog::Add(TimeToStr(Time()) + ":> File Store :> " + File);
  }
  catch(const Exception &e)
  {
    throw;
  }
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------
void __fastcall TForm1::IdFTPServer1UserLogin(TIdFTPServerThread *ASender,
const AnsiString AUsername, const AnsiString APassword, bool
&AAuthenticated)
{
  if(AUsername == EditUser->Text)
  {
    TRichEditLog::Add(TimeToStr(Time()) + ":> LOG USER OK :> " + AUsername);
    if(APassword == EditPassword->Text)
    {
      AAuthenticated = true;
      TRichEditLog::Add(TimeToStr(Time()) + ":> PASSWORD OK :> " +
APassword);
    }
  }
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
  if(IdFTPServer1->Active == true)
  {
    RadioGroupStatusServerFTP->ItemIndex = 0;
  }
  else
  {
    RadioGroupStatusServerFTP->ItemIndex = 1;
  }
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------
void __fastcall TForm1::IdFTPServer1ListDirectory(TIdFTPServerThread
*ASender, const AnsiString APath, TIdFTPListItems *ADirectoryListing)
{
  TSearchRec sr;
  AnsiString Mask = FTPToLocal(ASender->HomeDir, APath);

  DWORD Attrs = ::GetFileAttributes(Mask.c_str());

  if( (Attrs != 0xFFFFFFFF) && (Attrs & FILE_ATTRIBUTE_DIRECTORY) )
    Mask = IncludeTrailingBackslash(Mask) + "*.*";

  ADirectoryListing->Clear();
  ADirectoryListing->ListFormat = flfUnix;

  if( FindFirst(Mask, faAnyFile, sr) == 0 )
  {
    do
    {
      if( (sr.Name != ".") && (sr.Name != "..") )
      {
        TIdFTPListItem *Item = ADirectoryListing->Add();
        Item->FileName = sr.Name;
        Item->Size = sr.Size;
        Item->ModifiedDate = FileDateToDateTime(sr.Time);
        if( sr.Attr & faDirectory )
          Item->ItemType = ditDirectory;
        else
          Item->ItemType = ditFile;
      }
      TRichEditLog::Add(sr.Name);
    }
    while( FindNext(sr) == 0 );
    FindClose(sr);
  }
  TRichEditLog::Add("List Dir: " + AnsiString(ADirectoryListing->Count) + "
item(s)\
");
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------
void __fastcall TForm1::IdFTPServer1ChangeDirectory(TIdFTPServerThread
*ASender, AnsiString &VDirectory)
{
  AnsiString Dir = FTPToLocal(ASender->HomeDir, VDirectory);

  if( !DirectoryExists(Dir) )
  {
    TRichEditLog::Add(TimeToStr(Time()) + ":> Error on change Dir:> " +
VDirectory);
//    ASender->Reply->SendReply(550, AnsiQuotedStr(VDirectory, '"') + "
directory not found");
//    ASender->SendReply();
    return;
  }

  VDirectory = LocalToFTP(ASender->HomeDir, Dir);
  TRichEditLog::Add(TimeToStr(Time()) + ":> Dir Change :> " + VDirectory);
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------
AnsiString __fastcall TForm1::TrocaStr(AnsiString Str)
{
  AnsiString StrAux, StrAux2;

  StrAux = StringReplace(Str, "/", "\\" , TReplaceFlags() << rfReplaceAll );
  StrAux2 = StringReplace(StrAux, "\\\\", "\\", TReplaceFlags() <<
rfReplaceAll );

  return(StrAux2);
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
  IdFTPServer1->Active = false;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------
void __fastcall TForm1::ButtonDeactiveFTPServerClick(TObject *Sender)
{
  IdFTPServer1->Active = false;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------
AnsiString __fastcall TForm1::LocalToFTP(const AnsiString &ABase, const
AnsiString &APath)
{
  if( APath.SubString(1, ABase.Length()) == ABase )
    return ("/" + StringReplace(APath.SubString(ABase.Length()+1, MaxInt),
"\\", "/" , TReplaceFlags() << rfReplaceAll));

  AnsiString StrAux = APath;

  if( StrAux.Length() > 1 )
  {
    if( StrAux[2] == ':' )
    {
      if( StrAux.Length() == 2 )
        StrAux = IncludeTrailingBackslash(StrAux);
      else if( StrAux[3] != '\\' )
        StrAux.Insert("\\", 3);
      StrAux = ("\\" + StrAux);
    }
  }
  return StringReplace(StrAux, "\\", "/" , TReplaceFlags() <<
rfReplaceAll );
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------
void __fastcall TForm1::FormActivate(TObject *Sender)
{
  DirCorrente = ExtractFilePath(Application->ExeName);
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------------------------------------------------------------
//
EOF
//-------------------------------------------------------------------------------------------------------------------------------------------------------

"Edward" <eduglobo20…@globo.com> wrote in message
news:0EF0B750D2F1E240eduglobo20…@globo.com...
>
> Why the client are not receive the List of files ?
>
> That is my code:
>
> void __fastcall TForm1::IdFTPServer1ListDirectory(TIdFTPServerThread
> *ASender, const AnsiString APath, TIdFTPListItems *ADirectoryListing)
> {
>  TSearchRec sr;
>  int iAttributes = 0, ct=0;
>  String ADirectory;
>  ADirectory = ASender->HomeDir + APath;
>
>  if (FindFirst( AnsiString("*.*"), iAttributes, sr) == 0)
>  {
>    ADirectoryListing->Add();
>    ADirectoryListing->Items[ct]->FileName = sr.Name;
>    ADirectoryListing->Items[ct]->Size = sr.Size;
>    ADirectoryListing->Items[ct]->ModifiedDate =
> FileDateToDateTime(sr.Time);
>    if(sr.Name.SubString(1,1) == "." || sr.Name.SubString(1,2) == "..")
>    {
>      ADirectoryListing->Items[ct]->ItemType + ditDirectory;
>    }
>    else
>    {
>      ADirectoryListing->Items[ct]->ItemType + ditFile;
>    }
>  }
>  while(FindNext(sr) == 0)
>  {
>    ct++;
>    ADirectoryListing->Add();
>    ADirectoryListing->Items[ct]->FileName = sr.Name;
>    ADirectoryListing->Items[ct]->Size = sr.Size;
>    ADirectoryListing->Items[ct]->ModifiedDate =
> FileDateToDateTime(sr.Time);
>    if(sr.Name.SubString(1,1) == "." || sr.Name.SubString(1,2) == "..")
>    {
>      ADirectoryListing->Items[ct]->ItemType + ditDirectory;
>    }
>    else
>    {
>      ADirectoryListing->Items[ct]->ItemType + ditFile;
>    }
>  }
>  FindClose(sr);
> }
>
> Edward

Replies

In response to

FTPServer List of Files Does Not Send to Client. Why ? posted by Edward on Wed, 22 Mar 2006