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
| //------------------------------------------------------------------------------
(* SoLuTions is an Versatile Library for Delphi -
* -
* Copyright ou © ou Copr. "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;
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;
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; ReplaceExistring: Boolean = False): Boolean;
function DeleteFile(const AFile: string; 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 FileFilter: string read FFileFilter write SetFileFilter;
end;
/// <summary>Décrit un emplacement FTP</summary>
TSLTFTPConnectionInfo = class(TPersistent)
public
const
DEFAULT_PORT_FTP = 21;
private
// Membres privés
FHost: string;
FPort: Word;
FDirectory: TFileName;
FUser: string;
FPassword: string;
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;
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;
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.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.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.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; ReplaceExistring: Boolean = False): Boolean;
begin
if ReplaceExistring 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;
end;
end
else
inherited Assign(Source);
end;
//------------------------------------------------------------------------------
constructor TSLTFTPConnectionInfo.Create();
begin
inherited Create();
FPort := DEFAULT_PORT_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. |
Partager