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:
| unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, IdFTPList, IdFTPServer, IDTCPServer, IdSocketHandle, idglobal, IdHashCRC, IdBaseComponent, IdComponent;
type TForm1 = class(TForm) FTPServer: TIdFTPServer; procedure FTPServerUserLogin(ASender: TIdFTPServerThread; const AUsername, APassword: String; var AAuthenticated: Boolean); procedure FTPServerChangeDirectory(ASender: TIdFTPServerThread; var VDirectory: String); procedure FTPServerDeleteFile(ASender: TIdFTPServerThread; const APathName: String); procedure FTPServerGetFileSize(ASender: TIdFTPServerThread; const AFilename: String; var VFileSize: Int64); procedure FTPServerMakeDirectory(ASender: TIdFTPServerThread; var VDirectory: String); procedure FTPServerRemoveDirectory(ASender: TIdFTPServerThread; var VDirectory: String); procedure FTPServerStoreFile(ASender: TIdFTPServerThread; const AFileName: String; AAppend: Boolean; var VStream: TStream); procedure FTPServerRetrieveFile(ASender: TIdFTPServerThread; const AFileName: String; var VStream: TStream); procedure FTPServerRenameFile(ASender: TIdFTPServerThread; const ARenameFromFile, ARenameToFile: String); procedure FTPServerListDirectory(ASender: TIdFTPServerThread; const APath: String; ADirectoryListing: TIdFTPListItems); private public end;
var Form1: TForm1;
implementation
{$R *.dfm}
function CalculateCRC( const path: string ) : string; var f: tfilestream; value: dword; IdHashCRC32: TIdHashCRC32; begin IdHashCRC32 := nil; f := nil; try IdHashCRC32 := TIdHashCRC32.create; f := TFileStream.create( path, fmOpenRead or fmShareDenyWrite ) ; value := IdHashCRC32.HashValue( f ) ; result := inttohex( value, 8 ) ; finally f.free; IdHashCRC32.free; end; end;
function StartsWith( const str, substr: string ) : boolean; begin result := copy( str, 1, length( substr ) ) = substr; end;
function BackSlashToSlash( const str: string ) : string; var a: dword; begin result := str; for a := 1 to length( result ) do if result[a] = '\' then result[a] := '/'; end;
function SlashToBackSlash( const str: string ) : string; var a: dword; begin result := str; for a := 1 to length( result ) do if result[a] = '/' then result[a] := '\'; end;
function TransLatePath( const APathname, homeDir: string ) : string; var tmppath: string; begin result := SlashToBackSlash( homeDir ) ; tmppath := SlashToBackSlash( APathname ) ; if homedir = '/' then begin result := tmppath; exit; end;
if length( APathname ) = 0 then exit; if result[length( result ) ] = '\' then result := copy( result, 1, length( result ) - 1 ) ; if tmppath[1] <> '\' then result := result + '\'; result := result + tmppath; end;
function GetSizeOfFile( const APathname: string ) : int64; begin result := FileSizeByName( APathname ) ; end;
function GetNewDirectory( old, action: string ) : string; var a: integer; begin if action = '../' then begin if old = '/' then begin result := old; exit; end; a := length( old ) - 1; while ( old[a] <> '\' ) and ( old[a] <> '/' ) do dec( a ) ; result := copy( old, 1, a ) ; exit; end; if ( action[1] = '/' ) or ( action[1] = '\' ) then result := action else result := old + action; end;
procedure TForm1.FTPServerUserLogin(ASender: TIdFTPServerThread; const AUsername, APassword: String; var AAuthenticated: Boolean); begin AAuthenticated := ( AUsername = 'test' ) and ( APassword = '123' ) ; if not AAuthenticated then exit; ASender.HomeDir := '/'; asender.currentdir := '/'; end;
procedure TForm1.FTPServerChangeDirectory(ASender: TIdFTPServerThread; var VDirectory: String); begin VDirectory := GetNewDirectory( ASender.CurrentDir, VDirectory ) ; end;
procedure TForm1.FTPServerDeleteFile(ASender: TIdFTPServerThread; const APathName: String); begin DeleteFile( pchar( TransLatePath( ASender.CurrentDir + '/' + APathname, ASender.HomeDir ) ) ) ; end;
procedure TForm1.FTPServerGetFileSize(ASender: TIdFTPServerThread; const AFilename: String; var VFileSize: Int64); begin VFileSize := GetSizeOfFile( TransLatePath( AFilename, ASender.HomeDir ) ) ; end;
procedure TForm1.FTPServerMakeDirectory(ASender: TIdFTPServerThread; var VDirectory: String); begin MkDir( TransLatePath( VDirectory, ASender.HomeDir ) ) ; end;
procedure TForm1.FTPServerRemoveDirectory(ASender: TIdFTPServerThread; var VDirectory: String); begin RmDir( TransLatePath( VDirectory, ASender.HomeDir ) ) ; end;
procedure TForm1.FTPServerStoreFile(ASender: TIdFTPServerThread; const AFileName: String; AAppend: Boolean; var VStream: TStream); begin if FileExists( translatepath( AFilename, ASender.HomeDir ) ) and AAppend then begin VStream := TFileStream.create( translatepath( AFilename, ASender.HomeDir ) , fmOpenWrite or fmShareExclusive ) ; VStream.Seek( 0, soFromEnd ) ; end else VStream := TFileStream.create( translatepath( AFilename, ASender.HomeDir ) , fmCreate or fmShareExclusive ) ; end;
procedure TForm1.FTPServerRetrieveFile(ASender: TIdFTPServerThread; const AFileName: String; var VStream: TStream); begin VStream := TFileStream.create( translatepath( AFilename, ASender.HomeDir ) , fmopenread or fmShareDenyWrite ) ; end;
procedure TForm1.FTPServerRenameFile(ASender: TIdFTPServerThread; const ARenameFromFile, ARenameToFile: String); begin if not MoveFile( pchar( TransLatePath( ARenameFromFile, ASender.HomeDir ) ) , pchar( TransLatePath( ARenameToFile, ASender.HomeDir ) ) ) then RaiseLastWin32Error; end;
procedure TForm1.FTPServerListDirectory(ASender: TIdFTPServerThread; const APath: String; ADirectoryListing: TIdFTPListItems);
procedure AddlistItem( aDirectoryListing: TIdFTPListItems; Filename: string; ItemType: TIdDirItemType; size: int64; date: tdatetime ) ; var listitem: TIdFTPListItem; begin listitem := aDirectoryListing.Add; listitem.ItemType := ItemType; listitem.FileName := Filename; listitem.OwnerName := 'anonymous'; listitem.GroupName := 'all'; listitem.OwnerPermissions := '---'; listitem.GroupPermissions := '---'; listitem.UserPermissions := '---'; listitem.Size := size; listitem.ModifiedDate := date; end;
var f: tsearchrec; a: integer; begin ADirectoryListing.DirectoryName := apath;
a := FindFirst( TransLatePath( apath, ASender.HomeDir ) + '*.*', faAnyFile, f ) ; while ( a = 0 ) do begin if ( f.Attr and faDirectory > 0 ) then AddlistItem( ADirectoryListing, f.Name, ditDirectory, f.size, FileDateToDateTime( f.Time ) ) else AddlistItem( ADirectoryListing, f.Name, ditFile, f.size, FileDateToDateTime( f.Time ) ) ; a := FindNext( f ) ; end;
FindClose( f ) ; end;
end. |