144 lines
8.9 KiB
C#
144 lines
8.9 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Phares.Shared;
|
|
using System.Text.Json;
|
|
|
|
namespace View_by_Distance.Property.Models.Binder;
|
|
|
|
public class Configuration
|
|
{
|
|
|
|
public string? DateGroup { get; set; }
|
|
public string? FileNameDirectorySeparator { get; set; }
|
|
public bool? ForcePropertyLastWriteTimeToCreationTime { get; set; }
|
|
public string[]? IgnoreExtensions { get; set; }
|
|
public string[]? IgnoreRulesKeyWords { get; set; }
|
|
public int? IntMinValueLength { get; set; }
|
|
public int? MaxImagesInDirectoryForTopLevelFirstPass { get; set; }
|
|
public string? ModelName { init; get; }
|
|
public int? NumberOfJitters { init; get; }
|
|
public int? NumberOfTimesToUpsample { init; get; }
|
|
public int? Offset { init; get; }
|
|
public string? Pattern { get; set; }
|
|
public string? PersonBirthdayFormat { get; set; }
|
|
public bool? PopulatePropertyId { get; set; }
|
|
public string? PredictorModelName { get; set; }
|
|
public bool? PropertiesChangedForProperty { get; set; }
|
|
public string[]? PropertyContentCollectionFiles { get; set; }
|
|
public string? ResultAllInOne { get; set; }
|
|
public int? ResultAllInOneSubdirectoryLength { get; set; }
|
|
public string? ResultCollection { get; set; }
|
|
public string? ResultContent { get; set; }
|
|
public string? ResultContentCollection { get; set; }
|
|
public string? ResultSingleton { get; set; }
|
|
public string? RootDirectory { get; set; }
|
|
public string[]? ValidImageFormatExtensions { get; set; }
|
|
public string[]? ValidVideoFormatExtensions { get; set; }
|
|
public string[]? VerifyToSeason { get; set; }
|
|
|
|
public override string ToString()
|
|
{
|
|
string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });
|
|
return result;
|
|
}
|
|
|
|
private static void PreVerify(IConfigurationRoot configurationRoot, Configuration? configuration)
|
|
{
|
|
if (configuration?.DateGroup is null)
|
|
{
|
|
List<string> paths = [];
|
|
foreach (IConfigurationProvider configurationProvider in configurationRoot.Providers)
|
|
{
|
|
if (configurationProvider is not Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider jsonConfigurationProvider)
|
|
continue;
|
|
if (jsonConfigurationProvider.Source.FileProvider is not Microsoft.Extensions.FileProviders.PhysicalFileProvider physicalFileProvider)
|
|
continue;
|
|
paths.Add(physicalFileProvider.Root);
|
|
}
|
|
throw new NotSupportedException($"Not found!{Environment.NewLine}{string.Join(Environment.NewLine, paths.Distinct())}");
|
|
}
|
|
}
|
|
|
|
private static Models.Configuration Get(Configuration? configuration)
|
|
{
|
|
Models.Configuration result;
|
|
if (configuration is null) throw new NullReferenceException(nameof(configuration));
|
|
if (configuration.DateGroup is null) throw new NullReferenceException(nameof(configuration.DateGroup));
|
|
if (configuration.FileNameDirectorySeparator is null) throw new NullReferenceException(nameof(configuration.FileNameDirectorySeparator));
|
|
if (configuration.ForcePropertyLastWriteTimeToCreationTime is null) throw new NullReferenceException(nameof(configuration.ForcePropertyLastWriteTimeToCreationTime));
|
|
if (configuration.IgnoreExtensions is null) throw new NullReferenceException(nameof(configuration.IgnoreExtensions));
|
|
if (configuration.IgnoreRulesKeyWords is null) throw new NullReferenceException(nameof(configuration.IgnoreRulesKeyWords));
|
|
if (configuration.IntMinValueLength is null) throw new NullReferenceException(nameof(configuration.IntMinValueLength));
|
|
if (configuration.MaxImagesInDirectoryForTopLevelFirstPass is null) throw new NullReferenceException(nameof(configuration.MaxImagesInDirectoryForTopLevelFirstPass));
|
|
// if (configuration.ModelName is null) throw new NullReferenceException(nameof(configuration.ModelName));
|
|
// if (configuration.NumberOfJitters is null) throw new NullReferenceException(nameof(configuration.NumberOfJitters));
|
|
// if (configuration.NumberOfTimesToUpsample is null) throw new NullReferenceException(nameof(configuration.NumberOfTimesToUpsample));
|
|
if (configuration.Offset is null) throw new NullReferenceException(nameof(configuration.Offset)); ;
|
|
if (configuration.Pattern is null) throw new NullReferenceException(nameof(configuration.Pattern));
|
|
if (configuration.PersonBirthdayFormat is null) throw new NullReferenceException(nameof(configuration.PersonBirthdayFormat));
|
|
if (configuration.PopulatePropertyId is null) throw new NullReferenceException(nameof(configuration.PopulatePropertyId));
|
|
// if (configuration.PredictorModelName is null) throw new NullReferenceException(nameof(configuration.PredictorModelName));
|
|
if (configuration.PropertiesChangedForProperty is null) throw new NullReferenceException(nameof(configuration.PropertiesChangedForProperty));
|
|
// if (configuration.PropertyContentCollectionFiles is null) throw new NullReferenceException(nameof(configuration.PropertyContentCollectionFiles));
|
|
if (configuration.ResultAllInOne is null) throw new NullReferenceException(nameof(configuration.ResultAllInOne));
|
|
if (configuration.ResultAllInOneSubdirectoryLength is null) throw new NullReferenceException(nameof(configuration.ResultAllInOneSubdirectoryLength));
|
|
if (configuration.ResultCollection is null) throw new NullReferenceException(nameof(configuration.ResultCollection));
|
|
if (configuration.ResultContent is null) throw new NullReferenceException(nameof(configuration.ResultContent));
|
|
if (configuration.ResultContentCollection is null) throw new NullReferenceException(nameof(configuration.ResultContentCollection));
|
|
if (configuration.ResultSingleton is null) throw new NullReferenceException(nameof(configuration.ResultSingleton));
|
|
if (configuration.RootDirectory is null) throw new NullReferenceException(nameof(configuration.RootDirectory));
|
|
if (configuration.ValidImageFormatExtensions is null) throw new NullReferenceException(nameof(configuration.ValidImageFormatExtensions));
|
|
if (configuration.ValidVideoFormatExtensions is null) throw new NullReferenceException(nameof(configuration.ValidVideoFormatExtensions));
|
|
// if (configuration.VerifyToSeason is null) throw new NullReferenceException(nameof(configuration.VerifyToSeason));
|
|
result = new(configuration.DateGroup,
|
|
configuration.FileNameDirectorySeparator,
|
|
configuration.ForcePropertyLastWriteTimeToCreationTime.Value,
|
|
configuration.IgnoreExtensions,
|
|
configuration.IgnoreRulesKeyWords,
|
|
configuration.IntMinValueLength.Value,
|
|
configuration.MaxImagesInDirectoryForTopLevelFirstPass.Value,
|
|
configuration.ModelName,
|
|
configuration.NumberOfJitters,
|
|
configuration.NumberOfTimesToUpsample,
|
|
configuration.Offset.Value,
|
|
configuration.Pattern,
|
|
configuration.PersonBirthdayFormat,
|
|
configuration.PopulatePropertyId.Value,
|
|
configuration.PredictorModelName,
|
|
configuration.PropertiesChangedForProperty.Value,
|
|
configuration.PropertyContentCollectionFiles ?? [],
|
|
configuration.ResultAllInOne,
|
|
configuration.ResultAllInOneSubdirectoryLength.Value,
|
|
configuration.ResultCollection,
|
|
configuration.ResultContent,
|
|
configuration.ResultContentCollection,
|
|
configuration.ResultSingleton,
|
|
Path.GetFullPath(configuration.RootDirectory),
|
|
configuration.ValidImageFormatExtensions,
|
|
configuration.ValidVideoFormatExtensions,
|
|
configuration.VerifyToSeason ?? []);
|
|
return result;
|
|
}
|
|
|
|
public static Models.Configuration Get(IsEnvironment isEnvironment, IConfigurationRoot configurationRoot)
|
|
{
|
|
Models.Configuration result;
|
|
Configuration? configuration;
|
|
if (isEnvironment is null)
|
|
#pragma warning disable IL3050, IL2026
|
|
configuration = configurationRoot.Get<Configuration>();
|
|
#pragma warning restore IL3050, IL2026
|
|
else
|
|
{
|
|
string environmentName = IsEnvironment.GetEnvironmentName(isEnvironment);
|
|
string section = string.Concat(environmentName, ":", nameof(Configuration));
|
|
IConfigurationSection configurationSection = configurationRoot.GetSection(section);
|
|
#pragma warning disable IL3050, IL2026
|
|
configuration = configurationSection.Get<Configuration>();
|
|
#pragma warning restore IL3050, IL2026
|
|
}
|
|
PreVerify(configurationRoot, configuration);
|
|
result = Get(configuration);
|
|
return result;
|
|
}
|
|
|
|
} |