From cc2c8d2531bdeec513662939182a2e670df6ad6e Mon Sep 17 00:00:00 2001 From: rootdarkarchon Date: Fri, 17 Nov 2023 01:01:08 +0100 Subject: [PATCH] limit the hashes to take to locally available files only --- .../Services/FileCleanupService.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/FileCleanupService.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/FileCleanupService.cs index 7ba7932..0f724cc 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/FileCleanupService.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/FileCleanupService.cs @@ -5,6 +5,7 @@ using MareSynchronosShared.Models; using MareSynchronosShared.Services; using MareSynchronosStaticFilesServer.Utils; using Microsoft.EntityFrameworkCore; +using System.Globalization; namespace MareSynchronosStaticFilesServer.Services; @@ -164,8 +165,10 @@ public class FileCleanupService : IHostedService var prevTimeForcedDeletion = DateTime.Now.Subtract(TimeSpan.FromHours(forcedDeletionAfterHours)); DirectoryInfo dir = new(_cacheDir); var allFilesInDir = dir.GetFiles("*", SearchOption.AllDirectories); + var availableHashes = allFilesInDir.Select(k => char.ToUpper(k.Name[0], CultureInfo.InvariantCulture)).Distinct().ToList(); int filesToTake = 10000; - var filesChunk = await dbContext.Files.OrderBy(f => f.Hash).Take(filesToTake).ToListAsync().ConfigureAwait(false); + var files = dbContext.Files.Where(c => availableHashes.Contains(c.Hash.First())).OrderBy(f => f.Hash); + var filesChunk = await files.Take(filesToTake).ToListAsync(cancellationToken: ct).ConfigureAwait(false); int iterations = 1; var allFiles = new List(); while (filesChunk.Any()) @@ -222,7 +225,7 @@ public class FileCleanupService : IHostedService } allFiles.AddRange(filesChunk); - filesChunk = await dbContext.Files.OrderBy(f => f.Hash).Skip(filesToTake * iterations).Take(filesToTake).ToListAsync(cancellationToken: ct).ConfigureAwait(false); + filesChunk = await files.Skip(filesToTake * iterations).Take(filesToTake).ToListAsync(cancellationToken: ct).ConfigureAwait(false); iterations++; }