This commit is contained in:
2025-05-12 11:09:52 -07:00
parent 631d47ddf9
commit 33f550a51f
5 changed files with 40 additions and 109 deletions

View File

@ -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<Record> 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<Record> 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<Record> 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<Record> records = Record.GetRecords(matcher, directory);
if (records.Count == 0)
throw new Exception("No source records");
Review review = Review.Get(relativePath, records);