Files
lux/Lux.Report.Data/Services/FileService.cs
T

115 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
}
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 FileExists(string filePath)
{
// calcolo path file...
string fullPath = Path.Combine(basePath, filePath);
return File.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;
}
#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
}
}