From a1ed75a5117cb7133a1b16843748aea30e58f183 Mon Sep 17 00:00:00 2001 From: Jonasbg <1508560+jonasbg@users.noreply.github.com> Date: Sat, 6 Jan 2024 16:01:36 +0100 Subject: [PATCH 1/8] fix(post): removed extension toggling. --- Controllers/PostController.cs | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/Controllers/PostController.cs b/Controllers/PostController.cs index 7af154c..496ae63 100755 --- a/Controllers/PostController.cs +++ b/Controllers/PostController.cs @@ -36,8 +36,8 @@ public async Task Index(Payload payload) { if (Config.Password != null && !User.Identity.IsAuthenticated) { - _logger.LogWarning("Unauthenticated request with Config.Password set."); - return NotFound(); + _logger.LogWarning("Unauthenticated request with Config.Password set for image not as Cover"); + return NotAuthenticated(); } } @@ -67,18 +67,8 @@ private async Task Synology(string path) { } } - if (!System.IO.File.Exists(path)){ - if(path.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) || path.EndsWith(".JPG", StringComparison.OrdinalIgnoreCase)){ - path = ToggleCaseExtension(path); - if (!System.IO.File.Exists(path)){ - _logger.LogWarning("File does not exist after toggling case of extension: {0}", path); - return NotFound(); - } - } else { - _logger.LogWarning("File does not exist after toggling case of extension: {0}", path); - return NotFound(); - } - } + if (!System.IO.File.Exists(path)) + return NotFound(); HttpContext.Response.Headers.Add("ETag", ComputeMD5(path)); HttpContext.Response.Headers.Add("Cache-Control", "private, max-age=12000"); @@ -86,14 +76,6 @@ private async Task Synology(string path) { return new EmptyResult(); } - private string ToggleCaseExtension(string path) - { - string ext = System.IO.Path.GetExtension(path); - string oppositeCaseExt = ext.Equals(ext.ToLower()) ? ext.ToUpper() : ext.ToLower(); - _logger.LogDebug("Toggled case of extension. New path: {0}\nOld path: {1}", oppositeCaseExt, path); - return System.IO.Path.ChangeExtension(path, oppositeCaseExt); - } - private string ComputeMD5(string s) { using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create()) From e1becde2f132c4506860cd969f946d59a035ff01 Mon Sep 17 00:00:00 2001 From: Jonasbg <1508560+jonasbg@users.noreply.github.com> Date: Sat, 6 Jan 2024 16:31:04 +0100 Subject: [PATCH 2/8] Wrong usage --- Controllers/PostController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Controllers/PostController.cs b/Controllers/PostController.cs index 496ae63..703837d 100755 --- a/Controllers/PostController.cs +++ b/Controllers/PostController.cs @@ -37,7 +37,7 @@ public async Task Index(Payload payload) if (Config.Password != null && !User.Identity.IsAuthenticated) { _logger.LogWarning("Unauthenticated request with Config.Password set for image not as Cover"); - return NotAuthenticated(); + return Unauthorized(); } } From b59e131749b104ce01e1b781c29ead007f16844b Mon Sep 17 00:00:00 2001 From: Jonasbg <1508560+jonasbg@users.noreply.github.com> Date: Sun, 7 Jan 2024 10:34:58 +0100 Subject: [PATCH 3/8] bug(post): rewrite image fetch logic --- Controllers/PostController.cs | 80 +++++++++++++++++------------------ 1 file changed, 38 insertions(+), 42 deletions(-) diff --git a/Controllers/PostController.cs b/Controllers/PostController.cs index 703837d..20c0c67 100755 --- a/Controllers/PostController.cs +++ b/Controllers/PostController.cs @@ -52,7 +52,6 @@ public async Task Index(Payload payload) } } - private async Task Synology(string path) { if (Config.Synology) { @@ -65,6 +64,8 @@ private async Task Synology(string path) { path = synologyPath; _logger.LogDebug("Synology file exists. Updated path to: {0}", path); } + } else { + path = await ResizeIfNeeded(path); } if (!System.IO.File.Exists(path)) @@ -72,7 +73,7 @@ private async Task Synology(string path) { HttpContext.Response.Headers.Add("ETag", ComputeMD5(path)); HttpContext.Response.Headers.Add("Cache-Control", "private, max-age=12000"); - await HttpContext.Response.Body.WriteAsync(await resize(path)); + await HttpContext.Response.Body.WriteAsync(System.IO.File.ReadAllBytes(path)); return new EmptyResult(); } @@ -85,48 +86,43 @@ private string ComputeMD5(string s) } } - private async Task resize(string path) { - _logger.LogDebug("Resize method started for path: {0}", path); - try{ - var fileName = $"{Config.ConfigDir}/images{path}"; - if (System.IO.File.Exists(fileName)) { - using (var SourceStream = System.IO.File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - { - var result = new byte[SourceStream.Length]; - await SourceStream.ReadAsync(result, 0, (int)SourceStream.Length); - return result; - } + private async Task ResizeIfNeeded(string path) { + _logger.LogDebug("Checking if resize is needed for path: {0}", path); + var resizedPath = $"{Config.ConfigDir}/resized{path}"; + + if (System.IO.File.Exists(resizedPath)) + return resizedPath; + + try { + using (var image = await Image.LoadAsync(path)) { + int width, height; + + if (image.Height > image.Width) { + // Portrait or square + height = Math.Min(image.Height, Config.ImageMaxSize); + width = (image.Width * height) / image.Height; + } else { + // Landscape + width = Math.Min(image.Width, Config.ImageMaxSize); + height = (image.Height * width) / image.Width; + } + + if (image.Width != width || image.Height != height) { + image.Mutate(x => x.Resize(width, height)); + await SaveResizedImage(image, resizedPath); + } + } + } catch (Exception e) { + _logger.LogError(e, "Error resizing file: {0}", path); } - using (var outputStream = new MemoryStream()) - { - using (var image = await Image.LoadAsync(path)) - { - int width = image.Width / 2; - int height = image.Height / 2; - width = 0; - height = 0; - if(image.Height > image.Width && height > Config.ImageMaxSize) - height = Config.ImageMaxSize; - if (image.Width > image.Height && width > Config.ImageMaxSize) - width = Config.ImageMaxSize; - - if (width + height != 0) - image.Mutate(x => x.Resize(width, height)); - JpegEncoder encoder = new JpegEncoder(); - encoder.Quality = Config.ImageQuality; - await image.SaveAsJpegAsync(outputStream, encoder); - } - outputStream.Position = 0; - Directory.CreateDirectory(Path.GetDirectoryName(fileName)); - using var destination = System.IO.File.Create(fileName, bufferSize: 4096); - await outputStream.CopyToAsync(destination); + return resizedPath; + } - return outputStream.ToArray(); - } - } catch(Exception e){ - _logger.LogError(e, "Error Reading File: {0}", path); - throw; - } + private async Task SaveResizedImage(Image image, string path) { + JpegEncoder encoder = new JpegEncoder { Quality = Config.ImageQuality }; + Directory.CreateDirectory(Path.GetDirectoryName(path)); + await image.SaveAsync(path, encoder); } + } From 096f650b0a896b9376a0ceb53246f240c887e7f1 Mon Sep 17 00:00:00 2001 From: Jonasbg <1508560+jonasbg@users.noreply.github.com> Date: Sun, 7 Jan 2024 11:11:30 +0100 Subject: [PATCH 4/8] bug(post): rewrite image fetch logic --- Controllers/PostController.cs | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/Controllers/PostController.cs b/Controllers/PostController.cs index 20c0c67..ad5b5df 100755 --- a/Controllers/PostController.cs +++ b/Controllers/PostController.cs @@ -52,30 +52,31 @@ public async Task Index(Payload payload) } } - private async Task Synology(string path) { - if (Config.Synology) - { - var synologyFile = Path.GetFileName(path); - var directory = Path.GetDirectoryName(path); - var synologyPath = $"@eaDir/{synologyFile}/{Config.SynologySize()}"; - synologyPath = $"{directory}/{synologyPath}"; - - if (System.IO.File.Exists(synologyPath)) { - path = synologyPath; - _logger.LogDebug("Synology file exists. Updated path to: {0}", path); - } - } else { - path = await ResizeIfNeeded(path); +private async Task Synology(string path) { + if (Config.Synology) { + var synologyFile = Path.GetFileName(path); + var directory = Path.GetDirectoryName(path); + var synologyPath = $"{directory}/@eaDir/{synologyFile}/{Config.SynologySize()}"; + + if (System.IO.File.Exists(synologyPath)) { + path = synologyPath; + _logger.LogDebug("Synology file exists. Updated path to: {0}", path); + } } - if (!System.IO.File.Exists(path)) - return NotFound(); + if (!System.IO.File.Exists(path))) { + _logger.LogWarning("File does not exist at path: {0}", path); + return NotFound(); + } + + path = await ResizeIfNeeded(path); HttpContext.Response.Headers.Add("ETag", ComputeMD5(path)); HttpContext.Response.Headers.Add("Cache-Control", "private, max-age=12000"); await HttpContext.Response.Body.WriteAsync(System.IO.File.ReadAllBytes(path)); return new EmptyResult(); - } +} + private string ComputeMD5(string s) { From 925a2363454e1cd1904a0ab9cd5b431de9d04ea3 Mon Sep 17 00:00:00 2001 From: Jonasbg <1508560+jonasbg@users.noreply.github.com> Date: Sun, 7 Jan 2024 11:25:22 +0100 Subject: [PATCH 5/8] bug(post): rewrite image fetch logic --- Controllers/PostController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Controllers/PostController.cs b/Controllers/PostController.cs index ad5b5df..32f071e 100755 --- a/Controllers/PostController.cs +++ b/Controllers/PostController.cs @@ -64,7 +64,7 @@ private async Task Synology(string path) { } } - if (!System.IO.File.Exists(path))) { + if (!System.IO.File.Exists(path)) { _logger.LogWarning("File does not exist at path: {0}", path); return NotFound(); } From c09784b960588e93f26b8d04a2746eef86de5f23 Mon Sep 17 00:00:00 2001 From: Jonasbg <1508560+jonasbg@users.noreply.github.com> Date: Sun, 7 Jan 2024 12:04:34 +0100 Subject: [PATCH 6/8] bug(post): rewrite image fetch logic --- Controllers/PostController.cs | 46 +++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/Controllers/PostController.cs b/Controllers/PostController.cs index 32f071e..c6e4115 100755 --- a/Controllers/PostController.cs +++ b/Controllers/PostController.cs @@ -52,31 +52,35 @@ public async Task Index(Payload payload) } } -private async Task Synology(string path) { - if (Config.Synology) { - var synologyFile = Path.GetFileName(path); - var directory = Path.GetDirectoryName(path); - var synologyPath = $"{directory}/@eaDir/{synologyFile}/{Config.SynologySize()}"; - - if (System.IO.File.Exists(synologyPath)) { - path = synologyPath; - _logger.LogDebug("Synology file exists. Updated path to: {0}", path); - } - } + private async Task Synology(string path) { + if (Config.Synology) { + var synologyFile = Path.GetFileName(path); + var directory = Path.GetDirectoryName(path); + var synologyPath = $"{directory}/@eaDir/{synologyFile}/{Config.SynologySize()}"; + + if (System.IO.File.Exists(synologyPath)) { + path = synologyPath; + _logger.LogDebug("Synology file exists. Updated path to: {0}", path); + return await ReturnFileResponse(path); + } + } - if (!System.IO.File.Exists(path)) { - _logger.LogWarning("File does not exist at path: {0}", path); - return NotFound(); - } + if (!System.IO.File.Exists(path)) { + _logger.LogWarning("File does not exist at path: {0}", path); + return NotFound(); + } - path = await ResizeIfNeeded(path); + path = await ResizeIfNeeded(path); + return await ReturnFileResponse(path); + } - HttpContext.Response.Headers.Add("ETag", ComputeMD5(path)); - HttpContext.Response.Headers.Add("Cache-Control", "private, max-age=12000"); - await HttpContext.Response.Body.WriteAsync(System.IO.File.ReadAllBytes(path)); - return new EmptyResult(); -} + private async Task ReturnFileResponse(string filePath) { + HttpContext.Response.Headers.Add("ETag", ComputeMD5(filePath)); + HttpContext.Response.Headers.Add("Cache-Control", "private, max-age=12000"); + await HttpContext.Response.Body.WriteAsync(System.IO.File.ReadAllBytes(filePath)); + return new EmptyResult(); + } private string ComputeMD5(string s) { From b908f7db744577ad16eb363d4c677fab8dbd29d2 Mon Sep 17 00:00:00 2001 From: Jonasbg <1508560+jonasbg@users.noreply.github.com> Date: Sun, 9 Jun 2024 19:29:55 +0200 Subject: [PATCH 7/8] possible loginfix error --- Controllers/HomeController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs index cb8e417..93c5532 100755 --- a/Controllers/HomeController.cs +++ b/Controllers/HomeController.cs @@ -52,7 +52,7 @@ await HttpContext.SignInAsync( if (!String.IsNullOrEmpty(model.ReturnURL) && Url.IsLocalUrl(model.ReturnURL) && IsValidReturnUrl(model.ReturnURL)) return Redirect(model.ReturnURL); - return RedirectToAction("/"); + return Redirect("/"); } [Route("")] From a1ae8e27cbec20e5fd13e7fa63168b69a8912836 Mon Sep 17 00:00:00 2001 From: Jonasbg <1508560+jonasbg@users.noreply.github.com> Date: Sun, 9 Jun 2024 20:26:12 +0200 Subject: [PATCH 8/8] Spring cleaning --- CHANGELOG.md | 11 ++++++++++- LICENSE | 2 +- Views/Shared/_Layout.cshtml | 2 +- picoblog.csproj | 6 +++--- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e409863..efdbe64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.0.8.1] - 2024-05-07 +### Cleanup + +- Spring cleanup of the project. + +### Fixed + +- Redirect after login did not work correctly. + ## [0.0.8] - 2023-08-05 ### Added @@ -12,7 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Implemented global usings through `GlobalUsings.cs` for cleaner code (if applicable to your .NET version). - Parallel processing of markdown files during search - Log failed login attempts - + ### Changed - Updated the README to reflect new environment variables and features. diff --git a/LICENSE b/LICENSE index be12557..c849f90 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2023 Jonasbg +Copyright (c) 2024 Jonasbg Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/Views/Shared/_Layout.cshtml b/Views/Shared/_Layout.cshtml index ec86a38..69ec744 100755 --- a/Views/Shared/_Layout.cshtml +++ b/Views/Shared/_Layout.cshtml @@ -70,7 +70,7 @@ }
- © 2023 - picoblog - + © 2024 - picoblog -
diff --git a/picoblog.csproj b/picoblog.csproj index 7d7d531..b4e5dd9 100644 --- a/picoblog.csproj +++ b/picoblog.csproj @@ -16,9 +16,9 @@ - - - + + +