Skip to content

Commit dccbe44

Browse files
Merge pull request #15 from andreaskueffel/development
add DateTime format option for DestinationPath on AbstractProvider
2 parents fbef91e + fef24bc commit dccbe44

5 files changed

Lines changed: 57 additions & 20 deletions

File tree

FileSyncApp/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ static void RunProgram()
6262
jobOptions.Add("CleanJob", cleanJob);
6363

6464
var syncFromEdgeToLocal = FileSyncJobOptionsBuilder.CreateBuilder()
65-
.WithSourcePath("\\\\edgeip\\share\\service\\production")
65+
.WithSourcePath("scp://edgeip/service/production")
6666
.WithDestinationPath("temp")
67-
.WithFileSyncProvider(SyncProvider.SMBLib)
67+
.WithFileSyncProvider(SyncProvider.Abstract)
6868
.WithSubfolder("left")
6969
.WithSubfolder("right")
7070
.WithCredentials(new System.Net.NetworkCredential("USER", "Password", ""))

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)

FileSyncLibNet/SyncProviders/AbstractProvider.cs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,23 @@ public AbstractProvider(IFileJobOptions jobOptions) : base(jobOptions)
1818
throw new ArgumentException("this instance has no information about syncing files, it has type " + JobOptions.GetType().ToString());
1919
if (syncJobOptions.SourcePath.StartsWith("scp:"))
2020
{
21-
SourceAccess = new ScpAccessProvider(syncJobOptions.SourcePath, syncJobOptions.Credentials);
21+
SourceAccess = new ScpAccessProvider(syncJobOptions.Credentials);
22+
2223
}
2324
else
2425
{
25-
SourceAccess = new FileIoAccessProvider(syncJobOptions.SourcePath);
26+
SourceAccess = new FileIoAccessProvider();
2627
}
28+
SourceAccess.UpdateAccessPath(syncJobOptions.SourcePath);
2729
if (syncJobOptions.DestinationPath.StartsWith("scp:"))
2830
{
29-
DestinationAccess = new ScpAccessProvider(syncJobOptions.DestinationPath, syncJobOptions.Credentials);
31+
DestinationAccess = new ScpAccessProvider(syncJobOptions.Credentials);
3032
}
3133
else
3234
{
33-
DestinationAccess = new FileIoAccessProvider(syncJobOptions.DestinationPath);
35+
DestinationAccess = new FileIoAccessProvider();
3436
}
37+
3538
}
3639

3740
public override void DeleteFiles()
@@ -44,29 +47,40 @@ public override void SyncSourceToDest()
4447
if (!(JobOptions is IFileSyncJobOptions jobOptions))
4548
throw new ArgumentException("this instance has no information about syncing files, it has type " + JobOptions.GetType().ToString());
4649
var sw = Stopwatch.StartNew();
50+
4751
try
4852
{
49-
DestinationAccess.CreateDirectory("");
53+
string formattedDestinationPath = string.Format(jobOptions.DestinationPath, DateTime.Now);
54+
DestinationAccess.UpdateAccessPath(formattedDestinationPath);
5055
}
51-
catch (Exception ex) { logger?.LogError(ex, "exception creating destination directory {A}", jobOptions.DestinationPath); }
52-
if (JobOptions.Credentials != null)
53-
{
56+
catch (Exception ex) { logger?.LogError(ex, "exception formatting destination accesspath with DateTime.Now as 0 arg {A}", jobOptions.DestinationPath); }
5457

55-
}
58+
bool createDestinationDir = true;
5659
int copied = 0;
5760
int skipped = 0;
5861
var minimumLastWriteTime = jobOptions.RememberLastSync ? (LastRun - jobOptions.Interval - jobOptions.Interval) : DateTime.MinValue;
5962
bool error_occured = false;
6063
try
6164
{
62-
var sourceFiles = SourceAccess.GetFiles(minimumLastWriteTime.DateTime, JobOptions.SearchPattern, recursive: JobOptions.Recursive, subfolders: JobOptions.Subfolders);
63-
65+
var sourceFiles = SourceAccess.GetFiles(minimumLastWriteTime: minimumLastWriteTime.DateTime,
66+
pattern: JobOptions.SearchPattern,
67+
recursive: JobOptions.Recursive,
68+
subfolders: JobOptions.Subfolders);
6469
foreach (var sourceFile in sourceFiles)
6570
{
6671
var remoteFile = DestinationAccess.GetFileInfo(sourceFile.Name);
6772
bool copy = !remoteFile.Exists || remoteFile.Length != sourceFile.Length || remoteFile.LastWriteTime != sourceFile.LastWriteTime;
6873
if (copy)
6974
{
75+
if (createDestinationDir)
76+
{
77+
try
78+
{
79+
DestinationAccess.CreateDirectory("");
80+
}
81+
catch (Exception ex) { logger?.LogError(ex, "exception creating destination directory {A}", jobOptions.DestinationPath); }
82+
createDestinationDir = false;
83+
}
7084
try
7185
{
7286
logger.LogDebug("Copy {A}", sourceFile.Name);
@@ -100,7 +114,7 @@ public override void SyncSourceToDest()
100114
LastRun = DateTimeOffset.Now;
101115
}
102116
}
103-
catch(Exception exc)
117+
catch (Exception exc)
104118
{
105119
logger.LogError(exc, "Exception in main logic of abstract provider");
106120
}

0 commit comments

Comments
 (0)