|
| 1 | +using FileSyncLibNet.AccessProviders; |
| 2 | +using FileSyncLibNet.Commons; |
| 3 | +using FileSyncLibNet.FileSyncJob; |
| 4 | +using Microsoft.Extensions.Logging; |
| 5 | +using System; |
| 6 | +using System.Diagnostics; |
| 7 | + |
| 8 | +namespace FileSyncLibNet.SyncProviders |
| 9 | +{ |
| 10 | + internal class AbstractProvider : ProviderBase |
| 11 | + { |
| 12 | + IAccessProvider SourceAccess { get; set; } |
| 13 | + IAccessProvider DestinationAccess { get; set; } |
| 14 | + |
| 15 | + public AbstractProvider(IFileJobOptions jobOptions) : base(jobOptions) |
| 16 | + { |
| 17 | + if (!(JobOptions is IFileSyncJobOptions syncJobOptions)) |
| 18 | + throw new ArgumentException("this instance has no information about syncing files, it has type " + JobOptions.GetType().ToString()); |
| 19 | + if (syncJobOptions.SourcePath.StartsWith("scp:")) |
| 20 | + { |
| 21 | + SourceAccess = new ScpAccessProvider(syncJobOptions.SourcePath, syncJobOptions.Credentials); |
| 22 | + } |
| 23 | + else |
| 24 | + { |
| 25 | + SourceAccess = new FileIoAccessProvider(syncJobOptions.SourcePath); |
| 26 | + } |
| 27 | + if (syncJobOptions.DestinationPath.StartsWith("scp:")) |
| 28 | + { |
| 29 | + DestinationAccess = new ScpAccessProvider(syncJobOptions.DestinationPath, syncJobOptions.Credentials); |
| 30 | + } |
| 31 | + else |
| 32 | + { |
| 33 | + DestinationAccess = new FileIoAccessProvider(syncJobOptions.DestinationPath); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public override void DeleteFiles() |
| 38 | + { |
| 39 | + throw new NotImplementedException(); |
| 40 | + } |
| 41 | + |
| 42 | + public override void SyncSourceToDest() |
| 43 | + { |
| 44 | + if (!(JobOptions is IFileSyncJobOptions jobOptions)) |
| 45 | + throw new ArgumentException("this instance has no information about syncing files, it has type " + JobOptions.GetType().ToString()); |
| 46 | + var sw = Stopwatch.StartNew(); |
| 47 | + try |
| 48 | + { |
| 49 | + DestinationAccess.CreateDirectory(""); |
| 50 | + } |
| 51 | + catch (Exception ex) { logger?.LogError(ex, "exception creating destination directory {A}", jobOptions.DestinationPath); } |
| 52 | + if (JobOptions.Credentials != null) |
| 53 | + { |
| 54 | + |
| 55 | + } |
| 56 | + int copied = 0; |
| 57 | + int skipped = 0; |
| 58 | + var minimumLastWriteTime = jobOptions.RememberLastSync ? (LastRun - jobOptions.Interval - jobOptions.Interval) : DateTime.MinValue; |
| 59 | + bool error_occured = false; |
| 60 | + try |
| 61 | + { |
| 62 | + var sourceFiles = SourceAccess.GetFiles(minimumLastWriteTime.DateTime, JobOptions.SearchPattern, recursive: JobOptions.Recursive, subfolders: JobOptions.Subfolders); |
| 63 | + |
| 64 | + foreach (var sourceFile in sourceFiles) |
| 65 | + { |
| 66 | + var remoteFile = DestinationAccess.GetFileInfo(sourceFile.Name); |
| 67 | + bool copy = !remoteFile.Exists || remoteFile.Length != sourceFile.Length || remoteFile.LastWriteTime != sourceFile.LastWriteTime; |
| 68 | + if (copy) |
| 69 | + { |
| 70 | + try |
| 71 | + { |
| 72 | + logger.LogDebug("Copy {A}", sourceFile.Name); |
| 73 | + using (var sourceStream = SourceAccess.GetStream(sourceFile)) |
| 74 | + { |
| 75 | + DestinationAccess.WriteFile(sourceFile, sourceStream); |
| 76 | + } |
| 77 | + //file.copy |
| 78 | + copied++; |
| 79 | + if (jobOptions.DeleteSourceAfterBackup) |
| 80 | + { |
| 81 | + SourceAccess.Delete(sourceFile); |
| 82 | + } |
| 83 | + } |
| 84 | + catch (Exception exc) |
| 85 | + { |
| 86 | + error_occured = true; |
| 87 | + logger.LogError(exc, "Exception copying {A}", sourceFile); |
| 88 | + } |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + |
| 93 | + skipped++; |
| 94 | + logger.LogTrace("Skip {A}", sourceFile); |
| 95 | + } |
| 96 | + |
| 97 | + } |
| 98 | + if (!error_occured) |
| 99 | + { |
| 100 | + LastRun = DateTimeOffset.Now; |
| 101 | + } |
| 102 | + } |
| 103 | + catch(Exception exc) |
| 104 | + { |
| 105 | + logger.LogError(exc, "Exception in main logic of abstract provider"); |
| 106 | + } |
| 107 | + |
| 108 | + sw.Stop(); |
| 109 | + logger.LogInformation("{A} files copied, {B} files skipped in {C}s", copied, skipped, sw.ElapsedMilliseconds / 1000.0); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments