Files
marechai/Marechai/Areas/Admin/Controllers/CompanyLogosController.cs

350 lines
15 KiB
C#
Raw Normal View History

using System;
using System.IO;
2019-05-19 02:54:28 +01:00
using System.Linq;
using System.Text;
2019-05-19 02:54:28 +01:00
using System.Threading.Tasks;
using System.Xml;
2020-02-10 02:10:18 +00:00
using Marechai.Database.Models;
2020-02-10 02:20:48 +00:00
using Marechai.Areas.Admin.Models;
2019-05-19 02:54:28 +01:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
2019-05-19 02:54:28 +01:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
using SkiaSharp;
using SKSvg = SkiaSharp.Extended.Svg.SKSvg;
2019-05-19 02:54:28 +01:00
2020-02-10 02:20:48 +00:00
namespace Marechai.Areas.Admin.Controllers
2019-05-19 02:54:28 +01:00
{
[Area("Admin")]
[Authorize]
public class CompanyLogosController : Controller
{
2020-02-10 02:47:39 +00:00
readonly MarechaiContext _context;
readonly IHostingEnvironment hostingEnvironment;
2019-05-19 02:54:28 +01:00
2020-02-10 02:47:39 +00:00
public CompanyLogosController(MarechaiContext context, IHostingEnvironment env)
2019-05-19 02:54:28 +01:00
{
_context = context;
hostingEnvironment = env;
2019-05-19 02:54:28 +01:00
}
// GET: CompanyLogos
public async Task<IActionResult> Index()
{
2020-02-10 02:47:39 +00:00
IIncludableQueryable<CompanyLogo, Company> marechaiContext = _context.CompanyLogos.Include(c => c.Company);
return View(await marechaiContext.OrderBy(l => l.Company.Name).ThenBy(l => l.Year)
.Select(l => new CompanyLogoViewModel
{
Company = l.Company.Name, Id = l.Id, Year = l.Year
}).ToListAsync());
2019-05-19 02:54:28 +01:00
}
// GET: CompanyLogos/Details/5
public async Task<IActionResult> Details(int? id)
{
if(id == null) return NotFound();
CompanyLogo companyLogo = await _context.CompanyLogos
.Include(c => c.Company).FirstOrDefaultAsync(m => m.Id == id);
if(companyLogo == null) return NotFound();
return View(companyLogo);
}
// GET: CompanyLogos/Create
// TODO: Upload
public IActionResult Create()
{
ViewData["CompanyId"] =
new
SelectList(_context.Companies.Select(c => new CompanyViewModel {Name = c.Name, Id = c.Id}).OrderBy(c => c.Name),
"Id", "Name");
return View();
}
2019-05-19 02:54:28 +01:00
// POST: CompanyLogos/Create
2020-02-10 03:09:42 +00:00
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
2019-05-19 02:54:28 +01:00
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
// TODO: Upload
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,CompanyId,Year,SvgLogo")] CompanyLogo companyLogo)
{
if(!ModelState.IsValid)
{
ViewData["CompanyId"] =
new
SelectList(_context.Companies.Select(c => new CompanyViewModel {Name = c.Name, Id = c.Id}).OrderBy(c => c.Name),
"Id", "Name", companyLogo.CompanyId);
return View(companyLogo);
}
using(MemoryStream svgMs = new MemoryStream())
{
await companyLogo.SvgLogo.CopyToAsync(svgMs);
svgMs.Position = 0;
try
{
StreamReader sr = new StreamReader(svgMs, Encoding.UTF8);
string svgStr = await sr.ReadToEndAsync();
XmlDocument xml = new XmlDocument();
xml.LoadXml(svgStr);
}
2020-02-10 03:09:42 +00:00
catch(XmlException)
{
companyLogo.SvgLogo = null;
companyLogo.ErrorMessage = "Not a valid SVG file.";
ViewData["CompanyId"] =
new
SelectList(_context.Companies.Select(c => new CompanyViewModel {Name = c.Name, Id = c.Id}).OrderBy(c => c.Name),
"Id", "Name", companyLogo.CompanyId);
return View(companyLogo);
}
svgMs.Position = 0;
companyLogo.Guid = Guid.NewGuid();
string vectorial = Path.Combine(hostingEnvironment.WebRootPath, "assets/logos",
companyLogo.Guid + ".svg");
if(System.IO.File.Exists(vectorial))
{
companyLogo.SvgLogo = null;
companyLogo.ErrorMessage = "GUID clash, please retry and report to the administrator.";
ViewData["CompanyId"] =
new
SelectList(_context.Companies.Select(c => new CompanyViewModel {Name = c.Name, Id = c.Id}).OrderBy(c => c.Name),
"Id", "Name", companyLogo.CompanyId);
return View(companyLogo);
}
FileStream outSvg = new FileStream(vectorial, FileMode.CreateNew);
await svgMs.CopyToAsync(outSvg);
svgMs.Position = 0;
SKSvg svg = null;
try
{
foreach(string format in new[] {"png", "webp"})
{
if(!Directory.Exists(Path.Combine(hostingEnvironment.WebRootPath, "assets/logos", format))) ;
Directory.CreateDirectory(Path.Combine(hostingEnvironment.WebRootPath, "assets/logos", format));
SKEncodedImageFormat skFormat;
switch(format)
{
case "webp":
skFormat = SKEncodedImageFormat.Webp;
break;
default:
skFormat = SKEncodedImageFormat.Png;
break;
}
foreach(int multiplier in new[] {1, 2, 3})
{
if(!Directory.Exists(Path.Combine(hostingEnvironment.WebRootPath, "assets/logos", format,
2020-02-10 03:09:42 +00:00
$"{multiplier}x")))
Directory.CreateDirectory(Path.Combine(hostingEnvironment.WebRootPath, "assets/logos",
format, $"{multiplier}x"));
string rendered = Path.Combine(hostingEnvironment.WebRootPath, "assets/logos", format,
$"{multiplier}x", companyLogo.Guid + $".{format}");
if(System.IO.File.Exists(rendered))
{
companyLogo.SvgLogo = null;
companyLogo.ErrorMessage = "GUID clash, please retry and report to the administrator.";
ViewData["CompanyId"] =
new
SelectList(_context.Companies.Select(c => new CompanyViewModel {Name = c.Name, Id = c.Id}).OrderBy(c => c.Name),
"Id", "Name", companyLogo.CompanyId);
return View(companyLogo);
}
Console.WriteLine("Rendering {0}", rendered);
if(svg == null)
{
svg = new SKSvg();
svg.Load(svgMs);
}
SKRect svgSize = svg.Picture.CullRect;
SKMatrix matrix = SKMatrix.MakeScale(multiplier, multiplier);
SKBitmap bitmap = new SKBitmap((int)(svgSize.Width * multiplier),
(int)(svgSize.Height * multiplier));
SKCanvas canvas = new SKCanvas(bitmap);
canvas.DrawPicture(svg.Picture, ref matrix);
canvas.Flush();
SKImage image = SKImage.FromBitmap(bitmap);
SKData data = image.Encode(skFormat, 100);
FileStream outfs = new FileStream(rendered, FileMode.CreateNew);
data.SaveTo(outfs);
outfs.Close();
svgMs.Position = 0;
}
}
foreach(string format in new[] {"png", "webp"})
{
if(!Directory.Exists(Path.Combine(hostingEnvironment.WebRootPath, "assets/logos/thumbs",
2020-02-10 03:09:42 +00:00
format)))
Directory.CreateDirectory(Path.Combine(hostingEnvironment.WebRootPath, "assets/logos/thumbs",
format));
SKEncodedImageFormat skFormat;
switch(format)
{
case "webp":
skFormat = SKEncodedImageFormat.Webp;
break;
default:
skFormat = SKEncodedImageFormat.Png;
break;
}
foreach(int multiplier in new[] {1, 2, 3})
{
if(!Directory.Exists(Path.Combine(hostingEnvironment.WebRootPath, "assets/logos/thumbs",
2020-02-10 03:09:42 +00:00
format, $"{multiplier}x")))
Directory.CreateDirectory(Path.Combine(hostingEnvironment.WebRootPath,
"assets/logos/thumbs", format, $"{multiplier}x"));
string rendered = Path.Combine(hostingEnvironment.WebRootPath, "assets/logos/thumbs",
format, $"{multiplier}x", companyLogo.Guid + $".{format}");
if(System.IO.File.Exists(rendered))
{
companyLogo.SvgLogo = null;
companyLogo.ErrorMessage = "GUID clash, please retry and report to the administrator.";
ViewData["CompanyId"] =
new
SelectList(_context.Companies.Select(c => new CompanyViewModel {Name = c.Name, Id = c.Id}).OrderBy(c => c.Name),
"Id", "Name", companyLogo.CompanyId);
return View(companyLogo);
}
Console.WriteLine("Rendering {0}", rendered);
if(svg == null)
{
svg = new SKSvg();
svg.Load(svgMs);
}
SKRect svgSize = svg.Picture.CullRect;
float svgMax = Math.Max(svgSize.Width, svgSize.Height);
float canvasMin = 32 * multiplier;
float scale = canvasMin / svgMax;
SKMatrix matrix = SKMatrix.MakeScale(scale, scale);
SKBitmap bitmap =
new SKBitmap((int)(svgSize.Width * scale), (int)(svgSize.Height * scale));
SKCanvas canvas = new SKCanvas(bitmap);
canvas.DrawPicture(svg.Picture, ref matrix);
canvas.Flush();
SKImage image = SKImage.FromBitmap(bitmap);
SKData data = image.Encode(skFormat, 100);
FileStream outfs = new FileStream(rendered, FileMode.CreateNew);
data.SaveTo(outfs);
outfs.Close();
svgMs.Position = 0;
}
}
}
catch(IOException e)
{
Console.WriteLine(e);
throw;
}
}
_context.Add(companyLogo);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
2019-05-19 02:54:28 +01:00
// GET: CompanyLogos/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if(id == null) return NotFound();
CompanyLogo companyLogo = await _context.CompanyLogos.FirstOrDefaultAsync(c => c.Id == id);
if(companyLogo == null) return NotFound();
ViewData["CompanyId"] =
new SelectList(_context.Companies.OrderBy(l => l.Name), "Id", "Name", companyLogo.CompanyId);
2019-05-19 02:54:28 +01:00
return View(companyLogo);
}
// POST: CompanyLogos/Edit/5
2020-02-10 03:09:42 +00:00
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
2019-05-19 02:54:28 +01:00
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,CompanyId,Year,Guid")] CompanyLogo companyLogo)
{
if(id != companyLogo.Id) return NotFound();
if(ModelState.IsValid)
{
try
{
_context.Update(companyLogo);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
if(!CompanyLogoExists(companyLogo.Id)) return NotFound();
throw;
}
return RedirectToAction(nameof(Index));
}
ViewData["CompanyId"] =
new SelectList(_context.Companies.OrderBy(l => l.Name), "Id", "Name", companyLogo.CompanyId);
2019-05-19 02:54:28 +01:00
return View(companyLogo);
}
// GET: CompanyLogos/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if(id == null) return NotFound();
CompanyLogo companyLogo = await _context.CompanyLogos
.Include(c => c.Company).FirstOrDefaultAsync(m => m.Id == id);
if(companyLogo == null) return NotFound();
return View(companyLogo);
}
// POST: CompanyLogos/Delete/5
[HttpPost]
[ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
2019-05-26 23:24:08 +01:00
CompanyLogo companyLogo = await _context.CompanyLogos.FirstOrDefaultAsync(m => m.Id == id);
if(companyLogo == null) return NotFound();
2019-05-19 02:54:28 +01:00
_context.CompanyLogos.Remove(companyLogo);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
bool CompanyLogoExists(int id)
{
return _context.CompanyLogos.Any(e => e.Id == id);
}
}
}