configure grpc
This commit is contained in:
20
AZKiServer/Services/AZKiRpcService.cs
Normal file
20
AZKiServer/Services/AZKiRpcService.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using AZKiServer.Models;
|
||||
using AZKiServer.RPC;
|
||||
|
||||
using Google.Protobuf;
|
||||
|
||||
using Grpc.Core;
|
||||
|
||||
namespace AZKiServer.Services;
|
||||
public class AZKiRpcService(MediaService mediaService) : RPC.AZKi.AZKiBase
|
||||
{
|
||||
public override async Task<MediaList> GetMediaEntriesInRange(MediaRangeRequest request, ServerCallContext context)
|
||||
{
|
||||
var from = request.From.ToDateTime();
|
||||
var to = request.To.ToDateTime();
|
||||
var items = await mediaService.GetEntriesInRangeAsync(request.Type.FromRpc(), from, to);
|
||||
var result = new MediaList();
|
||||
result.Entries.AddRange(items.Select(e => e.ToRpc()));
|
||||
return result;
|
||||
}
|
||||
}
|
||||
55
AZKiServer/Services/FileScannerService.cs
Normal file
55
AZKiServer/Services/FileScannerService.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
|
||||
using AZKiServer.Models;
|
||||
|
||||
namespace AZKiServer.Services;
|
||||
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
_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");
|
||||
}
|
||||
}
|
||||
}
|
||||
44
AZKiServer/Services/MediaService.cs
Normal file
44
AZKiServer/Services/MediaService.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using AZKiServer.Models;
|
||||
|
||||
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 async Task<List<MediaEntry>> GetEntriesInRangeAsync(MediaType mediaType, DateTime from, DateTime to)
|
||||
{
|
||||
var filter = Builders<MediaEntry>.Filter
|
||||
.And([
|
||||
Builders<MediaEntry>.Filter.BitsAnySet(m => m.Type, (long)mediaType),
|
||||
Builders<MediaEntry>.Filter.Gte(m => m.Date, from),
|
||||
Builders<MediaEntry>.Filter.Lte(m => m.Date, to),
|
||||
]);
|
||||
|
||||
return _entries.Find(filter).ToList();
|
||||
|
||||
|
||||
}
|
||||
|
||||
public class IndexCreation : BackgroundService
|
||||
{
|
||||
protected override Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user