summaryrefslogtreecommitdiff
path: root/Services
diff options
context:
space:
mode:
Diffstat (limited to 'Services')
-rw-r--r--Services/ConfigService.cs14
-rw-r--r--Services/MediaService.cs55
2 files changed, 58 insertions, 11 deletions
diff --git a/Services/ConfigService.cs b/Services/ConfigService.cs
index 8460fd0..d2d1a06 100644
--- a/Services/ConfigService.cs
+++ b/Services/ConfigService.cs
@@ -1,11 +1,12 @@
namespace HyperBooru.Services;
public interface IConfigService {
- public string DataPath { get; }
- public string DbConnectionString { get; }
- public string MediaBasePath { get; }
- public string ThumbnailBasePath { get; }
- public bool EnableOcr { get; }
+ public string DataPath { get; }
+ public string DbConnectionString { get; }
+ public string MediaBasePath { get; }
+ public string ThumbnailBasePath { get; }
+ public string ConvertedMediaBasePath { get; }
+ public bool EnableOcr { get; }
}
public class ConfigService : IConfigService {
@@ -47,6 +48,9 @@ public class ConfigService : IConfigService {
public string ThumbnailBasePath =>
Path.Join(DataPath, "thumb");
+ public string ConvertedMediaBasePath =>
+ Path.Join(DataPath, "converted");
+
public bool EnableOcr =>
bool.TryParse(config["DisableOcr"], out bool x) ? !x : true;
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,