diff --git a/EgwProxy.Ftp.Test/Program.cs b/EgwProxy.Ftp.Test/Program.cs index c1c2d986..3a1b3e93 100644 --- a/EgwProxy.Ftp.Test/Program.cs +++ b/EgwProxy.Ftp.Test/Program.cs @@ -95,7 +95,7 @@ namespace EgwProxy.Ftp.Test if (item.paramList != null && item.paramList.Count > 0) { string dir2check = item.paramList[0]; - var dirCheckExists = ftpClient.dirExists(dir2check); + var dirCheckExists = ftpClient.DirExists(dir2check); esitoStep = dirCheckExists ? $"Directory {dir2check} found!" : $"Directory {dir2check} NOT found!"; } else @@ -108,7 +108,7 @@ namespace EgwProxy.Ftp.Test if (item.paramList != null && item.paramList.Count > 0) { string file2check = item.paramList[0]; - var dirCheckExists = ftpClient.fileExists(file2check); + var dirCheckExists = ftpClient.FileExists(file2check); esitoStep = dirCheckExists ? $"File {file2check} found!" : $"File {file2check} NOT found!"; } else @@ -121,14 +121,14 @@ namespace EgwProxy.Ftp.Test if (item.paramList != null && item.paramList.Count > 0) { string dir2check = item.paramList[0]; - var preTest = ftpClient.dirExists(dir2check); + var preTest = ftpClient.DirExists(dir2check); if (preTest) { esitoStep = "Error: Folder already exists!"; } else { - var dirCreate = ftpClient.createDir(dir2check); + var dirCreate = ftpClient.CreateDir(dir2check); esitoStep = dirCreate ? $"Directory {dir2check} created!" : $"Error: {dir2check} NOT created!"; } @@ -143,10 +143,10 @@ namespace EgwProxy.Ftp.Test if (item.paramList != null && item.paramList.Count > 0) { string dir2check = item.paramList[0]; - var preTest = ftpClient.dirExists(dir2check); + var preTest = ftpClient.DirExists(dir2check); if (preTest) { - var dirDelete = ftpClient.deleteDir(dir2check); + var dirDelete = ftpClient.DeleteDir(dir2check); esitoStep = dirDelete ? $"Directory {dir2check} deleted!" : $"Error: {dir2check} NOT deleted!"; } else @@ -165,10 +165,10 @@ namespace EgwProxy.Ftp.Test if (item.paramList != null && item.paramList.Count > 0) { string file2check = item.paramList[0]; - var preTest = ftpClient.fileExists(file2check); + var preTest = ftpClient.FileExists(file2check); if (preTest) { - var dirDelete = ftpClient.deleteFile(file2check); + var dirDelete = ftpClient.DeleteFile(file2check); esitoStep = dirDelete ? $"File {file2check} deleted!" : $"Error: {file2check} NOT deleted!"; } else @@ -188,10 +188,10 @@ namespace EgwProxy.Ftp.Test { string dir2check = item.paramList[0]; string localDir = Path.Combine(BaseDirectory, item.paramList[1]); - var preTest = ftpClient.dirExists(dir2check); + var preTest = ftpClient.DirExists(dir2check); if (preTest) { - var dirDelete = ftpClient.getDir(localDir, dir2check); + var dirDelete = ftpClient.GetDir(localDir, dir2check); esitoStep = dirDelete ? $"Directory {dir2check} downloaded!" : $"Error: {dir2check} NOT downloaded!"; } else @@ -211,10 +211,10 @@ namespace EgwProxy.Ftp.Test { string dir2check = item.paramList[0]; string localDir = Path.Combine(BaseDirectory, item.paramList[1]); - var preTest = ftpClient.dirExists(dir2check); + var preTest = ftpClient.DirExists(dir2check); if (preTest) { - var dirDelete = ftpClient.getDir(localDir, dir2check); + var dirDelete = ftpClient.GetDir(localDir, dir2check); esitoStep = dirDelete ? $"Directory {dir2check} downloaded!" : $"Error: {dir2check} NOT downloaded!"; } else @@ -234,7 +234,7 @@ namespace EgwProxy.Ftp.Test { string remoteDir = item.paramList[0]; string localDir = Path.Combine(BaseDirectory, item.paramList[1]); - var dirUploaded = ftpClient.sendDir(localDir, remoteDir); + var dirUploaded = ftpClient.SendDir(localDir, remoteDir); esitoStep = dirUploaded ? $"Directory {remoteDir} uploaded!" : $"Error: {remoteDir} NOT uploaded!"; } else @@ -248,7 +248,7 @@ namespace EgwProxy.Ftp.Test { string remotePath = item.paramList[0]; string localPath = Path.Combine(BaseDirectory, item.paramList[1]); - var fileUploaded = ftpClient.sendFile(localPath, remotePath); + var fileUploaded = ftpClient.SendFile(localPath, remotePath); esitoStep = fileUploaded ? $"File {remotePath} uploaded!" : $"Error: {remotePath} NOT uploaded!"; } else @@ -261,10 +261,10 @@ namespace EgwProxy.Ftp.Test if (item.paramList != null && item.paramList.Count > 0) { string remoteDir = item.paramList[0]; - var preTest = ftpClient.dirExists(remoteDir); + var preTest = ftpClient.DirExists(remoteDir); if (preTest) { - var listResult = ftpClient.listDir(remoteDir, false); + var listResult = ftpClient.ListDir(remoteDir, false); Console.WriteLine($"Content of dir {remoteDir}:"); foreach (var itemList in listResult) { @@ -335,13 +335,13 @@ namespace EgwProxy.Ftp.Test sw.Restart(); string localDir = Path.Combine(BaseDirectory, item.paramList[0]); string remoteDir = item.paramList[1]; - var preTest = ftpClient.dirExists(remoteDir); + var preTest = ftpClient.DirExists(remoteDir); if (!preTest) { - ftpClient.createDir(remoteDir); + ftpClient.CreateDir(remoteDir); } - var mirrorDone = ftpClient.sendDir(localDir, remoteDir, FluentFTP.FtpFolderSyncMode.Mirror); + var mirrorDone = ftpClient.SendDir(localDir, remoteDir, FluentFTP.FtpFolderSyncMode.Mirror); esitoStep = mirrorDone ? $"Directory {localDir} mirrored L2R!" : $"Error: {remoteDir} NOT mirrored!"; } @@ -359,10 +359,10 @@ namespace EgwProxy.Ftp.Test sw.Restart(); string remoteDir = item.paramList[0]; string localDir = Path.Combine(BaseDirectory, item.paramList[1]); - var preTest = ftpClient.dirExists(remoteDir); + var preTest = ftpClient.DirExists(remoteDir); if (preTest) { - var mirrorDone = ftpClient.getDir(localDir, remoteDir, FluentFTP.FtpFolderSyncMode.Mirror); + var mirrorDone = ftpClient.GetDir(localDir, remoteDir, FluentFTP.FtpFolderSyncMode.Mirror); esitoStep = mirrorDone ? $"Directory {remoteDir} mirrored R2L!" : $"Error: {remoteDir} NOT mirrored!"; } else @@ -414,7 +414,7 @@ namespace EgwProxy.Ftp.Test serverTest(ftpClient); Console.WriteLine("--- Folder ---"); - var testExists = ftpClient.dirExists($"data/test_folder"); + var testExists = ftpClient.DirExists($"data/test_folder"); if (testExists) { Console.WriteLine("Folder already exists!"); @@ -422,12 +422,12 @@ namespace EgwProxy.Ftp.Test else { Console.WriteLine("--- Create folder ---"); - var testCreate = ftpClient.createDir($"data/test_folder"); + var testCreate = ftpClient.CreateDir($"data/test_folder"); Console.WriteLine(); } Console.WriteLine("Contenuto folder remota: "); - var folderContent = ftpClient.listDir("data/", true); + var folderContent = ftpClient.ListDir("data/", true); foreach (var item in folderContent) { Console.WriteLine(item); @@ -437,9 +437,9 @@ namespace EgwProxy.Ftp.Test Console.ReadKey(); Console.WriteLine("--- Upload file ---"); - ftpClient.sendFile("test/FileProva.txt", "data/test_folder/FileProva.txt"); + ftpClient.SendFile("test/FileProva.txt", "data/test_folder/FileProva.txt"); Console.WriteLine("Contenuto folder remota: "); - folderContent = ftpClient.listDir("data/", true); + folderContent = ftpClient.ListDir("data/", true); foreach (var item in folderContent) { Console.WriteLine(item); @@ -451,9 +451,9 @@ namespace EgwProxy.Ftp.Test Console.WriteLine("--- Download File ---"); - ftpClient.getFile("test/FileProva_02.txt", "data/test_folder/FileProva.txt"); + ftpClient.GetFile("test/FileProva_02.txt", "data/test_folder/FileProva.txt"); Console.WriteLine("Contenuto folder remota: "); - folderContent = ftpClient.listDir("data/", true); + folderContent = ftpClient.ListDir("data/", true); foreach (var item in folderContent) { Console.WriteLine(item); @@ -470,9 +470,9 @@ namespace EgwProxy.Ftp.Test private static void serverTest(Manager ftpClient) { Console.WriteLine(separator); - var testServer = ftpClient.serverOk(); + var testServer = ftpClient.ServerOk(); Console.WriteLine($"Test connessione: esito {testServer}"); - var srvType = ftpClient.serverType(); + var srvType = ftpClient.ServerType(); Console.WriteLine($"Server: {srvType}"); Console.WriteLine(separator); Console.WriteLine(); diff --git a/EgwProxy.Ftp/EgwProxy.Ftp.csproj b/EgwProxy.Ftp/EgwProxy.Ftp.csproj index d2cefa74..d885fbba 100644 --- a/EgwProxy.Ftp/EgwProxy.Ftp.csproj +++ b/EgwProxy.Ftp/EgwProxy.Ftp.csproj @@ -37,8 +37,13 @@ ..\packages\Microsoft.Extensions.Logging.Abstractions.3.1.32\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll + + ..\packages\NLog.5.3.1\lib\net46\NLog.dll + + + ..\packages\System.ValueTuple.4.5.0\lib\net461\System.ValueTuple.dll diff --git a/EgwProxy.Ftp/Manager.cs b/EgwProxy.Ftp/Manager.cs index bde54b74..723dafe2 100644 --- a/EgwProxy.Ftp/Manager.cs +++ b/EgwProxy.Ftp/Manager.cs @@ -1,4 +1,5 @@ using FluentFTP; +using NLog; using System; using System.Collections.Generic; using System.Linq; @@ -7,9 +8,7 @@ using System.Net.Security; namespace EgwProxy.Ftp { /// - /// Client per operazioni FTP, basato su FluentFTP: - /// https://github.com/robinrodricks/FluentFTP - /// https://github.com/robinrodricks/FluentFTP/wiki/Quick-Start-Example + /// Client per operazioni FTP, basato su FluentFTP: https://github.com/robinrodricks/FluentFTP https://github.com/robinrodricks/FluentFTP/wiki/Quick-Start-Example /// public class Manager { @@ -54,11 +53,19 @@ namespace EgwProxy.Ftp /// Creazione directory remota /// /// Nome directory remota da creare (ad es: @"/public_html/videos") - public bool createDir(string remoteDir) + public bool CreateDir(string remoteDir) { - tryConnect(); - // upload della folder + files, cancellazione extra files = mirroring - bool answ = client.CreateDirectory(remoteDir); + bool answ = false; + try + { + tryConnect(); + // upload della folder + files, cancellazione extra files = mirroring + answ = client.CreateDirectory(remoteDir); + } + catch (Exception exc) + { + Log.Error($"Eccezione in CreateDir{Environment.NewLine}{exc}"); + } // chiudo! client.Disconnect(); return answ; @@ -68,18 +75,20 @@ namespace EgwProxy.Ftp /// Eliminazionedirectory remota /// /// Nome directory remota da eliminare - public bool deleteDir(string remoteDir) + public bool DeleteDir(string remoteDir) { - tryConnect(); bool answ = false; try { + tryConnect(); // Elimina folder client.DeleteDirectory(remoteDir); answ = true; } - catch - { } + catch (Exception exc) + { + Log.Error($"Eccezione in DeleteDir{Environment.NewLine}{exc}"); + } // chiudo! client.Disconnect(); return answ; @@ -89,18 +98,20 @@ namespace EgwProxy.Ftp /// Eliminazionedirectory remota /// /// Nome file remoto da eliminare - public bool deleteFile(string remoteFile) + public bool DeleteFile(string remoteFile) { - tryConnect(); bool answ = false; + tryConnect(); try { // Elimina folder client.DeleteFile(remoteFile); answ = true; } - catch - { } + catch (Exception exc) + { + Log.Error($"Eccezione in DeleteFile{Environment.NewLine}{exc}"); + } // chiudo! client.Disconnect(); return answ; @@ -111,11 +122,19 @@ namespace EgwProxy.Ftp /// /// Percorso remoto da testare (ad es "/htdocs/extras/") /// - public bool dirExists(string remotePath) + public bool DirExists(string remotePath) { - tryConnect(); - bool answ = client.DirectoryExists(remotePath); - // chiudo! + bool answ = false; + try + { + tryConnect(); + answ = client.DirectoryExists(remotePath); + } + catch (Exception exc) + { + Log.Error($"Eccezione in DirExists{Environment.NewLine}{exc}"); + } + // c chiudo! client.Disconnect(); return answ; } @@ -125,10 +144,18 @@ namespace EgwProxy.Ftp /// /// Percorso remoto da testare (ad es "/htdocs/big2.txt") /// - public bool fileExists(string remotePath) + public bool FileExists(string remotePath) { - tryConnect(); - bool answ = client.FileExists(remotePath); + bool answ = false; + try + { + tryConnect(); + answ = client.FileExists(remotePath); + } + catch (Exception exc) + { + Log.Error($"Eccezione in FileExists{Environment.NewLine}{exc}"); + } // chiudo! client.Disconnect(); return answ; @@ -140,7 +167,7 @@ namespace EgwProxy.Ftp /// Path directory da inviare (ad es:@"C:\website\videos\") /// Nome remoto file per caricamento (ad es: @"/public_html/videos") /// Modalità di sync (default = mirror) - public bool getDir(string localDir, string remoteDir, FtpFolderSyncMode syncMode = FtpFolderSyncMode.Mirror) + public bool GetDir(string localDir, string remoteDir, FtpFolderSyncMode syncMode = FtpFolderSyncMode.Mirror) { bool answ = false; try @@ -152,8 +179,10 @@ namespace EgwProxy.Ftp //answ = (result != null && result.Count > 0); answ = result != null && result.Where(x => !x.IsSuccess).Count() == 0; } - catch - { } + catch (Exception exc) + { + Log.Error($"Eccezione in GetDir{Environment.NewLine}{exc}"); + } // chiudo! client.Disconnect(); return answ; @@ -164,13 +193,20 @@ namespace EgwProxy.Ftp /// /// Path locale del file da inviare (ad es: @"C:\MyVideo.mp4") /// NOme remoto file per caricamento (ad es: "/htdocs/MyVideo.mp4") - public bool getFile(string fileName, string remoteName) + public bool GetFile(string fileName, string remoteName) { bool answ = false; - tryConnect(); - // effettuo caricamento puntuale - var result = client.DownloadFile(fileName, remoteName); - answ = result == FtpStatus.Success; + try + { + tryConnect(); + // effettuo caricamento puntuale + var result = client.DownloadFile(fileName, remoteName); + answ = result == FtpStatus.Success; + } + catch (Exception exc) + { + Log.Error($"Eccezione in GetFile{Environment.NewLine}{exc}"); + } // chiudo! client.Disconnect(); return answ; @@ -181,21 +217,29 @@ namespace EgwProxy.Ftp /// /// Nome directory remota da leggere (ad es: @"/public_html/videos") /// Indica se fare search ricorsivo - public List listDir(string remoteDir, bool recurse) + public List ListDir(string remoteDir, bool recurse) { - tryConnect(); - // upload della folder + files, cancellazione extra files = mirroring - FtpListItem[] dirContent; - if (recurse) + List answ = new List(); + try { - dirContent = client.GetListing(remoteDir, FtpListOption.Recursive); + tryConnect(); + // upload della folder + files, cancellazione extra files = mirroring + FtpListItem[] dirContent; + if (recurse) + { + dirContent = client.GetListing(remoteDir, FtpListOption.Recursive); + } + else + { + dirContent = client.GetListing(remoteDir); + } + answ = dirContent.Select(x => $"{x.Type} - {x.Name}").ToList(); } - else + catch (Exception exc) { - dirContent = client.GetListing(remoteDir); + Log.Error($"Eccezione in ListDir{Environment.NewLine}{exc}"); } client.Disconnect(); - var answ = dirContent.Select(x => $"{x.Type} - {x.Name}").ToList(); // chiudo! return answ; } @@ -206,7 +250,54 @@ namespace EgwProxy.Ftp /// Path directory da inviare (ad es:@"C:\website\videos\") /// Nome remoto file per caricamento (ad es: @"/public_html/videos") /// Modalità di sync (default = mirror) - public bool sendDir(string localDir, string remoteDir, FtpFolderSyncMode syncMode = FtpFolderSyncMode.Mirror) + public List MirrorLocalDir(string localDir, string remoteDir, FtpFolderSyncMode syncMode = FtpFolderSyncMode.Mirror) + { + List jobResult = new List(); + try + { + tryConnect(); + jobResult = client.UploadDirectory(localDir, remoteDir, syncMode); + //answ = (result != null && result.Count > 0); + } + catch (Exception exc) + { + Log.Error($"Eccezione in MirrorLocalDir{Environment.NewLine}{exc}"); + } + // chiudo! + client.Disconnect(); + return jobResult; + } + + /// + /// Scaricamento intera directory, modalità MIRROR, con restituzione elenco esiti + /// + /// Path directory da inviare (ad es:@"C:\website\videos\") + /// Nome remoto file per caricamento (ad es: @"/public_html/videos") + /// Modalità di sync (default = mirror) + public List MirrorRemoteDir(string localDir, string remoteDir, FtpFolderSyncMode syncMode = FtpFolderSyncMode.Mirror) + { + List jobResult = new List(); + try + { + tryConnect(); + jobResult = client.DownloadDirectory(localDir, remoteDir, syncMode); + } + catch (Exception exc) + { + Log.Error($"Eccezione in mirrorRemoteDir{Environment.NewLine}{exc}"); + } + // chiudo! + client.Disconnect(); + return jobResult; + } + + /// + /// Caricamento intera directory, modalità MIRROR + /// + /// Path directory da inviare (ad es:@"C:\website\videos\") + /// Nome remoto file per caricamento (ad es: @"/public_html/videos") + /// Modalità di sync (default = mirror) + public bool SendDir(string localDir, string remoteDir, FtpFolderSyncMode syncMode = FtpFolderSyncMode.Mirror) { bool answ = false; try @@ -218,8 +309,10 @@ namespace EgwProxy.Ftp //answ = (result != null && result.Count > 0); answ = result != null && result.Where(x => !x.IsSuccess).Count() == 0; } - catch - { } + catch (Exception exc) + { + Log.Error($"Eccezione in SendDir{Environment.NewLine}{exc}"); + } // chiudo! client.Disconnect(); return answ; @@ -230,17 +323,25 @@ namespace EgwProxy.Ftp /// /// Path locale del file da inviare (ad es: @"C:\MyVideo.mp4") /// NOme remoto file per caricamento (ad es: "/htdocs/MyVideo.mp4") - public bool sendFile(string fileName, string remoteName) + public bool SendFile(string fileName, string remoteName) { bool answ = false; - tryConnect(); - // effettuo caricamento puntuale - var result = client.UploadFile(fileName, remoteName); - answ = result == FtpStatus.Success; - // se insuccesso --> controllo se ci sia file... - if (!answ) + try { - answ = fileExists(remoteName); + tryConnect(); + tryConnect(); + // effettuo caricamento puntuale + var result = client.UploadFile(fileName, remoteName); + answ = result == FtpStatus.Success; + // se insuccesso --> controllo se ci sia file... + if (!answ) + { + answ = FileExists(remoteName); + } + } + catch (Exception exc) + { + Log.Error($"Eccezione in SendFile{Environment.NewLine}{exc}"); } // chiudo! client.Disconnect(); @@ -251,10 +352,18 @@ namespace EgwProxy.Ftp /// Verifica connessione con server FTP remoto /// /// - public bool serverOk() + public bool ServerOk() { - tryConnect(); - bool answ = client.IsConnected; + bool answ = false; + try + { + tryConnect(); + answ = client.IsConnected; + } + catch (Exception exc) + { + Log.Error($"Eccezione in ServerOk{Environment.NewLine}{exc}"); + } // chiudo! client.Disconnect(); return answ; @@ -264,13 +373,22 @@ namespace EgwProxy.Ftp /// Restituisce tipo server remoto /// /// - public string serverType() + public string ServerType() { - tryConnect(); - FtpServer srvType = client.ServerType; + string answ = ""; + try + { + tryConnect(); + FtpServer srvType = client.ServerType; + answ = $"{srvType}"; + } + catch (Exception exc) + { + Log.Error($"Eccezione in ServerType{Environment.NewLine}{exc}"); + } // chiudo! client.Disconnect(); - return $"{srvType}"; + return answ; } #endregion Public Methods @@ -284,14 +402,18 @@ namespace EgwProxy.Ftp #region Protected Properties protected string _passwd { get; set; } = ""; - protected string _rawCert { get; set; } = ""; protected string _server { get; set; } = ""; - protected string _userName { get; set; } = ""; #endregion Protected Properties + #region Private Fields + + private static Logger Log = LogManager.GetCurrentClassLogger(); + + #endregion Private Fields + #region Private Properties private FtpClient client { get; set; } @@ -308,9 +430,9 @@ namespace EgwProxy.Ftp } else { - Console.WriteLine($"{e.PolicyErrors}"); - Console.WriteLine($"Cert:{Environment.NewLine}{e.Certificate}"); - Console.WriteLine($"RawString:{Environment.NewLine}{e.Certificate.GetRawCertDataString()}"); + Log.Error($"{e.PolicyErrors}"); + Log.Error($"Cert:{Environment.NewLine}{e.Certificate}"); + Log.Error($"RawString:{Environment.NewLine}{e.Certificate.GetRawCertDataString()}"); throw new Exception($"{e.PolicyErrors}{Environment.NewLine}{e.Certificate.GetRawCertDataString()}"); } } diff --git a/EgwProxy.Ftp/packages.config b/EgwProxy.Ftp/packages.config index 946b7340..0f8c9cd5 100644 --- a/EgwProxy.Ftp/packages.config +++ b/EgwProxy.Ftp/packages.config @@ -2,5 +2,6 @@ + \ No newline at end of file diff --git a/IOB-WIN-NEXT/DATA/CONF/IMI_SONATEST.json b/IOB-WIN-NEXT/DATA/CONF/IMI_SONATEST.json index 3b36d5e9..23bb44ee 100644 --- a/IOB-WIN-NEXT/DATA/CONF/IMI_SONATEST.json +++ b/IOB-WIN-NEXT/DATA/CONF/IMI_SONATEST.json @@ -8,7 +8,7 @@ "ConnPasswd": "we4reFromB3rghem!", "RawCert": "", "SkipCert": true, - "ReExecVeto": 120, + "ReExecVeto": 90, "Note": "Sync periodico completo", "StepsList": [ { diff --git a/IOB-WIN-NEXT/IobNet/Ftp.cs b/IOB-WIN-NEXT/IobNet/Ftp.cs index 31f5c01e..124c11b0 100644 --- a/IOB-WIN-NEXT/IobNet/Ftp.cs +++ b/IOB-WIN-NEXT/IobNet/Ftp.cs @@ -168,10 +168,10 @@ namespace IOB_WIN_NEXT.IobNet public override void processCustomTaskLF() { lgInfo($"Richiesto processCustomTaskLF"); - // verifico di avere compiti da svolgere... if (currFtpTaskList != null && currFtpTaskList.ListTask != null && currFtpTaskList.ListTask.Count > 0) { + string sVal = ""; foreach (var srvFtp in currFtpTaskList.ListTask) { // verifico eventuale veto all'esecuzione... @@ -192,6 +192,16 @@ namespace IOB_WIN_NEXT.IobNet { bool fatto = doStep(step); stepDone += fatto ? 1 : 0; + // modificare: ricevere elenco di TUTTI i file con esito + +#if false + // per i file SCARICATI registrare in FLog... + string key = "FtpSync"; + string value = "SRC --> DEST | MB | ms"; + sVal = $"{key} | {value}"; + accodaFLog(sVal, qEncodeFLog(key, value)); +#endif + } lgInfo($"Completata esecuzione steps FTP | {srvFtp.ActionId} | {stepDone}/{srvFtp.StepsList.Count} Done/Req"); }