diff --git a/cicm_web/Helpers/AddPhoto.cs b/cicm_web/Helpers/AddPhoto.cs index 9eeab3fd..ede5164e 100644 --- a/cicm_web/Helpers/AddPhoto.cs +++ b/cicm_web/Helpers/AddPhoto.cs @@ -6,6 +6,7 @@ using System.Text; using Cicm.Database; using Cicm.Database.Models; using SixLabors.ImageSharp; +using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.MetaData; using SixLabors.ImageSharp.MetaData.Profiles.Exif; using SixLabors.ImageSharp.PixelFormats; @@ -31,108 +32,109 @@ namespace cicm_web.Helpers 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: + if(image.MetaData.ExifProfile != null) + foreach(ExifValue exif in image.MetaData.ExifProfile.Values.ToList()) + switch(exif.Tag) { - Rational rat = (Rational)exif.Value; - photo.Exposure = rat.Denominator == 1 ? rat.Numerator.ToString() : rat.ToString(); + 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; + 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; } - 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"))) @@ -156,8 +158,8 @@ namespace cicm_web.Helpers 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)); + 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) @@ -173,9 +175,9 @@ namespace cicm_web.Helpers 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")); + $"{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}"); @@ -210,5 +212,132 @@ namespace cicm_web.Helpers return photo; } + + public static void ImportPhotos(cicmContext context) + { + if(!Directory.Exists("wwwroot/assets/photos/computers") && + !Directory.Exists("wwwroot/assets/photos/consoles")) return; + + if(!(context.Users.FirstOrDefault() is ApplicationUser user)) + { + Console.WriteLine("Cannot import photos without an existing uberadmin, please create it before continuing..."); + return; + } + + License license = context.Licenses.FirstOrDefault(l => l.Name == "Fair use"); + if(license is null) + { + Console.WriteLine("Cannot import photos without the \"Fair use\" license, please create it before continuing..."); + return; + } + + foreach(string computer in Directory.EnumerateDirectories("wwwroot/assets/photos/computers", "*", + SearchOption.TopDirectoryOnly)) + { + string computerIdStr = Path.GetFileName(computer); + + if(!int.TryParse(computerIdStr, out int computerId)) + { + Console.WriteLine("{0} does not indicate a correct computer ID", computerIdStr); + continue; + } + + Machine machine = context.Machines.FirstOrDefault(m => m.Id == computerId); + + if(machine is null) + { + Console.WriteLine("Cannot find machine with ID {0}.", computerId); + continue; + } + + foreach(string computerPhoto in Directory.EnumerateFiles(computer, "*", SearchOption.TopDirectoryOnly)) + { + Console.WriteLine("Importing {0}...", computerPhoto); + Guid newId = Guid.NewGuid(); + + IImageFormat imageinfo = Image.DetectFormat(computerPhoto); + + string extension; + switch(imageinfo?.Name) + { + case "JPEG": + extension = ".jpg"; + break; + case "PNG": + extension = ".png"; + break; + case "GIF": + extension = ".gif"; + break; + default: + Console.WriteLine("Unsupported file format for {0}", computerPhoto); + continue; + } + + MachinePhoto photo = Add(computerPhoto, newId, "wwwroot", ".", extension); + + photo.Id = newId; + photo.User = user; + photo.License = license; + photo.Machine = machine; + + context.Add(photo); + } + } + + foreach(string console in Directory.EnumerateDirectories("wwwroot/assets/photos/consoles", "*", + SearchOption.TopDirectoryOnly)) + { + string consoleIdStr = Path.GetFileName(console); + + if(!int.TryParse(consoleIdStr, out int consoleId)) + { + Console.WriteLine("{0} does not indicate a correct console ID", consoleIdStr); + continue; + } + + Machine machine = context.Machines.FirstOrDefault(m => m.Id == consoleId + 356); + + if(machine is null) + { + Console.WriteLine("Cannot find machine with ID {0}.", consoleId + 356); + continue; + } + + foreach(string consolePhoto in Directory.EnumerateFiles(console, "*", SearchOption.TopDirectoryOnly)) + { + Console.WriteLine("Importing {0}...", consolePhoto); + Guid newId = Guid.NewGuid(); + + IImageFormat imageinfo = Image.DetectFormat(consolePhoto); + + string extension; + switch(imageinfo?.Name) + { + case "JPEG": + extension = ".jpg"; + break; + case "PNG": + extension = ".png"; + break; + case "GIF": + extension = ".gif"; + break; + default: + Console.WriteLine("Unsupported file format for {0}", consolePhoto); + continue; + } + + MachinePhoto photo = Add(consolePhoto, newId, "wwwroot", ".", extension); + + photo.Id = newId; + photo.User = user; + photo.License = license; + photo.Machine = machine; + + context.Add(photo); + } + } + } } } \ No newline at end of file diff --git a/cicm_web/Program.cs b/cicm_web/Program.cs index 59c584f5..9423c791 100644 --- a/cicm_web/Program.cs +++ b/cicm_web/Program.cs @@ -164,6 +164,22 @@ namespace cicm_web context.SaveChanges(); + end = DateTime.Now; + Console.WriteLine("\u001b[31;1mTook \u001b[32;1m{0} seconds\u001b[31;1m...\u001b[0m", + (end - start).TotalSeconds); + + start = DateTime.Now; + Console.WriteLine("\u001b[31;1mImporting photos...\u001b[0m"); + + try { Photos.ImportPhotos(context); } + catch(Exception e) + { + Console.WriteLine("Exception {0} importing photos, saving changes and continuing...", e); + throw; + } + + context.SaveChanges(); + end = DateTime.Now; Console.WriteLine("\u001b[31;1mTook \u001b[32;1m{0} seconds\u001b[31;1m...\u001b[0m", (end - start).TotalSeconds); diff --git a/cicm_web/cicm_web.csproj b/cicm_web/cicm_web.csproj index 9bc7c462..4bac9dc5 100644 --- a/cicm_web/cicm_web.csproj +++ b/cicm_web/cicm_web.csproj @@ -2,7 +2,7 @@ netcoreapp2.2 - 3.0.99.695 + 3.0.99.704 Canary Islands Computer Museum Copyright © 2003-2018 Natalia Portillo Canary Islands Computer Museum Website diff --git a/cicm_web/wwwroot/assets/photos/computers/.donotremove b/cicm_web/wwwroot/assets/photos/computers/.donotremove deleted file mode 100644 index e69de29b..00000000 diff --git a/cicm_web/wwwroot/assets/photos/consoles/.donotremove b/cicm_web/wwwroot/assets/photos/consoles/.donotremove deleted file mode 100644 index e69de29b..00000000