mirror of
https://github.com/claunia/marechai.git
synced 2025-12-16 19:14:25 +00:00
Import old photos on startup.
This commit is contained in:
@@ -6,6 +6,7 @@ using System.Text;
|
|||||||
using Cicm.Database;
|
using Cicm.Database;
|
||||||
using Cicm.Database.Models;
|
using Cicm.Database.Models;
|
||||||
using SixLabors.ImageSharp;
|
using SixLabors.ImageSharp;
|
||||||
|
using SixLabors.ImageSharp.Formats;
|
||||||
using SixLabors.ImageSharp.MetaData;
|
using SixLabors.ImageSharp.MetaData;
|
||||||
using SixLabors.ImageSharp.MetaData.Profiles.Exif;
|
using SixLabors.ImageSharp.MetaData.Profiles.Exif;
|
||||||
using SixLabors.ImageSharp.PixelFormats;
|
using SixLabors.ImageSharp.PixelFormats;
|
||||||
@@ -31,6 +32,7 @@ namespace cicm_web.Helpers
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(image.MetaData.ExifProfile != null)
|
||||||
foreach(ExifValue exif in image.MetaData.ExifProfile.Values.ToList())
|
foreach(ExifValue exif in image.MetaData.ExifProfile.Values.ToList())
|
||||||
switch(exif.Tag)
|
switch(exif.Tag)
|
||||||
{
|
{
|
||||||
@@ -156,7 +158,7 @@ namespace cicm_web.Helpers
|
|||||||
|
|
||||||
foreach(string format in new[] {"jpg", "webp"})
|
foreach(string format in new[] {"jpg", "webp"})
|
||||||
{
|
{
|
||||||
if(!Directory.Exists(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));
|
Directory.CreateDirectory(Path.Combine(webRootPath, "assets/photos/machines/thumbs", format));
|
||||||
|
|
||||||
SKEncodedImageFormat skFormat;
|
SKEncodedImageFormat skFormat;
|
||||||
@@ -173,7 +175,7 @@ namespace cicm_web.Helpers
|
|||||||
foreach(int multiplier in new[] {1, 2, 3})
|
foreach(int multiplier in new[] {1, 2, 3})
|
||||||
{
|
{
|
||||||
if(!Directory.Exists(Path.Combine(webRootPath, "assets/photos/machines/thumbs", format,
|
if(!Directory.Exists(Path.Combine(webRootPath, "assets/photos/machines/thumbs", format,
|
||||||
$"{multiplier}x"))) ;
|
$"{multiplier}x")))
|
||||||
Directory.CreateDirectory(Path.Combine(webRootPath, "assets/photos/machines/thumbs", format,
|
Directory.CreateDirectory(Path.Combine(webRootPath, "assets/photos/machines/thumbs", format,
|
||||||
$"{multiplier}x"));
|
$"{multiplier}x"));
|
||||||
|
|
||||||
@@ -210,5 +212,132 @@ namespace cicm_web.Helpers
|
|||||||
|
|
||||||
return photo;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -164,6 +164,22 @@ namespace cicm_web
|
|||||||
|
|
||||||
context.SaveChanges();
|
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;
|
end = DateTime.Now;
|
||||||
Console.WriteLine("\u001b[31;1mTook \u001b[32;1m{0} seconds\u001b[31;1m...\u001b[0m",
|
Console.WriteLine("\u001b[31;1mTook \u001b[32;1m{0} seconds\u001b[31;1m...\u001b[0m",
|
||||||
(end - start).TotalSeconds);
|
(end - start).TotalSeconds);
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp2.2</TargetFramework>
|
<TargetFramework>netcoreapp2.2</TargetFramework>
|
||||||
<Version>3.0.99.695</Version>
|
<Version>3.0.99.704</Version>
|
||||||
<Company>Canary Islands Computer Museum</Company>
|
<Company>Canary Islands Computer Museum</Company>
|
||||||
<Copyright>Copyright © 2003-2018 Natalia Portillo</Copyright>
|
<Copyright>Copyright © 2003-2018 Natalia Portillo</Copyright>
|
||||||
<Product>Canary Islands Computer Museum Website</Product>
|
<Product>Canary Islands Computer Museum Website</Product>
|
||||||
|
|||||||
Reference in New Issue
Block a user