file scanner
This commit is contained in:
@@ -1,16 +1,55 @@
|
||||
|
||||
using AZKiServer.Models;
|
||||
|
||||
namespace AZKiServer.Services;
|
||||
|
||||
public class FileScannerService : IHostedService
|
||||
public class FileScannerService(MediaService mediaService, IConfiguration config, ILogger<FileScannerService> logger) : IHostedService, IDisposable
|
||||
{
|
||||
private Timer? _timer;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_timer?.Dispose();
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var path = config["SCAN_PATH"];
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
return Task.CompletedTask;
|
||||
_timer = new Timer((_) =>
|
||||
{
|
||||
ScanFilesAsync(path).Wait();
|
||||
}, null, TimeSpan.FromMinutes(1), TimeSpan.FromHours(1));
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
_timer?.Dispose();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
||||
private async Task ScanFilesAsync(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
var files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
|
||||
var existingFiles = await mediaService.GetExistingFilePathsAsync();
|
||||
var entries = new List<MediaEntry>();
|
||||
foreach (var filePath in files)
|
||||
{
|
||||
var relativePath = Path.GetRelativePath(path, filePath);
|
||||
if (existingFiles.Contains(relativePath))
|
||||
continue;
|
||||
entries.Add(MediaEntry.Parse(relativePath));
|
||||
}
|
||||
await mediaService.AddMediaBulkAsync(entries);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Failed to read directory contents");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,30 @@
|
||||
namespace AZKiServer.Services;
|
||||
using AZKiServer.Models;
|
||||
|
||||
public class MediaService
|
||||
using MongoDB.Driver;
|
||||
|
||||
using System.Collections.Frozen;
|
||||
|
||||
namespace AZKiServer.Services;
|
||||
|
||||
public class MediaService(IMongoDatabase db)
|
||||
{
|
||||
public readonly IMongoCollection<MediaEntry> _entries = db.GetCollection<MediaEntry>("media");
|
||||
public async Task<FrozenSet<string>> GetExistingFilePathsAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
var files = await _entries.Find("{}").Project(m => m.Filepath).ToListAsync(cancellationToken);
|
||||
return files.ToFrozenSet();
|
||||
}
|
||||
|
||||
public async Task AddMediaBulkAsync(List<MediaEntry> entries, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await _entries.InsertManyAsync(entries, cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
public class IndexCreation : BackgroundService
|
||||
{
|
||||
protected override Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user