Files
2025-12-26 23:11:59 -05:00

543 lines
31 KiB
Plaintext

@page "/admin/assets"
@model Assets.IndexModel
@using DataServer.Data.Entities
@{
ViewData["Title"] = "CDN Assets";
}
<div class="space-y-6">
<!-- Header -->
<div class="flex items-center justify-between">
<div>
<h1 class="text-3xl font-bold text-white">CDN Assets</h1>
<p class="text-gray-400 mt-1">Browse MinIO bucket and manage tracked assets</p>
</div>
<form method="post" asp-page-handler="RefreshCache">
@Html.AntiForgeryToken()
<button type="submit" class="btn btn-secondary flex items-center gap-2">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"/>
</svg>
Refresh Asset Cache
</button>
</form>
</div>
<!-- Tabs -->
<div class="flex gap-2 border-b border-gray-700">
<a href="/admin/assets?tab=browse" class="px-4 py-2 @(Model.Tab == "browse" ? "text-white border-b-2 border-indigo-500" : "text-gray-400 hover:text-white")">
Browse CDN
</a>
<a href="/admin/assets?tab=tracked" class="px-4 py-2 @(Model.Tab == "tracked" ? "text-white border-b-2 border-indigo-500" : "text-gray-400 hover:text-white")">
Tracked Assets (@Model.TrackedCount)
</a>
@if (Model.Tab == "edit")
{
<span class="px-4 py-2 text-white border-b-2 border-indigo-500">Edit Asset</span>
}
</div>
@if (!string.IsNullOrEmpty(Model.Message))
{
<div class="p-4 rounded-lg @(Model.IsError ? "bg-red-900/50 border border-red-700 text-red-300" : "bg-green-900/50 border border-green-700 text-green-300")">
@Model.Message
</div>
}
@if (Model.Tab == "browse")
{
@if (!Model.CdnConfigured)
{
<div class="card p-6 text-center">
<svg class="w-16 h-16 mx-auto text-yellow-500 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"/>
</svg>
<h3 class="text-xl font-semibold text-white mb-2">MinIO Not Configured</h3>
<p class="text-gray-400 mb-4">To browse CDN files, set these environment variables (Kubernetes secret):</p>
<pre class="bg-gray-900 p-4 rounded text-left text-sm text-gray-300 max-w-md mx-auto">MINIO_ENDPOINT=assets.aeternalisgames.com
MINIO_ACCESS_KEY=your-access-key
MINIO_SECRET_KEY=your-secret-key
MINIO_BUCKET=downloads</pre>
</div>
}
else
{
<!-- Folder Navigation + Search -->
<div class="card p-4">
<div class="flex flex-wrap items-center gap-4 mb-4">
<label class="label mb-0">Folder:</label>
<div class="flex flex-wrap gap-2">
@foreach (var folder in Model.AvailableFolders)
{
<a href="/admin/assets?tab=browse&prefix=@Uri.EscapeDataString(folder)"
class="btn @(Model.Prefix == folder ? "btn-primary" : "btn-secondary") text-sm">
@folder.TrimEnd('/').Split('/').Last()
</a>
}
</div>
</div>
<!-- Search/Filter -->
<form method="get" class="flex gap-4">
<input type="hidden" name="tab" value="browse" />
<input type="hidden" name="prefix" value="@Model.Prefix" />
<div class="flex-1">
<input type="text" name="filter" value="@Model.Filter" class="input" placeholder="Filter by filename (e.g., ico, sword, skeleton)..." />
</div>
<button type="submit" class="btn btn-secondary">Filter</button>
@if (!string.IsNullOrEmpty(Model.Filter))
{
<a href="/admin/assets?tab=browse&prefix=@Uri.EscapeDataString(Model.Prefix ?? "")" class="btn btn-secondary">Clear</a>
}
</form>
<!-- Breadcrumb -->
<div class="flex items-center gap-1 text-sm mt-3">
<a href="/admin/assets?tab=browse&prefix=" class="text-indigo-400 hover:underline">root</a>
@{
var parts = Model.Prefix?.Split('/', StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty<string>();
var currentPath = "";
}
@foreach (var part in parts)
{
currentPath += part + "/";
<span class="text-gray-600">/</span>
<a href="/admin/assets?tab=browse&prefix=@Uri.EscapeDataString(currentPath)" class="text-indigo-400 hover:underline">@part</a>
}
</div>
<!-- Upload Form -->
<div class="mt-4 p-3 bg-gray-800 rounded-lg">
<form method="post" asp-page-handler="UploadFile" enctype="multipart/form-data" class="flex flex-wrap items-center gap-3">
@Html.AntiForgeryToken()
<input type="hidden" name="uploadPath" value="@Model.Prefix" />
<input type="file" name="file" class="flex-1 min-w-[200px] text-sm text-gray-400
file:mr-3 file:py-2 file:px-4
file:rounded file:border-0
file:text-sm file:font-medium
file:bg-indigo-600 file:text-white
hover:file:bg-indigo-500" />
<button type="submit" class="btn btn-primary flex items-center gap-2">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-8l-4-4m0 0L8 8m4-4v12"/>
</svg>
Upload to @(string.IsNullOrEmpty(Model.Prefix) ? "root" : Model.Prefix)
</button>
</form>
</div>
</div>
<!-- Subfolders -->
@if (Model.Subfolders.Any())
{
<div class="card p-4">
<h3 class="text-white font-medium mb-3">Subfolders</h3>
<div class="flex flex-wrap gap-2">
@foreach (var subfolder in Model.Subfolders)
{
<a href="/admin/assets?tab=browse&prefix=@Uri.EscapeDataString(subfolder)"
class="flex items-center gap-2 px-3 py-2 bg-gray-800 hover:bg-gray-700 rounded text-gray-300">
<svg class="w-4 h-4 text-yellow-500" fill="currentColor" viewBox="0 0 20 20">
<path d="M2 6a2 2 0 012-2h5l2 2h5a2 2 0 012 2v6a2 2 0 01-2 2H4a2 2 0 01-2-2V6z"/>
</svg>
@subfolder.TrimEnd('/').Split('/').Last()
</a>
}
</div>
</div>
}
<!-- Files Grid -->
@if (Model.Files.Any())
{
<div class="card">
<div class="p-4 border-b border-gray-700 flex items-center justify-between">
<div>
<h2 class="text-lg font-semibold text-white">
Files in @(string.IsNullOrEmpty(Model.Prefix) ? "root" : Model.Prefix)
@if (!string.IsNullOrEmpty(Model.Filter))
{
<span class="text-gray-400 font-normal">filtered by "@Model.Filter"</span>
}
</h2>
<p class="text-sm text-gray-500">@Model.FilteredFileCount files total. Click to copy path, + to register.</p>
</div>
</div>
<div class="p-4">
<div class="grid grid-cols-4 md:grid-cols-6 lg:grid-cols-8 xl:grid-cols-10 gap-3">
@foreach (var file in Model.Files)
{
var isTracked = Model.TrackedPaths.Contains(file.Key);
var isImage = file.Name.EndsWith(".png") || file.Name.EndsWith(".jpg") || file.Name.EndsWith(".webp");
<div class="relative group">
<div class="aspect-square bg-gray-900 rounded border @(isTracked ? "border-green-600" : "border-gray-700") flex items-center justify-center overflow-hidden">
@if (isImage)
{
<img src="@Html.Raw(Model.CdnBaseUrl)/downloads/@file.Key"
alt="@file.Name"
class="max-w-full max-h-full object-contain"
loading="lazy"
onerror="this.src='data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 24 24%22 fill=%22%23991b1b%22><text x=%2250%%22 y=%2250%%22 text-anchor=%22middle%22 dy=%22.3em%22 font-size=%228%22>ERR</text></svg>'" />
}
else
{
<div class="text-center text-gray-500">
<svg class="w-8 h-8 mx-auto" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
</svg>
<p class="text-xs mt-1">@System.IO.Path.GetExtension(file.Name)</p>
</div>
}
</div>
<p class="text-xs text-gray-500 truncate mt-1 text-center" title="@file.Key">@file.Name</p>
@if (isTracked)
{
<div class="absolute top-1 right-1 bg-green-600 rounded-full p-0.5">
<svg class="w-3 h-3 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="3" d="M5 13l4 4L19 7"/>
</svg>
</div>
}
<!-- Hover actions -->
<div class="absolute inset-0 bg-black/80 opacity-0 group-hover:opacity-100 transition-opacity flex items-center justify-center gap-1 rounded">
<button type="button" onclick="copyPath('@file.Key')" class="p-1.5 bg-gray-700 rounded hover:bg-gray-600" title="Copy Path">
<svg class="w-4 h-4 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"/>
</svg>
</button>
@if (!isTracked)
{
<form method="post" asp-page-handler="RegisterAsset" class="inline">
@Html.AntiForgeryToken()
<input type="hidden" name="filePath" value="@file.Key" />
<input type="hidden" name="assetType" value="@(isImage ? "Texture" : "Data")" />
<input type="hidden" name="fileSize" value="@file.Size" />
<input type="hidden" name="returnPrefix" value="@Model.Prefix" />
<input type="hidden" name="returnFilter" value="@Model.Filter" />
<input type="hidden" name="returnPage" value="@Model.BrowsePage" />
<button type="submit" class="p-1.5 bg-green-700 rounded hover:bg-green-600" title="Register">
<svg class="w-4 h-4 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
</svg>
</button>
</form>
}
else
{
<a href="/admin/assets?tab=edit&path=@Uri.EscapeDataString(file.Key)" class="p-1.5 bg-indigo-700 rounded hover:bg-indigo-600" title="Edit">
<svg class="w-4 h-4 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
</svg>
</a>
}
<!-- Delete from CDN -->
<form method="post" asp-page-handler="DeleteCdnFile" class="inline" onsubmit="return confirm('Delete @file.Name from CDN? This cannot be undone.')">
@Html.AntiForgeryToken()
<input type="hidden" name="filePath" value="@file.Key" />
<input type="hidden" name="returnPrefix" value="@Model.Prefix" />
<button type="submit" class="p-1.5 bg-red-700 rounded hover:bg-red-600" title="Delete from CDN">
<svg class="w-4 h-4 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
</svg>
</button>
</form>
</div>
</div>
}
</div>
</div>
<!-- Browse Pagination -->
@if (Model.BrowseTotalPages > 1)
{
<div class="p-4 border-t border-gray-700 flex items-center justify-between">
<p class="text-sm text-gray-400">
Page @Model.BrowsePage of @Model.BrowseTotalPages (@Model.FilteredFileCount files)
</p>
<div class="flex items-center gap-2">
@if (Model.BrowsePage > 1)
{
<a href="@Model.GetBrowsePageUrl(1)" class="btn btn-secondary text-xs">First</a>
<a href="@Model.GetBrowsePageUrl(Model.BrowsePage - 1)" class="btn btn-secondary">← Prev</a>
}
<select onchange="window.location.href=this.value" class="input w-24 text-sm">
@for (int i = 1; i <= Math.Min(Model.BrowseTotalPages, 100); i++)
{
<option value="@Model.GetBrowsePageUrl(i)" selected="@(Model.BrowsePage == i)">Page @i</option>
}
</select>
@if (Model.BrowsePage < Model.BrowseTotalPages)
{
<a href="@Model.GetBrowsePageUrl(Model.BrowsePage + 1)" class="btn btn-secondary">Next →</a>
<a href="@Model.GetBrowsePageUrl(Model.BrowseTotalPages)" class="btn btn-secondary text-xs">Last</a>
}
</div>
</div>
}
</div>
}
else if (!Model.Subfolders.Any())
{
<div class="card p-8 text-center">
<svg class="w-16 h-16 mx-auto text-gray-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z"/>
</svg>
<p class="text-gray-400 mt-4">
@if (!string.IsNullOrEmpty(Model.Filter))
{
<text>No files matching "@Model.Filter"</text>
}
else
{
<text>This folder is empty.</text>
}
</p>
</div>
}
}
}
else if (Model.Tab == "edit" && Model.EditingAsset != null)
{
<!-- Edit Asset -->
<div class="card p-6 max-w-2xl">
<h2 class="text-lg font-semibold text-white mb-4">Edit Asset</h2>
<form method="post" asp-page-handler="UpdateAsset" class="space-y-4">
@Html.AntiForgeryToken()
<input type="hidden" name="assetId" value="@Model.EditingAsset.Id" />
<div>
<label class="label">File Path (read-only)</label>
<input type="text" value="@Model.EditingAsset.FilePath" class="input bg-gray-800" readonly />
</div>
<div>
<label class="label">CDN Path (read-only)</label>
<input type="text" value="@Model.EditingAsset.DownloadPath" class="input bg-gray-800" readonly />
</div>
<div class="grid grid-cols-2 gap-4">
<div>
<label class="label">Asset Type</label>
<select name="assetType" class="input">
@foreach (var t in Enum.GetValues<ClientAssetType>())
{
<option value="@t" selected="@(Model.EditingAsset.AssetType == t)">@t</option>
}
</select>
</div>
<div>
<label class="label">Version</label>
<input type="number" name="version" value="@Model.EditingAsset.Version" class="input" min="1" />
</div>
</div>
<div class="grid grid-cols-2 gap-4">
<div>
<label class="label">File Hash (MD5)</label>
<input type="text" name="fileHash" value="@Model.EditingAsset.FileHash" class="input font-mono text-sm" maxlength="32" />
</div>
<div>
<label class="label">File Size (bytes)</label>
<input type="number" name="fileSize" value="@Model.EditingAsset.FileSize" class="input" min="0" />
</div>
</div>
<div>
<label class="label">Min Client Version</label>
<input type="text" name="minClientVersion" value="@Model.EditingAsset.MinClientVersion" class="input" placeholder="e.g., 1.0.0" maxlength="16" />
</div>
<div class="flex items-center gap-6">
<label class="flex items-center gap-2 cursor-pointer">
<input type="checkbox" name="isRequired" value="true" checked="@Model.EditingAsset.IsRequired" class="w-4 h-4 rounded border-gray-600 bg-gray-800 text-indigo-600" />
<span class="text-gray-300">Required (must download before game starts)</span>
</label>
<label class="flex items-center gap-2 cursor-pointer">
<input type="checkbox" name="isEnabled" value="true" checked="@Model.EditingAsset.IsEnabled" class="w-4 h-4 rounded border-gray-600 bg-gray-800 text-indigo-600" />
<span class="text-gray-300">Enabled</span>
</label>
</div>
<div class="flex items-center gap-4 pt-4">
<button type="submit" class="btn btn-primary">Save Changes</button>
<a href="/admin/assets?tab=tracked" class="btn btn-secondary">Cancel</a>
<div class="flex-1"></div>
<form method="post" asp-page-handler="DeleteAsset" class="inline">
@Html.AntiForgeryToken()
<input type="hidden" name="assetId" value="@Model.EditingAsset.Id" />
<button type="submit" class="btn bg-red-700 hover:bg-red-600 text-white" onclick="return confirm('Delete this asset?')">Delete</button>
</form>
</div>
</form>
<!-- Preview -->
@if (Model.EditingAsset.AssetType == ClientAssetType.Texture)
{
<div class="mt-6 pt-6 border-t border-gray-700">
<h3 class="text-white font-medium mb-3">Preview</h3>
<div class="bg-gray-900 rounded-lg p-4 inline-block">
<img src="@Html.Raw(Model.CdnBaseUrl)/downloads/@Model.EditingAsset.DownloadPath"
alt="Preview" class="max-w-xs max-h-48" />
</div>
</div>
}
</div>
}
else
{
<!-- Tracked Assets Tab -->
<div class="card p-4">
<form method="get" class="flex flex-wrap items-end gap-4">
<input type="hidden" name="tab" value="tracked" />
<div class="flex-1 min-w-[200px]">
<label class="label">Search</label>
<input type="text" name="search" value="@Model.Search" class="input" placeholder="Search by path..." />
</div>
<div class="w-40">
<label class="label">Type</label>
<select name="type" class="input">
<option value="">All Types</option>
@foreach (var t in Enum.GetValues<ClientAssetType>())
{
<option value="@t" selected="@(Model.AssetTypeFilter == t)">@t</option>
}
</select>
</div>
<button type="submit" class="btn btn-secondary">Filter</button>
<a href="/admin/assets?tab=tracked" class="btn btn-secondary">Clear</a>
</form>
</div>
<!-- Stats -->
<div class="grid grid-cols-2 md:grid-cols-4 gap-4">
<div class="card p-4">
<p class="text-2xl font-bold text-white">@Model.TrackedCount</p>
<p class="text-sm text-gray-400">Total Tracked</p>
</div>
<div class="card p-4">
<p class="text-2xl font-bold text-indigo-400">@Model.TextureCount</p>
<p class="text-sm text-gray-400">Textures</p>
</div>
<div class="card p-4">
<p class="text-2xl font-bold text-green-400">@Model.SceneCount</p>
<p class="text-sm text-gray-400">Scenes</p>
</div>
<div class="card p-4">
<p class="text-2xl font-bold text-yellow-400">@Model.ScriptCount</p>
<p class="text-sm text-gray-400">Scripts</p>
</div>
</div>
<!-- Assets Table -->
<div class="card overflow-hidden">
<table class="w-full">
<thead class="bg-gray-800">
<tr>
<th class="px-4 py-3 text-left text-xs font-medium text-gray-400 uppercase">Preview</th>
<th class="px-4 py-3 text-left text-xs font-medium text-gray-400 uppercase">Path</th>
<th class="px-4 py-3 text-left text-xs font-medium text-gray-400 uppercase">Type</th>
<th class="px-4 py-3 text-left text-xs font-medium text-gray-400 uppercase">Version</th>
<th class="px-4 py-3 text-left text-xs font-medium text-gray-400 uppercase">Size</th>
<th class="px-4 py-3 text-left text-xs font-medium text-gray-400 uppercase">Req</th>
<th class="px-4 py-3 text-right text-xs font-medium text-gray-400 uppercase">Actions</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-700">
@foreach (var asset in Model.Assets)
{
<tr class="hover:bg-gray-800/50">
<td class="px-4 py-2">
@if (asset.AssetType == ClientAssetType.Texture)
{
<img src="@Html.Raw(Model.CdnBaseUrl)/downloads/@asset.DownloadPath"
class="w-10 h-10 object-contain bg-gray-900 rounded" loading="lazy" />
}
else
{
<div class="w-10 h-10 bg-gray-900 rounded flex items-center justify-center text-gray-500 text-xs">
@asset.AssetType.ToString().Substring(0, 3)
</div>
}
</td>
<td class="px-4 py-2">
<p class="text-white text-sm truncate max-w-xs" title="@asset.FilePath">@asset.FilePath</p>
<p class="text-xs text-gray-500">@asset.FileHash.Substring(0, 8)...</p>
</td>
<td class="px-4 py-2 text-sm text-gray-400">@asset.AssetType</td>
<td class="px-4 py-2 text-sm text-gray-400">v@asset.Version</td>
<td class="px-4 py-2 text-sm text-gray-400">@FormatSize(asset.FileSize)</td>
<td class="px-4 py-2">
@if (asset.IsRequired)
{
<span class="text-green-400">✓</span>
}
</td>
<td class="px-4 py-2 text-right">
<a href="/admin/assets?tab=edit&path=@Uri.EscapeDataString(asset.FilePath)" class="btn btn-secondary text-xs px-2 py-1">Edit</a>
</td>
</tr>
}
@if (!Model.Assets.Any())
{
<tr>
<td colspan="7" class="px-4 py-8 text-center text-gray-400">No tracked assets found.</td>
</tr>
}
</tbody>
</table>
</div>
<!-- Tracked Pagination -->
@if (Model.TrackedTotalPages > 1)
{
<div class="flex items-center justify-between">
<p class="text-sm text-gray-400">
Page @Model.TrackedPage of @Model.TrackedTotalPages
</p>
<div class="flex items-center gap-2">
@if (Model.TrackedPage > 1)
{
<a href="@Model.GetTrackedPageUrl(Model.TrackedPage - 1)" class="btn btn-secondary">← Prev</a>
}
<select onchange="window.location.href=this.value" class="input w-24 text-sm">
@for (int i = 1; i <= Model.TrackedTotalPages; i++)
{
<option value="@Model.GetTrackedPageUrl(i)" selected="@(Model.TrackedPage == i)">Page @i</option>
}
</select>
@if (Model.TrackedPage < Model.TrackedTotalPages)
{
<a href="@Model.GetTrackedPageUrl(Model.TrackedPage + 1)" class="btn btn-secondary">Next →</a>
}
</div>
</div>
}
}
</div>
@functions {
string FormatSize(long bytes)
{
if (bytes < 1024) return $"{bytes} B";
if (bytes < 1024 * 1024) return $"{bytes / 1024.0:F1} KB";
return $"{bytes / (1024.0 * 1024.0):F1} MB";
}
}
@section Scripts {
<script>
function copyPath(path) {
navigator.clipboard.writeText(path).then(() => {
const toast = document.createElement('div');
toast.className = 'fixed bottom-4 left-4 bg-gray-800 text-white px-4 py-2 rounded shadow-lg z-50';
toast.textContent = 'Copied: ' + path;
document.body.appendChild(toast);
setTimeout(() => toast.remove(), 2000);
});
}
</script>
}