Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
334 lines (276 loc) · 9.04 KB

File metadata and controls

334 lines (276 loc) · 9.04 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
{
KLib Version = 4.0
The Clear BSD License
Copyright (c) 2020 by Karol De Nery Ortiz LLave. All rights reserved.
zitrokarol@gmail.com
Redistribution and use in source and binary forms, with or without
modification, are permitted (subject to the limitations in the disclaimer
below) provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
}
unit KLib.FileTransfer;
{
Protocol-agnostic file transfer abstraction (FTP / SFTP).
Unifies the BEHAVIOR behind a single interface (IFileTransferClient) while
keeping the connection DATA separated per protocol (TFtpCredentials in FTP,
TSFTPCredentials in SFTP). The caller programs against the interface and no
longer knows nor cares which protocol is in use; the protocol is chosen once,
by which factory overload is called.
FTP -> TFtpTransferClient (wraps KLib.MyIdFTP / KLib.Indy, Indy based)
SFTP -> TSftpTransferClient (wraps KLib.SFTP / libssh2)
remotePath semantics: relative to the base remote directory configured on the
credentials (TFtpCredentials.pathFTPDir for FTP, TSFTPCredentials.remoteDir for
SFTP). FTP changes into that directory on connect; the SFTP adapter prepends it
to remotePath. So passing a bare file name lands in the configured directory
for both protocols. An absolute remotePath (leading '/') bypasses the base
directory in SFTP.
Usage:
var
_client: IFileTransferClient;
begin
_client := getFileTransferClient(sftpCredentials); //or ftpCredentials
_client.connect;
try
_client.uploadFile('C:\out\doc.pdf', 'doc.pdf');
finally
_client.disconnect;
end;
end;
//no manual Free: IFileTransferClient is reference counted.
}
interface
uses
System.SysUtils,
KLib.Types;
type
EFileTransferException = class(Exception);
TFileTransferProtocol = (ftpProtocol, sftpProtocol);
IFileTransferClient = interface
['{6F3A1C42-9E7B-4D58-A1F0-2C9D8B4E7A31}']
procedure connect;
procedure disconnect;
procedure uploadFile(localPath: string; remotePath: string);
procedure downloadFile(remotePath: string; localPath: string);
procedure deleteFile(remotePath: string);
function isFileExists(remotePath: string): boolean;
procedure makeDir(remotePath: string);
end;
function getFileTransferClient(ftpCredentials: TFtpCredentials): IFileTransferClient; overload;
function getFileTransferClient(sftpCredentials: TSFTPCredentials): IFileTransferClient; overload;
implementation
uses
KLib.Constants,
KLib.MyIdFTP, KLib.Indy,
KLib.SFTP,
IdFTP;
type
TFtpTransferClient = class(TInterfacedObject, IFileTransferClient)
private
credentials: TFtpCredentials;
connection: TMyIdFTP;
isConnected: boolean;
procedure ensureConnected;
public
constructor create(ftpCredentials: TFtpCredentials);
destructor Destroy; override;
procedure connect;
procedure disconnect;
procedure uploadFile(localPath: string; remotePath: string);
procedure downloadFile(remotePath: string; localPath: string);
procedure deleteFile(remotePath: string);
function isFileExists(remotePath: string): boolean;
procedure makeDir(remotePath: string);
end;
TSftpTransferClient = class(TInterfacedObject, IFileTransferClient)
private
client: TSFTPClient;
remoteDir: string;
function resolveRemote(remotePath: string): string;
public
constructor create(sftpCredentials: TSFTPCredentials);
destructor Destroy; override;
procedure connect;
procedure disconnect;
procedure uploadFile(localPath: string; remotePath: string);
procedure downloadFile(remotePath: string; localPath: string);
procedure deleteFile(remotePath: string);
function isFileExists(remotePath: string): boolean;
procedure makeDir(remotePath: string);
end;
function getFileTransferClient(ftpCredentials: TFtpCredentials): IFileTransferClient;
begin
Result := TFtpTransferClient.create(ftpCredentials);
end;
function getFileTransferClient(sftpCredentials: TSFTPCredentials): IFileTransferClient;
begin
Result := TSftpTransferClient.create(sftpCredentials);
end;
{ TFtpTransferClient }
constructor TFtpTransferClient.create(ftpCredentials: TFtpCredentials);
begin
inherited create;
credentials := ftpCredentials;
connection := nil;
isConnected := false;
end;
destructor TFtpTransferClient.Destroy;
begin
disconnect;
inherited;
end;
procedure TFtpTransferClient.ensureConnected;
begin
if not isConnected then
begin
raise EFileTransferException.create('FTP: operation requested but not connected');
end;
end;
procedure TFtpTransferClient.connect;
begin
if isConnected then
begin
Exit;
end;
connection := getValidTMyIdFTP(credentials);
try
connection.Connect;
isConnected := true;
except
FreeAndNil(connection);
raise;
end;
end;
procedure TFtpTransferClient.disconnect;
begin
if Assigned(connection) then
begin
try
if connection.Connected then
begin
connection.Disconnect;
end;
except
//teardown: ignore disconnect errors
end;
FreeAndNil(connection);
end;
isConnected := false;
end;
procedure TFtpTransferClient.uploadFile(localPath: string; remotePath: string);
begin
ensureConnected;
connection.put(localPath, remotePath, FORCE_OVERWRITE);
end;
procedure TFtpTransferClient.downloadFile(remotePath: string; localPath: string);
begin
ensureConnected;
connection.Get(remotePath, localPath, True);
end;
procedure TFtpTransferClient.deleteFile(remotePath: string);
begin
ensureConnected;
connection.Delete(remotePath);
end;
function TFtpTransferClient.isFileExists(remotePath: string): boolean;
var
_isExisting: boolean;
begin
ensureConnected;
_isExisting := connection.checkIfFileExists(remotePath);
Result := _isExisting;
end;
procedure TFtpTransferClient.makeDir(remotePath: string);
begin
ensureConnected;
connection.makeDirIfNotExists(remotePath);
end;
{ TSftpTransferClient }
function combineRemotePath(baseDir: string; remotePath: string): string;
begin
if (baseDir = '') or ((remotePath <> '') and (remotePath[1] = '/')) then
begin
//no base dir, or remotePath already absolute
Result := remotePath;
end
else
begin
Result := baseDir;
if Result[Length(Result)] <> '/' then
begin
Result := Result + '/';
end;
Result := Result + remotePath;
end;
end;
constructor TSftpTransferClient.create(sftpCredentials: TSFTPCredentials);
begin
inherited create;
client := TSFTPClient.create(sftpCredentials);
remoteDir := sftpCredentials.remoteDir;
end;
function TSftpTransferClient.resolveRemote(remotePath: string): string;
begin
Result := combineRemotePath(remoteDir, remotePath);
end;
destructor TSftpTransferClient.Destroy;
begin
FreeAndNil(client);
inherited;
end;
procedure TSftpTransferClient.connect;
begin
client.connect;
end;
procedure TSftpTransferClient.disconnect;
begin
client.disconnect;
end;
procedure TSftpTransferClient.uploadFile(localPath: string; remotePath: string);
begin
client.uploadFile(localPath, resolveRemote(remotePath));
end;
procedure TSftpTransferClient.downloadFile(remotePath: string; localPath: string);
begin
client.downloadFile(resolveRemote(remotePath), localPath);
end;
procedure TSftpTransferClient.deleteFile(remotePath: string);
begin
client.deleteFile(resolveRemote(remotePath));
end;
function TSftpTransferClient.isFileExists(remotePath: string): boolean;
var
_isExisting: boolean;
begin
_isExisting := client.isFileExists(resolveRemote(remotePath));
Result := _isExisting;
end;
procedure TSftpTransferClient.makeDir(remotePath: string);
var
_remote: string;
begin
_remote := resolveRemote(remotePath);
if not client.isFileExists(_remote) then
begin
client.makeDir(_remote);
end;
end;
end.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.