CDN Integration for Admin

This commit is contained in:
dingo
2025-12-25 17:39:00 -05:00
parent 5afe3884fa
commit 1d5fbd29ce
11 changed files with 486 additions and 228 deletions
+65 -59
View File
@@ -2,6 +2,7 @@ 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;
@@ -9,10 +10,12 @@ namespace VoidScavengers.Services.Admin.Pages.Assets;
public class IndexModel : AdminPageModel
{
private readonly GameDbContext _db;
private readonly ICdnBrowserService _cdnService;
public IndexModel(GameDbContext db)
public IndexModel(GameDbContext db, ICdnBrowserService cdnService)
{
_db = db;
_cdnService = cdnService;
}
// Tab state
@@ -20,29 +23,36 @@ public class IndexModel : AdminPageModel
public string Tab { get; set; } = "browse";
// Browse tab
[FromQuery(Name = "folder")]
public string Folder { get; set; } = "textures/items";
[FromQuery(Name = "prefix")]
public string? Prefix { get; set; }
[FromQuery(Name = "files")]
public string? FileList { get; set; }
public List<string> PreviewFiles { get; set; } = new();
public HashSet<string> TrackedPaths { get; set; } = new();
public bool CdnConfigured => _cdnService.IsConfigured;
public List<CdnFile> Files { get; set; } = new();
public List<string> Subfolders { get; set; } = new();
public List<string> AvailableFolders { get; set; } = new()
{
"textures/items/",
"textures/mobs/",
"textures/objects/",
"textures/spells/",
"textures/ui/",
"scenes/mobs/"
};
// Tracked tab
public List<ClientAsset> Assets { get; set; } = new();
[FromQuery(Name = "search")]
public string? Search { get; set; }
[FromQuery(Name = "type")]
public ClientAssetType? TypeFilter { get; set; }
public ClientAssetType? AssetTypeFilter { get; set; }
[FromQuery(Name = "page")]
public int CurrentPage { get; set; } = 1;
[FromQuery(Name = "tpage")]
public int TrackedPage { get; set; } = 1;
public int PageSize { get; set; } = 24;
public int TotalPages { get; set; }
public List<ClientAsset> Assets { get; set; } = new();
public int TrackedTotalPages { get; set; }
public HashSet<string> TrackedPaths { get; set; } = new();
// Stats
public int TrackedCount { get; set; }
@@ -50,16 +60,18 @@ public class IndexModel : AdminPageModel
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 GetPageUrl(int page)
public string GetTrackedPageUrl(int page)
{
var parts = new List<string> { $"tab=tracked", $"page={page}" };
var parts = new List<string> { $"tab=tracked", $"tpage={page}" };
if (!string.IsNullOrEmpty(Search)) parts.Add($"search={Uri.EscapeDataString(Search)}");
if (TypeFilter.HasValue) parts.Add($"type={TypeFilter}");
if (AssetTypeFilter.HasValue) parts.Add($"type={AssetTypeFilter}");
return "/admin/assets?" + string.Join("&", parts);
}
@@ -77,64 +89,58 @@ public class IndexModel : AdminPageModel
// Get tracked paths for comparison
TrackedPaths = (await _db.ClientAssets.Select(a => a.FilePath).ToListAsync()).ToHashSet();
if (Tab == "browse")
if (Tab == "browse" && CdnConfigured)
{
// Generate preview files based on folder and file list
if (!string.IsNullOrEmpty(FileList))
{
var files = FileList.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
PreviewFiles = files.Select(f => $"{Folder}/{f}".Replace("//", "/")).ToList();
}
else
{
// Default preview files - try common naming patterns
// For items, try fb001.png through fb999.png pattern
if (Folder == "textures/items")
{
// Just show a few placeholder suggestions
PreviewFiles = Enumerable.Range(1, 50)
.Select(i => $"{Folder}/fb{i:D3}.png")
.ToList();
}
else if (Folder == "textures/mobs")
{
// Try common mob names
var mobNames = new[] { "skeleton", "zombie", "slime", "goblin", "orc", "spider", "bat", "wolf", "bear", "demon" };
PreviewFiles = mobNames.Select(m => $"{Folder}/{m}.png").ToList();
}
}
// 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
else if (Tab == "tracked")
{
// Tracked assets tab
if (CurrentPage < 1) CurrentPage = 1;
if (TrackedPage < 1) TrackedPage = 1;
var query = _db.ClientAssets.AsQueryable();
if (!string.IsNullOrWhiteSpace(Search))
query = query.Where(a => a.FilePath.Contains(Search));
if (TypeFilter.HasValue)
query = query.Where(a => a.AssetType == TypeFilter.Value);
if (AssetTypeFilter.HasValue)
query = query.Where(a => a.AssetType == AssetTypeFilter.Value);
var total = await query.CountAsync();
TotalPages = (int)Math.Ceiling(total / (double)PageSize);
TrackedTotalPages = (int)Math.Ceiling(total / (double)PageSize);
Assets = await query
.OrderByDescending(a => a.UpdatedAt)
.Skip((CurrentPage - 1) * PageSize)
.Skip((TrackedPage - 1) * PageSize)
.Take(PageSize)
.ToListAsync();
}
}
public async Task<IActionResult> OnPostRegisterAssetAsync(string filePath, ClientAssetType assetType)
public async Task<IActionResult> OnPostRegisterAssetAsync(string filePath, ClientAssetType assetType, long fileSize, string? returnPrefix)
{
if (string.IsNullOrWhiteSpace(filePath))
{
Message = "File path is required.";
IsError = true;
return RedirectToPage(new { tab = Tab, folder = Folder });
return RedirectToPage(new { tab = "browse", prefix = returnPrefix });
}
// Normalize path
@@ -145,9 +151,9 @@ public class IndexModel : AdminPageModel
var existing = await _db.ClientAssets.FirstOrDefaultAsync(a => a.FilePath == filePath);
if (existing != null)
{
Message = $"Asset '{filePath}' is already tracked.";
Message = $"Asset already tracked.";
IsError = true;
return RedirectToPage(new { tab = Tab, folder = Folder });
return RedirectToPage(new { tab = "browse", prefix = returnPrefix });
}
// Create new asset tracking entry
@@ -155,8 +161,8 @@ public class IndexModel : AdminPageModel
{
FilePath = filePath,
AssetType = assetType,
FileHash = GenerateHash(filePath), // Placeholder hash based on path
FileSize = 0, // Unknown without actually fetching the file
FileHash = GenerateHash(filePath),
FileSize = fileSize,
Version = 1,
IsRequired = false,
IsEnabled = true,
@@ -167,8 +173,8 @@ public class IndexModel : AdminPageModel
_db.ClientAssets.Add(asset);
await _db.SaveChangesAsync();
Message = $"Asset '{filePath}' registered successfully.";
return RedirectToPage(new { tab = Tab, folder = Folder });
Message = $"Asset registered.";
return RedirectToPage(new { tab = "browse", prefix = returnPrefix });
}
public async Task<IActionResult> OnPostDeleteAssetAsync(int assetId)
@@ -178,7 +184,7 @@ public class IndexModel : AdminPageModel
{
_db.ClientAssets.Remove(asset);
await _db.SaveChangesAsync();
Message = $"Asset '{asset.FilePath}' untracked.";
Message = $"Asset untracked.";
}
return RedirectToPage(new { tab = "tracked" });
}