added metadata loading to scanner

This commit is contained in:
2026-01-25 19:13:58 -05:00
parent 5042cc7ff5
commit 7c9ea505b0
5 changed files with 137 additions and 19 deletions

View File

@@ -1,5 +1,7 @@
using AZKiServer.Models;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using System.Collections.Frozen;
@@ -9,10 +11,15 @@ 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)
[BsonIgnoreExtraElements]
private record MediaInfo(string FilePath, int Version);
public async Task<FrozenDictionary<string, int>> GetExistingFilePathsAsync(CancellationToken cancellationToken = default)
{
var files = await _entries.Find("{}").Project(m => m.Filepath).ToListAsync(cancellationToken);
return files.ToFrozenSet();
var files = await _entries.Find("{}").As<MediaInfo>().ToListAsync(cancellationToken);
if (files.Count == 0)
return FrozenDictionary<string, int>.Empty;
return files.ToFrozenDictionary(m => m.FilePath, m => m.Version);
}
public async Task AddMediaBulkAsync(List<MediaEntry> entries, CancellationToken cancellationToken = default)
@@ -20,7 +27,7 @@ public class MediaService(IMongoDatabase db)
await _entries.InsertManyAsync(entries, cancellationToken: cancellationToken);
}
public async Task<List<MediaEntry>> GetEntriesInRangeAsync(MediaType mediaType, DateTime from, DateTime to)
public async Task<List<MediaEntry>> GetEntriesInRangeAsync(MediaType mediaType, DateTime from, DateTime to, CancellationToken cancellationToken = default)
{
var filter = Builders<MediaEntry>.Filter
.And([
@@ -29,11 +36,25 @@ public class MediaService(IMongoDatabase db)
Builders<MediaEntry>.Filter.Lte(m => m.Date, to),
]);
return _entries.Find(filter).ToList();
return await _entries.Find(filter).ToListAsync(cancellationToken);
}
public async Task DeleteAllEntriesAsync(IEnumerable<ObjectId> ids, CancellationToken cancellationToken = default)
{
await _entries.DeleteManyAsync(e => ids.Contains(e.Id), cancellationToken);
}
public async Task DeleteAllEntriesAsync(IEnumerable<string> paths, CancellationToken cancellationToken = default)
{
await _entries.DeleteManyAsync(e => paths.Contains(e.Filepath), cancellationToken);
}
#if DEBUG
public async Task DeleteAllEntriesAsync(CancellationToken cancellationToken = default) {
await _entries.DeleteManyAsync("{}", cancellationToken);
}
#endif
public class IndexCreation : BackgroundService
{
protected override Task ExecuteAsync(CancellationToken stoppingToken)
@@ -41,4 +62,6 @@ public class MediaService(IMongoDatabase db)
throw new NotImplementedException();
}
}
}