diff options
| -rw-r--r-- | Services/MediaService.cs | 68 | ||||
| -rw-r--r-- | Todo.md | 1 |
2 files changed, 50 insertions, 19 deletions
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<Exception> 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); @@ -9,7 +9,6 @@ - Store original filesize as part of uploaded files - Progressive page loading - Proper thumbnail generation - - Deleting media also deletes thumbnails - Video support - User/security support |
