using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Server.Data; using Server.Data.Entities; using Server.Services; using System.Security.Cryptography; namespace VoidScavengers.Services.Admin.Pages.Assets; public class IndexModel : AdminPageModel { private readonly GameDbContext _db; private readonly ICdnBrowserService _cdnService; public IndexModel(GameDbContext db, ICdnBrowserService cdnService) { _db = db; _cdnService = cdnService; } // Tab state [FromQuery(Name = "tab")] public string Tab { get; set; } = "browse"; // Browse tab [FromQuery(Name = "prefix")] public string? Prefix { get; set; } public bool CdnConfigured => _cdnService.IsConfigured; public List Files { get; set; } = new(); public List Subfolders { get; set; } = new(); public List AvailableFolders { get; set; } = new() { "textures/items/", "textures/mobs/", "textures/objects/", "textures/spells/", "textures/ui/", "scenes/mobs/" }; // Tracked tab [FromQuery(Name = "search")] public string? Search { get; set; } [FromQuery(Name = "type")] public ClientAssetType? AssetTypeFilter { get; set; } [FromQuery(Name = "tpage")] public int TrackedPage { get; set; } = 1; public List Assets { get; set; } = new(); public int TrackedTotalPages { get; set; } public HashSet TrackedPaths { get; set; } = new(); // Stats public int TrackedCount { get; set; } public int TextureCount { get; set; } public int SceneCount { get; set; } public int ScriptCount { get; set; } public int PageSize { get; set; } = 60; public string CdnBaseUrl { get; set; } = "https://assets.aeternalisgames.com"; public string? Message { get; set; } public bool IsError { get; set; } public string GetTrackedPageUrl(int page) { var parts = new List { $"tab=tracked", $"tpage={page}" }; if (!string.IsNullOrEmpty(Search)) parts.Add($"search={Uri.EscapeDataString(Search)}"); if (AssetTypeFilter.HasValue) parts.Add($"type={AssetTypeFilter}"); return "/admin/assets?" + string.Join("&", parts); } public async Task OnGetAsync() { var cdnUrl = Environment.GetEnvironmentVariable("CDN_BASE_URL"); if (!string.IsNullOrEmpty(cdnUrl)) CdnBaseUrl = cdnUrl; // Stats TrackedCount = await _db.ClientAssets.CountAsync(); TextureCount = await _db.ClientAssets.CountAsync(a => a.AssetType == ClientAssetType.Texture); SceneCount = await _db.ClientAssets.CountAsync(a => a.AssetType == ClientAssetType.Scene); ScriptCount = await _db.ClientAssets.CountAsync(a => a.AssetType == ClientAssetType.Script); // Get tracked paths for comparison TrackedPaths = (await _db.ClientAssets.Select(a => a.FilePath).ToListAsync()).ToHashSet(); if (Tab == "browse" && CdnConfigured) { // Normalize prefix Prefix = Prefix?.TrimEnd('/'); if (!string.IsNullOrEmpty(Prefix)) Prefix += "/"; // List files from MinIO var allItems = await _cdnService.ListFilesAsync(Prefix ?? "", 200); // Separate files and folders Subfolders = allItems .Where(f => f.IsDirectory) .Select(f => f.Key) .OrderBy(f => f) .ToList(); Files = allItems .Where(f => !f.IsDirectory) .OrderBy(f => f.Name) .ToList(); } else if (Tab == "tracked") { if (TrackedPage < 1) TrackedPage = 1; var query = _db.ClientAssets.AsQueryable(); if (!string.IsNullOrWhiteSpace(Search)) query = query.Where(a => a.FilePath.Contains(Search)); if (AssetTypeFilter.HasValue) query = query.Where(a => a.AssetType == AssetTypeFilter.Value); var total = await query.CountAsync(); TrackedTotalPages = (int)Math.Ceiling(total / (double)PageSize); Assets = await query .OrderByDescending(a => a.UpdatedAt) .Skip((TrackedPage - 1) * PageSize) .Take(PageSize) .ToListAsync(); } } public async Task OnPostRegisterAssetAsync(string filePath, ClientAssetType assetType, long fileSize, string? returnPrefix) { if (string.IsNullOrWhiteSpace(filePath)) { Message = "File path is required."; IsError = true; return RedirectToPage(new { tab = "browse", prefix = returnPrefix }); } // Normalize path filePath = filePath.Trim().Replace("\\", "/"); if (filePath.StartsWith("/")) filePath = filePath.Substring(1); // Check if already tracked var existing = await _db.ClientAssets.FirstOrDefaultAsync(a => a.FilePath == filePath); if (existing != null) { Message = $"Asset already tracked."; IsError = true; return RedirectToPage(new { tab = "browse", prefix = returnPrefix }); } // Create new asset tracking entry var asset = new ClientAsset { FilePath = filePath, AssetType = assetType, FileHash = GenerateHash(filePath), FileSize = fileSize, Version = 1, IsRequired = false, IsEnabled = true, CreatedAt = DateTime.UtcNow, UpdatedAt = DateTime.UtcNow }; _db.ClientAssets.Add(asset); await _db.SaveChangesAsync(); Message = $"Asset registered."; return RedirectToPage(new { tab = "browse", prefix = returnPrefix }); } public async Task OnPostDeleteAssetAsync(int assetId) { var asset = await _db.ClientAssets.FindAsync(assetId); if (asset != null) { _db.ClientAssets.Remove(asset); await _db.SaveChangesAsync(); Message = $"Asset untracked."; } return RedirectToPage(new { tab = "tracked" }); } private string GenerateHash(string input) { using var md5 = MD5.Create(); var bytes = System.Text.Encoding.UTF8.GetBytes(input + DateTime.UtcNow.Ticks); var hash = md5.ComputeHash(bytes); return Convert.ToHexString(hash).ToLower(); } }