From 7712b507a47b41f3c069bcf671d4592dcdc0f713 Mon Sep 17 00:00:00 2001 From: Jake Mannens Date: Tue, 29 Aug 2023 16:19:03 +1000 Subject: Deleting media now deletes thumbnails on disk --- Services/MediaService.cs | 68 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 18 deletions(-) (limited to 'Services/MediaService.cs') diff --git a/Services/MediaService.cs b/Services/MediaService.cs index ccee027..00eeab5 100644 --- a/Services/MediaService.cs +++ b/Services/MediaService.cs @@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore; using MimeDetective; using MimeDetective.Definitions; using System.Security.Cryptography; +using System.Text.RegularExpressions; namespace HyperBooru.Services; @@ -24,6 +25,8 @@ public interface IMediaService { public void Delete(Guid media); public void Delete(Media media); + public void DeleteThumbnails(Guid media); + public void DeleteThumbnails(Media media); public Stream GetThumbnail(Guid media, int? width, int? height); public Stream GetThumbnail(Media media, int? width, int? height); public string GetPath(Media media); @@ -177,29 +180,22 @@ public class MediaService : IMediaService { using var db = dbFactory.CreateDbContext(); var m = db.Media.First(m => m.Guid == media); - try { - System.IO.File.Delete( - Path.Join( - config.MediaBasePath, - m.Guid.ToString().Substring(0, 2), - m.Guid.ToString().Substring(2, 2), - m.Guid.ToString())); - } catch(IOException) {} + var path = Path.Join( + config.MediaBasePath, + m.Guid.ToString().Substring(0, 2), + m.Guid.ToString().Substring(2, 2), + m.Guid.ToString()); try { - System.IO.Directory.Delete( - Path.Join( - config.MediaBasePath, - m.Guid.ToString().Substring(0, 2), - m.Guid.ToString().Substring(2, 2))); + var fileInfo = new FileInfo(path); + fileInfo.Delete(); + fileInfo.Directory?.Delete(); + fileInfo.Directory?.Parent?.Delete(); } catch(IOException) {} try { - System.IO.Directory.Delete( - Path.Join( - config.MediaBasePath, - m.Guid.ToString().Substring(0, 2))); - } catch(IOException) {} + DeleteThumbnails(media); + } catch {} db.Media.Remove(m); db.SaveChanges(); @@ -208,6 +204,42 @@ public class MediaService : IMediaService { public void Delete(Media media) => Delete(media.Guid); + public void DeleteThumbnails(Guid media) { + var dir = new DirectoryInfo(Path.Join( + config.ThumbnailBasePath, + media.ToString().Substring(0, 2), + media.ToString().Substring(2, 2))); + + var pattern = new Regex($"^{media}-[0-9]+-[0-9]+$"); + var toDelete = dir.GetFiles() + .Where(f => pattern.IsMatch(f.Name)) + .ToList(); + + List exceptions = new(); + + foreach(var file in toDelete) { + try { + file.Delete(); + } catch(Exception e) { + exceptions.Add(e); + } + } + + try { + dir.Delete(); + dir.Parent?.Delete(); + } catch(Exception e) { + exceptions.Add(e); + } + + // TODO: wrap the AggregateException in a ThumbnailException + if(exceptions.Count() > 1) + throw new AggregateException(exceptions); + } + + public void DeleteThumbnails(Media media) => + DeleteThumbnails(media.Guid); + public Stream GetThumbnail(Guid media, int? width, int? height) { using var db = dbFactory.CreateDbContext(); var m = db.Media.First(m => m.Guid == media); -- cgit v1.3