Creado apartir del commit b888de6c92056de294456f581a56e25e734d4ab7 de develop
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
//using Microsoft.AspNetCore.Http;
|
||||
|
||||
using adas_core.Application.Services.Interfaces;
|
||||
using adas_core.Domain.Enums;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Domain.Models.DTO;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Serilog;
|
||||
|
||||
namespace adas_core.Application.Services;
|
||||
|
||||
public class FileService : IFileService
|
||||
{
|
||||
private readonly string? _assetsDirectory;
|
||||
|
||||
private readonly ILogger<FileService> _logger;
|
||||
private readonly string? _updateDirectory;
|
||||
|
||||
public FileService(
|
||||
IOptions<ApiSettings> apiSettings,
|
||||
ILogger<FileService> logger
|
||||
)
|
||||
{
|
||||
_logger = logger;
|
||||
|
||||
if (apiSettings.Value.PathUpdateFiles != null)
|
||||
_updateDirectory = Path.Combine(apiSettings.Value.PathUpdateFiles);
|
||||
if (apiSettings.Value.PathToDisplayAssets != null)
|
||||
_assetsDirectory = Path.Combine(apiSettings.Value.PathToDisplayAssets);
|
||||
}
|
||||
|
||||
|
||||
public async Task<bool> CopyUpdateFiles(ICollection<IFormFile> files)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_updateDirectory)) return false;
|
||||
|
||||
foreach (var file in files)
|
||||
{
|
||||
await using var stream = new FileStream(Path.Combine(_updateDirectory, file.FileName), FileMode.Create);
|
||||
await file.CopyToAsync(stream);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> UploadAssetFiles(ICollection<IFormFile> files, FileEnum.AssetTheme themeParse)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_assetsDirectory)) return false;
|
||||
var directoryInfo = new DirectoryInfo(Path.Combine(_assetsDirectory, themeParse.ToString()));
|
||||
if (!directoryInfo.Exists) directoryInfo.Create();
|
||||
foreach (var file in files)
|
||||
{
|
||||
await using var stream =
|
||||
new FileStream(Path.Combine(_assetsDirectory, themeParse.ToString(), file.FileName), FileMode.Create);
|
||||
await file.CopyToAsync(stream);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<AssetDto> GetAllAssetFile(FileEnum.AssetTheme themeParse)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_assetsDirectory)) return [];
|
||||
var listToReturn = new List<AssetDto>();
|
||||
var directoryInfo = new DirectoryInfo(Path.Combine(_assetsDirectory, themeParse.ToString()));
|
||||
|
||||
if (!directoryInfo.Exists) directoryInfo.Create();
|
||||
|
||||
var files = directoryInfo.GetFiles(); // Obtener todos los archivos en el directorio
|
||||
foreach (var file in files)
|
||||
{
|
||||
var assetDto = new AssetDto
|
||||
{
|
||||
Name = file.Name,
|
||||
Extension = file.Extension,
|
||||
Path = file.FullName // Obtener la ruta completa del archivo
|
||||
};
|
||||
listToReturn.Add(assetDto);
|
||||
}
|
||||
|
||||
return listToReturn;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError("Error while retrieving assets: {eMessage} {eStackTrace}", e.Message, e.StackTrace);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
public List<string> GetFilesInDirectory(string directoryPath)
|
||||
{
|
||||
List<string> fileList = [];
|
||||
|
||||
try
|
||||
{
|
||||
if (Directory.Exists(directoryPath))
|
||||
// Obtiene todos los archivos en el directorio
|
||||
fileList.AddRange(Directory.GetFiles(directoryPath));
|
||||
else
|
||||
Log.Warning("La ruta proporcionada no existe: {directoryPath}", directoryPath);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error("Ocurrió un error al buscar archivos: {exMessage}", ex.Message);
|
||||
}
|
||||
|
||||
return fileList;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user