mirror of
https://github.com/claunia/marechai.git
synced 2025-12-16 19:14:25 +00:00
Refactor and reorganize code.
This commit is contained in:
@@ -2,8 +2,8 @@ using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Marechai.Database.Models;
|
||||
using Marechai.Areas.Admin.Models;
|
||||
using Marechai.Database.Models;
|
||||
using Marechai.Helpers;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
@@ -16,15 +16,14 @@ using SixLabors.ImageSharp.Formats;
|
||||
|
||||
namespace Marechai.Areas.Admin.Controllers
|
||||
{
|
||||
[Area("Admin")]
|
||||
[Authorize]
|
||||
[Area("Admin"), Authorize]
|
||||
public class MachinePhotosController : Controller
|
||||
{
|
||||
readonly MarechaiContext _context;
|
||||
readonly MarechaiContext _context;
|
||||
readonly IHostingEnvironment hostingEnvironment;
|
||||
readonly UserManager<ApplicationUser> userManager;
|
||||
|
||||
public MachinePhotosController(MarechaiContext context, IHostingEnvironment hostingEnvironment,
|
||||
public MachinePhotosController(MarechaiContext context, IHostingEnvironment hostingEnvironment,
|
||||
UserManager<ApplicationUser> userManager)
|
||||
{
|
||||
_context = context;
|
||||
@@ -33,74 +32,57 @@ namespace Marechai.Areas.Admin.Controllers
|
||||
}
|
||||
|
||||
// GET: MachinePhotos
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
return 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());
|
||||
}
|
||||
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)
|
||||
{
|
||||
if(id == null) return NotFound();
|
||||
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();
|
||||
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);
|
||||
}
|
||||
@@ -108,26 +90,31 @@ namespace Marechai.Areas.Admin.Controllers
|
||||
// GET: MachinePhotos/Create
|
||||
public IActionResult Create()
|
||||
{
|
||||
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");
|
||||
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");
|
||||
|
||||
return View();
|
||||
}
|
||||
|
||||
// POST: MachinePhotos/Create
|
||||
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
||||
// 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.
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
[HttpPost, ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Create([Bind("MachineId,LicenseId,Photo,Source")]
|
||||
MachinePhotoViewModel machinePhoto)
|
||||
{
|
||||
if(!ModelState.IsValid) return View(machinePhoto);
|
||||
if(!ModelState.IsValid)
|
||||
return View(machinePhoto);
|
||||
|
||||
Guid newId = Guid.NewGuid();
|
||||
var newId = Guid.NewGuid();
|
||||
string tmpPath = Path.GetTempPath();
|
||||
string tmpFileName = newId + ".tmp";
|
||||
string tmpFile = Path.Combine(tmpPath, tmpFileName);
|
||||
@@ -135,26 +122,31 @@ namespace Marechai.Areas.Admin.Controllers
|
||||
if(System.IO.File.Exists(tmpFile))
|
||||
{
|
||||
machinePhoto.ErrorMessage = "Colliding temp file please retry.";
|
||||
|
||||
return View(machinePhoto);
|
||||
}
|
||||
|
||||
using(FileStream tmpStream = new FileStream(tmpFile, FileMode.CreateNew))
|
||||
using(var tmpStream = new FileStream(tmpFile, FileMode.CreateNew))
|
||||
await machinePhoto.Photo.CopyToAsync(tmpStream);
|
||||
|
||||
IImageFormat imageinfo = Image.DetectFormat(tmpFile);
|
||||
|
||||
string extension;
|
||||
|
||||
switch(imageinfo?.Name)
|
||||
{
|
||||
case "JPEG":
|
||||
case"JPEG":
|
||||
extension = ".jpg";
|
||||
|
||||
break;
|
||||
case "PNG":
|
||||
case"PNG":
|
||||
extension = ".png";
|
||||
|
||||
break;
|
||||
default:
|
||||
System.IO.File.Delete(tmpFile);
|
||||
machinePhoto.ErrorMessage = "Unsupported file format, only JPEG and PNG are allowed at the moment.";
|
||||
|
||||
return View(machinePhoto);
|
||||
}
|
||||
|
||||
@@ -168,40 +160,52 @@ namespace Marechai.Areas.Admin.Controllers
|
||||
|
||||
_context.Add(photo);
|
||||
await _context.SaveChangesAsync();
|
||||
return RedirectToAction(nameof(Details), new {Id = newId});
|
||||
|
||||
return RedirectToAction(nameof(Details), new
|
||||
{
|
||||
Id = newId
|
||||
});
|
||||
}
|
||||
|
||||
// GET: MachinePhotos/Edit/5
|
||||
public async Task<IActionResult> Edit(Guid? id)
|
||||
{
|
||||
if(id == null) return NotFound();
|
||||
if(id == null)
|
||||
return NotFound();
|
||||
|
||||
MachinePhoto machinePhoto = await _context.MachinePhotos.FindAsync(id);
|
||||
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);
|
||||
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
|
||||
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
||||
// 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.
|
||||
[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)
|
||||
[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)
|
||||
{
|
||||
if(id != machinePhoto.Id) return NotFound();
|
||||
if(id != machinePhoto.Id)
|
||||
return NotFound();
|
||||
|
||||
if(!ModelState.IsValid) return View(machinePhoto);
|
||||
if(!ModelState.IsValid)
|
||||
return View(machinePhoto);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -210,7 +214,8 @@ namespace Marechai.Areas.Admin.Controllers
|
||||
}
|
||||
catch(DbUpdateConcurrencyException)
|
||||
{
|
||||
if(!MachinePhotoExists(machinePhoto.Id)) return NotFound();
|
||||
if(!MachinePhotoExists(machinePhoto.Id))
|
||||
return NotFound();
|
||||
|
||||
throw;
|
||||
}
|
||||
@@ -221,42 +226,43 @@ namespace Marechai.Areas.Admin.Controllers
|
||||
// GET: MachinePhotos/Delete/5
|
||||
public async Task<IActionResult> Delete(Guid? id)
|
||||
{
|
||||
if(id == null) return NotFound();
|
||||
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);
|
||||
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();
|
||||
if(machinePhoto == null)
|
||||
return NotFound();
|
||||
|
||||
return View(machinePhoto);
|
||||
}
|
||||
|
||||
// POST: MachinePhotos/Delete/5
|
||||
[HttpPost]
|
||||
[ActionName("Delete")]
|
||||
[ValidateAntiForgeryToken]
|
||||
[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();
|
||||
|
||||
foreach(string format in new[] {"jpg", "webp"})
|
||||
foreach(string format in new[]
|
||||
{
|
||||
foreach(int multiplier in new[] {1, 2, 3})
|
||||
"jpg", "webp"
|
||||
})
|
||||
{
|
||||
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}"));
|
||||
}
|
||||
@@ -264,9 +270,6 @@ namespace Marechai.Areas.Admin.Controllers
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
bool MachinePhotoExists(Guid id)
|
||||
{
|
||||
return _context.MachinePhotos.Any(e => e.Id == id);
|
||||
}
|
||||
bool MachinePhotoExists(Guid id) => _context.MachinePhotos.Any(e => e.Id == id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user