Fix admin route access and backend configuration

- Added /admin redirect to login page in nginx config
- Fixed backend server.js route ordering for proper admin handling
- Updated authentication middleware and routes
- Added user management routes
- Configured PostgreSQL integration
- Updated environment configuration
This commit is contained in:
Local Server
2025-12-13 22:34:11 -06:00
parent 8bb6430a70
commit 703ab57984
253 changed files with 29870 additions and 157 deletions

View File

@@ -0,0 +1,106 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using SkyArtShop.Services;
namespace SkyArtShop.Controllers
{
[Route("admin/upload")]
[Authorize(Roles="Admin")]
public class AdminUploadController : Controller
{
private readonly IWebHostEnvironment _environment;
public AdminUploadController(IWebHostEnvironment environment)
{
_environment = environment;
}
[HttpGet("")]
public IActionResult Index()
{
var uploadsPath = Path.Combine(_environment.WebRootPath, "uploads", "images");
var images = new List<string>();
if (Directory.Exists(uploadsPath))
{
var files = Directory.GetFiles(uploadsPath)
.Select(f => $"/uploads/images/{Path.GetFileName(f)}")
.OrderByDescending(f => f)
.ToList();
images = files;
}
return View(images);
}
[HttpPost("image")]
public async Task<IActionResult> UploadImage(IFormFile file)
{
if (file == null || file.Length == 0)
{
return Json(new { success = false, message = "No file uploaded" });
}
var allowedExtensions = new[] { ".jpg", ".jpeg", ".png", ".gif", ".webp" };
var extension = Path.GetExtension(file.FileName).ToLowerInvariant();
if (!allowedExtensions.Contains(extension))
{
return Json(new { success = false, message = "Invalid file type" });
}
try
{
var uploadsPath = Path.Combine(_environment.WebRootPath, "uploads", "images");
if (!Directory.Exists(uploadsPath)) Directory.CreateDirectory(uploadsPath);
var fileName = $"{Guid.NewGuid()}{extension}";
var filePath = Path.Combine(uploadsPath, fileName);
using var stream = new FileStream(filePath, FileMode.Create);
await file.CopyToAsync(stream);
return Json(new { success = true, url = $"/uploads/images/{fileName}" });
}
catch (Exception ex)
{
return Json(new { success = false, message = ex.Message });
}
}
[HttpPost("multiple")]
public async Task<IActionResult> UploadMultiple(List<IFormFile> files)
{
var uploadedUrls = new List<string>();
foreach (var file in files)
{
if (file == null || file.Length == 0) continue;
var extension = Path.GetExtension(file.FileName).ToLowerInvariant();
var allowedExtensions = new[] { ".jpg", ".jpeg", ".png", ".gif", ".webp" };
if (!allowedExtensions.Contains(extension)) continue;
var uploadsPath = Path.Combine(_environment.WebRootPath, "uploads", "images");
if (!Directory.Exists(uploadsPath)) Directory.CreateDirectory(uploadsPath);
var fileName = $"{Guid.NewGuid()}{extension}";
var filePath = Path.Combine(uploadsPath, fileName);
using var stream = new FileStream(filePath, FileMode.Create);
await file.CopyToAsync(stream);
uploadedUrls.Add($"/uploads/images/{fileName}");
}
return Json(new { success = true, urls = uploadedUrls });
}
[HttpPost("delete")]
public IActionResult DeleteImage([FromBody] string imageUrl)
{
try
{
var fileName = Path.GetFileName(imageUrl);
var filePath = Path.Combine(_environment.WebRootPath, "uploads", "images", fileName);
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
return Json(new { success = true });
}
return Json(new { success = false, message = "File not found" });
}
catch (Exception ex)
{
return Json(new { success = false, message = ex.Message });
}
}
}
}