2026-05-14 12:31:35 +02:00
|
|
|
using AuthorBuddy.Web.Data;
|
2026-05-20 06:12:54 +02:00
|
|
|
using LiteDB;
|
2026-05-14 12:31:35 +02:00
|
|
|
|
|
|
|
|
namespace AuthorBuddy.Web.Services;
|
|
|
|
|
|
|
|
|
|
public class FileScannerService : IFileScannerService
|
|
|
|
|
{
|
|
|
|
|
private readonly IOllamaService _ollama;
|
2026-05-20 06:12:54 +02:00
|
|
|
private readonly ILiteDatabase _db;
|
2026-05-14 12:31:35 +02:00
|
|
|
|
2026-05-20 06:12:54 +02:00
|
|
|
public FileScannerService(IOllamaService ollama, ILiteDatabase db)
|
2026-05-14 12:31:35 +02:00
|
|
|
{
|
|
|
|
|
_ollama = ollama;
|
2026-05-20 06:12:54 +02:00
|
|
|
_db = db;
|
2026-05-14 12:31:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task ScanProjectFolderAsync(int projectId, string rootPath, IProgress<string> progress)
|
|
|
|
|
{
|
|
|
|
|
var files = Directory.GetFiles(rootPath, "*.md", SearchOption.AllDirectories)
|
|
|
|
|
.Where(f => f.Contains("00_Drafts") || f.Contains("01_Fakten"));
|
|
|
|
|
|
|
|
|
|
foreach (var file in files)
|
|
|
|
|
{
|
|
|
|
|
var fileInfo = new FileInfo(file);
|
2026-05-20 06:12:54 +02:00
|
|
|
bool needsUpdate = CheckIfUpdateRequired(projectId, file, fileInfo.LastWriteTimeUtc);
|
2026-05-14 12:31:35 +02:00
|
|
|
|
|
|
|
|
if (needsUpdate)
|
|
|
|
|
{
|
|
|
|
|
progress?.Report($"Verarbeite: {fileInfo.Name}...");
|
2026-05-20 06:12:54 +02:00
|
|
|
await ProcessFile(projectId, file, fileInfo.LastWriteTimeUtc);
|
2026-05-14 12:31:35 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
progress?.Report("Scan abgeschlossen.");
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-20 06:12:54 +02:00
|
|
|
private bool CheckIfUpdateRequired(int projectId, string file, DateTime lastWriteTimeUtc)
|
2026-05-14 12:31:35 +02:00
|
|
|
{
|
2026-05-20 06:12:54 +02:00
|
|
|
var col = _db.GetCollection<DocumentMetaEntity>("documentMeta");
|
|
|
|
|
var meta = col.FindOne(m => m.ProjectId == projectId && m.FilePath == file);
|
2026-05-14 12:31:35 +02:00
|
|
|
if (meta == null) return true;
|
2026-05-20 06:12:54 +02:00
|
|
|
return meta.LastModified < lastWriteTimeUtc;
|
2026-05-14 12:31:35 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-20 06:12:54 +02:00
|
|
|
private async Task<bool> ProcessFile(int projectId, string path, DateTime lastModified)
|
2026-05-14 12:31:35 +02:00
|
|
|
{
|
|
|
|
|
string content = await File.ReadAllTextAsync(path);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-05-20 06:12:54 +02:00
|
|
|
var col = _db.GetCollection<DocumentMetaEntity>("documentMeta");
|
|
|
|
|
var existing = col.FindOne(m => m.ProjectId == projectId && m.FilePath == path);
|
2026-05-14 12:31:35 +02:00
|
|
|
|
2026-05-20 06:12:54 +02:00
|
|
|
if (existing != null)
|
|
|
|
|
{
|
|
|
|
|
existing.LastModified = lastModified;
|
|
|
|
|
col.Update(existing);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
col.Insert(new DocumentMetaEntity
|
|
|
|
|
{
|
|
|
|
|
ProjectId = projectId,
|
|
|
|
|
FilePath = path,
|
|
|
|
|
LastModified = lastModified,
|
|
|
|
|
FileType = ".md"
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-05-14 12:31:35 +02:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|