Dependency Injection Style
AOT Compiling Switched to Secret from Development json file Added Kanbn Humanizer HelperCreateNoteFiles.CleanExistingFiles HelperPackageFilesByDate Added SRP Helper Hardcoded File Search and Sort Set Date from Zip Entry
This commit is contained in:
84
Helpers/HelperPackageFilesByDate.cs
Normal file
84
Helpers/HelperPackageFilesByDate.cs
Normal file
@ -0,0 +1,84 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace File_Folder_Helper.Helpers;
|
||||
|
||||
internal static class HelperPackageFilesByDate
|
||||
{
|
||||
|
||||
public record PackageJson(
|
||||
[property: JsonPropertyName("name")] string Name,
|
||||
[property: JsonPropertyName("time")] Dictionary<string, DateTime> Times,
|
||||
[property: JsonPropertyName("_rev")] string Rev,
|
||||
[property: JsonPropertyName("_id")] string Id
|
||||
);
|
||||
|
||||
internal static void SetDateFromJsonEntry(ILogger log, string sourceDirectory, SearchOption searchOption = SearchOption.AllDirectories)
|
||||
{
|
||||
string json;
|
||||
DateTime dateTime;
|
||||
FileInfo fileInfo;
|
||||
string[] segments;
|
||||
string[] tgzFiles;
|
||||
PackageJson? packageJson;
|
||||
string[] packageJsonFiles;
|
||||
string? packageJsonDirectory;
|
||||
string fileNameWithoutExtension;
|
||||
string packageJsonDirectoryName;
|
||||
List<DateTime> dateTimes = new();
|
||||
if (log is null)
|
||||
throw new NullReferenceException();
|
||||
if (!Directory.Exists(sourceDirectory))
|
||||
_ = Directory.CreateDirectory(sourceDirectory);
|
||||
for (int i = 1; i < 2; i++)
|
||||
{
|
||||
packageJsonFiles = Directory.GetFiles(sourceDirectory, "package.json", searchOption);
|
||||
foreach (string packageJsonFile in packageJsonFiles)
|
||||
{
|
||||
packageJsonDirectory = Path.GetDirectoryName(packageJsonFile);
|
||||
if (string.IsNullOrEmpty(packageJsonDirectory))
|
||||
continue;
|
||||
json = File.ReadAllText(packageJsonFile);
|
||||
packageJson = JsonSerializer.Deserialize<PackageJson>(json);
|
||||
if (packageJson is null || !packageJson.Times.Any())
|
||||
continue;
|
||||
packageJsonDirectoryName = Path.GetFileName(packageJsonDirectory);
|
||||
tgzFiles = Directory.GetFiles(packageJsonDirectory, "*.tgz", SearchOption.TopDirectoryOnly);
|
||||
foreach (string tgzFile in tgzFiles)
|
||||
{
|
||||
dateTimes.Clear();
|
||||
fileNameWithoutExtension = Path.GetFileNameWithoutExtension(tgzFile);
|
||||
segments = fileNameWithoutExtension.Split('-');
|
||||
foreach (string segment in segments)
|
||||
{
|
||||
if (!packageJson.Times.TryGetValue(segment, out dateTime))
|
||||
continue;
|
||||
dateTimes.Add(dateTime);
|
||||
fileInfo = new(tgzFile);
|
||||
if (fileInfo.LastWriteTime != dateTime)
|
||||
File.SetLastWriteTime(fileInfo.FullName, dateTime);
|
||||
}
|
||||
if (!dateTimes.Any())
|
||||
{
|
||||
if (fileNameWithoutExtension.Length + 1 < packageJsonDirectoryName.Length)
|
||||
continue;
|
||||
if (!packageJson.Times.TryGetValue(fileNameWithoutExtension[(packageJsonDirectoryName.Length + 1)..], out dateTime))
|
||||
continue;
|
||||
dateTimes.Add(dateTime);
|
||||
fileInfo = new(tgzFile);
|
||||
if (fileInfo.LastWriteTime != dateTime)
|
||||
File.SetLastWriteTime(fileInfo.FullName, dateTime);
|
||||
}
|
||||
}
|
||||
if (!dateTimes.Any())
|
||||
continue;
|
||||
dateTime = dateTimes.Max();
|
||||
fileInfo = new(packageJsonFile);
|
||||
if (fileInfo.LastWriteTime != dateTime)
|
||||
File.SetLastWriteTime(fileInfo.FullName, dateTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user