feat: Перенесен IntervalService

This commit is contained in:
Mikhail Trubnikov
2026-04-14 13:49:43 +10:00
parent d4176721eb
commit 13dd2815d4
17 changed files with 43 additions and 181 deletions

View File

@@ -4,6 +4,7 @@ using PARR.AIHITMainLoader.Models;
using PARR.AIHITMainLoader.Services;
using PARR.AIHITMainLoader.Settings;
using PARR.BLL.Services.Interfaces;
using PARR.Core.Common;
using PARR.Core.Common.RabbitServices;
using System.Text.Encodings.Web;
using System.Text.Json;

View File

@@ -3,6 +3,7 @@ using PARR.AIHITRelationshipsSyncer.Models;
using PARR.AIHITRelationshipsSyncer.Services.Interfaces;
using PARR.AIHITRelationshipsSyncer.Settings;
using PARR.BLL.Services.Interfaces;
using PARR.Core.Common;
using System.Text.Json;
namespace PARR.AIHITRelationshipsSyncer

View File

@@ -16,14 +16,13 @@ namespace PARR.BLL
services.AddSingleton(storageSettings);
services.AddTransient<IFileService, FileService>();
//services.AddTransient<IFileService, FileService>();
//services.AddTransient<IMqService, MqService>();
//services.AddTransient<IMqService, MqServiceV2>();
//services.AddHttpClient<IMqAdminService, MqAdminService>();
services.AddTransient<ITransformService, TransformService>();
services.AddTransient<IIntervalService, IntervalService>();
services.AddTransient<ICalendarService, CalendarService>();
services.AddTransient<ITemplateMaskValidator, TemplateMaskValidator>();
}

View File

@@ -1,124 +0,0 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using PARR.BLL.Domain;
using PARR.BLL.Services.Interfaces;
using PARR.BLL.Settings;
namespace PARR.BLL.Services.Implementations
{
internal class FileService : IFileService
{
private readonly StorageSettings storageSettings;
private readonly ILogger<FileService> logger;
public FileService(StorageSettings storageSettings, ILogger<FileService> logger)
{
this.storageSettings = storageSettings;
this.logger = logger;
}
public bool IsExtensionAllowed(IFormFile formFile, string[] allowedExtensions)
{
var extension = Path.GetExtension(formFile.FileName).ToLower();
return !string.IsNullOrEmpty(extension) && allowedExtensions.Contains(extension);
}
public bool IsNotExceededLimit(IFormFile formFile, int limitMb)
{
var fileSizeByte = formFile.Length;
var fileSizeMByte = fileSizeByte / 1024.0 / 1024.0;
return fileSizeMByte < limitMb;
}
public async Task<UploadResult?> UploadAsync(IFormFile formFile, string[] rootFolderWithoutStorage)
{
InitRootFolders(rootFolderWithoutStorage);
//Генерим уникальное имя файла: старое имя + рандом в середину
var newFileName = $"{Path.GetFileNameWithoutExtension(formFile.FileName)}-{Path.GetFileNameWithoutExtension(Path.GetRandomFileName())}{Path.GetExtension(formFile.FileName)}";
var relativePath = Path.Combine(Path.Combine(rootFolderWithoutStorage), newFileName);
var absPath = Path.Combine(storageSettings.StoragePath, relativePath);
if (File.Exists(absPath))
{
logger.LogError($"Не могу сохранить файл, такой файл уже существует: {absPath}");
return null;
}
try
{
var origFileName = Path.GetFileName(formFile.FileName);
using (var stream = new FileStream(absPath, FileMode.Create))
{
await formFile.CopyToAsync(stream);
logger.LogInformation($"Файл сохранен {origFileName}, {absPath}");
}
return new UploadResult { FileName = origFileName, Path = relativePath };
}
catch (Exception ex)
{
logger.LogError(ex, $"Ошибка при сохранении файла {absPath}");
return null;
}
}
private void InitRootFolders(string[] rootFolderWithoutStorage)
{
if (!rootFolderWithoutStorage.Any())
return;
var path = string.Empty;
foreach (var item in rootFolderWithoutStorage)
{
path = Path.Combine(path, item);
CreateDirectory(path);
}
}
private void CreateDirectory(string relativePath)
{
var absolutePath = Path.Combine(storageSettings.StoragePath, relativePath);
try
{
if (!Directory.Exists(absolutePath))
Directory.CreateDirectory(absolutePath);
// не нужно ничего возвращать, при сохраненнии файла будет ошибка и ее обработаем
//return true;
}
catch (Exception e)
{
logger.LogError(e, $"Не могу создать директорию: {absolutePath}");
//return false;
}
}
public bool DeleteFile(string path)
{
logger.LogInformation($"Удаляю файл: {path}");
try
{
if (!File.Exists(path))
return true;
File.Delete(path);
logger.LogInformation($"Файл удален: {path}");
return true;
}
catch (Exception e)
{
logger.LogError(e, $"Ошибка при удалении файла {path}");
return false;
}
}
}
}

