copy-to-combined-enum-and-index-format (Day-Helper-2025-07-26)
This commit is contained in:
106
ADO2025/PI6/Helper-2025-07-26.cs
Normal file
106
ADO2025/PI6/Helper-2025-07-26.cs
Normal file
@ -0,0 +1,106 @@
|
||||
|
||||
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 Settings(ResultSettings? ResultSettings, MetadataSettings? MetadataSettings);
|
||||
|
||||
private record Record(CombinedEnumAndIndex CombinedEnumAndIndex, FilePath FilePath, bool HasFlagHidden);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true)]
|
||||
[JsonSerializable(typeof(Settings))]
|
||||
private partial class SettingsSourceGenerationContext : 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]);
|
||||
Settings? settings = JsonSerializer.Deserialize(json, SettingsSourceGenerationContext.Default.Settings);
|
||||
if (settings.ResultSettings is null || settings.ResultSettings.ResultAllInOneSubdirectoryLength < 1 || settings.MetadataSettings is null) {
|
||||
throw new Exception(nameof(Settings));
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user