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

275 lines
12 KiB
C#
Raw Normal View History

using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
2020-02-10 02:20:48 +00:00
using Marechai.Areas.Admin.Models;
2020-02-10 22:44:18 +00:00
using Marechai.Database.Models;
2020-02-10 02:20:48 +00:00
using Marechai.Helpers;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
2019-05-27 19:38:17 +01:00
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
2020-02-10 02:20:48 +00:00
namespace Marechai.Areas.Admin.Controllers
{
2020-02-10 22:44:18 +00:00
[Area("Admin"), Authorize]
public class MachinePhotosController : Controller
{
2020-02-10 22:44:18 +00:00
readonly MarechaiContext _context;
2019-05-28 21:53:53 +01:00
readonly IHostingEnvironment hostingEnvironment;
readonly UserManager<ApplicationUser> userManager;
2020-02-10 22:44:18 +00:00
public MachinePhotosController(MarechaiContext context, IHostingEnvironment hostingEnvironment,
2019-05-28 21:53:53 +01:00
UserManager<ApplicationUser> userManager)
{
2019-05-28 21:53:53 +01:00
_context = context;
this.hostingEnvironment = hostingEnvironment;
2019-05-28 21:53:53 +01:00
this.userManager = userManager;
}
// GET: MachinePhotos
2020-02-10 22:44:18 +00:00
public async Task<IActionResult> Index() => View(await _context.
MachinePhotos.Include(m => m.Machine).
Include(m => m.Machine.Company).Include(m => m.User).
Select(p => new MachinePhotoViewModel
{
Id = p.Id, Author = p.Author,
License = p.License.Name,
Machine =
$"{p.Machine.Company.Name} {p.Machine.Name}",
UploadDate = p.UploadDate,
UploadUser = p.User.UserName,
LicenseId = p.License.Id
}).OrderBy(p => p.Machine).ThenBy(p => p.UploadUser).
ThenBy(p => p.UploadDate).ToListAsync());
// GET: MachinePhotos/Details/5
public async Task<IActionResult> Details(Guid? id)
{
2020-02-10 22:44:18 +00:00
if(id == null)
return NotFound();
MachinePhotoDetailsViewModel machinePhoto =
await _context.MachinePhotos.Select(m => new MachinePhotoDetailsViewModel
{
Id = m.Id, CameraManufacturer = m.CameraManufacturer,
CameraModel = m.CameraModel,
ColorSpace = m.ColorSpace, Comments = m.Comments,
Contrast = m.Contrast,
CreationDate = m.CreationDate, DigitalZoomRatio = m.DigitalZoomRatio,
ExifVersion = m.ExifVersion,
Exposure = m.Exposure, ExposureProgram = m.ExposureProgram,
Flash = m.Flash, Focal = m.Focal,
FocalLength = m.FocalLength, FocalLengthEquivalent = m.FocalLengthEquivalent,
HorizontalResolution = m.HorizontalResolution, IsoRating = m.IsoRating,
Lens = m.Lens,
LightSource = m.LightSource, MeteringMode = m.MeteringMode,
ResolutionUnit = m.ResolutionUnit,
Orientation = m.Orientation, Saturation = m.Saturation,
SceneCaptureType = m.SceneCaptureType,
SensingMethod = m.SensingMethod, Sharpness = m.Sharpness,
SoftwareUsed = m.SoftwareUsed,
SubjectDistanceRange = m.SubjectDistanceRange, UploadDate = m.UploadDate,
VerticalResolution = m.VerticalResolution, WhiteBalance = m.WhiteBalance,
License = m.License.Name,
UploadUser = m.User.UserName,
Machine = $"{m.Machine.Company.Name} {m.Machine.Name}",
MachineId = m.Machine.Id, Source = m.Source
}).FirstOrDefaultAsync(m => m.Id == id);
if(machinePhoto == null)
return NotFound();
return View(machinePhoto);
}
// GET: MachinePhotos/Create
2019-05-27 19:38:17 +01:00
public IActionResult Create()
{
2020-02-10 22:44:18 +00:00
ViewData["MachineId"] = new SelectList(_context.Machines.OrderBy(c => c.Company.Name).ThenBy(c => c.Name).
Select(m => new
{
m.Id, Name = $"{m.Company.Name} {m.Name}"
}), "Id", "Name");
ViewData["LicenseId"] = new SelectList(_context.Licenses.OrderBy(c => c.Name).Select(l => new
{
l.Id, l.Name
}), "Id", "Name");
2019-05-27 19:38:17 +01:00
return View();
}
// POST: MachinePhotos/Create
2020-02-10 22:44:18 +00:00
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
2020-02-10 22:44:18 +00:00
[HttpPost, ValidateAntiForgeryToken]
2019-05-29 01:00:27 +01:00
public async Task<IActionResult> Create([Bind("MachineId,LicenseId,Photo,Source")]
MachinePhotoViewModel machinePhoto)
{
2020-02-10 22:44:18 +00:00
if(!ModelState.IsValid)
return View(machinePhoto);
2020-02-10 22:44:18 +00:00
var newId = Guid.NewGuid();
string tmpPath = Path.GetTempPath();
string tmpFileName = newId + ".tmp";
string tmpFile = Path.Combine(tmpPath, tmpFileName);
if(System.IO.File.Exists(tmpFile))
{
machinePhoto.ErrorMessage = "Colliding temp file please retry.";
2020-02-10 22:44:18 +00:00
return View(machinePhoto);
}
2020-02-10 22:44:18 +00:00
using(var tmpStream = new FileStream(tmpFile, FileMode.CreateNew))
await machinePhoto.Photo.CopyToAsync(tmpStream);
IImageFormat imageinfo = Image.DetectFormat(tmpFile);
string extension;
2020-02-10 22:44:18 +00:00
switch(imageinfo?.Name)
{
2020-02-10 22:44:18 +00:00
case"JPEG":
extension = ".jpg";
2020-02-10 22:44:18 +00:00
break;
2020-02-10 22:44:18 +00:00
case"PNG":
extension = ".png";
2020-02-10 22:44:18 +00:00
break;
default:
System.IO.File.Delete(tmpFile);
machinePhoto.ErrorMessage = "Unsupported file format, only JPEG and PNG are allowed at the moment.";
2020-02-10 22:44:18 +00:00
return View(machinePhoto);
}
MachinePhoto photo = Photos.Add(tmpFile, newId, hostingEnvironment.WebRootPath,
hostingEnvironment.ContentRootPath, extension);
2019-05-28 21:53:53 +01:00
photo.Id = newId;
photo.User = await userManager.GetUserAsync(HttpContext.User);
photo.License = await _context.Licenses.FindAsync(machinePhoto.LicenseId);
photo.Machine = await _context.Machines.FindAsync(machinePhoto.MachineId);
_context.Add(photo);
await _context.SaveChangesAsync();
2020-02-10 22:44:18 +00:00
return RedirectToAction(nameof(Details), new
{
Id = newId
});
}
// GET: MachinePhotos/Edit/5
public async Task<IActionResult> Edit(Guid? id)
{
2020-02-10 22:44:18 +00:00
if(id == null)
return NotFound();
MachinePhoto machinePhoto = await _context.MachinePhotos.FindAsync(id);
2020-02-10 22:44:18 +00:00
if(machinePhoto == null)
return NotFound();
ViewData["MachineId"] = new SelectList(_context.Machines.OrderBy(m => m.Company.Name).ThenBy(m => m.Name).
Select(m => new
{
m.Id, Name = $"{m.Company.Name} {m.Name}"
}), "Id", "Name", machinePhoto.MachineId);
ViewData["LicenseId"] = new SelectList(_context.Licenses.OrderBy(l => l.Name).Select(l => new
{
l.Id, l.Name
}), "Id", "Name", machinePhoto.LicenseId);
return View(machinePhoto);
}
// POST: MachinePhotos/Edit/5
2020-02-10 22:44:18 +00:00
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
2020-02-10 22:44:18 +00:00
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(
Guid id,
[Bind("Author,CameraManufacturer,CameraModel,ColorSpace,Comments,Contrast,CreationDate,DigitalZoomRatio,ExifVersion,Exposure,ExposureMethod,ExposureProgram,Flash,Focal,FocalLength,FocalLengthEquivalent,HorizontalResolution,IsoRating,Lens,LicenseId,LightSource,MachineId,MeteringMode,ResolutionUnit,Orientation,Saturation,SceneCaptureType,SensingMethod,Sharpness,SoftwareUsed,SubjectDistanceRange,VerticalResolution,WhiteBalance,Id,Source")]
MachinePhoto machinePhoto)
{
2020-02-10 22:44:18 +00:00
if(id != machinePhoto.Id)
return NotFound();
2020-02-10 22:44:18 +00:00
if(!ModelState.IsValid)
return View(machinePhoto);
2019-05-28 23:57:28 +01:00
try
{
_context.Update(machinePhoto);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
2020-02-10 22:44:18 +00:00
if(!MachinePhotoExists(machinePhoto.Id))
return NotFound();
2019-05-28 23:57:28 +01:00
throw;
}
2019-05-28 23:57:28 +01:00
return RedirectToAction(nameof(Index));
}
// GET: MachinePhotos/Delete/5
public async Task<IActionResult> Delete(Guid? id)
{
2020-02-10 22:44:18 +00:00
if(id == null)
return NotFound();
MachinePhotoViewModel machinePhoto =
await _context.MachinePhotos.Include(m => m.Machine).Include(m => m.Machine.Company).
Include(m => m.User).Select(p => new MachinePhotoViewModel
{
Id = p.Id, Author = p.Author,
License = p.License.Name,
Machine = $"{p.Machine.Company.Name} {p.Machine.Name}", UploadDate = p.UploadDate,
UploadUser = p.User.UserName, LicenseId = p.License.Id
}).OrderBy(p => p.Machine).ThenBy(p => p.UploadUser).ThenBy(p => p.UploadDate).
FirstOrDefaultAsync(m => m.Id == id);
if(machinePhoto == null)
return NotFound();
return View(machinePhoto);
}
// POST: MachinePhotos/Delete/5
2020-02-10 22:44:18 +00:00
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(Guid id)
{
MachinePhoto machinePhoto = await _context.MachinePhotos.FindAsync(id);
_context.MachinePhotos.Remove(machinePhoto);
await _context.SaveChangesAsync();
2020-02-10 22:44:18 +00:00
foreach(string format in new[]
{
"jpg", "webp"
})
{
2020-02-10 22:44:18 +00:00
foreach(int multiplier in new[]
{
1, 2, 3
})
System.IO.File.Delete(Path.Combine(hostingEnvironment.WebRootPath, "assets/photos/machines/thumbs",
format, $"{multiplier}x", id + $".{format}"));
}
return RedirectToAction(nameof(Index));
}
2020-02-10 22:44:18 +00:00
bool MachinePhotoExists(Guid id) => _context.MachinePhotos.Any(e => e.Id == id);
}
}