2019-05-27 03:46:21 +01:00
|
|
|
using System;
|
2019-05-28 01:09:12 +01:00
|
|
|
using System.IO;
|
2019-05-27 03:46:21 +01:00
|
|
|
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;
|
2019-05-27 03:46:21 +01:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2019-05-28 01:09:12 +01:00
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
2019-05-27 03:46:21 +01:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2019-05-27 19:38:17 +01:00
|
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
2019-05-27 03:46:21 +01:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2019-05-28 01:09:12 +01:00
|
|
|
using SixLabors.ImageSharp;
|
|
|
|
|
using SixLabors.ImageSharp.Formats;
|
2019-05-27 03:46:21 +01:00
|
|
|
|
2020-02-10 02:20:48 +00:00
|
|
|
namespace Marechai.Areas.Admin.Controllers
|
2019-05-27 03:46:21 +01:00
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
[Area("Admin"), Authorize]
|
2019-05-27 03:46:21 +01:00
|
|
|
public class MachinePhotosController : Controller
|
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
readonly MarechaiContext _context;
|
2020-05-21 05:01:35 +01:00
|
|
|
readonly IWebHostEnvironment hostingEnvironment;
|
2019-05-28 01:09:12 +01:00
|
|
|
readonly UserManager<ApplicationUser> userManager;
|
2019-05-27 03:46:21 +01:00
|
|
|
|
2020-05-21 05:01:35 +01:00
|
|
|
public MachinePhotosController(MarechaiContext context, IWebHostEnvironment hostingEnvironment,
|
2019-05-28 21:53:53 +01:00
|
|
|
UserManager<ApplicationUser> userManager)
|
2019-05-27 03:46:21 +01:00
|
|
|
{
|
2019-05-28 21:53:53 +01:00
|
|
|
_context = context;
|
2019-05-28 01:09:12 +01:00
|
|
|
this.hostingEnvironment = hostingEnvironment;
|
2019-05-28 21:53:53 +01:00
|
|
|
this.userManager = userManager;
|
2019-05-27 03:46:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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());
|
2019-05-27 03:46:21 +01:00
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
{
|
2020-05-21 05:01:35 +01:00
|
|
|
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,
|
2020-02-10 22:44:18 +00:00
|
|
|
SubjectDistanceRange = m.SubjectDistanceRange, UploadDate = m.UploadDate,
|
2020-05-21 05:01:35 +01:00
|
|
|
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
|
2020-02-10 22:44:18 +00:00
|
|
|
}).FirstOrDefaultAsync(m => m.Id == id);
|
|
|
|
|
|
|
|
|
|
if(machinePhoto == null)
|
|
|
|
|
return NotFound();
|
2019-05-27 03:46:21 +01:00
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
2019-05-27 03:46:21 +01:00
|
|
|
|
|
|
|
|
// 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
|
2019-05-27 03:46:21 +01:00
|
|
|
// 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)
|
2019-05-28 01:09:12 +01:00
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
if(!ModelState.IsValid)
|
|
|
|
|
return View(machinePhoto);
|
2019-05-28 01:09:12 +01:00
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
var newId = Guid.NewGuid();
|
2019-05-28 01:09:12 +01:00
|
|
|
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
|
|
|
|
2019-05-28 01:09:12 +01:00
|
|
|
return View(machinePhoto);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
using(var tmpStream = new FileStream(tmpFile, FileMode.CreateNew))
|
2019-05-28 01:09:12 +01:00
|
|
|
await machinePhoto.Photo.CopyToAsync(tmpStream);
|
|
|
|
|
|
|
|
|
|
IImageFormat imageinfo = Image.DetectFormat(tmpFile);
|
|
|
|
|
|
|
|
|
|
string extension;
|
2020-02-10 22:44:18 +00:00
|
|
|
|
2019-05-28 01:09:12 +01:00
|
|
|
switch(imageinfo?.Name)
|
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
case"JPEG":
|
2019-05-28 01:09:12 +01:00
|
|
|
extension = ".jpg";
|
2020-02-10 22:44:18 +00:00
|
|
|
|
2019-05-28 01:09:12 +01:00
|
|
|
break;
|
2020-02-10 22:44:18 +00:00
|
|
|
case"PNG":
|
2019-05-28 01:09:12 +01:00
|
|
|
extension = ".png";
|
2020-02-10 22:44:18 +00:00
|
|
|
|
2019-05-28 01:09:12 +01: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
|
|
|
|
2019-05-28 01:09:12 +01:00
|
|
|
return View(machinePhoto);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-01 01:44:03 +01:00
|
|
|
MachinePhoto photo = Photos.Add(tmpFile, newId, hostingEnvironment.WebRootPath,
|
|
|
|
|
hostingEnvironment.ContentRootPath, extension);
|
2019-05-28 01:09:12 +01:00
|
|
|
|
2019-05-28 21:53:53 +01:00
|
|
|
photo.Id = newId;
|
|
|
|
|
photo.User = await userManager.GetUserAsync(HttpContext.User);
|
2019-05-28 01:09:12 +01:00
|
|
|
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
|
|
|
|
|
});
|
2019-05-28 01:09:12 +01:00
|
|
|
}
|
2019-05-27 03:46:21 +01:00
|
|
|
|
|
|
|
|
// GET: MachinePhotos/Edit/5
|
|
|
|
|
public async Task<IActionResult> Edit(Guid? id)
|
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
if(id == null)
|
|
|
|
|
return NotFound();
|
2019-05-27 03:46:21 +01:00
|
|
|
|
|
|
|
|
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);
|
2019-05-28 23:50:22 +01:00
|
|
|
|
2019-05-27 03:46:21 +01:00
|
|
|
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
|
2019-05-27 03:46:21 +01:00
|
|
|
// 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)
|
2019-05-27 03:46:21 +01:00
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
if(id != machinePhoto.Id)
|
|
|
|
|
return NotFound();
|
2019-05-27 03:46:21 +01:00
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
if(!ModelState.IsValid)
|
|
|
|
|
return View(machinePhoto);
|
2019-05-27 03:46:21 +01:00
|
|
|
|
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-27 03:46:21 +01:00
|
|
|
|
2019-05-28 23:57:28 +01:00
|
|
|
throw;
|
2019-05-27 03:46:21 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-28 23:57:28 +01:00
|
|
|
return RedirectToAction(nameof(Index));
|
2019-05-27 03:46:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
{
|
2020-05-21 05:01:35 +01:00
|
|
|
Id = p.Id, Author = p.Author, License = p.License.Name,
|
2020-02-10 22:44:18 +00:00
|
|
|
Machine = $"{p.Machine.Company.Name} {p.Machine.Name}", UploadDate = p.UploadDate,
|
2020-05-21 05:01:35 +01:00
|
|
|
UploadUser = p.User.UserName, LicenseId = p.License.Id
|
2020-02-10 22:44:18 +00:00
|
|
|
}).OrderBy(p => p.Machine).ThenBy(p => p.UploadUser).ThenBy(p => p.UploadDate).
|
|
|
|
|
FirstOrDefaultAsync(m => m.Id == id);
|
|
|
|
|
|
|
|
|
|
if(machinePhoto == null)
|
|
|
|
|
return NotFound();
|
2019-05-27 03:46:21 +01:00
|
|
|
|
|
|
|
|
return View(machinePhoto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// POST: MachinePhotos/Delete/5
|
2020-02-10 22:44:18 +00:00
|
|
|
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
|
2019-05-27 03:46:21 +01:00
|
|
|
public async Task<IActionResult> DeleteConfirmed(Guid id)
|
|
|
|
|
{
|
|
|
|
|
MachinePhoto machinePhoto = await _context.MachinePhotos.FindAsync(id);
|
|
|
|
|
_context.MachinePhotos.Remove(machinePhoto);
|
|
|
|
|
await _context.SaveChangesAsync();
|
2019-05-29 00:39:39 +01:00
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
foreach(string format in new[]
|
|
|
|
|
{
|
|
|
|
|
"jpg", "webp"
|
|
|
|
|
})
|
2019-05-29 00:39:39 +01:00
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
foreach(int multiplier in new[]
|
|
|
|
|
{
|
|
|
|
|
1, 2, 3
|
|
|
|
|
})
|
2019-05-29 00:39:39 +01:00
|
|
|
System.IO.File.Delete(Path.Combine(hostingEnvironment.WebRootPath, "assets/photos/machines/thumbs",
|
|
|
|
|
format, $"{multiplier}x", id + $".{format}"));
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-27 03:46:21 +01:00
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
bool MachinePhotoExists(Guid id) => _context.MachinePhotos.Any(e => e.Id == id);
|
2019-05-27 03:46:21 +01:00
|
|
|
}
|
|
|
|
|
}
|