summaryrefslogtreecommitdiff
path: root/Services/MediaService.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Services/MediaService.cs')
-rw-r--r--Services/MediaService.cs75
1 files changed, 40 insertions, 35 deletions
diff --git a/Services/MediaService.cs b/Services/MediaService.cs
index 104d0db..a5803f9 100644
--- a/Services/MediaService.cs
+++ b/Services/MediaService.cs
@@ -31,8 +31,10 @@ 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 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 string GetPath(Media media, int? width, int? height);
}
@@ -259,37 +261,29 @@ public class MediaService : IMediaService {
public void DeleteThumbnails(Media media) =>
DeleteThumbnails(media.Guid);
- public Stream GetThumbnail(Guid media, int? width, int? height) {
- using var db = dbFactory.CreateDbContext();
+ public Stream GetThumbnail(Guid mediaId, int? width, int? height) {
+ if(width is null && height is null)
+ throw new ThumbnailException(
+ "Both width and height cannot be null!",
+ mediaId);
- var m = db.Media
- .Include(m => m.CurrentUploadedFile)
- .First(m => m.Guid == media);
- if(m is null)
- throw new ObjectNotFoundException(media);
+ var thumbPath = GetPath(mediaId, width, height);
- if(m.CurrentUploadedFile.MimeType.Split("/")[0] != "image")
- throw new ThumbnailException("Media object not an image", m);
+ if(File.Exists(thumbPath))
+ return System.IO.File.OpenRead(thumbPath);
- using var image = new MagickImage(GetPath(m));
+ if(!File.Exists(GetPath(mediaId)))
+ throw new ObjectNotFoundException(mediaId);
- if(width is null && height is null)
- throw new ThumbnailException("Both width and height cannot be null!", m);
+ using var image = new MagickImage(GetPath(mediaId));
if(width > image.Width || height > image.Height)
- throw new ThumbnailException("Requested thumbnail size is larger than original media", m);
-
- #pragma warning disable CS8629
- int w = (int) (width is not null ? width : image.Width * height / image.Height);
- int h = (int) (height is not null ? height : image.Height * width / image.Width);
- #pragma warning restore CS8629
-
- var thumbPath = GetPath(m, w, h);
+ throw new ThumbnailException(
+ "Requested thumbnail size is larger than original media",
+ mediaId);
- if(!File.Exists(thumbPath)) {
- image.Resize((uint) w, (uint) h);
- image.Write(thumbPath);
- }
+ image.Thumbnail((uint) (width ?? -1), (uint) (height ?? -1));
+ image.Write(thumbPath, MagickFormat.Jpeg);
return System.IO.File.OpenRead(thumbPath);
}
@@ -297,29 +291,40 @@ public class MediaService : IMediaService {
public Stream GetThumbnail(Media media, int? width, int? height) =>
GetThumbnail(media.Guid, width, height);
- public string GetPath(Media media) {
+ public string GetPath(Guid mediaId) {
var fileInfo = new FileInfo(
Path.Join(
config.MediaBasePath,
- media.Guid.ToString().Substring(0, 2),
- media.Guid.ToString().Substring(2, 2),
- media.Guid.ToString()));
+ mediaId.ToString().Substring(0, 2),
+ mediaId.ToString().Substring(2, 2),
+ mediaId.ToString()));
- Directory.CreateDirectory(fileInfo.Directory.FullName);
+ Directory.CreateDirectory(fileInfo.Directory!.FullName);
return fileInfo.FullName;
}
- public string GetPath(Media media, int width, int height) {
+ public string GetPath(Guid mediaId, int? width, int? height) {
+ if(width is null && height is null)
+ throw new ThumbnailException(
+ "Both width and height cannot be null!",
+ mediaId);
+
var fileInfo = new FileInfo(Path.Join(
config.ThumbnailBasePath,
- media.Guid.ToString().Substring(0, 2),
- media.Guid.ToString().Substring(2, 2),
- $"{media.Guid.ToString()}-{width}-{height}"));
+ mediaId.ToString().Substring(0, 2),
+ mediaId.ToString().Substring(2, 2),
+ $"{mediaId.ToString()}-{(width ?? 0)}-{(height ?? 0)}"));
- Directory.CreateDirectory(fileInfo.Directory.FullName);
+ 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);
+
private int GetUploadedFileHash(UploadedFile uf) => (
uf.CreateTime,
uf.LastWriteTime,