Skip to content

Commit 1268ce6

Browse files
Integrate RemoteState and MaxAge for AbstractProvider
1 parent 84c4a07 commit 1268ce6

5 files changed

Lines changed: 51 additions & 10 deletions

File tree

FileSyncLibNet/AccessProviders/FileIoAccessProvider.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ namespace FileSyncLibNet.AccessProviders
1010
internal class FileIoAccessProvider : IAccessProvider
1111
{
1212
public string AccessPath { get; private set; }
13+
private readonly RemoteState remoteState;
1314
private readonly ILogger logger;
14-
public FileIoAccessProvider(ILogger logger)
15+
public FileIoAccessProvider(ILogger logger, string stateFilename)
1516
{
1617
this.logger = logger;
18+
if (!string.IsNullOrEmpty(stateFilename))
19+
remoteState = new RemoteState(stateFilename);
20+
1721
}
1822
public void UpdateAccessPath(string accessPath)
1923
{
@@ -27,6 +31,9 @@ public void CreateDirectory(string path)
2731

2832
public FileInfo2 GetFileInfo(string path)
2933
{
34+
if(null!=remoteState)
35+
return remoteState.GetFileInfo(Path.Combine(AccessPath, path));
36+
3037
var fi = new FileInfo(Path.Combine(AccessPath, path));
3138

3239
return new FileInfo2(path, fi.Exists)
@@ -75,12 +82,13 @@ public void WriteFile(FileInfo2 file, Stream content)
7582
content.CopyTo(stream);
7683
}
7784
File.SetLastWriteTime(realFilename, file.LastWriteTime);
78-
85+
remoteState?.SetFileInfo(realFilename, file);
7986
}
8087
public void Delete(FileInfo2 fileInfo)
8188
{
8289
var realFilename = Path.Combine(AccessPath, fileInfo.Name);
8390
File.Delete(realFilename);
91+
remoteState?.RemoveFileInfo(realFilename);
8492
}
8593
}
8694
}

FileSyncLibNet/AccessProviders/ScpAccessProvider.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ internal class ScpAccessProvider : IAccessProvider
1515
public string AccessPathUri { get; private set; }
1616
NetworkCredential Credentials { get; }
1717
SftpClient ftpClient;
18+
private readonly RemoteState remoteState;
1819
public string AccessPath { get; private set; }
20+
1921
private readonly ILogger logger;
20-
public ScpAccessProvider(NetworkCredential credentials, ILogger logger)
22+
public ScpAccessProvider(NetworkCredential credentials, ILogger logger, string stateFilename)
2123
{
24+
if (!string.IsNullOrEmpty(stateFilename))
25+
remoteState = new RemoteState(stateFilename);
2226
this.logger = logger;
2327
Credentials = credentials;
2428
}
@@ -101,8 +105,11 @@ public void CreateDirectory(string path)
101105

102106
public FileInfo2 GetFileInfo(string path)
103107
{
104-
EnsureConnected();
105108
var realFilename = System.IO.Path.Combine(AccessPath, path).Replace("\\", "/");
109+
if (remoteState != null)
110+
return remoteState.GetFileInfo(realFilename);
111+
112+
EnsureConnected();
106113
if (ftpClient.Exists(realFilename))
107114
{
108115
var fi = ftpClient.ListDirectory(realFilename);
@@ -149,6 +156,7 @@ public List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, s
149156
try
150157
{
151158
var files = ftpClient.ListDirectory(basePath);
159+
var sepChar="/";
152160
if (recursive)
153161
{
154162
foreach (var folder in files.Where(x => x.IsDirectory && !(x.Name == ".") && !(x.Name == "..")))
@@ -158,7 +166,7 @@ public List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, s
158166
}
159167
}
160168
ret_val.AddRange(files.Where(x => MatchesPattern(x.Name, pattern)).Where(x => x.LastWriteTime >= minimumLastWriteTime && !x.IsDirectory).Select(x =>
161-
new FileInfo2($"{basePath.Substring(AccessPath.Length + 1)}/{x.Name}", exists: true) { LastWriteTime = x.LastWriteTime, Length = x.Length }).ToList());
169+
new FileInfo2($"{(AccessPath.Length + 1<basePath.Length?(basePath.Substring(AccessPath.Length+1))+sepChar:string.Empty)}{x.Name}", exists: true) { LastWriteTime = x.LastWriteTime, Length = x.Length }).ToList());
162170
}
163171
catch (Exception exc)
164172
{
@@ -188,13 +196,15 @@ public void WriteFile(FileInfo2 file, Stream content)
188196

189197
content.CopyTo(stream);
190198
}
199+
remoteState?.SetFileInfo(filePath, file);
191200
}
192201

