diff options
| author | Jake Mannens <jake@asger.xyz> | 2026-03-30 04:39:14 +1100 |
|---|---|---|
| committer | Jake Mannens <jake@asger.xyz> | 2026-03-30 04:39:14 +1100 |
| commit | dfe942d3cf4a60c06a530c64793981bfff5fe9fd (patch) | |
| tree | 07e351fb7aa412a90559a352908927593b38a171 /Services/MediaService.cs | |
| parent | 9f4033649a53222b0543343fc02b0c74a72235b4 (diff) | |
v0.5av0.5a
Diffstat (limited to 'Services/MediaService.cs')
| -rw-r--r-- | Services/MediaService.cs | 55 |
1 files changed, 49 insertions, 6 deletions
diff --git a/Services/MediaService.cs b/Services/MediaService.cs index a5803f9..2f7eac6 100644 --- a/Services/MediaService.cs +++ b/Services/MediaService.cs @@ -1,4 +1,5 @@ using ImageMagick; +using ImageMagick.Formats; using Microsoft.EntityFrameworkCore; using MimeDetective; using MimeDetective.Definitions; @@ -31,14 +32,21 @@ public interface IMediaService { public void DeleteThumbnails(Media media); public Stream GetThumbnail(Guid media, int? width, int? height); public Stream GetThumbnail(Media media, int? width, int? height); + public Stream GetConverted(Guid mediaId, string mimeType = "image/png"); + public Stream GetConverted(Media media, string mimeType = "image/png"); public string GetPath(Guid media); - public string GetPath(Guid media, int? width, int? height); public string GetPath(Media media); - public string GetPath(Media media, int? width, int? height); } public class MediaService : IMediaService { + private readonly Dictionary<string,MagickFormat> FormatMap = new() { + ["image/jpeg"] = MagickFormat.Jpeg, + ["image/jpg"] = MagickFormat.Jpg, + ["image/png"] = MagickFormat.Png, + ["image/webp"] = MagickFormat.WebP + }; + private IDbContextFactory<HBContext> dbFactory; private IConfigService config; @@ -267,7 +275,7 @@ public class MediaService : IMediaService { "Both width and height cannot be null!", mediaId); - var thumbPath = GetPath(mediaId, width, height); + var thumbPath = GetThumbnailPath(mediaId, width, height); if(File.Exists(thumbPath)) return System.IO.File.OpenRead(thumbPath); @@ -288,9 +296,30 @@ public class MediaService : IMediaService { return System.IO.File.OpenRead(thumbPath); } + public Stream GetConverted(Guid mediaId, string mimeType) { + if(!FormatMap.TryGetValue(mimeType, out var format)) + throw new MediaException($"Cannot convert to unknown format ({mimeType})", mediaId); + + var convertedPath = GetConvertedPath(mediaId, mimeType); + + if(File.Exists(convertedPath)) + return System.IO.File.OpenRead(convertedPath); + + if(!File.Exists(GetPath(mediaId))) + throw new ObjectNotFoundException(mediaId); + + using var image = new MagickImage(GetPath(mediaId)); + image.Write(convertedPath, format); + + return System.IO.File.OpenRead(convertedPath); + } + public Stream GetThumbnail(Media media, int? width, int? height) => GetThumbnail(media.Guid, width, height); + public Stream GetConverted(Media media, string mimeType) => + GetConverted(media.Guid, mimeType); + public string GetPath(Guid mediaId) { var fileInfo = new FileInfo( Path.Join( @@ -303,7 +332,7 @@ public class MediaService : IMediaService { return fileInfo.FullName; } - public string GetPath(Guid mediaId, int? width, int? height) { + public string GetThumbnailPath(Guid mediaId, int? width, int? height) { if(width is null && height is null) throw new ThumbnailException( "Both width and height cannot be null!", @@ -319,11 +348,25 @@ public class MediaService : IMediaService { return fileInfo.FullName; } + public string GetConvertedPath(Guid mediaId, string mimeType) { + var fileInfo = new FileInfo(Path.Join( + config.ConvertedMediaBasePath, + mediaId.ToString().Substring(0, 2), + mediaId.ToString().Substring(2, 2), + $"{mediaId.ToString()}-{mimeType.Split('/')[1]}")); + + Directory.CreateDirectory(fileInfo.Directory!.FullName); + return fileInfo.FullName; + } + public string GetPath(Media media) => GetPath(media.Guid); - public string GetPath(Media media, int? width, int? height) => - GetPath(media.Guid, width, height); + public string GetThumbnailPath(Media media, int? width, int? height) => + GetThumbnailPath(media.Guid, width, height); + + public string GetConvertedPath(Media media, string mimeType) => + GetConvertedPath(media.Guid, mimeType); private int GetUploadedFileHash(UploadedFile uf) => ( uf.CreateTime, |
