add possibility to upload and download from specific folder

This commit is contained in:
Alex Bezdieniezhnykh
2024-12-24 12:48:02 +02:00
parent 81d2409009
commit 0945635a1c
2 changed files with 21 additions and 14 deletions
+15 -8
View File
@@ -11,15 +11,17 @@ namespace Azaion.Services;
public interface IResourcesService
{
Task<Stream> GetEncryptedResource(string fileName, string key, CancellationToken cancellationToken = default);
Task SaveResource(IFormFile data, CancellationToken cancellationToken = default);
Task<Stream> GetEncryptedResource(string? dataFolder, string fileName, string key, CancellationToken cancellationToken = default);
Task SaveResource(string? dataFolder, IFormFile data, CancellationToken cancellationToken = default);
}
public class ResourcesService(IOptions<ResourcesConfig> resourcesConfig, ILogger<ResourcesService> logger) : IResourcesService
{
public async Task<Stream> GetEncryptedResource(string fileName, string key, CancellationToken cancellationToken = default)
public async Task<Stream> GetEncryptedResource(string? dataFolder, string fileName, string key, CancellationToken cancellationToken = default)
{
var resourcePath = Path.Combine(resourcesConfig.Value.ResourcesFolder, fileName);
var resourcePath = string.IsNullOrWhiteSpace(dataFolder)
? Path.Combine(resourcesConfig.Value.ResourcesFolder, fileName)
: Path.Combine(resourcesConfig.Value.ResourcesFolder, dataFolder, fileName);
var fileStream = new FileStream(resourcePath, FileMode.Open, FileAccess.Read);
var ms = new MemoryStream();
@@ -28,14 +30,19 @@ public class ResourcesService(IOptions<ResourcesConfig> resourcesConfig, ILogger
return ms;
}
public async Task SaveResource(IFormFile data, CancellationToken cancellationToken = default)
public async Task SaveResource(string? dataFolder, IFormFile data, CancellationToken cancellationToken = default)
{
if (data == null)
throw new BusinessException(ExceptionEnum.NoFileProvided);
if (!Directory.Exists(resourcesConfig.Value.ResourcesFolder))
Directory.CreateDirectory(resourcesConfig.Value.ResourcesFolder);
var resourcePath = Path.Combine(resourcesConfig.Value.ResourcesFolder, data.FileName);
var resourceFolder = string.IsNullOrWhiteSpace(dataFolder)
? resourcesConfig.Value.ResourcesFolder
: Path.Combine(resourcesConfig.Value.ResourcesFolder, dataFolder);
if (!Directory.Exists(resourceFolder))
Directory.CreateDirectory(resourceFolder);
var resourcePath = Path.Combine(resourceFolder, data.FileName);
await using var fileStream = new FileStream(resourcePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
await data.CopyToAsync(fileStream, cancellationToken);
logger.LogInformation($"Resource {data.FileName} Saved Successfully");