-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathFtpManager.cs
More file actions
305 lines (268 loc) · 10.2 KB
/
FtpManager.cs
File metadata and controls
305 lines (268 loc) · 10.2 KB
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
// FtpManager.cs, 10.06.2019
// Copyright (C) Dominic Beger 17.06.2019
using System;
using System.Collections.Generic;
using System.Net;
using System.Reflection;
using System.Security;
using nUpdate.Administration.Ftp.Service;
using nUpdate.Administration.TransferInterface;
using Starksoft.Aspen.Ftps;
using TransferProgressEventArgs = nUpdate.Administration.TransferInterface.TransferProgressEventArgs;
namespace nUpdate.Administration
{
public class FtpManager
{
private bool _disposed;
private ITransferProvider _transferProvider;
public FtpManager()
{
GetTransferProvider();
}
public FtpManager(string host, int port, string directory, string username, SecureString password,
WebProxy proxy, bool usePassiveMode, string transferAssemblyFilePath, int protcol,
NetworkVersion networkVersion)
{
TransferAssemblyPath = transferAssemblyFilePath;
GetTransferProvider();
_transferProvider.Host = host;
_transferProvider.Port = port;
_transferProvider.Directory = directory;
_transferProvider.Username = username;
_transferProvider.Password = password.Copy();
_transferProvider.Proxy = proxy;
_transferProvider.UsePassiveMode = usePassiveMode;
if (!string.IsNullOrWhiteSpace(transferAssemblyFilePath))
return;
Protocol = (FtpsSecurityProtocol) protcol;
NetworkVersion = networkVersion;
}
/// <summary>
/// The directory.
/// </summary>
public string Directory
{
get => _transferProvider.Directory;
set => _transferProvider.Directory = value;
}
/// <summary>
/// The FTP-server.
/// </summary>
public string Host
{
get => _transferProvider.Host;
set => _transferProvider.Host = value;
}
public NetworkVersion NetworkVersion
{
get => ((FtpTransferService) _transferProvider).NetworkVersion;
set
{
if (_transferProvider.GetType() == typeof(FtpTransferService))
((FtpTransferService) _transferProvider).NetworkVersion = value;
}
}
/// <summary>
/// Gets or sets the exception appearing during the package upload.
/// </summary>
public Exception PackageUploadException
{
get => _transferProvider.PackageUploadException;
set => _transferProvider.PackageUploadException = value;
}
/// <summary>
/// The password.
/// </summary>
public SecureString Password
{
get => _transferProvider.Password.Copy();
set => _transferProvider.Password = value;
}
/// <summary>
/// The port.
/// </summary>
public int Port
{
get => _transferProvider.Port;
set => _transferProvider.Port = value;
}
/// <summary>
/// Sets the protocol to use, if FTP is used as protocol.
/// </summary>
public FtpsSecurityProtocol Protocol
{
get => ((FtpTransferService) _transferProvider).Protocol;
set
{
if (_transferProvider.GetType() == typeof(FtpTransferService))
((FtpTransferService) _transferProvider).Protocol = value;
}
}
/// <summary>
/// The proxy to use, if wished.
/// </summary>
public WebProxy Proxy
{
get => _transferProvider.Proxy;
set => _transferProvider.Proxy = value;
}
/// <summary>
/// Gets or sets the path of the assembly containing the custom transfer handlers.
/// </summary>
public string TransferAssemblyPath { get; set; }
/// <summary>
/// Sets if passive mode should be used.
/// </summary>
public bool UsePassiveMode
{
get => _transferProvider.UsePassiveMode;
set => _transferProvider.UsePassiveMode = value;
}
/// <summary>
/// The username.
/// </summary>
public string Username
{
get => _transferProvider.Username;
set => _transferProvider.Username = value;
}
public event EventHandler<EventArgs> CancellationFinished
{
add => _transferProvider.CancellationFinished += value;
remove => _transferProvider.CancellationFinished -= value;
}
/// <summary>
/// Terminates the package upload process.
/// </summary>
public void CancelPackageUpload()
{
_transferProvider.CancelPackageUpload();
}
/// <summary>
/// Deletes a directory on the server.
/// </summary>
/// <param name="directoryPath">The name of the directory to delete.</param>
public void DeleteDirectory(string directoryPath)
{
_transferProvider.DeleteDirectory(directoryPath);
}
/// <summary>
/// Deletes a file on the server.
/// </summary>
/// <param name="fileName">The name of the file to delete.</param>
public void DeleteFile(string fileName)
{
_transferProvider.DeleteFile(fileName);
}
/// <summary>
/// Deletes a file on the server which is located at the specified path.
/// </summary>
/// <param name="directoryPath">The path of the directory where the file is located.</param>
/// <param name="fileName">The name of the file to delete.</param>
public void DeleteFile(string directoryPath, string fileName)
{
_transferProvider.DeleteFile(directoryPath, fileName);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public virtual void Dispose(bool disposing)
{
if (!disposing || _disposed)
return;
Password.Dispose();
_disposed = true;
}
private IServiceProvider GetDefaultServiceProvider()
{
var assembly = Assembly.GetExecutingAssembly();
return ServiceProviderHelper.CreateServiceProvider(assembly);
}
private void GetTransferProvider()
{
if (string.IsNullOrWhiteSpace(TransferAssemblyPath))
{
var provider = GetDefaultServiceProvider();
_transferProvider = (ITransferProvider) provider.GetService(typeof(ITransferProvider));
((FtpTransferService) _transferProvider).Protocol = Protocol;
// Default integrated services define a protocol as there are multiple ones available
}
else
{
var assembly = Assembly.LoadFrom(TransferAssemblyPath);
var provider = ServiceProviderHelper.CreateServiceProvider(assembly) ?? GetDefaultServiceProvider();
_transferProvider = (ITransferProvider) provider.GetService(typeof(ITransferProvider));
}
}
/// <summary>
/// Returns if a file or directory is existing on the server.
/// </summary>
/// <param name="destinationName">The name of the file or folder to check.</param>
/// <returns>Returns "true" if the file or folder exists, otherwise "false".</returns>
public bool IsExisting(string destinationName)
{
return _transferProvider.IsExisting(destinationName);
}
/// <summary>
/// Returns if a file or directory is existing on the server.
/// </summary>
/// <param name="directoryPath">The directory in which the file should be existing.</param>
/// <param name="destinationName">The name of the file or folder to check.</param>
/// <returns>Returns "true" if the file or folder exists, otherwise "false".</returns>
public bool IsExisting(string directoryPath, string destinationName)
{
return _transferProvider.IsExisting(directoryPath, destinationName);
}
/// <summary>
/// Lists the directories and files of the current FTP-directory.
/// </summary>
public IEnumerable<ServerItem> ListDirectoriesAndFiles(string path, bool recursive)
{
return _transferProvider.ListDirectoriesAndFiles(path, recursive);
}
public void MakeDirectory(string name)
{
_transferProvider.MakeDirectory(name);
}
/// <summary>
/// Moves all files and subdirectories from the current FTP-directory to the given aim directory.
/// </summary>
/// <param name="aimPath">The aim directory to move the files and subdirectories to.</param>
public void MoveContent(string aimPath)
{
_transferProvider.MoveContent(aimPath);
}
public event EventHandler<TransferProgressEventArgs> ProgressChanged
{
add => _transferProvider.ProgressChanged += value;
remove => _transferProvider.ProgressChanged -= value;
}
public void RenameDirectory(string oldName, string newName)
{
_transferProvider.RenameDirectory(oldName, newName);
}
public void TestConnection()
{
_transferProvider.TestConnection();
}
/// <summary>
/// Uploads a file to the server.
/// </summary>
/// <param name="filePath">The local path of the file to upload.</param>
public void UploadFile(string filePath)
{
_transferProvider.UploadFile(filePath);
}
/// <summary>
/// Uploads an update package to the server.
/// </summary>
/// <param name="packagePath">The local path of the package..</param>
/// <param name="packageVersion">The package version for the directory name.</param>
public void UploadPackage(string packagePath, string packageVersion)
{
_transferProvider.UploadPackage(packagePath, packageVersion);
}
}
}