193202
public void Delete(FileInfo2 fileInfo)
194203
{
195204
EnsureConnected();
196205
var filePath = System.IO.Path.Combine(AccessPath, fileInfo.Name).Replace("\\", "/");
197206
ftpClient.Delete(filePath);
207+
remoteState?.RemoveFileInfo(filePath);
198208
}
199209
}
200210
}

FileSyncLibNet/Commons/FileJobOptionsBase.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.Extensions.Logging;
44
using System;
55
using System.Collections.Generic;
6+
using System.IO;
67
using System.Net;
78

89
namespace FileSyncLibNet.FileCleanJob
@@ -17,5 +18,19 @@ public abstract class FileJobOptionsBase : IFileJobOptions
1718
public List<string> Subfolders { get; set; } = new List<string>();
1819
public bool Recursive { get; set; } = true;
1920
public SyncProvider FileSyncProvider { get; set; } = SyncProvider.FileIO;
21+
22+
public virtual string GetHashedName()
23+
{
24+
string readableInfo = $"{Path.GetFileName(DestinationPath.TrimEnd(Path.DirectorySeparatorChar))}_{Interval.TotalMinutes}min";
25+
string allProperties = $"{DestinationPath}_{SearchPattern}_{Interval}_{Recursive}_{string.Join(",", Subfolders)}";
26+
string hash;
27+
using (var sha256 = System.Security.Cryptography.SHA256.Create())
28+
{
29+
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(allProperties);
30+
byte[] hashBytes = sha256.ComputeHash(bytes);
31+
hash = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
32+
}
33+
return $"{readableInfo}_{hash}";
34+
}
2035
}
2136
}

FileSyncLibNet/Commons/IFileJobOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ public interface IFileJobOptions
1717
bool Recursive { get; set; }
1818
string SearchPattern { get; set; }
1919
List<string> Subfolders { get; set; }
20+
string GetHashedName();
2021
}
2122
}

FileSyncLibNet/SyncProviders/AbstractProvider.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,21 @@ 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.Credentials, jobOptions.Logger);
21+
SourceAccess = new ScpAccessProvider(syncJobOptions.Credentials, jobOptions.Logger, stateFilename: null);
2222
}
2323
else
2424
{
25-
SourceAccess = new FileIoAccessProvider(jobOptions.Logger);
25+
SourceAccess = new FileIoAccessProvider(jobOptions.Logger, stateFilename: null);
2626
}
2727
SourceAccess.UpdateAccessPath(syncJobOptions.SourcePath);
28+
string stateFilename = syncJobOptions.RememberRemoteState ? syncJobOptions.GetHashedName() : null;
2829
if (syncJobOptions.DestinationPath.StartsWith("scp:"))
2930
{
30-
DestinationAccess = new ScpAccessProvider(syncJobOptions.Credentials, jobOptions.Logger);
31+
DestinationAccess = new ScpAccessProvider(syncJobOptions.Credentials, jobOptions.Logger, stateFilename);
3132
}
3233
else
3334
{
34-
DestinationAccess = new FileIoAccessProvider(jobOptions.Logger);
35+
DestinationAccess = new FileIoAccessProvider(jobOptions.Logger, stateFilename);
3536
}
3637

3738
}
@@ -57,7 +58,13 @@ public override void SyncSourceToDest()
5758
bool createDestinationDir = true;
5859
int copied = 0;
5960
int skipped = 0;
60-
var minimumLastWriteTime = jobOptions.RememberLastSync ? (LastRun - jobOptions.Interval - jobOptions.Interval) : DateTime.MinValue;
61+
var minimumLastWriteTime = jobOptions.RememberLastSync ?
62+
(LastRun - jobOptions.Interval - jobOptions.Interval) :
63+
(jobOptions.MaxAge < jobOptions.Interval ?
64+
DateTimeOffset.MinValue :
65+
DateTimeOffset.Now - jobOptions.MaxAge
66+
);
67+
6168
bool error_occured = false;
6269
try
6370
{

0 commit comments

Comments
 (0)