diff --git a/MyWebsite/MyWebsite/Areas/Admin/Controllers/GalleryController.cs b/MyWebsite/MyWebsite/Areas/Admin/Controllers/GalleryController.cs index d85faf2..1140767 100644 --- a/MyWebsite/MyWebsite/Areas/Admin/Controllers/GalleryController.cs +++ b/MyWebsite/MyWebsite/Areas/Admin/Controllers/GalleryController.cs @@ -6,6 +6,8 @@ using MyWebsite.Models; using MyWebsite.Models.Databases; using System; using System.IO; +using System.Drawing; +using System.Threading; namespace MyWebsite.Areas.Admin.Controllers { @@ -50,6 +52,8 @@ namespace MyWebsite.Areas.Admin.Controllers System.IO.File.SetAttributes(Directory.GetCurrentDirectory() + "/wwwroot/images/Gallery/" + model?.FileName, FileAttributes.Normal); System.IO.File.Delete(Directory.GetCurrentDirectory() + "/wwwroot/images/Gallery/" + model?.FileName); + UpdateCache(); + return RedirectToAction("Index"); } @@ -79,6 +83,39 @@ namespace MyWebsite.Areas.Admin.Controllers Database.Gallery.Add(model); Database.SaveChanges(); + UpdateCache(); + + return RedirectToAction("Index"); + } + + [HttpGet] + public IActionResult UpdateCache() + { + if (Directory.Exists(Directory.GetCurrentDirectory() + "/wwwroot/images/Gallery/Cache")) + Directory.Delete(Directory.GetCurrentDirectory() + "/wwwroot/images/Gallery/Cache", true); + + Thread.Sleep(10); + DirectoryInfo cache = Directory.CreateDirectory(Directory.GetCurrentDirectory() + "/wwwroot/images/Gallery/Cache"); + + foreach (string file in Directory.GetFiles(Directory.GetCurrentDirectory() + "/wwwroot/images/Gallery")) + { + if (file.EndsWith(".svg", StringComparison.OrdinalIgnoreCase)) + continue; + + FileInfo info = new FileInfo(file); + + using Bitmap original = new Bitmap(file); + using Bitmap resized = new Bitmap(600, (int)(600d / original.Width * original.Height)); + using Graphics grapics = Graphics.FromImage(resized); + + grapics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed; + grapics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy; + grapics.DrawImage(original, 0, 0, resized.Width, resized.Height); + + using Stream writeStream = System.IO.File.Open(cache.FullName + "\\" + info.Name + ".jpg", FileMode.Create); + resized.Save(writeStream, System.Drawing.Imaging.ImageFormat.Jpeg); + } + return RedirectToAction("Index"); } } diff --git a/MyWebsite/MyWebsite/Areas/Admin/Views/Gallery/Index.cshtml b/MyWebsite/MyWebsite/Areas/Admin/Views/Gallery/Index.cshtml index 4b945e0..8266278 100644 --- a/MyWebsite/MyWebsite/Areas/Admin/Views/Gallery/Index.cshtml +++ b/MyWebsite/MyWebsite/Areas/Admin/Views/Gallery/Index.cshtml @@ -6,7 +6,8 @@
Back to main menu

Gallery

- //  Upload new + //  Upload new
+ // Cache gallery
@@ -16,7 +17,7 @@ - + diff --git a/MyWebsite/MyWebsite/Views/Gallery/Index.cshtml b/MyWebsite/MyWebsite/Views/Gallery/Index.cshtml index 0d913c2..808558a 100644 --- a/MyWebsite/MyWebsite/Views/Gallery/Index.cshtml +++ b/MyWebsite/MyWebsite/Views/Gallery/Index.cshtml @@ -11,7 +11,7 @@ @if (Model.Images.Count > 0) foreach (ImageModel image in Model.Images) { - + } else {