refactor(archival): ♻ push Id -> FileName translation to persistence layer

This commit is contained in:
Collin M. Barrett 2020-11-15 14:14:41 -06:00
parent dc059bdc37
commit beb172ba76
5 changed files with 19 additions and 33 deletions

View file

@ -84,8 +84,7 @@ private ListArchive GetList(
CancellationToken cancellationToken)
{
var segmentsAsync = GetSegmentsAsync(segmentUrls, cancellationToken);
var target = new ListFileName(listId);
return new ListArchive(segmentsAsync, target);
return new ListArchive(listId, segmentsAsync);
}
private async IAsyncEnumerable<ListArchiveSegment> GetSegmentsAsync(

View file

@ -4,13 +4,14 @@ namespace FilterLists.Archival.Domain.ListArchives
{
public class ListArchive
{
public ListArchive(IAsyncEnumerable<ListArchiveSegment> segments, ListFileName targetFileName)
public ListArchive(int id, IAsyncEnumerable<ListArchiveSegment> segments)
{
Id = id;
Segments = segments;
TargetFileName = targetFileName;
}
public int Id { get; }
public IAsyncEnumerable<ListArchiveSegment> Segments { get; }
public ListFileName TargetFileName { get; }
}
}

View file

@ -12,6 +12,7 @@ public ListArchiveSegment(Uri sourceUri, Stream content)
}
public ListFileExtension Extension { get; }
public Stream Content { get; }
}
}

View file

@ -1,21 +0,0 @@
using System.Collections.Generic;
using System.Globalization;
using FilterLists.Archival.Domain.SeedWork;
namespace FilterLists.Archival.Domain.ListArchives
{
public sealed class ListFileName : ValueObject
{
public ListFileName(int listId)
{
Value = listId.ToString(CultureInfo.InvariantCulture).PadLeft(5, '0');
}
public string Value { get; }
protected override IEnumerable<object> GetEqualityComponents()
{
return new[] {Value};
}
}
}

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
@ -39,19 +40,19 @@ public async Task AddAsync(ListArchive listArchive, CancellationToken cancellati
if (strategy is default(IStreamToPlainTextConversionStrategy))
{
_logger.LogWarning(
"No stream to plain text conversion strategy found for extension {Extension} for target {Target}. Skipping list",
"No stream to plain text conversion strategy found for extension {Extension} for list {ListId}. Skipping list",
segment.Extension,
listArchive.TargetFileName.Value);
listArchive.Id);
return;
}
var fileInfo = GetTargetFile(listArchive.TargetFileName, segmentNumber, segment.Extension);
var fileInfo = GetTargetFile(listArchive.Id, segmentNumber, segment.Extension);
if (fileInfo is default(FileInfo))
{
_logger.LogWarning(
"Writing from non-plain text extension {Extension} for target {Target} not yet supported. Skipping list",
"Writing from non-plain text extension {Extension} for list {ListId} not yet supported. Skipping list",
segment.Extension,
listArchive.TargetFileName.Value);
listArchive.Id);
return;
}
@ -99,7 +100,7 @@ public void Dispose()
_repo.CheckoutPaths("HEAD", _writtenFiles.Select(f => f.Name));
}
private FileInfo? GetTargetFile(ListFileName baseTargetFileName, int segmentNumber, ListFileExtension extension)
private FileInfo? GetTargetFile(int listId, int segmentNumber, ListFileExtension extension)
{
string targetExtension;
if (extension.IsPlainText)
@ -112,10 +113,15 @@ public void Dispose()
return default;
}
var targetFileName = baseTargetFileName.Value +
var targetFileName = GetTargetFileNamePrefix(listId) +
(segmentNumber == 1 ? string.Empty : $"-{segmentNumber}") +
targetExtension;
return new FileInfo(Path.Combine(_options.RepositoryPath, targetFileName));
}
private static string GetTargetFileNamePrefix(int listId)
{
return listId.ToString(CultureInfo.InvariantCulture).PadLeft(5, '0');
}
}
}