summaryrefslogtreecommitdiff
path: root/Services/MediaService.cs
diff options
context:
space:
mode:
authorJake Mannens <jake@asger.xyz>2023-08-29 16:19:03 +1000
committerJake Mannens <jake@asger.xyz>2023-08-29 16:19:03 +1000
commit7712b507a47b41f3c069bcf671d4592dcdc0f713 (patch)
tree46a0cf61c3804a604e668444baa9c4213ee1f09d /Services/MediaService.cs
parentcb8c17917d0363f09eeaa3c379617dc213b2dacf (diff)
Deleting media now deletes thumbnails on disk
Diffstat (limited to 'Services/MediaService.cs')
-rw-r--r--Services/MediaService.cs68
1 files changed, 50 insertions, 18 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);