using Azaion.Common; using Azaion.Common.Configs; using Azaion.Common.Database; using Azaion.Common.Entities; using LinqToDB; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Options; namespace Azaion.Services; public interface IResourcesService { Task GetEncryptedResource(string fileName, string key, CancellationToken cancellationToken = default); Task SaveResource(IFormFile data, CancellationToken cancellationToken = default); } public class ResourcesService(IOptions resourcesConfig) : IResourcesService { public async Task GetEncryptedResource(string fileName, string key, CancellationToken cancellationToken = default) { var resourcePath = Path.Combine(resourcesConfig.Value.ResourcesFolder, fileName); var fileStream = new FileStream(resourcePath, FileMode.Open, FileAccess.Read); var ms = new MemoryStream(); await fileStream.EncryptTo(ms, key, cancellationToken); ms.Seek(0, SeekOrigin.Begin); return ms; } public async Task SaveResource(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); await using var fileStream = new FileStream(resourcePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); await data.CopyToAsync(fileStream, cancellationToken); } }