using Microsoft.Extensions.Configuration; using NLog; namespace EgwCoreLib.Lux.Data.Services { /// /// Gestione IO File-based /// public class FileService { #region Public Constructors public FileService(IConfiguration config) { _config = config; basePath = _config.GetValue("ServerConf:FileSharePath") ?? "unsafe_uploads"; } #endregion Public Constructors #region Public Methods /// /// Eliminazione file (old) /// /// Nome secure da impiegare /// Contenuto file public bool DeleteOldFile(string folderPath, string secureName) { bool answ = false; if (!string.IsNullOrEmpty(folderPath)) { // calcolo path file... string filePath = Path.Combine(basePath, folderPath, secureName); // se esiste... if (File.Exists(filePath)) { File.Delete(filePath); } } return answ; } /// /// Restituisce il contenuto del file salvato /// /// /// /// public string LoadFileContent(string folderPath, string secureName) { string answ = ""; if (!string.IsNullOrEmpty(folderPath)) { try { // calcolo path file... string filePath = Path.Combine(basePath, folderPath, secureName); if (File.Exists(filePath)) { answ = File.ReadAllText(filePath); } } catch (Exception exc) { Log.Error($"Exception on LoadFileContent{Environment.NewLine}{exc}"); } } return answ; } /// /// Esegue salvataggio del file ricevuto path, securename e content /// /// Path relativo x file (tipicamente UID parent order) /// Nome secure da impiegare /// Contenuto file public bool SaveFileContent(string folderPath, string secureName, string content) { bool answ = false; if (!string.IsNullOrEmpty(folderPath)) { // calcolo path file... string filePath = Path.Combine(basePath, folderPath, secureName); string? directoryPath = Path.GetDirectoryName(filePath); try { if (!string.IsNullOrEmpty(directoryPath)) { Directory.CreateDirectory(directoryPath); } File.WriteAllText(filePath, content); } catch (Exception exc) { Log.Error($"Exception on save{Environment.NewLine}{exc}"); } } return answ; } #endregion Public Methods #region Private Fields private static Logger Log = LogManager.GetCurrentClassLogger(); private IConfiguration _config; /// /// Base path x network share files /// private string basePath = "unsafe_uploads"; #endregion Private Fields } }