diff --git a/cicm_web/Areas/Admin/Controllers/MachinePhotosController.cs b/cicm_web/Areas/Admin/Controllers/MachinePhotosController.cs index 0d0cbd24..b5a44414 100644 --- a/cicm_web/Areas/Admin/Controllers/MachinePhotosController.cs +++ b/cicm_web/Areas/Admin/Controllers/MachinePhotosController.cs @@ -1,12 +1,10 @@ using System; -using System.Globalization; using System.IO; using System.Linq; -using System.Text; using System.Threading.Tasks; -using Cicm.Database; using Cicm.Database.Models; using cicm_web.Areas.Admin.Models; +using cicm_web.Helpers; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Identity; @@ -15,11 +13,6 @@ using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Formats; -using SixLabors.ImageSharp.MetaData; -using SixLabors.ImageSharp.MetaData.Profiles.Exif; -using SixLabors.ImageSharp.PixelFormats; -using SixLabors.ImageSharp.Primitives; -using SkiaSharp; namespace cicm_web.Areas.Admin.Controllers { @@ -165,202 +158,8 @@ namespace cicm_web.Areas.Admin.Controllers return View(machinePhoto); } - Image image = Image.Load(tmpFile); - - MachinePhoto photo = new MachinePhoto(); - - foreach(ImageProperty prop in image.MetaData.Properties) - switch(prop.Name) - { - case "aux:Lens": - photo.Lens = prop.Value; - break; - } - - foreach(ExifValue exif in image.MetaData.ExifProfile.Values.ToList()) - switch(exif.Tag) - { - case ExifTag.Artist: - photo.Author = exif.Value as string; - break; - case ExifTag.Make: - photo.CameraManufacturer = exif.Value as string; - break; - case ExifTag.ColorSpace: - photo.ColorSpace = (ColorSpace)exif.Value; - break; - case ExifTag.UserComment: - photo.Comments = Encoding.ASCII.GetString(exif.Value as byte[]); - break; - case ExifTag.Contrast: - photo.Contrast = (Contrast)exif.Value; - break; - case ExifTag.DateTimeDigitized: - photo.CreationDate = - DateTime.ParseExact(exif.Value.ToString(), "yyyy:MM:dd HH:mm:ss", - CultureInfo.InvariantCulture); - break; - case ExifTag.DigitalZoomRatio: - photo.DigitalZoomRatio = ((Rational)exif.Value).ToDouble(); - break; - case ExifTag.ExifVersion: - photo.ExifVersion = Encoding.ASCII.GetString(exif.Value as byte[]); - break; - case ExifTag.ExposureTime: - { - Rational rat = (Rational)exif.Value; - photo.Exposure = rat.Denominator == 1 ? rat.Numerator.ToString() : rat.ToString(); - - break; - } - - case ExifTag.ExposureMode: - photo.ExposureMethod = (ExposureMode)exif.Value; - break; - case ExifTag.ExposureProgram: - photo.ExposureProgram = (ExposureProgram)exif.Value; - break; - case ExifTag.Flash: - photo.Flash = (Flash)exif.Value; - break; - case ExifTag.FNumber: - photo.Focal = ((Rational)exif.Value).ToDouble(); - break; - case ExifTag.FocalLength: - photo.FocalLength = ((Rational)exif.Value).ToDouble(); - break; - case ExifTag.FocalLengthIn35mmFilm: - photo.FocalLengthEquivalent = exif.Value as ushort?; - break; - case ExifTag.XResolution: - photo.HorizontalResolution = ((Rational)exif.Value).ToDouble(); - break; - case ExifTag.ISOSpeedRatings: - photo.IsoRating = (ushort)exif.Value; - break; - case ExifTag.LensModel: - photo.Lens = exif.Value as string; - break; - case ExifTag.LightSource: - photo.LightSource = (LightSource)exif.Value; - break; - case ExifTag.MeteringMode: - photo.MeteringMode = (MeteringMode)exif.Value; - break; - case ExifTag.ResolutionUnit: - photo.ResolutionUnit = (ResolutionUnit)exif.Value; - break; - case ExifTag.Orientation: - photo.Orientation = (Orientation)exif.Value; - break; - case ExifTag.Saturation: - photo.Saturation = (Saturation)exif.Value; - break; - case ExifTag.SceneCaptureType: - photo.SceneCaptureType = (SceneCaptureType)exif.Value; - break; - case ExifTag.SensingMethod: - photo.SensingMethod = (SensingMethod)exif.Value; - break; - case ExifTag.Software: - photo.SoftwareUsed = exif.Value as string; - break; - case ExifTag.SubjectDistanceRange: - photo.SubjectDistanceRange = (SubjectDistanceRange)exif.Value; - break; - case ExifTag.YResolution: - photo.VerticalResolution = ((Rational)exif.Value).ToDouble(); - break; - case ExifTag.WhiteBalance: - photo.WhiteBalance = (WhiteBalance)exif.Value; - break; - default: - image.MetaData.ExifProfile.RemoveValue(exif.Tag); - break; - } - - if(!Directory.Exists(Path.Combine(hostingEnvironment.WebRootPath, "assets", "photos"))) - Directory.CreateDirectory(Path.Combine(hostingEnvironment.WebRootPath, "assets", "photos")); - if(!Directory.Exists(Path.Combine(hostingEnvironment.WebRootPath, "assets", "photos", "machines"))) - Directory.CreateDirectory(Path.Combine(hostingEnvironment.WebRootPath, "assets", "photos", "machines")); - if(!Directory.Exists(Path.Combine(hostingEnvironment.WebRootPath, "assets", "photos", "machines", "thumbs")) - ) - Directory.CreateDirectory(Path.Combine(hostingEnvironment.WebRootPath, "assets", "photos", "machines", - "thumbs")); - - string outJpeg = Path.Combine(hostingEnvironment.WebRootPath, "assets", "photos", "machines", - newId + ".jpg"); - - using(FileStream jpegStream = - new FileStream(outJpeg, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None)) - image.SaveAsJpeg(jpegStream); - - int imgMax = Math.Max(image.Width, image.Height); - int width = image.Width; - int height = image.Height; - - image.Dispose(); - - SKBitmap skBitmap = SKBitmap.Decode(outJpeg); - - foreach(string format in new[] {"jpg", "webp"}) - { - if(!Directory.Exists(Path.Combine(hostingEnvironment.WebRootPath, "assets/photos/machines/thumbs", - format))) ; - Directory.CreateDirectory(Path.Combine(hostingEnvironment.WebRootPath, "assets/photos/machines/thumbs", - format)); - - SKEncodedImageFormat skFormat; - switch(format) - { - case "webp": - skFormat = SKEncodedImageFormat.Webp; - break; - default: - skFormat = SKEncodedImageFormat.Jpeg; - break; - } - - foreach(int multiplier in new[] {1, 2, 3}) - { - if(!Directory.Exists(Path.Combine(hostingEnvironment.WebRootPath, "assets/photos/machines/thumbs", - format, $"{multiplier}x"))) ; - Directory.CreateDirectory(Path.Combine(hostingEnvironment.WebRootPath, - "assets/photos/machines/thumbs", format, $"{multiplier}x")); - - string resized = Path.Combine(hostingEnvironment.WebRootPath, "assets/photos/machines/thumbs", - format, $"{multiplier}x", newId + $".{format}"); - - if(System.IO.File.Exists(resized)) continue; - - float canvasMin = 256 * multiplier; - - float scale = canvasMin / imgMax; - - // Do not enlarge images - if(scale > 1) scale = 1; - - SKBitmap skResized = skBitmap.Resize(new SKImageInfo((int)(width * scale), (int)(height * scale)), - SKFilterQuality.High); - SKImage skImage = SKImage.FromBitmap(skResized); - SKData data = skImage.Encode(skFormat, 100); - FileStream outfs = new FileStream(resized, FileMode.CreateNew); - data.SaveTo(outfs); - outfs.Close(); - } - } - - if(!Directory.Exists(Path.Combine(hostingEnvironment.ContentRootPath, "originals", "photos"))) - Directory.CreateDirectory(Path.Combine(hostingEnvironment.ContentRootPath, "originals", "photos")); - if(!Directory.Exists(Path.Combine(hostingEnvironment.ContentRootPath, "originals", "photos"))) - Directory.CreateDirectory(Path.Combine(hostingEnvironment.ContentRootPath, "originals", "photos")); - if(!Directory.Exists(Path.Combine(hostingEnvironment.ContentRootPath, "originals", "photos", "machines"))) - Directory.CreateDirectory(Path.Combine(hostingEnvironment.ContentRootPath, "originals", "photos", - "machines")); - - System.IO.File.Move(tmpFile, - Path.Combine(hostingEnvironment.ContentRootPath, "originals", "photos", "machines", - newId + extension)); + MachinePhoto photo = Photos.Add(tmpFile, newId, hostingEnvironment.WebRootPath, + hostingEnvironment.ContentRootPath, extension); photo.Id = newId; photo.User = await userManager.GetUserAsync(HttpContext.User); diff --git a/cicm_web/Helpers/AddPhoto.cs b/cicm_web/Helpers/AddPhoto.cs new file mode 100644 index 00000000..9eeab3fd --- /dev/null +++ b/cicm_web/Helpers/AddPhoto.cs @@ -0,0 +1,214 @@ +using System; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Text; +using Cicm.Database; +using Cicm.Database.Models; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.MetaData; +using SixLabors.ImageSharp.MetaData.Profiles.Exif; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Primitives; +using SkiaSharp; + +namespace cicm_web.Helpers +{ + public class Photos + { + public static MachinePhoto Add(string tmpFile, Guid newId, string webRootPath, string contentRootPath, + string extension) + { + Image image = Image.Load(tmpFile); + + MachinePhoto photo = new MachinePhoto(); + + foreach(ImageProperty prop in image.MetaData.Properties) + switch(prop.Name) + { + case "aux:Lens": + photo.Lens = prop.Value; + break; + } + + foreach(ExifValue exif in image.MetaData.ExifProfile.Values.ToList()) + switch(exif.Tag) + { + case ExifTag.Artist: + photo.Author = exif.Value as string; + break; + case ExifTag.Make: + photo.CameraManufacturer = exif.Value as string; + break; + case ExifTag.ColorSpace: + photo.ColorSpace = (ColorSpace)exif.Value; + break; + case ExifTag.UserComment: + photo.Comments = Encoding.ASCII.GetString(exif.Value as byte[]); + break; + case ExifTag.Contrast: + photo.Contrast = (Contrast)exif.Value; + break; + case ExifTag.DateTimeDigitized: + photo.CreationDate = + DateTime.ParseExact(exif.Value.ToString(), "yyyy:MM:dd HH:mm:ss", + CultureInfo.InvariantCulture); + break; + case ExifTag.DigitalZoomRatio: + photo.DigitalZoomRatio = ((Rational)exif.Value).ToDouble(); + break; + case ExifTag.ExifVersion: + photo.ExifVersion = Encoding.ASCII.GetString(exif.Value as byte[]); + break; + case ExifTag.ExposureTime: + { + Rational rat = (Rational)exif.Value; + photo.Exposure = rat.Denominator == 1 ? rat.Numerator.ToString() : rat.ToString(); + + break; + } + + case ExifTag.ExposureMode: + photo.ExposureMethod = (ExposureMode)exif.Value; + break; + case ExifTag.ExposureProgram: + photo.ExposureProgram = (ExposureProgram)exif.Value; + break; + case ExifTag.Flash: + photo.Flash = (Flash)exif.Value; + break; + case ExifTag.FNumber: + photo.Focal = ((Rational)exif.Value).ToDouble(); + break; + case ExifTag.FocalLength: + photo.FocalLength = ((Rational)exif.Value).ToDouble(); + break; + case ExifTag.FocalLengthIn35mmFilm: + photo.FocalLengthEquivalent = exif.Value as ushort?; + break; + case ExifTag.XResolution: + photo.HorizontalResolution = ((Rational)exif.Value).ToDouble(); + break; + case ExifTag.ISOSpeedRatings: + photo.IsoRating = (ushort)exif.Value; + break; + case ExifTag.LensModel: + photo.Lens = exif.Value as string; + break; + case ExifTag.LightSource: + photo.LightSource = (LightSource)exif.Value; + break; + case ExifTag.MeteringMode: + photo.MeteringMode = (MeteringMode)exif.Value; + break; + case ExifTag.ResolutionUnit: + photo.ResolutionUnit = (ResolutionUnit)exif.Value; + break; + case ExifTag.Orientation: + photo.Orientation = (Orientation)exif.Value; + break; + case ExifTag.Saturation: + photo.Saturation = (Saturation)exif.Value; + break; + case ExifTag.SceneCaptureType: + photo.SceneCaptureType = (SceneCaptureType)exif.Value; + break; + case ExifTag.SensingMethod: + photo.SensingMethod = (SensingMethod)exif.Value; + break; + case ExifTag.Software: + photo.SoftwareUsed = exif.Value as string; + break; + case ExifTag.SubjectDistanceRange: + photo.SubjectDistanceRange = (SubjectDistanceRange)exif.Value; + break; + case ExifTag.YResolution: + photo.VerticalResolution = ((Rational)exif.Value).ToDouble(); + break; + case ExifTag.WhiteBalance: + photo.WhiteBalance = (WhiteBalance)exif.Value; + break; + default: + image.MetaData.ExifProfile.RemoveValue(exif.Tag); + break; + } + + if(!Directory.Exists(Path.Combine(webRootPath, "assets", "photos"))) + Directory.CreateDirectory(Path.Combine(webRootPath, "assets", "photos")); + if(!Directory.Exists(Path.Combine(webRootPath, "assets", "photos", "machines"))) + Directory.CreateDirectory(Path.Combine(webRootPath, "assets", "photos", "machines")); + if(!Directory.Exists(Path.Combine(webRootPath, "assets", "photos", "machines", "thumbs"))) + Directory.CreateDirectory(Path.Combine(webRootPath, "assets", "photos", "machines", "thumbs")); + + string outJpeg = Path.Combine(webRootPath, "assets", "photos", "machines", newId + ".jpg"); + + using(FileStream jpegStream = + new FileStream(outJpeg, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None)) + image.SaveAsJpeg(jpegStream); + + int imgMax = Math.Max(image.Width, image.Height); + int width = image.Width; + int height = image.Height; + + image.Dispose(); + + SKBitmap skBitmap = SKBitmap.Decode(outJpeg); + + foreach(string format in new[] {"jpg", "webp"}) + { + if(!Directory.Exists(Path.Combine(webRootPath, "assets/photos/machines/thumbs", format))) ; + Directory.CreateDirectory(Path.Combine(webRootPath, "assets/photos/machines/thumbs", format)); + + SKEncodedImageFormat skFormat; + switch(format) + { + case "webp": + skFormat = SKEncodedImageFormat.Webp; + break; + default: + skFormat = SKEncodedImageFormat.Jpeg; + break; + } + + foreach(int multiplier in new[] {1, 2, 3}) + { + if(!Directory.Exists(Path.Combine(webRootPath, "assets/photos/machines/thumbs", format, + $"{multiplier}x"))) ; + Directory.CreateDirectory(Path.Combine(webRootPath, "assets/photos/machines/thumbs", format, + $"{multiplier}x")); + + string resized = Path.Combine(webRootPath, "assets/photos/machines/thumbs", format, + $"{multiplier}x", newId + $".{format}"); + + if(File.Exists(resized)) continue; + + float canvasMin = 256 * multiplier; + + float scale = canvasMin / imgMax; + + // Do not enlarge images + if(scale > 1) scale = 1; + + SKBitmap skResized = skBitmap.Resize(new SKImageInfo((int)(width * scale), (int)(height * scale)), + SKFilterQuality.High); + SKImage skImage = SKImage.FromBitmap(skResized); + SKData data = skImage.Encode(skFormat, 100); + FileStream outfs = new FileStream(resized, FileMode.CreateNew); + data.SaveTo(outfs); + outfs.Close(); + } + } + + if(!Directory.Exists(Path.Combine(contentRootPath, "originals", "photos"))) + Directory.CreateDirectory(Path.Combine(contentRootPath, "originals", "photos")); + if(!Directory.Exists(Path.Combine(contentRootPath, "originals", "photos"))) + Directory.CreateDirectory(Path.Combine(contentRootPath, "originals", "photos")); + if(!Directory.Exists(Path.Combine(contentRootPath, "originals", "photos", "machines"))) + Directory.CreateDirectory(Path.Combine(contentRootPath, "originals", "photos", "machines")); + + File.Move(tmpFile, Path.Combine(contentRootPath, "originals", "photos", "machines", newId + extension)); + + return photo; + } + } +} \ No newline at end of file diff --git a/cicm_web/SvgRender.cs b/cicm_web/Helpers/SvgRender.cs similarity index 99% rename from cicm_web/SvgRender.cs rename to cicm_web/Helpers/SvgRender.cs index ad659bc6..b2466f5b 100644 --- a/cicm_web/SvgRender.cs +++ b/cicm_web/Helpers/SvgRender.cs @@ -34,7 +34,7 @@ using Cicm.Database.Models; using SkiaSharp; using SKSvg = SkiaSharp.Extended.Svg.SKSvg; -namespace cicm_web +namespace cicm_web.Helpers { public static class SvgRender { diff --git a/cicm_web/Program.cs b/cicm_web/Program.cs index de0c33db..59c584f5 100644 --- a/cicm_web/Program.cs +++ b/cicm_web/Program.cs @@ -32,6 +32,7 @@ using System; using System.Linq; using Cicm.Database; using Cicm.Database.Models; +using cicm_web.Helpers; using DiscImageChef.Interop; using Markdig; using Microsoft.AspNetCore; diff --git a/cicm_web/cicm_web.csproj b/cicm_web/cicm_web.csproj index baa4b566..9bc7c462 100644 --- a/cicm_web/cicm_web.csproj +++ b/cicm_web/cicm_web.csproj @@ -2,7 +2,7 @@ netcoreapp2.2 - 3.0.99.688 + 3.0.99.695 Canary Islands Computer Museum Copyright © 2003-2018 Natalia Portillo Canary Islands Computer Museum Website