View File

@@ -1,33 +0,0 @@
using Microsoft.AspNetCore.Http;
using PARR.BLL.Domain;
namespace PARR.BLL.Services.Interfaces
{
public interface IFileService
{
Task<UploadResult?> UploadAsync(IFormFile formFile, string[] rootFolderWithoutStorage);
/// <summary>
/// Размер файла не превышает лимит?
/// </summary>
/// <param name="formFile"></param>
/// <param name="limit"></param>
/// <returns></returns>
bool IsNotExceededLimit(IFormFile formFile, int limitMb);
/// <summary>
/// Разрешено это расширение?
/// </summary>
/// <param name="formFile"></param>
/// <param name="allowedExtensions"></param>
/// <returns></returns>
bool IsExtensionAllowed(IFormFile formFile, string[] allowedExtensions);
/// <summary>
/// Удалить файл
/// </summary>
/// <param name="path">Полный путь к файлу</param>
/// <returns></returns>
bool DeleteFile(string path);
}
}

View File

@@ -1,15 +0,0 @@
namespace PARR.BLL.Services.Interfaces
{
public delegate Task IntervalHandlerDelegate();
public interface IIntervalService
{
/// <summary>
/// Метод выполняется с интервалом
/// </summary>
/// <param name="intervalHandler">Метод который нужно выполнить</param>
/// <param name="interval">Интервал</param>
/// <returns></returns>
Task IntervalInitAsync(IntervalHandlerDelegate intervalHandler, TimeSpan interval);
}
}

View File

@@ -0,0 +1,16 @@
namespace PARR.Core.Common
{
//public delegate Task IntervalHandlerDelegate();
public interface IIntervalService
{
/// <summary>
/// Метод выполняется с интервалом
/// </summary>
/// <param name="handler">Метод который нужно выполнить</param>
/// <param name="interval">Интервал</param>
/// <returns></returns>
Task IntervalInitAsync(Func<Task> handler, TimeSpan interval);
//Task IntervalInitAsync(IntervalHandlerDelegate intervalHandler, TimeSpan interval);
}
}

View File

@@ -1,6 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PARR.BLL.Services.Interfaces;
using PARR.Core.Common;
using PARR.DAL.Contracts;
using PARR.EsppApi;
using PARR.EsppApi.Models;

View File

