From 33f550a51fbb4e961dc6f67da5ca37fd4d361a79 Mon Sep 17 00:00:00 2001 From: Mike Phares Date: Mon, 12 May 2025 11:09:52 -0700 Subject: [PATCH] Remove --- Models/Record.cs | 20 ++------ Models/RelativePath.cs | 3 +- Models/Review.cs | 96 +++++--------------------------------- Models/Segment.cs | 4 +- Models/SyncV1Repository.cs | 26 +++++++++-- 5 files changed, 40 insertions(+), 109 deletions(-) diff --git a/Models/Record.cs b/Models/Record.cs index 2326b20..4b93349 100644 --- a/Models/Record.cs +++ b/Models/Record.cs @@ -11,35 +11,23 @@ public record Record(string RelativePath, long Ticks) { - internal static ReadOnlyCollection GetCollection(SyncConfiguration syncConfiguration, string rightDirectory) - { - ReadOnlyCollection results; - Matcher matcher = new(); - string excludePatternsFile = Path.Combine(rightDirectory, syncConfiguration.ExcludePatternsFile); - string includePatternsFile = Path.Combine(rightDirectory, syncConfiguration.IncludePatternsFile); - matcher.AddIncludePatterns(!File.Exists(includePatternsFile) ? ["*"] : File.ReadAllLines(includePatternsFile)); - matcher.AddExcludePatterns(!File.Exists(excludePatternsFile) ? ["System Volume Information"] : File.ReadAllLines(excludePatternsFile)); - results = GetRecords(rightDirectory, matcher); - return results; - } - - private static ReadOnlyCollection GetRecords(string rightDirectory, Matcher matcher) + internal static ReadOnlyCollection GetRecords(Matcher matcher, string directory) { List results = []; Record record; FileInfo fileInfo; string relativePath; - ReadOnlyCollection> collection = GetFilesCollection(rightDirectory, "*", "*"); + ReadOnlyCollection> collection = GetFilesCollection(directory, "*", "*"); foreach (ReadOnlyCollection c in collection) { foreach (string f in c) { - if (!matcher.Match(rightDirectory, f).HasMatches) + if (!matcher.Match(directory, f).HasMatches) continue; fileInfo = new(f); if (fileInfo.Length == 0) continue; - relativePath = Path.GetRelativePath(rightDirectory, fileInfo.FullName); + relativePath = Path.GetRelativePath(directory, fileInfo.FullName); record = new(RelativePath: relativePath, Size: fileInfo.Length, Ticks: fileInfo.LastWriteTime.ToUniversalTime().Ticks); diff --git a/Models/RelativePath.cs b/Models/RelativePath.cs index a49b95e..e950281 100644 --- a/Models/RelativePath.cs +++ b/Models/RelativePath.cs @@ -5,7 +5,8 @@ using System.Threading.Tasks; namespace FileExposer.Models; -public record RelativePath(string Path, +public record RelativePath(string LeftDirectory, + string? RightDirectory, Record[] Records) { diff --git a/Models/Review.cs b/Models/Review.cs index 1fbb0b9..a4cc35d 100644 --- a/Models/Review.cs +++ b/Models/Review.cs @@ -36,29 +36,17 @@ public record Review(Segment[]? AreEqual, private static ReadOnlyCollection GetAreEqual(RelativePath relativePath, ReadOnlyCollection records) { List results = []; - Record? record; Segment segment; double totalSeconds; - string? checkDirectory = null; ReadOnlyDictionary keyValuePairs = GetKeyValuePairs(relativePath); foreach (Record r in records) { - if (checkDirectory is null && r.Size == 0 && r.Ticks == 0) - { - checkDirectory = r.RelativePath; - continue; - } - if (r.RelativePath == relativePath.Path) - continue; - if (!keyValuePairs.TryGetValue(r.RelativePath, out record)) + if (!keyValuePairs.TryGetValue(r.RelativePath, out Record? record)) continue; totalSeconds = new TimeSpan(record.Ticks - r.Ticks).TotalSeconds; if (record.Size != r.Size || totalSeconds is > 2 or < -2) continue; - segment = new(Left: r, - LeftDirectory: checkDirectory, - Right: record, - RightDirectory: relativePath.Path); + segment = new(Left: r, Right: record); results.Add(segment); } return results.AsReadOnly(); @@ -78,31 +66,19 @@ public record Review(Segment[]? AreEqual, private static ReadOnlyCollection GetNotEqualBut(RelativePath relativePath, ReadOnlyCollection records) { List results = []; - Record? record; Segment segment; double totalSeconds; - string? checkDirectory = null; ReadOnlyDictionary keyValuePairs = GetKeyValuePairs(relativePath); foreach (Record r in records) { - if (checkDirectory is null && r.Size == 0 && r.Ticks == 0) - { - checkDirectory = r.RelativePath; - continue; - } - if (r.RelativePath == relativePath.Path) - continue; - if (!keyValuePairs.TryGetValue(r.RelativePath, out record)) + if (!keyValuePairs.TryGetValue(r.RelativePath, out Record? record)) continue; if (record.Size == r.Size) continue; totalSeconds = new TimeSpan(record.Ticks - r.Ticks).TotalSeconds; if (totalSeconds is >= 2 or <= -2) continue; - segment = new(Left: r, - LeftDirectory: checkDirectory, - Right: record, - RightDirectory: relativePath.Path); + segment = new(Left: r, Right: record); results.Add(segment); } return results.AsReadOnly(); @@ -111,25 +87,13 @@ public record Review(Segment[]? AreEqual, private static ReadOnlyCollection GetLeftSideOnly(RelativePath relativePath, ReadOnlyCollection records) { List results = []; - Record? record; Segment segment; - string? checkDirectory = null; ReadOnlyDictionary keyValuePairs = GetKeyValuePairs(relativePath); foreach (Record r in records) { - if (checkDirectory is null && r.Size == 0 && r.Ticks == 0) - { - checkDirectory = r.RelativePath; + if (keyValuePairs.TryGetValue(r.RelativePath, out Record? record)) continue; - } - if (r.RelativePath == relativePath.Path) - continue; - if (keyValuePairs.TryGetValue(r.RelativePath, out record)) - continue; - segment = new(Left: r, - LeftDirectory: checkDirectory, - Right: record, - RightDirectory: relativePath.Path); + segment = new(Left: r, Right: record); results.Add(segment); } return results.AsReadOnly(); @@ -138,25 +102,13 @@ public record Review(Segment[]? AreEqual, private static ReadOnlyCollection GetRightSideOnly(RelativePath relativePath, ReadOnlyCollection records) { List results = []; - Record? record; Segment segment; - string? checkDirectory = null; ReadOnlyDictionary keyValuePairs = GetKeyValuePairs(records); foreach (Record r in relativePath.Records) { - if (checkDirectory is null && r.Size == 0 && r.Ticks == 0) - { - checkDirectory = r.RelativePath; + if (keyValuePairs.TryGetValue(r.RelativePath, out Record? record)) continue; - } - if (r.RelativePath == relativePath.Path) - continue; - if (keyValuePairs.TryGetValue(r.RelativePath, out record)) - continue; - segment = new(Left: record, - LeftDirectory: null, - Right: r, - RightDirectory: relativePath.Path); + segment = new(Left: record, Right: r); results.Add(segment); } return results.AsReadOnly(); @@ -165,29 +117,17 @@ public record Review(Segment[]? AreEqual, private static ReadOnlyCollection GetLeftSideIsNewer(RelativePath relativePath, ReadOnlyCollection records) { List results = []; - Record? record; Segment segment; double totalSeconds; - string? checkDirectory = null; ReadOnlyDictionary keyValuePairs = GetKeyValuePairs(relativePath); foreach (Record r in records) { - if (checkDirectory is null && r.Size == 0 && r.Ticks == 0) - { - checkDirectory = r.RelativePath; - continue; - } - if (r.RelativePath == relativePath.Path) - continue; - if (!keyValuePairs.TryGetValue(r.RelativePath, out record)) + if (!keyValuePairs.TryGetValue(r.RelativePath, out Record? record)) continue; totalSeconds = new TimeSpan(record.Ticks - r.Ticks).TotalSeconds; if (totalSeconds is > -2) continue; - segment = new(Left: r, - LeftDirectory: checkDirectory, - Right: record, - RightDirectory: relativePath.Path); + segment = new(Left: r, Right: record); results.Add(segment); } return results.AsReadOnly(); @@ -196,29 +136,17 @@ public record Review(Segment[]? AreEqual, private static ReadOnlyCollection GetRightSideIsNewer(RelativePath relativePath, ReadOnlyCollection records) { List results = []; - Record? record; Segment segment; double totalSeconds; - string? checkDirectory = null; ReadOnlyDictionary keyValuePairs = GetKeyValuePairs(records); foreach (Record r in relativePath.Records) { - if (checkDirectory is null && r.Size == 0 && r.Ticks == 0) - { - checkDirectory = r.RelativePath; - continue; - } - if (r.RelativePath == relativePath.Path) - continue; - if (!keyValuePairs.TryGetValue(r.RelativePath, out record)) + if (!keyValuePairs.TryGetValue(r.RelativePath, out Record? record)) continue; totalSeconds = new TimeSpan(record.Ticks - r.Ticks).TotalSeconds; if (totalSeconds is > -2) continue; - segment = new(Left: record, - LeftDirectory: null, - Right: r, - RightDirectory: relativePath.Path); + segment = new(Left: record, Right: r); results.Add(segment); } return results.AsReadOnly(); diff --git a/Models/Segment.cs b/Models/Segment.cs index 6f1957c..75a73ef 100644 --- a/Models/Segment.cs +++ b/Models/Segment.cs @@ -3,9 +3,7 @@ using System.Text.Json.Serialization; namespace FileExposer.Models; public record Segment(Record? Left, - string? LeftDirectory, - Record? Right, - string RightDirectory); + Record? Right); [JsonSourceGenerationOptions(WriteIndented = true)] [JsonSerializable(typeof(Segment))] diff --git a/Models/SyncV1Repository.cs b/Models/SyncV1Repository.cs index f04bbd8..6f57ecd 100644 --- a/Models/SyncV1Repository.cs +++ b/Models/SyncV1Repository.cs @@ -5,6 +5,7 @@ using System.Collections.ObjectModel; using System.IO; using System.Text.Json; using System.Threading.Tasks; +using Microsoft.Extensions.FileSystemGlobbing; public class SyncV1Repository(AppSettings appSettings) : ISyncV1Repository { @@ -28,20 +29,31 @@ public class SyncV1Repository(AppSettings appSettings) : ISyncV1Repository if (size is not null && ticks is not null) { FileInfo fileInfo = Verify(path, size.Value, ticks.Value); - result = new (JSON: null, Bytes: File.ReadAllBytes(fileInfo.FullName)); + result = new(JSON: null, Bytes: File.ReadAllBytes(fileInfo.FullName)); } else { if (File.Exists(path)) throw new Exception("Must pass size and ticks when passing a file!"); string directory = Path.GetFullPath(path); - ReadOnlyCollection records = Record.GetCollection(_AppSettings.SyncConfiguration, directory); - RelativePath relativePath = new(Path: path, Records: [.. records]); - result = new (JSON: JsonSerializer.Serialize(relativePath, RelativePathSourceGenerationContext.Default.RelativePath), Bytes: null); + string excludePatternsFile = Path.Combine(directory, _AppSettings.SyncConfiguration.ExcludePatternsFile); + string includePatternsFile = Path.Combine(directory, _AppSettings.SyncConfiguration.IncludePatternsFile); + Matcher matcher = GetMatcher(excludePatternsFile, includePatternsFile); + ReadOnlyCollection records = Record.GetRecords(matcher, directory); + RelativePath relativePath = new(LeftDirectory: path, Records: [.. records], RightDirectory: null); + result = new(JSON: JsonSerializer.Serialize(relativePath, RelativePathSourceGenerationContext.Default.RelativePath), Bytes: null); } return result; } + private static Matcher GetMatcher(string excludePatternsFile, string includePatternsFile) + { + Matcher result = new(); + result.AddIncludePatterns(!File.Exists(includePatternsFile) ? ["*"] : File.ReadAllLines(includePatternsFile)); + result.AddExcludePatterns(!File.Exists(excludePatternsFile) ? ["System Volume Information"] : File.ReadAllLines(excludePatternsFile)); + return result; + } + private FileInfo Verify(string path, long size, long ticks) { FileInfo fileInfo = new(path); @@ -58,7 +70,11 @@ public class SyncV1Repository(AppSettings appSettings) : ISyncV1Repository string result; RelativePath? relativePath = RelativePath.Get(stream) ?? throw new MissingFieldException(); - ReadOnlyCollection records = Record.GetCollection(_AppSettings.SyncConfiguration, relativePath.Path); + string directory = Path.GetFullPath(relativePath.LeftDirectory); + string excludePatternsFile = Path.Combine(directory, _AppSettings.SyncConfiguration.ExcludePatternsFile); + string includePatternsFile = Path.Combine(directory, _AppSettings.SyncConfiguration.IncludePatternsFile); + Matcher matcher = GetMatcher(excludePatternsFile, includePatternsFile); + ReadOnlyCollection records = Record.GetRecords(matcher, directory); if (records.Count == 0) throw new Exception("No source records"); Review review = Review.Get(relativePath, records);