Skip to content

Commit d27705c

Browse files
add UpdateAccessPath method to AccessProviders
1 parent 8823fcf commit d27705c

3 files changed

Lines changed: 29 additions & 6 deletions

File tree

FileSyncLibNet/AccessProviders/FileIoAccessProvider.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ namespace FileSyncLibNet.AccessProviders
88
{
99
internal class FileIoAccessProvider : IAccessProvider
1010
{
11-
public string AccessPath { get; }
12-
public FileIoAccessProvider(string accessPath)
11+
public string AccessPath { get; private set; }
12+
public FileIoAccessProvider()
13+
{
14+
}
15+
public void UpdateAccessPath(string accessPath)
1316
{
1417
AccessPath = accessPath;
1518
}

FileSyncLibNet/AccessProviders/IAccessProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace FileSyncLibNet.AccessProviders
88
internal interface IAccessProvider
99
{
1010
void CreateDirectory(string path);
11+
void UpdateAccessPath(string path);
1112
List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, string path = null, bool recursive = false, List<string> subfolders = null);
1213
FileInfo2 GetFileInfo(string path);
1314
void Delete(FileInfo2 fileInfo);

FileSyncLibNet/AccessProviders/ScpAccessProvider.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,35 @@ namespace FileSyncLibNet.AccessProviders
1111
{
1212
internal class ScpAccessProvider : IAccessProvider
1313
{
14-
string AccessPathUri { get; }
14+
public string AccessPathUri { get; private set; }
1515
NetworkCredential Credentials { get; }
1616
SftpClient ftpClient;
1717
public string AccessPath { get; private set; }
1818

19-
public ScpAccessProvider(string accessPath, NetworkCredential credentials)
19+
public ScpAccessProvider(NetworkCredential credentials)
2020
{
21-
AccessPathUri = accessPath;
2221
Credentials = credentials;
23-
//CreateClient();
2422
}
23+
public void UpdateAccessPath(string accessPath)
24+
{
25+
AccessPathUri = accessPath;
26+
var pattern = @"scp://(?:(?<user>[^@]+)@)?(?<host>[^:/]+)(?::(?<port>\d+))?(?<path>/.*)?";
27+
var match = Regex.Match(AccessPathUri, pattern);
28+
29+
30+
if (!match.Success)
31+
{
32+
throw new UriFormatException($"Unable to match scp pattern with given URL {AccessPathUri}, use format scp://host:port/path");
33+
}
34+
else
35+
{
36+
AccessPath = match.Groups["path"].Value;
37+
if (AccessPath.StartsWith("/~"))
38+
AccessPath = AccessPath.Substring(1);
39+
}
40+
}
41+
42+
2543
void CreateClient()
2644
{
2745
var pattern = @"scp://(?:(?<user>[^@]+)@)?(?<host>[^:/]+)(?::(?<port>\d+))?(?<path>/.*)?";
@@ -44,6 +62,7 @@ void CreateClient()
4462
ftpClient = new SftpClient(host, port, Credentials.UserName, Credentials.Password);
4563
}
4664
}
65+
4766
void EnsureConnected()
4867
{
4968
if (ftpClient == null)

0 commit comments

Comments
 (0)