180 lines
6.2 KiB
C#
180 lines
6.2 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using NLog;
|
|
|
|
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
|
|
|
|
/// <summary>
|
|
/// Eliminazione file (old)
|
|
/// </summary>
|
|
/// <param name="secureName">Nome secure da impiegare</param>
|
|
/// <param name="content">Contenuto file</param>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Restituisce il contenuto del file salvato
|
|
/// </summary>
|
|
/// <param name="folderPath"></param>
|
|
/// <param name="secureName"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Esegue salvataggio del file ricevuto path, securename e content
|
|
/// </summary>
|
|
/// <param name="folderPath">Path relativo x file (tipicamente UID parent order)</param>
|
|
/// <param name="secureName">Nome secure da impiegare</param>
|
|
/// <param name="content">Contenuto file</param>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Esegue salvataggio del file ricevuto path, securename e content
|
|
/// </summary>
|
|
/// <param name="folderPath">Path relativo x file (tipicamente UID parent order)</param>
|
|
/// <param name="secureName">Nome secure da impiegare</param>
|
|
/// <param name="content">Contenuto file</param>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua salvataggio di un ContentStream generico (non semplice string)
|
|
/// </summary>
|
|
/// <param name="folderPath"></param>
|
|
/// <param name="secureName"></param>
|
|
/// <param name="contentStream"></param>
|
|
/// <returns></returns>
|
|
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
|
|
}
|
|
} |