using Microsoft.Extensions.Configuration; using NLog; namespace EgwCoreLib.Lux.Data.Services.General { /// /// Gestione IO File-based /// public class FileService : IFileService { #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 SaveFileContent{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 async Task SaveFileContentAsync(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); } await File.WriteAllTextAsync(filePath, content); } catch (Exception exc) { Log.Error($"Exception on SaveFileContentAsync{Environment.NewLine}{exc}"); } } return answ; } /// /// Effettua salvataggio di un ContentStream generico (non semplice string) /// /// /// /// /// public async Task SaveFileStreamAsync(string folderPath, string secureName, Stream contentStream) { bool answ = false; if (string.IsNullOrEmpty(folderPath)) return false; string filePath = Path.Combine(basePath, folderPath, secureName); string? directoryPath = Path.GetDirectoryName(filePath); try { if (!string.IsNullOrEmpty(directoryPath)) Directory.CreateDirectory(directoryPath); using var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None); await contentStream.CopyToAsync(fileStream); answ = true; } catch (Exception exc) { Log.Error($"Exception on SaveFileStreamAsync{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 } }