From 71c53f4fe7551363601fb1056a9375e7dcb0290a Mon Sep 17 00:00:00 2001 From: Mark Otway Date: Mon, 18 Jan 2021 22:40:16 +0000 Subject: [PATCH] First working duplicated page #78 --- Damselfly.Web/Data/ImageService.cs | 25 +++++++++++++++--------- Damselfly.Web/Pages/DuplicatesPage.razor | 8 +++++++- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/Damselfly.Web/Data/ImageService.cs b/Damselfly.Web/Data/ImageService.cs index b2b64052..73be7e5d 100644 --- a/Damselfly.Web/Data/ImageService.cs +++ b/Damselfly.Web/Data/ImageService.cs @@ -130,20 +130,27 @@ public static List> GetImagesWithDuplicates() var watch = new Stopwatch("GetImagesWithDupes"); // Craft the SQL manually as server-side groupby isn't supported by EF Core. - var sql = "SELECT im.* from ImageMetaData im where im.hash in (SELECT hash from ImageMetaData where hash is not null group by hash having count( distinct ImageID ) > 1)"; + // Select all images that have the same hash as more than one image. + var subQuery = "SELECT hash from ImageMetaData where hash is not null and hash <> \"\" group by hash having count( distinct ImageID ) > 1"; + var sql = $"SELECT im.* from ImageMetaData im where im.hash in ({subQuery})"; - var dupeImageMetaData = db.ImageMetaData.FromSqlRaw(sql) - .Include(x => x.Image) - .ThenInclude(x => x.Folder) - .Select( x => x.Image ) - .ToList(); + var dupes = db.ImageMetaData.FromSqlRaw(sql) + .Where(x => x.Hash != null) + .Include(x => x.Image) + .ThenInclude(x => x.Folder) + .ToList(); + + // Backfill the metadata for the child image object, so we can select it. + dupes.ForEach(x => x.Image.MetaData = x ); - var listOfLists = dupeImageMetaData.Where( x => x.MetaData != null ) + // Group by the hash and pick all of the images for each one. + var listOfLists = dupes.Select( x => x.Image ) .GroupBy(x => x.MetaData.Hash) - .Where( x => x != null ) - .Select( x => x.ToList() ) + .Select( x => x.OrderBy( x => x.SortDate ).ToList() ) .ToList(); + watch.Stop(); + return listOfLists; } diff --git a/Damselfly.Web/Pages/DuplicatesPage.razor b/Damselfly.Web/Pages/DuplicatesPage.razor index 3c7c1106..0a158ea3 100644 --- a/Damselfly.Web/Pages/DuplicatesPage.razor +++ b/Damselfly.Web/Pages/DuplicatesPage.razor @@ -23,7 +23,8 @@ @foreach (var img in list) {
- @img.FileName + @img.FullPath + ;
} @@ -42,6 +43,11 @@ List> imageLists = new List>(); + private string GetImgUrl( Image image ) + { + return $"/thumb/{ThumbSize.Small}/{image.ImageId}"; + } + public Task>> LoadData() { var watch = new Stopwatch("DupesLoadData");