Skip to content

Commit

Permalink
Merge pull request #8 from andreaskueffel/development
Browse files Browse the repository at this point in the history
add withSubfolder
  • Loading branch information
andreaskueffel authored Sep 8, 2023
2 parents 1ac0773 + 36c72f9 commit 225b00f
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 52 deletions.
7 changes: 6 additions & 1 deletion FileSyncJob/FileSyncJobOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Net;

namespace FileSyncLibNet.FileSyncJob
Expand All @@ -12,11 +13,15 @@ public class FileSyncJobOptions : IFileSyncJobOptions
public NetworkCredential Credentials { get; set; }
public TimeSpan Interval { get; set; } = TimeSpan.Zero;
public string SearchPattern { get; set; } = "*.*";
public List<string> Subfolders { get; set; }
public bool Recursive { get; set; } = true;
public bool SyncDeleted { get; set; } = false;
public FileSyncProvider FileSyncProvider { get; set; } = FileSyncProvider.FileIO;

public FileSyncJobOptions() { }
public FileSyncJobOptions()
{
Subfolders = new List<string>();
}

}
}
6 changes: 6 additions & 0 deletions FileSyncJob/FileSyncJobOptionsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public IFileSyncJobOptionsBuilderSetProperties WithSearchPattern(string searchPa
jobOptions.SearchPattern = searchPattern;
return this;
}
public IFileSyncJobOptionsBuilderSetProperties WithSubfolder(string subfolder)
{
jobOptions.Subfolders.Add(subfolder);
return this;
}
public IFileSyncJobOptionsBuilderSetProperties WithInterval(TimeSpan interval)
{
jobOptions.Interval = interval;
Expand Down Expand Up @@ -118,6 +123,7 @@ public interface IFileSyncJobOptionsBuilderSetProperties : IFileSyncJobOptionsBu
IFileSyncJobOptionsBuilderSetProperties WithCredentials(string username, string password);
IFileSyncJobOptionsBuilderSetProperties WithCredentials(NetworkCredential networkCredential);
IFileSyncJobOptionsBuilderSetProperties WithSearchPattern(string searchPattern);
IFileSyncJobOptionsBuilderSetProperties WithSubfolder(string subfolder);
IFileSyncJobOptionsBuilderSetProperties WithLogger(ILogger logger);
IFileSyncJobOptionsBuilderSetProperties WithLogger(Action<string> loggerAction);
}
Expand Down
2 changes: 2 additions & 0 deletions FileSyncJob/IFileSyncJobOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Net;

namespace FileSyncLibNet.FileSyncJob
Expand All @@ -13,6 +14,7 @@ public interface IFileSyncJobOptions
ILogger Logger { get; set; }
bool Recursive { get; set; }
string SearchPattern { get; set; }
List<string> Subfolders { get; set; }
string SourcePath { get; set; }
bool SyncDeleted { get; set; }
}
Expand Down
192 changes: 170 additions & 22 deletions SyncProviders/FileIOProvider.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using FileSyncLibNet.FileSyncJob;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;

namespace FileSyncLibNet.SyncProviders
{
Expand All @@ -22,38 +26,47 @@ public override void SyncSourceToDest()
int copied = 0;
int skipped = 0;
DirectoryInfo _di = new DirectoryInfo(JobOptions.SourcePath);
var _fi = _di.EnumerateFiles(
searchPattern: JobOptions.SearchPattern,
searchOption: JobOptions.Recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
//Dateien ins Backup kopieren
if (JobOptions.Credentials != null)
{

}

foreach (FileInfo f in _fi)
}


foreach (var dir in JobOptions.Subfolders.Count > 0 ? _di.GetDirectories() : new[] {_di})
{
var relativeFilename = f.FullName.Substring(Path.GetFullPath(JobOptions.SourcePath).Length).TrimStart('\\');
var remotefile = new FileInfo(Path.Combine(JobOptions.DestinationPath, relativeFilename));
bool copy = !remotefile.Exists || remotefile.Length != f.Length || remotefile.LastWriteTime != f.LastWriteTime;
if (copy)
if (JobOptions.Subfolders.Count > 0 && !JobOptions.Subfolders.Select(x => x.ToLower()).Contains(dir.Name.ToLower()))
continue;
var _fi = dir.EnumerateFiles(
searchPattern: JobOptions.SearchPattern,
searchOption: JobOptions.Recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);


foreach (FileInfo f in _fi)
{
try

var relativeFilename = f.FullName.Substring(Path.GetFullPath(JobOptions.SourcePath).Length).TrimStart('\\');
var remotefile = new FileInfo(Path.Combine(JobOptions.DestinationPath, relativeFilename));
bool copy = !remotefile.Exists || remotefile.Length != f.Length || remotefile.LastWriteTime != f.LastWriteTime;
if (copy)
{
logger.LogDebug("Copy {A}", relativeFilename);
File.Copy(f.FullName, remotefile.FullName, true);
copied++;
try
{
logger.LogDebug("Copy {A}", relativeFilename);
File.Copy(f.FullName, remotefile.FullName, true);
copied++;
}
catch (Exception exc)
{
logger.LogError(exc, "Exception copying {A}", relativeFilename);
}
}
catch(Exception exc)
else
{
logger.LogError(exc, "Exception copying {A}", relativeFilename);
skipped++;
logger.LogTrace("Skip {A}", relativeFilename);
}
}
else
{
skipped++;
logger.LogTrace("Skip {A}", relativeFilename);
}
}
sw.Stop();
logger.LogInformation("{A} files copied, {B} files skipped in {C}s", copied, skipped, sw.ElapsedMilliseconds / 1000.0);
Expand All @@ -74,7 +87,142 @@ void CopyFileWithBuffer(FileInfo source, FileInfo destination)
//destination.Attributes = source.Attributes;

}

