Removed Renci.SshNet
This commit is contained in:
@ -1,7 +1,5 @@
|
||||
using DiscUtils.Iso9660;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Renci.SshNet;
|
||||
using Renci.SshNet.Sftp;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.IO.Compression;
|
||||
@ -15,7 +13,7 @@ internal static partial class Helper20241217
|
||||
|
||||
private record Record(string Directory, Job? Job, string Path);
|
||||
private record Job(string AlternatePath, string Directory, string Extension, File[] Files, int FilesCount, double FilesTotalLength, int Keep, Target[] Targets);
|
||||
private record SecureShell(string Host, string Key, string Path, bool Required, string User);
|
||||
private record SecureShell();
|
||||
private record ServerMessageBlock(string Path, bool Required);
|
||||
private record Target(SecureShell? SecureShell, ServerMessageBlock? ServerMessageBlock);
|
||||
private record File(long LastWriteTicks, long Length, string RelativePath);
|
||||
@ -138,17 +136,25 @@ internal static partial class Helper20241217
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void WriteISO(Record record, ReadOnlyCollection<File> files, string path, string directoryName)
|
||||
private static void WriteISO(char destinationDriveLetter, Record record, ReadOnlyCollection<File> files, string path, string directoryName)
|
||||
{
|
||||
string checkFile = $"{destinationDriveLetter}{path[1..]}";
|
||||
string checkDirectory = Path.GetDirectoryName(checkFile) ?? throw new Exception();
|
||||
if (!Directory.Exists(checkDirectory))
|
||||
_ = Directory.CreateDirectory(checkDirectory);
|
||||
CDBuilder builder = new() { UseJoliet = true, VolumeIdentifier = directoryName.Length < 25 ? directoryName : directoryName[..25] };
|
||||
foreach (File file in files)
|
||||
_ = builder.AddFile(file.RelativePath, Path.Combine(record.Directory, file.RelativePath));
|
||||
builder.Build(path);
|
||||
builder.Build(checkFile);
|
||||
}
|
||||
|
||||
private static void WriteZIP(Record record, ReadOnlyCollection<File> files, string path)
|
||||
private static void WriteZIP(char destinationDriveLetter, Record record, ReadOnlyCollection<File> files, string path)
|
||||
{
|
||||
using ZipArchive zip = ZipFile.Open(path, ZipArchiveMode.Create);
|
||||
string checkFile = $"{destinationDriveLetter}{path[1..]}";
|
||||
string checkDirectory = Path.GetDirectoryName(checkFile) ?? throw new Exception();
|
||||
if (!Directory.Exists(checkDirectory))
|
||||
_ = Directory.CreateDirectory(checkDirectory);
|
||||
using ZipArchive zip = ZipFile.Open(checkFile, ZipArchiveMode.Create);
|
||||
string directoryEntry;
|
||||
List<string> directoryEntries = [];
|
||||
foreach (File file in files)
|
||||
@ -163,39 +169,17 @@ internal static partial class Helper20241217
|
||||
_ = zip.CreateEntryFromFile(Path.Combine(record.Directory, file.RelativePath), file.RelativePath);
|
||||
}
|
||||
|
||||
private static void WriteExtension(Record record, Job job, ReadOnlyCollection<File> files, string path)
|
||||
private static void WriteExtension(char destinationDriveLetter, Record record, Job job, ReadOnlyCollection<File> files, string path)
|
||||
{
|
||||
string directoryName = Path.GetFileName(record.Directory);
|
||||
if (job.Extension.Equals(".iso", StringComparison.OrdinalIgnoreCase))
|
||||
WriteISO(record, files, path, directoryName);
|
||||
WriteISO(destinationDriveLetter, record, files, path, directoryName);
|
||||
else if (job.Extension.Equals(".zip", StringComparison.OrdinalIgnoreCase))
|
||||
WriteZIP(record, files, path);
|
||||
WriteZIP(destinationDriveLetter, record, files, path);
|
||||
else
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private static void PushTo(Job job, SecureShell secureShell, string path)
|
||||
{
|
||||
string remotePath = string.Concat(secureShell.Path, '/', Path.GetFileName(path));
|
||||
using SftpClient client = new(secureShell.Host, secureShell.User, new PrivateKeyFile(secureShell.Key));
|
||||
client.Connect();
|
||||
if (job.Files.Length == 0)
|
||||
{
|
||||
string directoryName = Path.GetDirectoryName(secureShell.Path) ?? throw new Exception();
|
||||
try
|
||||
{ client.CreateDirectory(Path.GetDirectoryName(directoryName) ?? throw new Exception()); }
|
||||
catch (Exception) { }
|
||||
try
|
||||
{ client.CreateDirectory(directoryName); }
|
||||
catch (Exception) { }
|
||||
try
|
||||
{ client.CreateDirectory(secureShell.Path); }
|
||||
catch (Exception) { }
|
||||
}
|
||||
using FileStream fileStream = System.IO.File.OpenRead(path);
|
||||
client.UploadFile(fileStream, remotePath);
|
||||
}
|
||||
|
||||
private static void PushTo(ServerMessageBlock serverMessageBlock, string path)
|
||||
{
|
||||
string remotePath = Path.Combine(serverMessageBlock.Path, Path.GetFileName(path));
|
||||
@ -214,15 +198,7 @@ internal static partial class Helper20241217
|
||||
foreach (Target target in job.Targets)
|
||||
{
|
||||
if (target.SecureShell is not null)
|
||||
{
|
||||
try
|
||||
{ PushTo(job, target.SecureShell, path); }
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (target.SecureShell.Required)
|
||||
results.Add(ex);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
else if (target.ServerMessageBlock is not null)
|
||||
{
|
||||
try
|
||||
@ -239,23 +215,6 @@ internal static partial class Helper20241217
|
||||
return results.AsReadOnly();
|
||||
}
|
||||
|
||||
private static void DeleteOld(Job job, SecureShell secureShell, string path)
|
||||
{
|
||||
List<string> results = [];
|
||||
using SftpClient client = new(secureShell.Host, secureShell.User, new PrivateKeyFile(secureShell.Key));
|
||||
client.Connect();
|
||||
foreach (ISftpFile file in client.ListDirectory(secureShell.Path))
|
||||
{
|
||||
if (file.Name == path)
|
||||
continue;
|
||||
if (!file.Name.EndsWith(job.Extension, StringComparison.OrdinalIgnoreCase))
|
||||
continue;
|
||||
results.Add(file.FullName);
|
||||
}
|
||||
for (int i = job.Keep - 1; i < results.Count; i++)
|
||||
client.DeleteFile(results[i]);
|
||||
}
|
||||
|
||||
private static void DeleteOld(Job job, ServerMessageBlock serverMessageBlock, string path)
|
||||
{
|
||||
List<string> results = [];
|
||||
@ -276,15 +235,7 @@ internal static partial class Helper20241217
|
||||
foreach (Target target in job.Targets)
|
||||
{
|
||||
if (target.SecureShell is not null)
|
||||
{
|
||||
try
|
||||
{ DeleteOld(job, target.SecureShell, path); }
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (target.SecureShell.Required)
|
||||
results.Add(ex);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
else if (target.ServerMessageBlock is not null)
|
||||
{
|
||||
try
|
||||
@ -304,7 +255,7 @@ internal static partial class Helper20241217
|
||||
private static void Verify(string searchPattern, string[] ignoreFileNames)
|
||||
{
|
||||
List<Target> targets = [
|
||||
new(new SecureShell("free.file.sync.root", "C:/Users/phares/.ssh/id_ed25519", "\\home", true, "root"), null),
|
||||
new(new SecureShell(), null),
|
||||
new(null, new ServerMessageBlock("\\\\mesfs.infineon.com\\EC_APC\\DEV", true))
|
||||
];
|
||||
string directory = Path.Combine(Environment.CurrentDirectory, ".vscode", "helper");
|
||||
@ -337,6 +288,7 @@ internal static partial class Helper20241217
|
||||
ReadOnlyCollection<Exception> exceptions;
|
||||
string[] ignoreFileNames = args[3].Split('|');
|
||||
string sourceDirectory = Path.GetFullPath(args[0]);
|
||||
char destinationDriveLetter = args[4].Split(':')[0][0];
|
||||
logger.LogInformation("Searching <{sourceDirectory}> with search pattern {searchPattern}", args[0], searchPattern);
|
||||
if (Debugger.IsAttached)
|
||||
Verify(searchPattern, ignoreFileNames);
|
||||
@ -353,7 +305,7 @@ internal static partial class Helper20241217
|
||||
directoryName = Path.GetFileName(record.Directory);
|
||||
path = Path.Combine(record.Directory, $"{directoryName}-{DateTime.Now:yyyy-MM-dd-HH-mm-ss-fff}{record.Job.Extension}");
|
||||
logger.LogInformation("Writing <{directory}> extension", record.Directory);
|
||||
WriteExtension(record, record.Job, files, path);
|
||||
WriteExtension(destinationDriveLetter, record, record.Job, files, path);
|
||||
logger.LogInformation("Pushing <{directory}> extension", record.Directory);
|
||||
exceptions = PushTo(record.Job, path);
|
||||
if (exceptions.Count != 0)
|
||||
|
Reference in New Issue
Block a user