mirror of
https://github.com/azaion/admin.git
synced 2026-04-22 10:46:33 +00:00
6d8ea6c74f
add logging add scripts for server
43 lines
1.8 KiB
C#
43 lines
1.8 KiB
C#
using Azaion.Common;
|
|
using Azaion.Common.Configs;
|
|
using Azaion.Common.Database;
|
|
using Azaion.Common.Entities;
|
|
using LinqToDB;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace Azaion.Services;
|
|
|
|
public interface IResourcesService
|
|
{
|
|
Task<Stream> GetEncryptedResource(string fileName, string key, CancellationToken cancellationToken = default);
|
|
Task SaveResource(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)
|
|
{
|
|
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);
|
|
logger.LogInformation($"Resource {data.FileName} Saved Successfully");
|
|
}
|
|
} |