179 lines
5.8 KiB
C#
179 lines
5.8 KiB
C#
namespace EgwCoreLib.Lux.Data.Services.General
|
|
{
|
|
/// <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 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;
|
|
}
|
|
|
|
/// <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 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;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
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;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> 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;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> 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;
|
|
|
|
/// <summary>
|
|
/// Base path x network share files
|
|
/// </summary>
|
|
private string basePath = "unsafe_uploads";
|
|
|
|
#endregion Private Fields
|
|
}
|
|
} |