Files
2026-04-09 12:18:23 +02:00

141 lines
4.2 KiB
C#

namespace Lux.Report.Data.Services
{
/// <summary>
/// Gestione IO File-based
/// </summary>
public class FileService : IFileService
{
#region Public Constructors
public FileService(IConfiguration config)
{
_config = config;
basePath = _config.GetValue<string>("ServerConf:FileSharePath") ?? "unsafe_uploads";
}
#endregion Public Constructors
#region Public Methods
/// <inheritdoc />
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;
}
/// <inheritdoc />
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;
}
/// <inheritdoc />
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;
}
/// <inheritdoc />
public bool FileExists(string filePath)
{
// calcolo path file...
string fullPath = Path.Combine(basePath, filePath);
return File.Exists(fullPath);
}
/// <inheritdoc />
public bool FolderExists(string folderPath)
{
// calcolo path file...
string fullPath = Path.Combine(basePath, folderPath);
return Directory.Exists(fullPath);
}
/// <inheritdoc />
public List<string> ListFile(string folderPath, string pattern, bool hideDot)
{
List<string> 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;
}
/// <inheritdoc />
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;
/// <summary>
/// Base path x network share files
/// </summary>
private string basePath = "unsafe_uploads";
#endregion Private Fields
}
}