You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.1 KiB
38 lines
1.1 KiB
using System.IO.Abstractions;
|
|
using System.Text.Json;
|
|
using Recyclarr.Json;
|
|
|
|
namespace Recyclarr.Repo;
|
|
|
|
public class TrashRepoMetadataBuilder(ITrashGuidesRepo repo) : IRepoMetadataBuilder
|
|
{
|
|
private RepoMetadata? _metadata;
|
|
private readonly IDirectoryInfo _repoPath = repo.Path;
|
|
|
|
public IFileInfo MetadataPath => _repoPath.File("metadata.json");
|
|
public IDirectoryInfo DocsDirectory => _repoPath.SubDirectory("docs");
|
|
|
|
private static RepoMetadata Deserialize(IFileInfo jsonFile)
|
|
{
|
|
using var stream = jsonFile.OpenRead();
|
|
|
|
var obj = JsonSerializer.Deserialize<RepoMetadata>(stream, GlobalJsonSerializerSettings.Guide);
|
|
if (obj is null)
|
|
{
|
|
throw new InvalidDataException($"Unable to deserialize {jsonFile}");
|
|
}
|
|
|
|
return obj;
|
|
}
|
|
|
|
public IReadOnlyList<IDirectoryInfo> ToDirectoryInfoList(IEnumerable<string> listOfDirectories)
|
|
{
|
|
return listOfDirectories.Select(x => _repoPath.SubDirectory(x)).ToList();
|
|
}
|
|
|
|
public RepoMetadata GetMetadata()
|
|
{
|
|
return _metadata ??= Deserialize(MetadataPath);
|
|
}
|
|
}
|