mirror of
https://github.com/azaion/admin.git
synced 2026-04-22 07:06:34 +00:00
add possibility to upload and download from specific folder
This commit is contained in:
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user