namespace Lux.Report.Data.Services { /// /// 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 /// public int CountFile(string folderPath, string pattern, bool hideDot) { int answ = 0; if (!string.IsNullOrEmpty(folderPath)) { // calcolo path file... string dirPath = Path.Combine(basePath, folderPath); // se esiste... if (Directory.Exists(dirPath)) { var fFound = Directory.GetFiles(dirPath, pattern); if (hideDot) { answ = fFound .Where(path => !Path.GetFileName(path).StartsWith(".")) .Count(); } else { answ = fFound.Count(); } } } return answ; } /// public bool FileCopy(string origPath, string destPath) { bool done = false; string fullPathFrom = Path.Combine(basePath, origPath); if (File.Exists(fullPathFrom)) { string fullPathTo = Path.Combine(basePath, destPath); File.Copy(fullPathFrom, fullPathTo, true); done = File.Exists(fullPathTo); } return done; } /// public bool FileDelete(string origPath) { bool done = false; string fullPathFrom = Path.Combine(basePath, origPath); if (File.Exists(fullPathFrom)) { File.Delete(fullPathFrom); done = !File.Exists(fullPathFrom); } return done; } /// public bool FileExists(string filePath) { // calcolo path file... string fullPath = Path.Combine(basePath, filePath); return File.Exists(fullPath); } /// public bool FolderExists(string folderPath) { // calcolo path file... string fullPath = Path.Combine(basePath, folderPath); return Directory.Exists(fullPath); } /// public List ListFile(string folderPath, string pattern, bool hideDot) { List answ = new(); if (!string.IsNullOrEmpty(folderPath)) { // calcolo path file... string dirPath = Path.Combine(basePath, folderPath); // se esiste... if (Directory.Exists(dirPath)) { var rawList = Directory.GetFiles(dirPath, pattern).ToList(); if (hideDot) { answ = rawList .Where(path => !Path.GetFileName(path).StartsWith(".")) .ToList(); } else { answ = rawList; } } } return answ; } /// public byte[] ReadAllBytes(string filePath) { string fullPath = Path.Combine(basePath, filePath); var rawData = File.ReadAllBytes(fullPath); return rawData; } #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 } }