Improvements to CleanupBrokenPhotosMaintenanceJob

This commit is contained in:
jvyden 2021-11-24 18:27:21 -05:00
commit f680c90d47
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
2 changed files with 54 additions and 12 deletions

View file

@ -1,8 +1,11 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Types;
using LBPUnion.ProjectLighthouse.Types.Files;
namespace LBPUnion.ProjectLighthouse.Maintenance.MaintenanceJobs
{
@ -17,22 +20,49 @@ namespace LBPUnion.ProjectLighthouse.Maintenance.MaintenanceJobs
{
foreach (Photo photo in this.database.Photos)
{
bool hashNullOrEmpty = string.IsNullOrEmpty
(photo.LargeHash) ||
string.IsNullOrEmpty(photo.MediumHash) ||
string.IsNullOrEmpty(photo.SmallHash) ||
string.IsNullOrEmpty(photo.PlanHash);
bool hashNullOrEmpty = false;
bool noHashesExist = false;
bool largeHashIsInvalidFile = false;
bool allHashesDontExist = FileHelper.ResourcesNotUploaded(photo.LargeHash, photo.MediumHash, photo.SmallHash, photo.PlanHash).Length != 0;
hashNullOrEmpty = string.IsNullOrEmpty
(photo.LargeHash) ||
string.IsNullOrEmpty(photo.MediumHash) ||
string.IsNullOrEmpty(photo.SmallHash) ||
string.IsNullOrEmpty(photo.PlanHash);
if (hashNullOrEmpty) goto removePhoto;
if (hashNullOrEmpty || allHashesDontExist)
List<string> hashes = new()
{
Console.WriteLine
(
$"Removing photo (id: {photo.PhotoId}): {nameof(hashNullOrEmpty)}: {hashNullOrEmpty}, {nameof(allHashesDontExist)}: {allHashesDontExist}"
);
this.database.Photos.Remove(photo);
photo.LargeHash,
photo.MediumHash,
photo.SmallHash,
photo.PlanHash,
};
noHashesExist = FileHelper.ResourcesNotUploaded(hashes.ToArray()).Length != 0;
if (noHashesExist) goto removePhoto;
LbpFile? file = LbpFile.FromHash(photo.LargeHash);
// Console.WriteLine(file.FileType, );
if (file == null || file.FileType != LbpFileType.Jpeg && file.FileType != LbpFileType.Png)
{
largeHashIsInvalidFile = true;
goto removePhoto;
}
continue;
removePhoto:
Console.WriteLine
(
$"Removing photo (id: {photo.PhotoId}): " +
$"{nameof(hashNullOrEmpty)}: {hashNullOrEmpty}, " +
$"{nameof(noHashesExist)}: {noHashesExist}, " +
$"{nameof(largeHashIsInvalidFile)}: {largeHashIsInvalidFile}"
);
this.database.Photos.Remove(photo);
}
await this.database.SaveChangesAsync();

View file

@ -1,3 +1,5 @@
#nullable enable
using System.IO;
using LBPUnion.ProjectLighthouse.Helpers;
namespace LBPUnion.ProjectLighthouse.Types.Files
@ -20,5 +22,15 @@ namespace LBPUnion.ProjectLighthouse.Types.Files
this.Data = data;
this.FileType = FileHelper.DetermineFileType(this.Data);
}
public static LbpFile? FromHash(string hash)
{
string path = FileHelper.GetResourcePath(hash);
if (!File.Exists(path)) return null;
byte[] data = File.ReadAllBytes(path);
return new LbpFile(data);
}
}
}