void EnsureAccess()
{
//#region Backup
//if (!string.IsNullOrWhiteSpace(einst.BasicSettings["BackupPath"]))
//{
// //Dateien sichern:
// bool useCredentials = ((string)einst.BasicSettings["BackupPath"]).StartsWith(@"\\");
// string backupTarget = einst.BasicSettings["BackupPath"];

// if (backupTarget.StartsWith(@"\\")) //Netzwerk
// {
// try
// {
// //Probe network access:
// bool accesible = false;
// bool writeable = false;
// try
// {
// DirectoryInfo di = new DirectoryInfo(backupTarget);
// while (di != null && !di.Exists)
// {
// di = di.Parent;
// }
// if (di != null && di.Exists)
// accesible = true;
// if (accesible)
// {
// Directory.CreateDirectory(backupTarget);
// File.WriteAllText(backupTarget + "\\hri_write_probing", "probing write access");
// try
// {
// File.Delete(backupTarget + "\\hri_write_probing");
// }
// catch (Exception exc)
// {
// log.LogWarning(exc, "Exception when deleting 'hri_write_probing' file, assuming writable");
// }
// writeable = true;
// }
// }
// catch { }
// if (!accesible || !writeable)
// {
// DirectoryInfo di = new DirectoryInfo(backupTarget);
// string netpath = di.Root.FullName;

// if (accesible) //Change credentials..
// {
// NetworkShare.CancelExistingConnection(netpath);
// }

// string username = ((string)Einstellungen.BasicSettings["NetworkCredentials"]).Split(':')[0];
// string password = ((string)Einstellungen.BasicSettings["NetworkCredentials"]).Split(':')[1];
// NetworkCredential networkCredential = new NetworkCredential(username, password);
// NetworkShare net = null;
// try { net = new NetworkShare(netpath, networkCredential, true); }
// catch (Win32Exception exception) { log.LogInformation("Backup: Fehler beim herstellen einer Netzwerkverbindung zu " + netpath + ":\r\n" + exception.ToString()); }
// }
// }
// catch (Exception e)
// {
// log.LogError("backuptimer_tick", e);
// log.LogInformation("Backup: Fehler beim Zugriff auf Netzlaufwerk:\r\n" + e.ToString());
// }

// }
// //else //Filesystem
// {
// try
// {
// //Beim Netzlaufwerk versuchen es erneut zu verbinden
// string drive = backupTarget.Split(':')[0];
// //if (!Map(drive + ":"))
// // log.LogInformation("Fehler beim mappen des Laufwerkes " + drive + ": - Bitte Einstellungen überprüfen");
// bool canWrite = true;
// if (!Directory.Exists(backupTarget))
// {
// try
// {
// Directory.CreateDirectory(backupTarget);
// }
// catch { log.LogInformation($"Konnte nicht auf Backuppfad {backupTarget} schreiben"); canWrite = false; }
// }
// if (canWrite)
// foreach (string folder in new string[] { "HriFFTLog", "HriShockLog", "HriLog", "HriDebugLog", "HRI" })
// {
// string _path = Path.Combine(ProductionDataPath, folder);
// if (!Directory.Exists(_path))
// Directory.CreateDirectory(_path);
// string destPath = Path.Combine(backupTarget, folder);
// if (Directory.Exists(backupTarget))
// if (!Directory.Exists(destPath))
// Directory.CreateDirectory(destPath);
// DirectoryInfo _di = new DirectoryInfo(_path);
// FileInfo[] _fi = _di.GetFiles();
// //Dateien ins Backup kopieren
// foreach (FileInfo f in _fi)
// {
// bool copy = false;
// string remotefilename = Path.Combine(destPath, f.Name);
// if (!File.Exists(remotefilename))
// {
// copy = true;
// }
// else
// {
// FileInfo remoteFile = new FileInfo(remotefilename);
// if (remoteFile.Length != f.Length)
// copy = true;
// }
// if (copy)
// {
// File.Copy(f.FullName, remotefilename, true);
// Thread.Sleep(15);
// }
// else
// {
// Thread.Sleep(5);
// }
// }
// }


// }
// catch (Exception e)
// {
// log.LogError("backuptimer_tick", e);
// log.LogInformation("Fehler beim erstellen des Backups!\r\n" + e.ToString());
// }
// }
//}
//#endregion


}




Expand Down
Loading

0 comments on commit 225b00f

Please sign in to comment.