Files
file-folder-helper/ADO2025/PI6/Helper-2025-07-26.cs

106 lines
5.6 KiB
C#

using System.Collections.ObjectModel;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Logging;
using Phares.Shared.Models;
using Phares.Shared.Models.Stateless;
namespace File_Folder_Helper.ADO2025.PI6;
internal static partial class Helper20250726 {
private record Helper20250726Settings(ResultSettings? ResultSettings, MetadataSettings? MetadataSettings);
private record Record(CombinedEnumAndIndex CombinedEnumAndIndex, FilePath FilePath, bool HasFlagHidden);
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(Helper20250726Settings))]
private partial class Helper20250726SettingsSourceGenerationContext : JsonSerializerContext {
}
internal static void CopyToCombinedEnumAndIndexFormat(ILogger<Worker> logger, List<string> args) {
logger.LogInformation(args[0]);
logger.LogInformation(args[1]);
logger.LogInformation(args[2]);
logger.LogInformation(args[3]);
logger.LogInformation(args[4]);
string searchPattern = args[3];
string jsonFile = Path.GetFullPath(args[2]);
if (!File.Exists(jsonFile)) {
throw new Exception($"json file doesn't exist! <{jsonFile}>");
}
string json = File.ReadAllText(jsonFile);
string sourceDirectory = Path.GetFullPath(args[0].Split('~')[0]);
string destinationDirectory = Path.GetFullPath(args[4].Split('~')[0]);
Helper20250726Settings? settings = JsonSerializer.Deserialize(json, Helper20250726SettingsSourceGenerationContext.Default.Helper20250726Settings);
if (settings?.ResultSettings is null || settings.ResultSettings.ResultAllInOneSubdirectoryLength < 1 || settings.MetadataSettings is null) {
throw new Exception(nameof(Helper20250726Settings));
}
string[] files = Directory.GetFiles(sourceDirectory, searchPattern, SearchOption.AllDirectories);
ReadOnlyDictionary<int, ReadOnlyDictionary<byte, ReadOnlyCollection<string>>> keyValuePairs = GetKeyValuePairs(destinationDirectory, settings.ResultSettings);
ReadOnlyCollection<Record> records = GetRecords(logger, settings.ResultSettings, settings.MetadataSettings, files);
ReadOnlyDictionary<byte, ReadOnlyCollection<string>> keyValues = keyValuePairs.ElementAt(0).Value;
CopyToCombinedEnumAndIndexFormat(logger, records, keyValues);
Helpers.HelperDeleteEmptyDirectories.DeleteEmptyDirectories(logger, destinationDirectory);
}
private static ReadOnlyDictionary<int, ReadOnlyDictionary<byte, ReadOnlyCollection<string>>> GetKeyValuePairs(string destinationDirectory, ResultSettings resultSettings) {
Dictionary<int, ReadOnlyDictionary<byte, ReadOnlyCollection<string>>> results = [];
ReadOnlyDictionary<int, ReadOnlyDictionary<string, ReadOnlyDictionary<byte, ReadOnlyCollection<string>>>> keyValuePairs = IPath.GetKeyValuePairs(resultSettings, destinationDirectory, [resultSettings.ResultSingleton]);
foreach (KeyValuePair<int, ReadOnlyDictionary<string, ReadOnlyDictionary<byte, ReadOnlyCollection<string>>>> keyValuePair in keyValuePairs) {
foreach (KeyValuePair<string, ReadOnlyDictionary<byte, ReadOnlyCollection<string>>> keyValue in keyValuePair.Value) {
if (keyValue.Key != resultSettings.ResultSingleton)
throw new Exception("Never should happen!");
results.Add(keyValuePair.Key, keyValue.Value);
}
}
return results.AsReadOnly();
}
private static ReadOnlyCollection<Record> GetRecords(ILogger<Worker> logger, ResultSettings resultSettings, MetadataSettings metadataSettings, string[] files) {
List<Record> results = [];
Record record;
FileInfo fileInfo;
FilePath filePath;
bool hasFlagHidden;
FileHolder fileHolder;
CombinedEnumAndIndex combinedEnumAndIndex;
foreach (string file in files) {
fileInfo = new(file);
fileHolder = FileHolder.Get(fileInfo, id: null);
filePath = FilePath.Get(resultSettings, metadataSettings, fileHolder, index: null);
if (!filePath.IsIntelligentIdFormat) {
logger.LogWarning("<{file}> skipped because of name format!", filePath.Name);
continue;
}
hasFlagHidden = fileInfo.Attributes.HasFlag(FileAttributes.Hidden);
combinedEnumAndIndex = IPath.GetCombinedEnumAndIndex(resultSettings, filePath);
record = new(CombinedEnumAndIndex: combinedEnumAndIndex, FilePath: filePath, HasFlagHidden: hasFlagHidden);
results.Add(record);
}
return results.AsReadOnly();
}
private static void CopyToCombinedEnumAndIndexFormat(ILogger<Worker> logger, ReadOnlyCollection<Record> records, ReadOnlyDictionary<byte, ReadOnlyCollection<string>> keyValuePairs) {
string checkFile;
FileInfo fileInfo;
FileAttributes fileAttributes;
foreach (Record record in records) {
checkFile = Path.Combine(keyValuePairs[record.CombinedEnumAndIndex.Enum][record.CombinedEnumAndIndex.Index], record.FilePath.Name);
if (File.Exists(checkFile)) {
logger.LogWarning("<{file}> skipped because it already exists!", record.FilePath.Name);
continue;
}
File.Copy(record.FilePath.FullName, checkFile);
if (record.HasFlagHidden) {
fileInfo = new(checkFile);
fileAttributes = fileInfo.Attributes & ~FileAttributes.Hidden;
File.SetAttributes(fileInfo.FullName, fileAttributes);
}
}
}
}