IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Web & réseau Delphi Discussion :

Tethering ou Client/Serveur


Sujet :

Web & réseau Delphi

  1. #1
    Membre régulier
    Développeur informatique
    Inscrit en
    Décembre 2010
    Messages
    228
    Détails du profil
    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Décembre 2010
    Messages : 228
    Points : 113
    Points
    113
    Par défaut Tethering ou Client/Serveur
    Bonjour,

    Je souhaite transférer des fichiers entre un poste A et un poste B en bidirectionnel. Ces 2 postes sont sous Windows et les applis sont en VCL.

    Pour le Tethering, j'ai trouvé pas mal d'infos sur le forum (echange-donnees-entre-applications-reseau-local;partage-donnees-tethering) et ailleurs (malcolmgroves). J'ai donc commencé à tester cette méthode mais ça ne colle pas totalement à ce que je souhaite réaliser.

    Ma problématique:
    1. Poste A et poste B sont connectés en WiFi sur le même réseau.
    2. Poste A doit envoyer sur poste B des fichiers dans un répertoire (définit par A) A priori c'est bon. Poste A envoi le répertoire de destination à B. Puis on transfère les fichiers binaires propriétaires dont je connais la structure. Je devrais pouvoir les passer en stream.
    3. Poste A doit lister du poste B les fichiers d'un répertoire (définit par A) puis les récupérer. Mon idée: A envoie à B le chemin du répertoire des fichiers, B liste les fichiers de ce répertoire, transfert la liste à A, puis transfère les fichiers à A.
    4. Aucune action utilisateur ne doit être faite sur le poste B. Dans l'idéal, on doit juste lancer un exe; le user ne doit pas cliquer sur des boutons.
    5. Transférer les fichiers sans les ouvrir car ils peuvent être de plusieurs 100 Mo. je ne vois pas ?


    Ça me paraît faisable mais bon...

    L'utilisation d'un client/serveur ne me paraît pas nécessaire (sauf si lepoint 5 n'est pas réalisable) même si je me pose la question.

    Merci pour vos conseils.

  2. #2
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 042
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 67
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 042
    Points : 40 952
    Points
    40 952
    Billets dans le blog
    62
    Par défaut
    Bonjour,

    cela ressemble beaucoup à du FTP (FileZilla Server (ou autre) sur le poste B tournant en service) ?
    Il n'y a que cette histoire de "dans un répertoire défini par A" qui pose souci sauf si c'est un sous-répertoire d'un repertoire racine ftp
    MVP Embarcadero
    Delphi installés : D3,D7,D2010,XE4,XE7,D10 (Rio, Sidney), D11 (Alexandria), D12 (Athènes)
    SGBD : Firebird 2.5, 3, SQLite
    générateurs États : FastReport, Rave, QuickReport
    OS : Window Vista, Windows 10, Windows 11, Ubuntu, Androïd

  3. #3
    Membre régulier
    Développeur informatique
    Inscrit en
    Décembre 2010
    Messages
    228
    Détails du profil
    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Décembre 2010
    Messages : 228
    Points : 113
    Points
    113
    Par défaut
    Pour tests, je me suis lancé dans Tethering.

    Dans une même procédure (ci-dessous), je transfère de A vers B, les noms des fichiers et les fichiers.
    J'ai 5 fichiers png à transférer.
    Le transfert des noms de fichier est OK: B reçoit 5 noms de fichiers différents.
    Mais lors du transfert des images de A vers B, seule la dernière image de la liste est copiée et autant de fois que le nombre total de fichiers de A (= 5 fois).

    Poste A - envoi du nom et du fichier
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    procedure TForm1.Button3Click(Sender: TObject);
    ...
    begin
      Ret := FindFirst (Edit1.Text + '\*.png', faAnyFile, SR);
      Try
        // Tiles pour les routes
        While (Ret = 0) do
        Begin
          // Transfert du nom de l'image
          TetheringAppProfile1.Resources.FindByName('SomePngFileName').Value := SR.Name;
     
          // Transfert de l'image
          APNGImage := TPNGImage.Create;
          try
            APNGImage.LoadFromFile(Edit1.Text + '\' + SR.Name);
            Lstream := TMemoryStream.Create;
            try
              APNGImage.SaveToStream(LStream);
              LStream.Position := 0;
              TetheringAppProfile1.Resources.FindByName('SomePng').Value := LStream;
            finally
              LStream.Free ;
            end;
          finally
            APNGImage.Free ;
          end;
          Ret := FindNext(SR);
        end ;
      Finally
        FindClose(SR);
      End;
    end;
    Poste B - reçoit et enregistre le png
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    procedure TForm1.TetheringAppProfile1Resources3ResourceReceived(
      const Sender: TObject; const AResource: TRemoteResource);
    var APNGImage: TPNGImage ;
    begin
      Aresource.Value.AsStream.Position := 0;
     
      // Transfert de l'image
      APNGImage := TPNGImage.Create;
      try
        APNGImage.LoadFromStream(Aresource.Value.AsStream) ;
        APNGImage.SaveToFile(Edit2.Text + 'test_' + cnt.ToString + '.png') ;
        inc(cnt) ;
      finally
        APNGImage.Free ;
      end;
    end;



    Citation Envoyé par SergioMaster Voir le message
    Bonjour,

    cela ressemble beaucoup à du FTP (FileZilla Server (ou autre) sur le poste B tournant en service) ?
    Il n'y a que cette histoire de "dans un répertoire défini par A" qui pose souci sauf si c'est un sous-répertoire d'un repertoire racine ftp
    Merci SergioMaster pour ton message.
    Oui, A définit un sous-répertoire du répertoire racine ftp de B.
    Tu me conseilles d'utiliser FileZilla (par ex) et d'y connecter mon application ?

  4. #4
    Expert éminent sénior
    Avatar de Paul TOTH
    Homme Profil pro
    Freelance
    Inscrit en
    Novembre 2002
    Messages
    8 964
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Freelance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2002
    Messages : 8 964
    Points : 28 445
    Points
    28 445
    Par défaut
    alors 2 choses

    1) Tethering apporte essentiellement la possibilité aux deux postes de se détecter l'un l'autre automatiquement (via broadcast UDP)...donc si A et B connaissent leurs adresses respectives, ce n'est pas forcément nécessaire.

    2) Tethering peut aussi être utilise juste pour la détection...une fois les adresses connues, on peut basculer sur un mode TCP/IP classique

    TCP/IP c'est essentiellement une connexion d'un client vers un serveur (dont il doit connaître l'adresse IP), puis des échanges via les fonctions send() et recv()

    les différents protocoles, FTP par exemple, permettent simplement de définir le dialogue effectué dans send() et recv()...et en effet B pourrait être serveur FTP et A client FTP, pour autant que je comprenne la demande, vu que c'est justement un protocole de transfert de fichiers et de liste de fichiers....en plus ça permet de tester facilement le serveur avec un client FTP quelconque.
    Developpez.com: Mes articles, forum FlashPascal
    Entreprise: Execute SARL
    Le Store Excute Store

  5. #5
    Expert éminent sénior
    Avatar de ShaiLeTroll
    Homme Profil pro
    Développeur C++\Delphi
    Inscrit en
    Juillet 2006
    Messages
    13 459
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Développeur C++\Delphi
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2006
    Messages : 13 459
    Points : 24 873
    Points
    24 873
    Par défaut
    Effectivement soit un FileZilla Server qui va transformer le poste en Server FTP tout simplement
    Mais implémenter un TIdFTPServer n'est pas difficile non plus.
    Ce sont des approches orientées fichier, le Protocole FTP est fait pour.


    Sinon, tu devrais donner un nom différent à chaque ressource, on pourrait penser que tu écrases le contenu du fichier par le suivant trop vite, le client n'ayant pas le temps de lire.

    Përso, j'ai fait cela il y a très longtemps en simple TCP\IP tient un code très grossier pour envoyer un BITMAP compressé en JPEG ... je sais que j'ai écrit sur le forum une version multi-fichier, un principe très simple :
    - Header contenant, taille header, taille de l'image, taille du nom de l'image, nom de l'image
    - Body, le fichier
    - Footer, juste un FE FF

    Tu peux combiner les technologies, le FTP pour la partie transfert de fichier (nom+contenu) mais du Tethering pour la synchronisation entre les applications, plutôt comme Monitoring et Déclencheur des taches.
    Il te faut juste bien choisir le Serveur, dans ton cas, c'est B qui expose des fichiers, A n'est qu'un client qui dépose et ponctionne, tout cela étant basique en FTP

    B contenant une implémentation des évènements du TIdFTPServer, tu pourras le tester avec un simple BAT et la commande FTP mais aussi avec un Navigateur Web en FTP:\\
    Ensuite A contiendra un TIdFTPClient


    Le Serveur
    A la base, je devais avoir un serveur FTP autonome fourni par l'admin réseau, quand je lui annoncé la fréquence de scan de fichier et le nombre de fichier par heure, il m'a dit ... euh ça va pas le faire.
    Du coup, j'ai intégré au cas où un serveur FTP interne, et il tourne depuis 2015, je pense que le million de fichier échangé a été franchi et ListDirectoryEventHandler a été lancé toutes les secondes tous les joursn 24h/24 depuis.


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    TxxxSpareFTPServer = class(TObject)
      private
        const
          PATH_DELIMITER_FTP = '/';
          PATH_DELIMITER_WIN = '\';
        type
          TParsableFileStream = class(TFileStream)
          strict private
            FServer: TxxxSpareFTPServer;
          public
            constructor Create(AServer: TxxxSpareFTPServer; const AFileName: string; Mode: Word);
            destructor Destroy(); override;
          end;
      strict private
        FFTPServer: TIdFTPServer;
        FLocalRootDirectory: TFileName;
        FOnPotentiallyParsableFile: TNotifyEvent;
        FOnError: TxxxSpareFTPServerErrorEvent;
        // Méthodes - Gestionnaires d'évènements
        procedure UserLoginEventHandler(ASender: TIdFTPServerContext; const AUsername, APassword: string; var AAuthenticated: Boolean);
        procedure AfterUserLoginEventHandler(ASender: TIdFTPServerContext);
        procedure ChangeDirectoryEventHandler(ASender: TIdFTPServerContext; var VDirectory: TIdFTPFileName);
        procedure ListDirectoryEventHandler(ASender: TIdFTPServerContext; const APath: TIdFTPFileName; ADirectoryListing: TIdFTPListOutput; const ACmd: string; const ASwitches: string);
        procedure MakeDirectoryEventHandler(ASender: TIdFTPServerContext; var VDirectory: TIdFTPFileName);
        procedure RemoveDirectoryEventHandler(ASender: TIdFTPServerContext; var VDirectory: TIdFTPFileName);
        procedure DeleteFileEventHandler(ASender: TIdFTPServerContext; const APathName: TIdFTPFileName);
        procedure GetFileSizeEventHandler(ASender: TIdFTPServerContext; const AFilename: TIdFTPFileName; var VFileSize: Int64);
        procedure RenameFileEventHandler(ASender: TIdFTPServerContext; const ARenameFromFile, ARenameToFile: TIdFTPFileName);
        procedure RetrieveFileEventHandler(ASender: TIdFTPServerContext; const AFileName: TIdFTPFileName; var VStream: TStream);
        procedure StoreFileEventHandler(ASender: TIdFTPServerContext; const AFileName: TIdFTPFileName; AAppend: Boolean; var VStream: TStream);
      private
        // Méthodes - Déclencher d'évènements
        procedure DoPotentiallyParsableFile(const AFileName: TFileName);
        procedure DoError(const AFileName: TFileName; const AErrorMsg: string);
      public
        // Constructeurs
        constructor Create();
        destructor Destroy(); override;
     
        // Méthodes
        procedure Open();
        procedure Close();
        function ForceDirectories(const ADirectory: TIdFTPFileName): Boolean;
     
        // Propriétés
        property OnPotentiallyParsableFile: TNotifyEvent read FOnPotentiallyParsableFile write FOnPotentiallyParsableFile;
        property OnError: TxxxSpareFTPServerErrorEvent read FOnError write FOnError;
      public
        // Méthodes Diverses
        class function GetLocalRootDirectory(): TFileName;
        class function ChangeToWindowsPathDelimiter(const ADirectory: TIdFTPFileName): TFileName;
        class function ChangeToFTPPathDelimiter(const ADirectory: TIdFTPFileName): TFileName;
        class function ExpandWindowsPath(const ARootDirectory, ACurrentDirectory, ADirectoryOrFileName: TFileName): TFileName;
        class function IsSpareHost(const AHost: string): Boolean;
      end;
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
     
    { TxxxSpareFTPServer }
     
    //------------------------------------------------------------------------------
    procedure TxxxSpareFTPServer.AfterUserLoginEventHandler(ASender: TIdFTPServerContext);
    begin
      ASender.HomeDir := PATH_DELIMITER_FTP;
      ASender.CurrentDir := PATH_DELIMITER_FTP;
    end;
     
    //------------------------------------------------------------------------------
    procedure TxxxSpareFTPServer.ChangeDirectoryEventHandler(ASender: TIdFTPServerContext; var VDirectory: TIdFTPFileName);
    var
      WinCurrentDir, WinNewDir, WinNewCompleteDir: TFileName;
    begin
      try
        if not StartsStr('..', VDirectory) then
        begin
          WinCurrentDir := ChangeToWindowsPathDelimiter(ASender.CurrentDir);
          WinNewDir := ChangeToWindowsPathDelimiter(VDirectory);
          WinNewCompleteDir := ExpandWindowsPath(FLocalRootDirectory, WinCurrentDir, WinNewDir);
          if not DirectoryExists(WinNewCompleteDir) then
            raise ExxxSpareFTPServerException.Create(Format('%s: Failed to change directory', [VDirectory]));
        end
        else
        begin
          WinCurrentDir := ChangeToWindowsPathDelimiter(ASender.CurrentDir);
          WinCurrentDir := ExtractFileDir(WinCurrentDir);
          ASender.CurrentDir := '';
          VDirectory := ChangeToFTPPathDelimiter(WinCurrentDir);
          WinNewCompleteDir := ExpandWindowsPath(FLocalRootDirectory, '', WinCurrentDir);
        end;
     
        if FileExists(IncludeTrailingPathDelimiter(WinNewCompleteDir) + ParamStr(0)) then
          raise ExxxSpareFTPServerException.Create(Format('%s: Not allowed to change directory', [VDirectory]));
     
      except
        on E: Exception do
        begin
          DoError(VDirectory, E.Message);
          raise;
        end;
      end;
    end;
     
    //------------------------------------------------------------------------------
    class function TxxxSpareFTPServer.ChangeToFTPPathDelimiter(const ADirectory: TIdFTPFileName): TFileName;
    begin
      Result := TStringSLTHelper.CharReplace(ADirectory, PATH_DELIMITER_WIN, PATH_DELIMITER_FTP);
    end;
     
    //------------------------------------------------------------------------------
    class function TxxxSpareFTPServer.ChangeToWindowsPathDelimiter(const ADirectory: TIdFTPFileName): TFileName;
    begin
      Result := TStringSLTHelper.CharReplace(ADirectory, PATH_DELIMITER_FTP, PATH_DELIMITER_WIN);
    end;
     
    //------------------------------------------------------------------------------
    procedure TxxxSpareFTPServer.Close();
    begin
      try
        // Fermeture
        FFTPServer.Active := False;
      except
        on E: EIdConnClosedGracefully do
          OutputDebugString(PChar('TxxxSpareFTPServer.Close: ' + E.Message));
        on E: Exception do
          raise ExxxSpareFTPServerException.CreateFmt('Impossible d''arrêter le serveur FTP : %s', [E.Message]);
      end;
    end;
     
    //------------------------------------------------------------------------------
    constructor TxxxSpareFTPServer.Create();
    begin
      inherited Create();
     
      FFTPServer := TIdFTPServer.Create(nil);
      FFTPServer.OnUserLogin := UserLoginEventHandler;
      FFTPServer.OnAfterUserLogin := AfterUserLoginEventHandler;
      FFTPServer.OnChangeDirectory := ChangeDirectoryEventHandler;
      FFTPServer.OnListDirectory := ListDirectoryEventHandler;
      FFTPServer.OnMakeDirectory := MakeDirectoryEventHandler;
      FFTPServer.OnRemoveDirectory := RemoveDirectoryEventHandler;
      FFTPServer.OnDeleteFile := DeleteFileEventHandler;
      FFTPServer.OnGetFileSize := GetFileSizeEventHandler;
      FFTPServer.OnRenameFile := RenameFileEventHandler;
      FFTPServer.OnRetrieveFile := RetrieveFileEventHandler;
      FFTPServer.OnStoreFile := StoreFileEventHandler;
    end;
     
    //------------------------------------------------------------------------------
    procedure TxxxSpareFTPServer.DeleteFileEventHandler(ASender: TIdFTPServerContext; const APathName: TIdFTPFileName);
    var
      WinCurrentDir, WinPathName, WinCompleteFileName: TFileName;
    begin
      try
        WinCurrentDir := ChangeToWindowsPathDelimiter(ASender.CurrentDir);
        WinPathName := ChangeToWindowsPathDelimiter(APathName);
        WinCompleteFileName := ExpandWindowsPath(FLocalRootDirectory, WinCurrentDir, WinPathName);
        if not FileExists(WinCompleteFileName) or not DeleteFile(PChar(WinCompleteFileName)) then
          raise ExxxSpareFTPServerException.CreateFmt('%s: Failed to delete', [APathName]);
      except
        on E: Exception do
        begin
          DoError(APathName, E.Message);
          raise;
        end;
      end;
    end;
     
    //------------------------------------------------------------------------------
    destructor TxxxSpareFTPServer.Destroy();
    begin
      FreeAndNil(FFTPServer);
     
      inherited Destroy();
    end;
     
    //------------------------------------------------------------------------------
    procedure TxxxSpareFTPServer.DoError(const AFileName: TFileName; const AErrorMsg: string);
    begin
      try
        if Assigned(FOnError) then
          FOnError(Self, AFileName, AErrorMsg);
      except
        on E: Exception do
          OutputDebugString(PChar('TxxxSpareFTPServer.DoError: ' + E.Message));
      end;
    end;
     
    //------------------------------------------------------------------------------
    procedure TxxxSpareFTPServer.DoPotentiallyParsableFile(const AFileName: TFileName);
    var
      fn: TFileName;
    begin
      try
        if Assigned(FOnPotentiallyParsableFile) then
        begin
          fn := ExtractFileName(AFileName);
          if SameText(ExtractFileExt(fn), '.dat') then
            if StartsText('ACK_', fn) or StartsText('Cnt_', fn) or StartsText('Mqt_', fn)  then
              FOnPotentiallyParsableFile(Self);
        end;
      except
        on E: Exception do
          OutputDebugString(PChar('TxxxSpareFTPServer.DoPotentiallyParsableFile: ' + E.Message));
      end;
    end;
     
    //------------------------------------------------------------------------------
    class function TxxxSpareFTPServer.ExpandWindowsPath(const ARootDirectory, ACurrentDirectory, ADirectoryOrFileName: TFileName): TFileName;
     
      function ExcludeFirstPathDelimiter(const S: string): string;
      begin
        if not IsPathDelimiter(Result, 1) then
          Result := Copy(S, 2, Length(S) - 1)
        else
          Result := S;
      end;
     
    begin
      // Chemin complet commençant par \
      if StartsStr(PATH_DELIMITER_WIN, ADirectoryOrFileName) then
      begin
        Result := ExcludeTrailingPathDelimiter(ARootDirectory) + ADirectoryOrFileName;
      end
      else
      begin
        // Répertoire Parent
        if StartsStr('..', ADirectoryOrFileName) then
          Result := IncludeTrailingPathDelimiter(IncludeTrailingPathDelimiter(ARootDirectory) + ExcludeFirstPathDelimiter(ACurrentDirectory)) + ADirectoryOrFileName
        // Répertoire Courant
        else if StartsStr('.', ADirectoryOrFileName) then
          Result := IncludeTrailingPathDelimiter(ARootDirectory) + ExcludeFirstPathDelimiter(ACurrentDirectory)
        // Un fichier avec une extension
        else if ContainsStr(ADirectoryOrFileName, '.') then
          Result := IncludeTrailingPathDelimiter(IncludeTrailingPathDelimiter(ARootDirectory) + ExcludeFirstPathDelimiter(ACurrentDirectory)) + ADirectoryOrFileName
        else
          raise ExxxSpareFTPServerException.CreateFmt('%s: Failed to expand invalid name', [ADirectoryOrFileName]);
      end;
    end;
     
    //------------------------------------------------------------------------------
    function TxxxSpareFTPServer.ForceDirectories(const ADirectory: TIdFTPFileName): Boolean;
    var
      WinNewPath: TFileName;
    begin
      WinNewPath := ChangeToWindowsPathDelimiter(ADirectory);
      Result := System.IOUtils.TFile.ForceDirectories(IncludeTrailingPathDelimiter(FLocalRootDirectory) + WinNewPath);
    end;
     
    //------------------------------------------------------------------------------
    procedure TxxxSpareFTPServer.GetFileSizeEventHandler(ASender: TIdFTPServerContext; const AFilename: TIdFTPFileName; var VFileSize: Int64);
    var
      WinCurrentDir, WinFilename, WinCompleteFileName: TFileName;
      sr: TSearchRec;
    begin
      try
        WinFilename := ChangeToWindowsPathDelimiter(ASender.CurrentDir);
        WinFilename := ChangeToWindowsPathDelimiter(AFilename);
        WinCompleteFileName := ExpandWindowsPath(FLocalRootDirectory, WinCurrentDir, WinFilename);
     
        if System.SysUtils.FindFirst(WinCompleteFileName, faNormal, sr) = 0 then
        begin
          VFileSize := sr.Size;
          System.SysUtils.FindClose(sr);
        end
        else
          raise ExxxSpareFTPServerException.CreateFmt('%s: Failed to retrieve the size of file', [AFilename]);
      except
        on E: Exception do
        begin
          DoError(AFilename, E.Message);
          raise;
        end;
      end;
    end;
     
    //------------------------------------------------------------------------------
    class function TxxxSpareFTPServer.GetLocalRootDirectory(): TFileName;
    begin
      Result := ExtractFilePath(ParamStr(0)) + 'SpareFTPServer';
    end;
     
    //------------------------------------------------------------------------------
    class function TxxxSpareFTPServer.IsSpareHost(const AHost: string): Boolean;
    begin
      Result := SameText(AHost, 'localhost') or SameText(AHost, TAutomateLogistique.ModuleTool.GetWindowsComputer());
    end;
     
    //------------------------------------------------------------------------------
    procedure TxxxSpareFTPServer.ListDirectoryEventHandler(ASender: TIdFTPServerContext; const APath: TIdFTPFileName; ADirectoryListing: TIdFTPListOutput; const ACmd, ASwitches: string);
     
      procedure AddItemToListOutput(const AItemName: TFileName; AItemType: TIdDirItemType; AItemSize: Int64; AItemDate: TDateTime);
      begin
        with ADirectoryListing.Add() do
        begin
          ItemType := AItemType;
          FileName := AItemName;
          Size := AItemSize;
          ModifiedDate := AItemDate;
          // Simule les même droits que sur "ftp.xxxxxxxxxxx.com" pour l'utilisateur "xxx" !
          OwnerName := ASender.Username;
          GroupName := 'all';
          if ItemType = ditDirectory then
          begin
            UnixOwnerPermissions := 'rwx';
            UnixGroupPermissions := 'r-x';
            UnixOtherPermissions := 'r-x';
          end
          else
          begin
            UnixOwnerPermissions := 'rw-';
            UnixGroupPermissions := 'r--';
            UnixOtherPermissions := 'r--';
          end;
        end;
      end;
     
      procedure EnumFiles(const ALocalDir: TFileName; const AFilter: TFileName);
      var
        sr: TSearchRec;
      begin
        if System.SysUtils.FindFirst(IncludeTrailingPathDelimiter(ALocalDir) + AFilter, faNormal, sr) = 0 then
        begin
          try
            repeat
              AddItemToListOutput(sr.Name, ditFile, sr.Size, sr.TimeStamp);
            until FindNext(sr) <> 0;
          finally
            System.SysUtils.FindClose(sr);
          end;
        end;
      end;
     
      procedure EnumDirectories(const ALocalDir: TFileName);
      var
        Filter: string;
        sr: TSearchRec;
      begin
        Filter := IncludeTrailingPathDelimiter(ALocalDir) + '*';
        if System.SysUtils.FindFirst(Filter, faDirectory, sr) = 0 then
        begin
          try
            repeat
              if ((sr.Attr and faDirectory) = faDirectory) and (sr.Name <> '.') and (sr.Name <> '..') then
              begin
                AddItemToListOutput(sr.Name, ditDirectory, sr.Size, sr.TimeStamp);
              end;
     
            until System.SysUtils.FindNext(sr) <> 0;
          finally
            System.SysUtils.FindClose(sr);
          end;
        end;
      end;
     
    var
      WinCurrentDir, WinNewDir, WinNewCompleteDir: TFileName;
      ld: Integer;
      Filter: TFileName;
    begin
      try
        WinCurrentDir := ChangeToWindowsPathDelimiter(ASender.CurrentDir);
        if ContainsStr(APath, '*') then
        begin
          if APath[Length(APath)] = PATH_DELIMITER_FTP then
            WinNewDir := Copy(APath, 1, Length(APath) - 1)
          else
            WinNewDir := APath;
     
          ld := LastDelimiter(PATH_DELIMITER_FTP, WinNewDir);
          Filter := Copy(WinNewDir, ld + 1, MaxInt);
          WinNewDir := ChangeToWindowsPathDelimiter(Copy(WinNewDir, 1, ld));
        end
        else
          WinNewDir := ChangeToWindowsPathDelimiter(APath);
     
        WinNewCompleteDir := ExpandWindowsPath(FLocalRootDirectory, WinCurrentDir, WinNewDir);
        if DirectoryExists(WinNewCompleteDir) then
        begin
          if Filter = '' then
          begin
            // Les Fichiers !
            // Toutes les extensions pour les voir via FileZilla même ce n'est ni des .dat et ni des .tmp
            EnumFiles(WinNewCompleteDir, '*.*');
            // Les Dossiers !
            EnumDirectories(WinNewCompleteDir);
          end
          else
            EnumFiles(WinNewCompleteDir, Filter);
        end
        else
          raise ExxxSpareFTPServerException.CreateFmt('%s: Failed to retrieve the contents of the folder', [APath]);
      except
        on E: Exception do
        begin
          DoError(APath, E.Message);
          raise;
        end;
      end;
    end;
     
     
    //------------------------------------------------------------------------------
    procedure TxxxSpareFTPServer.MakeDirectoryEventHandler(ASender: TIdFTPServerContext; var VDirectory: TIdFTPFileName);
    var
      WinCurrentDir, WinNewDir, WinNewCompleteDir: TFileName;
    begin
        try
        WinCurrentDir := ChangeToWindowsPathDelimiter(ASender.CurrentDir);
        WinNewDir := ChangeToWindowsPathDelimiter(VDirectory);
        WinNewCompleteDir := ExpandWindowsPath(FLocalRootDirectory, WinCurrentDir, WinNewDir);
     
        if not System.IOUtils.TFile.ForceDirectories(WinNewCompleteDir) then
          raise ExxxSpareFTPServerException.Create(Format('%s: Failed to make directory', [VDirectory]));
      except
        on E: Exception do
        begin
          DoError(VDirectory, E.Message);
          raise;
        end;
      end;
    end;
     
    //------------------------------------------------------------------------------
    procedure TxxxSpareFTPServer.Open();
    begin
      try
        // Création de l'emplacement de stockage des fichiers
        FLocalRootDirectory := GetLocalRootDirectory();
     
        if System.IOUtils.TFile.ForceDirectories(FLocalRootDirectory) then
        begin
          // Ouverture (Ecoute des connexions)
          if FFTPServer.Bindings.Count = 0 then
          begin
            with FFTPServer.Bindings.Add() do
            begin
              // Ajouter le Bindings en 0.0.0.0 refuse la Connexion en "[::1]:21..."
              // Cela provoque sous FileZilla : Échec de la tentative de connexion avec "ECONNREFUSED - Connection refused by server", essai de l'adresse suivante.
              // Cela utilise ensuite l'IP réelle ou l'IP 127.0.0.1 ce qui évite une erreur sur la commande EPSV : "501 Impossible de lier le socket. L'adresse et le port sont déjà en cours d'utilisation."
              // D'ailleurs, cela change le choix de FileZilla dans ces commandes car il execute plutôt un PASV (Passive Mode) et non un EPSV (Extended Passive Mode)
              // Si le serveur et le client sont sur le même poste, sans le Bindings, je ne peux ouvrir qu'un client Indy mais pas client FileZilla
              // Sur FileZilla, cela se produit lors de la commande EPSV ce qui impacte la commande EPRT
              // Sur InternetExplorer, cela ne se connecte pas du tout
              // Si les clients (même plusieurs) sont lancés depuis un autre poste, il n'y a pas de problème !
              // Avec un client Delphi Indy, je peux sans problème me connecter et envoyer un fichier même sans Bindings !
              // Par précaution, je force une IP explicite
              IP := '0.0.0.0';
              Port := 21;
            end;
          end;
     
          FFTPServer.Active := True;
        end
        else
          raise Exception.Create('Dossier racine inaccessible');
      except
        on E: Exception do
          raise ExxxSpareFTPServerException.CreateFmt('Impossible de démarrer le serveur FTP : %s', [E.Message]);
      end;
    end;
     
    //------------------------------------------------------------------------------
    procedure TxxxSpareFTPServer.RemoveDirectoryEventHandler(ASender: TIdFTPServerContext; var VDirectory: TIdFTPFileName);
    var
      WinCurrentDir, WinNewDir, WinNewCompleteDir: TFileName;
    begin
      try
        WinCurrentDir := ChangeToWindowsPathDelimiter(ASender.CurrentDir);
        WinNewDir := ChangeToWindowsPathDelimiter(VDirectory);
        WinNewCompleteDir := ExpandWindowsPath(FLocalRootDirectory, WinCurrentDir, WinNewDir);
     
        if DirectoryExists(WinNewCompleteDir) then
        begin
          if not RemoveDirectory(PChar(WinNewCompleteDir)) then
            raise ExxxSpareFTPServerException.Create(Format('%s: Failed to remove directory', [VDirectory]));
        end
        else
          raise ExxxSpareFTPServerException.Create(Format('%s: Failed to remove inexisting directory', [VDirectory]));
      except
        on E: Exception do
        begin
          DoError(VDirectory, E.Message);
          raise;
        end;
      end;
    end;
     
    //------------------------------------------------------------------------------
    procedure TxxxSpareFTPServer.RenameFileEventHandler(ASender: TIdFTPServerContext; const ARenameFromFile, ARenameToFile: TIdFTPFileName);
    var
      WinCurrentPath, WinRenameFromFile, WinCompletePathFrom, WinRenameToFile, WinCompletePathTo: TFileName;
    begin
      try
        WinCurrentPath := ChangeToWindowsPathDelimiter(ASender.CurrentDir);
        WinRenameFromFile := ChangeToWindowsPathDelimiter(ARenameFromFile);
        WinCompletePathFrom := ExpandWindowsPath(FLocalRootDirectory, WinCurrentPath, WinRenameFromFile);
        WinRenameToFile := ChangeToWindowsPathDelimiter(ARenameToFile);
        WinCompletePathTo := ExpandWindowsPath(FLocalRootDirectory, WinCurrentPath, WinRenameToFile);
     
        if MoveFileEx(PChar(WinCompletePathFrom), PChar(WinCompletePathTo), MOVEFILE_COPY_ALLOWED or MOVEFILE_REPLACE_EXISTING) then
          DoPotentiallyParsableFile(WinCompletePathTo)
        else
          raise ExxxSpareFTPServerException.CreateFmt('%s: Failed to rename file to %s', [ARenameFromFile, ARenameToFile]);
      except
        on E: Exception do
        begin
          DoError(ARenameFromFile + ' -> ' + ARenameToFile, E.Message);
          raise;
        end;
      end;
    end;
     
    //------------------------------------------------------------------------------
    procedure TxxxSpareFTPServer.RetrieveFileEventHandler(ASender: TIdFTPServerContext; const AFileName: TIdFTPFileName; var VStream: TStream);
    var
      WinCurrentDir, WinFileName, WinCompleteFileName: TFileName;
    begin
      try
        WinCurrentDir := ChangeToWindowsPathDelimiter(ASender.CurrentDir);
        WinFileName := ChangeToWindowsPathDelimiter(AFileName);
        WinCompleteFileName := ExpandWindowsPath(FLocalRootDirectory, WinCurrentDir, WinFileName);
        if FileExists(WinCompleteFileName) then
          VStream := TFileStream.create(WinCompleteFileName, fmOpenRead or fmShareDenyWrite) // Libéré par Indy !
        else
          raise ExxxSpareFTPServerException.CreateFmt('%s: Failed to retrieve file', [AFilename]);
      except
        on E: Exception do
        begin
          DoError(AFileName, E.Message);
          raise;
        end;
      end;
    end;
     
    //------------------------------------------------------------------------------
    procedure TxxxSpareFTPServer.StoreFileEventHandler(ASender: TIdFTPServerContext; const AFileName: TIdFTPFileName; AAppend: Boolean; var VStream: TStream);
    var
      WinCurrentDir, WinFileName, WinCompleteFileName: TFileName;
    begin
      try
        WinCurrentDir := ChangeToWindowsPathDelimiter(ASender.CurrentDir);
        WinFileName := ChangeToWindowsPathDelimiter(AFileName);
        WinCompleteFileName := ExpandWindowsPath(FLocalRootDirectory, WinCurrentDir, WinFileName);
        if DirectoryExists(ExtractFilePath(WinCompleteFileName)) then
        begin
          if AAppend then
          begin
            VStream := TParsableFileStream.Create(Self, WinCompleteFileName, fmOpenWrite or fmShareExclusive);
            VStream.Seek(0, soFromEnd);
          end
          else
            VStream := TParsableFileStream.Create(Self, WinCompleteFileName, fmCreate or fmShareExclusive);
        end
        else
          raise ExxxSpareFTPServerException.CreateFmt('%s: Failed to store file', [AFilename]);
      except
        on E: Exception do
        begin
          DoError(AFileName, E.Message);
          raise;
        end;
      end;
    end;
     
    //------------------------------------------------------------------------------
    procedure TxxxSpareFTPServer.UserLoginEventHandler(ASender: TIdFTPServerContext; const AUsername, APassword: string; var AAuthenticated: Boolean);
    begin
      AAuthenticated := SameText(AUsername, '???') and SameStr(APassword, '???'); // un petit travail ici
    end;

    Tient, une base de travail pour le Client

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    //------------------------------------------------------------------------------
    (*                SoLuTions is an Versatile Library for Delphi                 -
     *                                                                             -
     *  Copyright "SLT Solutions", (©2006)                                         -
     *  contributeur : ShaiLeTroll (2006) - Migration TNMFTP vers TIdFTP lors du passge de Delphi 5 à Delphi 7
     *  contributeur : ShaiLeTroll (2006) - Séparation FTP et SFTP, pour supprimer la dépendance à SecureBlackBox et ne conserver que IdFTP fourni avec Delphi 7
     *  contributeur : ShaiLeTroll (2012) - Renommage Fichier et Correction XE2    -
     *  contributeur : ShaiLeTroll (2012) - Documentation Insight                  -
     *                                                                             -
     *                                                                             -
     * Ce logiciel est un programme informatique servant à aider les développeurs  -
     * Delphi avec une bibliothèque polyvalente, adaptable et fragmentable.        -
     *                                                                             -
     * Ce logiciel est régi par la licence CeCILL-C soumise au droit français et   -
     * respectant les principes de diffusion des logiciels libres. Vous pouvez     -
     * utiliser, modifier et/ou redistribuer ce programme sous les conditions      -
     * de la licence CeCILL-C telle que diffusée par le CEA, le CNRS et l'INRIA    -
     * sur le site "http://www.cecill.info".                                       -
     *                                                                             -
     * En contrepartie de l'accessibilité au code source et des droits de copie,   -
     * de modification et de redistribution accordés par cette licence, il n'est   -
     * offert aux utilisateurs qu'une garantie limitée.  Pour les mêmes raisons,   -
     * seule une responsabilité restreinte pèse sur l'auteur du programme,  le     -
     * titulaire des droits patrimoniaux et les concédants successifs.             -
     *                                                                             -
     * A cet égard  l'attention de l'utilisateur est attirée sur les risques       -
     * associés au chargement,  à l'utilisation,  à la modification et/ou au       -
     * développement et à la reproduction du logiciel par l'utilisateur étant      -
     * donné sa spécificité de logiciel libre, qui peut le rendre complexe à       -
     * manipuler et qui le réserve donc à des développeurs et des professionnels   -
     * avertis possédant  des  connaissances  informatiques approfondies.  Les     -
     * utilisateurs sont donc invités à charger  et  tester  l'adéquation  du      -
     * logiciel à leurs besoins dans des conditions permettant d'assurer la        -
     * sécurité de leurs systèmes et ou de leurs données et, plus généralement,    -
     * à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.          -
     *                                                                             -
     * Le fait que vous puissiez accéder à cet en-tête signifie que vous avez      -
     * pris connaissance de la licence CeCILL-C, et que vous en avez accepté les   -
     * termes.                                                                     -
     *                                                                             -
     *----------------------------------------------------------------------------*)
    unit SLT.Common.FTP;
     
    interface
     
    {*$DEFINE DEBUG_SLT_FTP*}
     
    uses System.Classes, System.SysUtils,
      IdFTP, IdReplyRFC;
     
    type
      { Forward class declarations }
      TSLTFTP = class;
      TSLTFTPConnectionInfo = class;
     
      { class declarations }
     
      /// <summary>Encapsule une connection FTP avec un emplacement de travail prédéfini</summary>
      TSLTFTP = class(TObject)
      private
        // Membres privés
        FConnection: TIdFTP;
        FConnectionInfo: TSLTFTPConnectionInfo;
        FFiles: TStrings;
        FFileFilter: string;
     
        function InitializeFiles(): Boolean;
        function ChangeDirectory(const ADirectory: string): Boolean; overload;
        function ChangeDirectory(const ADirectory: string; out AOldDirectory: string): Boolean; overload;
     
        {$IFDEF DEBUG_SLT_FTP}
        procedure OutputDebugFTP(const Msg: string); inline;
        {$ENDIF DEBUG_SLT_FTP}
     
      protected
        // Accesseurs
        procedure SetConnectionInfo(Value: TSLTFTPConnectionInfo);
        function GetFileCount(): Integer;
        function GetFile(Index: Integer): TFileName;
        function GetFileSize(Index: Integer): Int64;
        function GetFileDate(Index: Integer): TDateTime;
        procedure SetFileFilter(const Value: string);
      public
        // Constructeurs
        constructor Create();
        destructor Destroy(); override;
     
        // Méthodes
        function Connect(): Boolean;
        function Disconnect(): Boolean;
     
        function Get(const ASourceFile: string; ADest: TStream; const ASourceDirectory: string = ''): Boolean;
        function Put(const ASource: TStream; const ADestFile: string; const ADestDirectory: string = ''): Boolean;
        function RenameFile(const ASourceFile: string; const ADestFile: string): Boolean;
        function MoveFile(const ASourceFile: string; const ADestFile: string; const ASourceDirectory: string = ''; const ADestDirectory: string = ''): Boolean;
        function MoveFileTo(const AFile: string; const ADestDirectory: string; AReplaceExisting: Boolean = False): Boolean;
        function DeleteFile(const AFile: string; const ADirectory: string = ''): Boolean;
        function CreateDirectory(const ADirectory: string): Boolean;
        function ExistsDirectory(const ADirectory: string): Boolean;
     
        // Propriétés
        property ConnectionInfo: TSLTFTPConnectionInfo read FConnectionInfo write SetConnectionInfo;
        property FileCount: Integer read GetFileCount;
        property Files[Index: Integer]: TFileName read GetFile;
        property FileSizes[Index: Integer]: Int64 read GetFileSize;
        property FileDates[Index: Integer]: TDateTime read GetFileDate;
        property FileFilter: string read FFileFilter write SetFileFilter;
      end;
     
      /// <summary>Décrit un emplacement FTP</summary>
      TSLTFTPConnectionInfo = class(TPersistent)
      public
        const
          DEFAULT_PORT_FTP = 21;
          DEFAULT_PASSIVE_FTP = IdFTP.Id_TIdFTP_Passive;
      private
        // Membres privés
        FHost: string;
        FPort: Word;
        FDirectory: TFileName;
        FUser: string;
        FPassword: string;
        FPassive: Boolean;
      public
        // Constructeurs
        constructor Create();
     
        // Méthodes - Redéfinition de TPersistent
        procedure Assign(Source: TPersistent); override;
        // Méthodes - Redéfinition de TObject
        function ToString(): string; override;
     
        // Propriétés
        property Host: string read FHost write FHost;
        property Port: Word read FPort write FPort default DEFAULT_PORT_FTP;
        property Directory: TFileName read FDirectory write FDirectory;
        property User: string read FUser write FUser;
        property Password: string read FPassword write FPassword;
        property Passive: Boolean read FPassive write FPassive;
      end;
     
    implementation
     
    {$IFDEF DEBUG_SLT_FTP}
    uses
      SLT.Common.Tracing;
    {$ENDIF DEBUG_SLT_FTP}
     
    { TSLTFTP }
     
    //------------------------------------------------------------------------------
    function TSLTFTP.ChangeDirectory(const ADirectory: string): Boolean;
    begin
      FConnection.ChangeDir(ADirectory);
      Result := SameText(FConnection.RetrieveCurrentDir, ADirectory);
    end;
     
    //------------------------------------------------------------------------------
    function TSLTFTP.ChangeDirectory(const ADirectory: string; out AOldDirectory: string): Boolean;
    var
      OldD: string;
    begin
      OldD := FConnection.RetrieveCurrentDir;
      Result := ChangeDirectory(ADirectory);
      if Result then
        AOldDirectory := OldD;
    end;
     
    //------------------------------------------------------------------------------
    function TSLTFTP.Connect(): Boolean;
    begin
      Result := False;
     
      Disconnect();
     
      FConnection.Host := FConnectionInfo.Host;
      FConnection.Port := FConnectionInfo.Port;
      FConnection.Username := FConnectionInfo.User;
      FConnection.Password := FConnectionInfo.Password;
      FConnection.Passive := FConnectionInfo.Passive;
     
      try
        FConnection.Connect();
        if FConnection.Connected then
        begin
          if FConnectionInfo.Directory <> '' then
            Result := ChangeDirectory(FConnectionInfo.Directory)
          else
            Result := True;
        end
        else
          Abort;
      except
        on E: Exception do
        begin
          {$IFDEF DEBUG_SLT_FTP}OutputDebugFTP('Connect : ' + E.Message);{$ENDIF DEBUG_SLT_FTP}
        end;
      end;
    end;
     
    //------------------------------------------------------------------------------
    constructor TSLTFTP.Create();
    begin
      inherited Create();
     
      FConnection := TIdFTP.Create(nil);
      FConnectionInfo := TSLTFTPConnectionInfo.Create();
      FFileFilter := '.';
    end;
     
    //------------------------------------------------------------------------------
    function TSLTFTP.CreateDirectory(const ADirectory: string): Boolean;
    begin
      if FConnection.Connected then
      begin
        try
          // Si le dossier existe déjà, il ne faut pas le créer sinon exception
          if not ExistsDirectory(ADirectory) then
            FConnection.MakeDir(ADirectory);
     
          Result := True;
        except
          on E: Exception do
          begin
            {$IFDEF DEBUG_SLT_FTP}OutputDebugFTP('CreateDirectory : ' + E.Message);{$ENDIF DEBUG_SLT_FTP}
            Result := False;
          end;
        end;
      end
      else
        Result := False;
    end;
     
    //------------------------------------------------------------------------------
    function TSLTFTP.DeleteFile(const AFile: string; const ADirectory: string = ''): Boolean;
    var
      lDel: string;
    begin
      if FConnection.Connected then
      begin
        try
          if ADirectory <> '' then
            lDel := ADirectory + '/' + AFile
          else
            lDel := AFile;
     
          if FConnection.Size(lDel) >= 0 then
            FConnection.Delete(lDel);
     
          Result := True;
        except
          on E: Exception do
          begin
            {$IFDEF DEBUG_SLT_FTP}OutputDebugFTP('DeleteFile : ' + E.Message);{$ENDIF DEBUG_SLT_FTP}
            Result := False;
          end;
        end;
      end
      else
        Result := False;
    end;
     
    //------------------------------------------------------------------------------
    destructor TSLTFTP.Destroy();
    begin
      Disconnect();
     
      FreeAndNil(FConnectionInfo);
      FreeAndNil(FConnection);
     
      inherited Destroy();
    end;
     
    //------------------------------------------------------------------------------
    function TSLTFTP.Disconnect(): Boolean;
    begin
      FreeAndNil(FFiles);
     
      try
        if FConnection.Connected then
        begin
          FConnection.Disconnect();
          Result := not FConnection.Connected;
        end
        else
          Result := True;
      except
        on E: Exception do
        begin
          {$IFDEF DEBUG_SLT_FTP}OutputDebugFTP('Disconnect : ' + E.Message);{$ENDIF DEBUG_SLT_FTP}
          Result := False;
        end;
      end;
    end;
     
    //------------------------------------------------------------------------------
    function TSLTFTP.ExistsDirectory(const ADirectory: string): Boolean;
    const
      ERROR_FILE_UNAVAILABLE = 550;
    var
      OldDirectory: string;
    begin
      Result := False;
     
      // Même technique que FileZilla, changement de répertoire pour en tester l'existence
      if FConnection.Connected then
      begin
        try
          OldDirectory := FConnection.RetrieveCurrentDir;
          try
            try
              Result := ChangeDirectory(ADirectory);
            except
              on E: EIdReplyRFCError do
                if E.ErrorCode <> ERROR_FILE_UNAVAILABLE then
                  raise;
            end;
          finally
            if not ChangeDirectory(OldDirectory) then
              Abort;
          end;
        except
          on E: Exception do
          begin
            {$IFDEF DEBUG_SLT_FTP}OutputDebugFTP('ExistsDirectory : ' + E.Message);{$ENDIF DEBUG_SLT_FTP}
            Result := False;
          end;
        end;
      end
      else
        Result := False;
    end;
     
    //------------------------------------------------------------------------------
    function TSLTFTP.Get(const ASourceFile: string; ADest: TStream; const ASourceDirectory: string = ''): Boolean;
    var
      OldDirectory: string;
    begin
      if FConnection.Connected then
      begin
        try
          if ASourceDirectory <> '' then
            if not ChangeDirectory(ASourceDirectory, OldDirectory) then
              Abort;
     
          try
            FConnection.Get(ASourceFile, ADest);
            Result := True;
          finally
            if (ASourceDirectory <> '') and (OldDirectory <> '') then
              if not ChangeDirectory(OldDirectory) then
                Abort;
          end;
        except
          on E: Exception do
          begin
            {$IFDEF DEBUG_SLT_FTP}OutputDebugFTP('Get : ' + E.Message);{$ENDIF DEBUG_SLT_FTP}
            raise;
          end;
        end;
      end
      else
        Result := False;
    end;
     
     
    //------------------------------------------------------------------------------
    function TSLTFTP.GetFile(Index: Integer): TFileName;
    begin
      if InitializeFiles() then
        Result := FFiles.Strings[Index]
      else
        Result := '';
    end;
     
    //------------------------------------------------------------------------------
    function TSLTFTP.GetFileCount(): Integer;
    begin
      if InitializeFiles() then
        Result := FFiles.Count
      else
        Result := 0;
    end;
     
    //------------------------------------------------------------------------------
    function TSLTFTP.GetFileDate(Index: Integer): TDateTime;
    begin
      if InitializeFiles() then
        Result := FConnection.FileDate(Files[Index])
      else
        Result := 0;
    end;
     
    //------------------------------------------------------------------------------
    function TSLTFTP.GetFileSize(Index: Integer): Int64;
    begin
      if InitializeFiles() then
        Result := FConnection.Size(Files[Index])
      else
        Result := 0;
    end;
     
    //------------------------------------------------------------------------------
    function TSLTFTP.InitializeFiles(): Boolean;
    var
      TmpList: TStrings;
    begin
      Result := False;
      if FConnection.Connected then
      begin
        if not Assigned(FFiles) then
        begin
          TmpList := TStringList.Create();
          try
            try
              FConnection.List(TmpList, FFileFilter, False);
              if TmpList.Count > 0 then
              begin
                FFiles := TmpList;
                Exit(True);
              end;
            except
              on E: Exception do
              begin
                {$IFDEF DEBUG_SLT_FTP}OutputDebugFTP('InitializeFiles : ' + E.Message);{$ENDIF DEBUG_SLT_FTP}
                Result := False;
              end;
            end;
          finally
            if not Result then
              TmpList.Free();
          end;
        end;
      end;
     
      Result := Assigned(FFiles) and (FFiles.Count > 0);
    end;
     
    //------------------------------------------------------------------------------
    function TSLTFTP.MoveFile(const ASourceFile: string; const ADestFile: string; const ASourceDirectory: string = ''; const ADestDirectory: string = ''): Boolean;
    var
      lSrc, lDst: string;
    begin
      if FConnection.Connected then
      begin
        try
          if ASourceDirectory <> '' then
            lSrc := ASourceDirectory + '/' + ASourceFile
          else
            lSrc := ASourceFile;
     
          if ADestDirectory <> '' then
            lDst := ADestDirectory + '/' + ADestFile
          else
            lDst := ADestFile;
     
          FConnection.Rename(lSrc, lDst);
          Result := True;
        except
          on E: Exception do
          begin
            {$IFDEF DEBUG_SLT_FTP}OutputDebugFTP('MoveFile : ' + E.Message);{$ENDIF DEBUG_SLT_FTP}
            Result := False;
          end;
        end;
      end
      else
        Result := False;
    end;
     
    //------------------------------------------------------------------------------
    function TSLTFTP.MoveFileTo(const AFile: string; const ADestDirectory: string; AReplaceExisting: Boolean = False): Boolean;
    begin
      if AReplaceExisting then
        DeleteFile(AFile, ADestDirectory);
     
      Result := MoveFile(AFile, AFile, '', ADestDirectory);
    end;
     
    //------------------------------------------------------------------------------
    function TSLTFTP.Put(const ASource: TStream; const ADestFile: string; const ADestDirectory: string = ''): Boolean;
    var
      OldDirectory: string;
    begin
      if FConnection.Connected then
      begin
        try
          if ADestDirectory <> '' then
            if not ChangeDirectory(ADestDirectory, OldDirectory) then
              Abort;
     
          try
            FConnection.Put(ASource, ADestFile);
            Result := True;
          finally
            if (ADestDirectory <> '') and (OldDirectory <> '') then
              if not ChangeDirectory(OldDirectory) then
                Abort;
          end;
        except
          on E: Exception do
          begin
            {$IFDEF DEBUG_SLT_FTP}OutputDebugFTP('Put : ' + E.Message);{$ENDIF DEBUG_SLT_FTP}
            Result := False;
          end;
        end;
      end
      else
        Result := False;
    end;
     
    //------------------------------------------------------------------------------
    function TSLTFTP.RenameFile(const ASourceFile, ADestFile: string): Boolean;
    begin
      if FConnection.Connected then
      begin
        try
          FConnection.Rename(ASourceFile, ADestFile);
          Result := True;
        except
          on E: Exception do
          begin
            {$IFDEF DEBUG_SLT_FTP}OutputDebugFTP('RenameFile : ' + E.Message);{$ENDIF DEBUG_SLT_FTP}
            Result := False;
          end;
        end;
      end
      else
        Result := False;
    end;
     
    //------------------------------------------------------------------------------
    procedure TSLTFTP.SetConnectionInfo(Value: TSLTFTPConnectionInfo);
    begin
      FConnectionInfo.Assign(Value);
    end;
     
    //------------------------------------------------------------------------------
    procedure TSLTFTP.SetFileFilter(const Value: string);
    begin
      if not SameText(FFileFilter, Value) then
      begin
        FFileFilter := Value;
        FreeAndNil(FFiles);
      end;
    end;
     
    //------------------------------------------------------------------------------
    {$IFDEF DEBUG_SLT_FTP}
    procedure TSLTFTP.OutputDebugFTP(const Msg: string);
    begin
      TSLTDebugLogger.OutputDebugString('[SLT.FTP]', Format('%0:s : %1:s', [ConnectionInfo.Host, Msg]));
    end;
    {$ENDIF DEBUG_SLT_FTP}
     
     
    { TSLTFTPConnectionInfo }
     
    //------------------------------------------------------------------------------
    procedure TSLTFTPConnectionInfo.Assign(Source: TPersistent);
    begin
      if Source is TSLTFTPConnectionInfo then
      begin
        with TSLTFTPConnectionInfo(Source) do
        begin
          Self.FHost := FHost;
          Self.FPort := FPort;
          Self.FDirectory := FDirectory;
          Self.FUser := FUser;
          Self.FPassword := FPassword;
          Self.FPassive := FPassive;
        end;
      end
      else
        inherited Assign(Source);
    end;
     
    //------------------------------------------------------------------------------
    constructor TSLTFTPConnectionInfo.Create();
    begin
      inherited Create();
     
      FPort := DEFAULT_PORT_FTP;
      Passive := DEFAULT_PASSIVE_FTP;
    end;
     
    //------------------------------------------------------------------------------
    function TSLTFTPConnectionInfo.ToString(): string;
     
      function ExcludeFirstPathDelimiter(const S: string): string;
      begin
        if IsDelimiter(S, '/', 1) then
          Result := Copy(S, 2, Length(S) - 1)
        else
          Result := S;
      end;
     
    begin
      // Concaténation des informations de connexion sous la forme d'une URL "ftp://user@ftpserver/url-path"
      Result := Format('ftp://%s@%s/%s', [User, Host, ExcludeFirstPathDelimiter(Directory)]);
    end;
     
    end.
    Aide via F1 - FAQ - Guide du développeur Delphi devant un problème - Pensez-y !
    Attention Troll Méchant !
    "Quand un homme a faim, mieux vaut lui apprendre à pêcher que de lui donner un poisson" Confucius
    Mieux vaut se taire et paraître idiot, Que l'ouvrir et de le confirmer !
    L'ignorance n'excuse pas la médiocrité !

    L'expérience, c'est le nom que chacun donne à ses erreurs. (Oscar Wilde)
    Il faut avoir le courage de se tromper et d'apprendre de ses erreurs

  6. #6
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 042
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 67
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 042
    Points : 40 952
    Points
    40 952
    Billets dans le blog
    62
    Par défaut
    Citation Envoyé par lefju cabro Voir le message
    Oui, A définit un sous-répertoire du répertoire racine ftp de B.
    Tu me conseilles d'utiliser FileZilla (par ex) et d'y connecter mon application ?
    ben oui, AMHA ce serait plus simple, reste juste un problème de taille si les fichiers sont supérieurs à 2Go
    MVP Embarcadero
    Delphi installés : D3,D7,D2010,XE4,XE7,D10 (Rio, Sidney), D11 (Alexandria), D12 (Athènes)
    SGBD : Firebird 2.5, 3, SQLite
    générateurs États : FastReport, Rave, QuickReport
    OS : Window Vista, Windows 10, Windows 11, Ubuntu, Androïd

  7. #7
    Membre régulier
    Développeur informatique
    Inscrit en
    Décembre 2010
    Messages
    228
    Détails du profil
    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Décembre 2010
    Messages : 228
    Points : 113
    Points
    113
    Par défaut
    Avec toutes vos infos, je laisse tomber Tethering pour le transfert de fichiers. En plus, le fait d'ouvrir le fichier pour le transférer ne me convient pas en terme de performance.

    @Paul TOTH
    Merci pour le détail des infos. Je ne connais pas les @IP, je peux utiliser le tethering pour cela.

    @ShaiLeTroll
    Merci pour ton exemple complet.

    @SergioMaster
    Je vais également évaluer l'utilisation de FileZilla.

    Bonne journée à vous

  8. #8
    Expert éminent sénior
    Avatar de Paul TOTH
    Homme Profil pro
    Freelance
    Inscrit en
    Novembre 2002
    Messages
    8 964
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Freelance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2002
    Messages : 8 964
    Points : 28 445
    Points
    28 445
    Par défaut
    note que tu peux aussi faire de la découverte réseau juste avec des composants UDP...il suffit de faire un broadcast UDP en disant "coucou je suis là" et quand une machine reçoit ce message tu as l'@IP de l'expéditeur donc tu peux le contacter (attention, la machine reçoit elle-même son propre message, et si tu as plusieurs cartes réseau il faut broadcaster sur chaque carte)
    Developpez.com: Mes articles, forum FlashPascal
    Entreprise: Execute SARL
    Le Store Excute Store

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Web contre client/serveur que choisir??
    Par silvermoon dans le forum Débats sur le développement - Le Best Of
    Réponses: 41
    Dernier message: 24/01/2004, 15h53
  2. Quel outil pour du développement Client/Serveur (Win XP) ?
    Par jey_bonnet dans le forum Débats sur le développement - Le Best Of
    Réponses: 5
    Dernier message: 02/11/2002, 14h57
  3. Réponses: 2
    Dernier message: 01/10/2002, 12h25
  4. comment gerer plusieurs connexions client/serveur
    Par naili dans le forum C++Builder
    Réponses: 3
    Dernier message: 14/08/2002, 16h58
  5. Langage le mieux adapté pour application client serveur ?
    Par guenus dans le forum Débats sur le développement - Le Best Of
    Réponses: 4
    Dernier message: 17/06/2002, 15h46

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo