Skip to content
Snippets Groups Projects
Commit 419ed9b5 authored by Lucca Santangelo's avatar Lucca Santangelo
Browse files

add file cleaner hosted service

parent c0fb1b27
No related branches found
No related tags found
2 merge requests!26Develop,!21course sections
Pipeline #10295 passed
...@@ -5,3 +5,4 @@ ...@@ -5,3 +5,4 @@
.vs .vs
obj obj
bin bin
StaticFiles
...@@ -68,7 +68,7 @@ namespace Tsi1.Api ...@@ -68,7 +68,7 @@ namespace Tsi1.Api
services.AddSingleton<IJwtAuthManager, JwtAuthManager>(); services.AddSingleton<IJwtAuthManager, JwtAuthManager>();
services.AddHostedService<JwtRefreshTokenCache>(); services.AddHostedService<JwtRefreshTokenCache>();
services.AddHostedService<JwtVerificationCodeCache>(); services.AddHostedService<JwtVerificationCodeCache>();
services.AddHostedService<FileCleanerService>(); services.AddHostedService<FileCleaner>();
services.AddSingleton<IMessageService, MessageService>(); services.AddSingleton<IMessageService, MessageService>();
......
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System; using System;
using System.Collections.Generic; using System.Linq;
using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Tsi1.BusinessLayer.Interfaces; using Tsi1.BusinessLayer.Interfaces;
namespace Tsi1.BusinessLayer.HostedServices namespace Tsi1.BusinessLayer.HostedServices
{ {
public class FileCleanerService : IHostedService, IDisposable public class FileCleaner : IHostedService, IDisposable
{ {
private Timer _timer; private Timer _timer;
private readonly IFileService _fileService; private readonly IServiceScopeFactory _serviceScopeFactory;
public FileCleanerService(IFileService fileService) public FileCleaner(IServiceScopeFactory serviceScopeFactory)
{ {
_fileService = fileService; _serviceScopeFactory = serviceScopeFactory;
} }
public Task StartAsync(CancellationToken cancellationToken) public Task StartAsync(CancellationToken cancellationToken)
{ {
throw new NotImplementedException(); _timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromDays(1));
return Task.CompletedTask;
} }
private void DoWork(object state) private async void DoWork(object state)
{ {
using (var scope = _serviceScopeFactory.CreateScope())
{
var fileService = scope.ServiceProvider.GetRequiredService<IFileService>();
var dbFileNames = await fileService.GetFileNames();
var fsFiles = fileService.GetPhysicalFiles();
var filesToDelete = fsFiles.Except(dbFileNames);
foreach (var file in filesToDelete)
{
fileService.DeleteFile(file);
}
}
} }
public Task StopAsync(CancellationToken cancellationToken) public Task StopAsync(CancellationToken cancellationToken)
{ {
throw new NotImplementedException(); _timer?.Change(Timeout.Infinite, 0);
return Task.CompletedTask;
} }
public void Dispose() public void Dispose()
......
...@@ -14,5 +14,6 @@ namespace Tsi1.BusinessLayer.Interfaces ...@@ -14,5 +14,6 @@ namespace Tsi1.BusinessLayer.Interfaces
Task<List<string>> GetFileNames(); Task<List<string>> GetFileNames();
ServiceResult<bool> DeleteFile(string filePath); ServiceResult<bool> DeleteFile(string filePath);
bool ExistFile(string relativePath); bool ExistFile(string relativePath);
List<string> GetPhysicalFiles();
} }
} }
...@@ -19,9 +19,7 @@ namespace Tsi1.BusinessLayer.Services ...@@ -19,9 +19,7 @@ namespace Tsi1.BusinessLayer.Services
public class FileService : IFileService public class FileService : IFileService
{ {
private readonly Tsi1Context _context; private readonly Tsi1Context _context;
private readonly IMapper _mapper; private readonly IMapper _mapper;
private readonly string _path; private readonly string _path;
public FileService(Tsi1Context context, IMapper mapper, IHostingEnvironment hostingEnvironment) public FileService(Tsi1Context context, IMapper mapper, IHostingEnvironment hostingEnvironment)
...@@ -108,21 +106,22 @@ namespace Tsi1.BusinessLayer.Services ...@@ -108,21 +106,22 @@ namespace Tsi1.BusinessLayer.Services
return System.IO.File.Exists(path); return System.IO.File.Exists(path);
} }
public async Task<List<string>> GetFileNames()
{
return await _context.Files.Select(x => x.Path).ToListAsync();
}
public List<string> GetPhysicalFiles()
{
return Directory.GetFiles(_path, "*", SearchOption.AllDirectories).ToList();
}
private string GenerateFileName(IFormFile file) private string GenerateFileName(IFormFile file)
{ {
var ext = file.FileName.Split(".").Last(); var ext = file.FileName.Split(".").Last();
var fileName = Guid.NewGuid().ToString(); var fileName = Guid.NewGuid().ToString();
return fileName + "." + ext; return fileName + "." + ext;
} }
public async Task<List<string>> GetFileNames()
{
var fileNames = await _context.Files.Select(x => Path.GetFileName(x.Path)).ToListAsync();
fileNames.RemoveAll(x => x.Length == 0);
return fileNames;
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment