Day Helpers
net8.0 Wrap in try Zip with directories
This commit is contained in:
@ -16,9 +16,11 @@ internal static class HelperDeleteEmptyDirectories
|
||||
foreach (string file in files)
|
||||
{
|
||||
fileInfo = new(file);
|
||||
if (fileInfo.LastWriteTime.Ticks > ticks)
|
||||
if (fileInfo.LastWriteTime.Ticks > ticks.Value)
|
||||
continue;
|
||||
File.Delete(file);
|
||||
try
|
||||
{ File.Delete(file); }
|
||||
catch (IOException) { }
|
||||
}
|
||||
}
|
||||
if (directories.Length > 0)
|
||||
@ -78,7 +80,7 @@ internal static class HelperDeleteEmptyDirectories
|
||||
|
||||
internal static void DeleteOldLogFilesAndDeleteEmptyDirectories(ILogger<Worker> logger, string rootDirectory)
|
||||
{
|
||||
long ticks = DateTime.Now.AddHours(-120).Ticks;
|
||||
long ticks = DateTime.Now.AddHours(-84).Ticks;
|
||||
DeleteOldLogFilesAndDeleteEmptyDirectories(logger, ticks, "*.log*", rootDirectory);
|
||||
}
|
||||
|
||||
|
@ -1,65 +0,0 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Net.Http.Json;
|
||||
|
||||
namespace File_Folder_Helper.Helpers;
|
||||
|
||||
internal static class HelperDownload
|
||||
{
|
||||
|
||||
internal static void SaveJson(ILogger log, string argsZero)
|
||||
{
|
||||
string[] lines;
|
||||
int? jsonBodyLine;
|
||||
string? userAgent;
|
||||
string[] segments;
|
||||
HttpClient httpClient;
|
||||
HttpRequestMessage httpRequestMessage;
|
||||
string[] files = Directory.GetFiles(argsZero, "*.post", SearchOption.TopDirectoryOnly);
|
||||
foreach (string file in files)
|
||||
{
|
||||
jsonBodyLine = null;
|
||||
userAgent = null;
|
||||
lines = File.ReadAllLines(file);
|
||||
if (lines.Length < 2)
|
||||
continue;
|
||||
httpRequestMessage = new(HttpMethod.Post, lines[1]);
|
||||
log.LogInformation("reading {fileName}", Path.GetFileName(file));
|
||||
for (int i = 2; i < lines.Length; i++)
|
||||
{
|
||||
if (lines.Length < 1)
|
||||
continue;
|
||||
if (lines[i][0] == '#')
|
||||
continue;
|
||||
else if (lines[i][0] == '{')
|
||||
jsonBodyLine = i;
|
||||
else
|
||||
{
|
||||
segments = lines[i].Split(": ");
|
||||
if (segments.Length < 2)
|
||||
continue;
|
||||
if (segments[0] == "User-Agent")
|
||||
userAgent = segments[1];
|
||||
else
|
||||
httpRequestMessage.Headers.Add(segments[0], segments[1]);
|
||||
}
|
||||
}
|
||||
if (jsonBodyLine is not null)
|
||||
httpRequestMessage.Content = JsonContent.Create(lines[jsonBodyLine.Value]);
|
||||
httpClient = new(new HttpClientHandler { UseCookies = false }) { BaseAddress = new Uri(lines[0]) };
|
||||
if (userAgent is not null)
|
||||
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(userAgent);
|
||||
Task<HttpResponseMessage> httpResponseMessageTask = httpClient.SendAsync(httpRequestMessage);
|
||||
httpResponseMessageTask.Wait();
|
||||
if (!httpResponseMessageTask.Result.IsSuccessStatusCode)
|
||||
throw new Exception(httpResponseMessageTask.Result.StatusCode.ToString());
|
||||
Task<Stream> streamTask = httpResponseMessageTask.Result.Content.ReadAsStreamAsync();
|
||||
streamTask.Wait();
|
||||
if (!streamTask.Result.CanRead)
|
||||
throw new NullReferenceException(nameof(streamTask));
|
||||
using FileStream fileStream = new($"{file}.json", FileMode.CreateNew);
|
||||
Task task = streamTask.Result.CopyToAsync(fileStream);
|
||||
task.Wait();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -270,12 +270,30 @@ internal static partial class HelperZipFilesBy
|
||||
private static void ZipDirectory(ILogger<Worker> logger, string directory)
|
||||
{
|
||||
logger.LogInformation("{directory}", directory);
|
||||
string zipFile = $"{directory}.zip";
|
||||
int skipChars = directory.Length + 1;
|
||||
string[] files = Directory.GetFiles(directory, "*", SearchOption.AllDirectories);
|
||||
using ZipArchive zip = ZipFile.Open($"{directory}.zip", ZipArchiveMode.Create);
|
||||
foreach (string file in files)
|
||||
string[] directories = Directory.GetDirectories(directory, "*", SearchOption.AllDirectories);
|
||||
ZipArchiveMode zipArchiveMode = File.Exists(zipFile) ? ZipArchiveMode.Update : ZipArchiveMode.Create;
|
||||
for (int i = 1; i < 3; i++)
|
||||
{
|
||||
_ = zip.CreateEntryFromFile(file, Path.GetFileName(file));
|
||||
File.Delete(file);
|
||||
try
|
||||
{
|
||||
using ZipArchive zip = ZipFile.Open(zipFile, zipArchiveMode);
|
||||
for (int j = 0; j < directories.Length; j++)
|
||||
_ = zip.CreateEntry($"{directories[j][skipChars..]}/");
|
||||
foreach (string file in files)
|
||||
{
|
||||
_ = zip.CreateEntryFromFile(file, file[skipChars..]);
|
||||
File.Delete(file);
|
||||
}
|
||||
break;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
File.Delete(zipFile);
|
||||
zipArchiveMode = ZipArchiveMode.Create;
|
||||
}
|
||||
}
|
||||
Directory.Delete(directory, recursive: true);
|
||||
}
|
||||
|
Reference in New Issue
Block a user