122 lines
4.9 KiB
C#
122 lines
4.9 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Phares.Shared;
|
|
using Serilog;
|
|
using ShellProgressBar;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using View_by_Distance.Save.Image.Info.Models;
|
|
using View_by_Distance.Shared.Models;
|
|
using View_by_Distance.Shared.Models.Methods;
|
|
|
|
namespace View_by_Distance.Save.Image.Info;
|
|
|
|
public class SaveImageInfo
|
|
{
|
|
|
|
private readonly AppSettings _AppSettings;
|
|
private readonly string _WorkingDirectory;
|
|
private readonly Configuration _Configuration;
|
|
private readonly IsEnvironment _IsEnvironment;
|
|
private readonly IConfigurationRoot _ConfigurationRoot;
|
|
private readonly Property.Models.Configuration _PropertyConfiguration;
|
|
|
|
public SaveImageInfo(List<string> args, IsEnvironment isEnvironment, IConfigurationRoot configurationRoot, AppSettings appSettings, string workingDirectory, bool isSilent, IConsole console)
|
|
{
|
|
if (isSilent)
|
|
{ }
|
|
if (console is null)
|
|
{ }
|
|
_AppSettings = appSettings;
|
|
_IsEnvironment = isEnvironment;
|
|
_WorkingDirectory = workingDirectory;
|
|
_ConfigurationRoot = configurationRoot;
|
|
ILogger? log = Log.ForContext<SaveImageInfo>();
|
|
Property.Models.Configuration propertyConfiguration = Property.Models.Binder.Configuration.Get(isEnvironment, configurationRoot);
|
|
Configuration configuration = Models.Binder.Configuration.Get(isEnvironment, configurationRoot, propertyConfiguration);
|
|
_PropertyConfiguration = propertyConfiguration;
|
|
_Configuration = configuration;
|
|
propertyConfiguration.Update();
|
|
log.Information(propertyConfiguration.RootDirectory);
|
|
Verify();
|
|
List<string> lines = SaveImageInfoFilesInDirectories(log);
|
|
File.WriteAllLines($"D:/Tmp/Phares/{DateTime.Now.Ticks}.tsv", lines);
|
|
if (!lines.Any())
|
|
_ = Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(propertyConfiguration.RootDirectory);
|
|
}
|
|
|
|
private void Verify()
|
|
{
|
|
if (_AppSettings is null)
|
|
{ }
|
|
if (_IsEnvironment is null)
|
|
{ }
|
|
if (_Configuration is null)
|
|
{ }
|
|
if (_ConfigurationRoot is null)
|
|
{ }
|
|
if (_WorkingDirectory is null)
|
|
{ }
|
|
if (_PropertyConfiguration is null)
|
|
{ }
|
|
}
|
|
|
|
private StringBuilder GetLines(ProgressBar progressBar, List<string[]> filesCollection)
|
|
{
|
|
StringBuilder result = new();
|
|
string json;
|
|
string? directory;
|
|
ImageInfo imageInfo;
|
|
FileHolder[] fileHolders;
|
|
List<string> lines = new();
|
|
int rootDirectoryLength = _PropertyConfiguration.RootDirectory.Length;
|
|
foreach (string[] files in filesCollection)
|
|
{
|
|
lines.Clear();
|
|
progressBar.Tick();
|
|
fileHolders = (from l in files select new FileHolder(l)).ToArray();
|
|
foreach (FileHolder fileHolder in from l in fileHolders orderby l.Name.Length, l.Name select l)
|
|
{
|
|
if (fileHolder.ExtensionLowered == ".id" || fileHolder.ExtensionLowered == ".lsv" || fileHolder.DirectoryName is null)
|
|
continue;
|
|
imageInfo = new(fileHolder.CreationTime, fileHolder.LastWriteTime, fileHolder.Length, fileHolder.Name);
|
|
json = JsonSerializer.Serialize(imageInfo);
|
|
lines.Add(json);
|
|
}
|
|
if (lines.Any())
|
|
{
|
|
directory = Path.GetDirectoryName(files.First());
|
|
if (directory is null || directory.Length < rootDirectoryLength)
|
|
continue;
|
|
_ = result.AppendLine(directory[rootDirectoryLength..]);
|
|
foreach (string line in lines)
|
|
_ = result.AppendLine(line);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private void Save(StringBuilder stringBuilder)
|
|
{
|
|
string checkFile = Path.Combine(_PropertyConfiguration.RootDirectory, ".json");
|
|
string text = stringBuilder.ToString();
|
|
_ = Shared.Models.Stateless.Methods.IPath.WriteAllText(checkFile, text, updateToWhenMatches: null, compareBeforeWrite: true, updateDateWhenMatches: false);
|
|
}
|
|
|
|
private List<string> SaveImageInfoFilesInDirectories(ILogger log)
|
|
{
|
|
List<string> results = new();
|
|
ProgressBar progressBar;
|
|
const string fileSearchFilter = "*";
|
|
string message = nameof(SaveImageInfo);
|
|
const string directorySearchFilter = "*";
|
|
List<string> distinctDirectories = new();
|
|
List<string[]> filesCollection = Shared.Models.Stateless.Methods.IDirectory.GetFilesCollection(_PropertyConfiguration.RootDirectory, directorySearchFilter, fileSearchFilter);
|
|
ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true };
|
|
progressBar = new(filesCollection.Count, message, options);
|
|
StringBuilder lines = GetLines(progressBar, filesCollection);
|
|
progressBar.Dispose();
|
|
Save(lines);
|
|
return results;
|
|
}
|
|
|
|
} |