@@ -7,7 +7,7 @@ namespace PARR.EsppTemplateSync
internal class TemplateFileSyncer : ITemplateSyncer
{
private readonly ILogger<TemplateFileSyncer> logger;
private readonly IFileService fileService;
//private readonly IFileService fileService;
private readonly GlobalSettings globalSettings;
//private readonly IManager manager;
private readonly string dirPath;
@@ -18,13 +18,13 @@ namespace PARR.EsppTemplateSync
public TemplateFileSyncer(
BLL.Settings.StorageSettings storageSettings,
ILogger<TemplateFileSyncer> logger,
IFileService fileService,
//IFileService fileService,
GlobalSettings globalSettings//,
// IManager manager
)
{
this.logger = logger;
this.fileService = fileService;
//this.fileService = fileService;
this.globalSettings = globalSettings;
//this.manager = manager;
if (storageSettings.EsppTemplates == null)

View File

@@ -1,6 +1,8 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using PARR.Core.Common;
using PARR.Core.Common.RabbitServices;
using PARR.Infrastructure.Interval;
using PARR.Infrastructure.Rabbit;
namespace PARR.Infrastructure
@@ -20,6 +22,7 @@ namespace PARR.Infrastructure
services.AddHttpClient<IRabbitAdminService, RabbitAdminService>();
services.AddTransient<IRabbitService, RabbitService>();
services.AddTransient<IIntervalService, IntervalService>();
//services.AddTransient<IEmailService, EmailService>();
//services.AddTransient<IRabbitService, RabbitService>();

View File

@@ -1,7 +1,7 @@
using Microsoft.Extensions.Logging;
using PARR.BLL.Services.Interfaces;
using PARR.Core.Common;
namespace PARR.BLL.Services.Implementations
namespace PARR.Infrastructure.Interval
{
internal class IntervalService : IIntervalService
{
@@ -12,7 +12,8 @@ namespace PARR.BLL.Services.Implementations
this.logger = logger;
}
public async Task IntervalInitAsync(IntervalHandlerDelegate intervalHandler, TimeSpan interval)
//public async Task IntervalInitAsync(IntervalHandlerDelegate intervalHandler, TimeSpan interval)
public async Task IntervalInitAsync(Func<Task> handler, TimeSpan interval)
{
logger.LogInformation("Инициализация интервального сервиса с интервалом: {Interval}", interval);
@@ -26,7 +27,7 @@ namespace PARR.BLL.Services.Implementations
try
{
await intervalHandler.Invoke();
await handler.Invoke();
}
catch (Exception ex)
{

View File

@@ -2,6 +2,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PARR.BLL.Services.Interfaces;
using PARR.Core.Common;
using PARR.Core.Common.RabbitServices;
using PARR.DAL.Services.Interfaces.Job;
using PARR.Domain.Common.Rabbit.Messages;

View File

@@ -1,6 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PARR.BLL.Services.Interfaces;
using PARR.Core.Common;
using PARR.Master.Services;
using PARR.Master.Settings;

View File

@@ -1,7 +1,9 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using PARR.BLL;
using PARR.Core;
using PARR.DAL;
using PARR.Infrastructure;
using PARR.NextRun.Services;
using PARR.NextRun.Settings;
@@ -14,6 +16,9 @@ namespace PARR.NextRun
services.InstallBllServices(configuration);
services.InstallDalServices(configuration);
services.AddCoreServices(configuration);
services.AddInfrastructureServices(configuration);
var settings = new WorkerSettings();
configuration.GetSection(nameof(WorkerSettings)).Bind(settings);
services.AddSingleton(settings);

View File

@@ -1,6 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PARR.BLL.Services.Interfaces;
using PARR.Core.Common;
using PARR.Domain.Entities.Base.History;
using PARR.Domain.Enums;
using PARR.NextRun.Services;

View File

@@ -8,7 +8,10 @@
<ItemGroup>
<ProjectReference Include="..\PARR.BLL\PARR.BLL.csproj" />
<ProjectReference Include="..\PARR.Common\PARR.Common.csproj" />
<ProjectReference Include="..\PARR.DAL\PARR.DAL.csproj" />
<ProjectReference Include="..\PARR.Domain\PARR.Domain.csproj" />
<ProjectReference Include="..\PARR.Infrastructure\PARR.Infrastructure.csproj" />
</ItemGroup>
</Project>

View File

@@ -1,5 +1,6 @@
using Microsoft.EntityFrameworkCore;
using PARR.BLL.Services.Interfaces;
using PARR.Core.Common;
using PARR.DAL.DomainServices.Interfaces;
using PARR.DAL.Services.Interfaces;
using PARR.Domain.Enums;