All 26 pages had @attribute [Authorize(Roles = "Admin,SuperAdmin")] which
conflicts with the Keycloak role sor-admin. The fallback policy in Program.cs
already enforces RequireRole("sor-admin") globally, making these attributes
redundant. Removing them fixes Access Denied for all admin pages.
Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)
Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
809 lines
31 KiB
Plaintext
809 lines
31 KiB
Plaintext
@page "/content/cdn"
|
|
@implements IDisposable
|
|
@using Amazon.S3
|
|
@using Amazon.S3.Model
|
|
@using Microsoft.AspNetCore.Components.Forms
|
|
@using Microsoft.EntityFrameworkCore
|
|
@using Microsoft.Extensions.Options
|
|
@using SOR.Cdn
|
|
@using SOR.Database.Contexts
|
|
@inject IAmazonS3 S3
|
|
@inject IOptions<CdnOptions> CdnOptionsAccessor
|
|
@inject IJSRuntime Js
|
|
@inject CatalogDbContext Db
|
|
|
|
<PageTitle>CDN Browser - SOR Admin</PageTitle>
|
|
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<div>
|
|
<h1 class="mb-0">CDN Browser</h1>
|
|
<div class="text-muted small">@_bucketLabel</div>
|
|
</div>
|
|
<button class="btn btn-outline-secondary btn-sm" @onclick="RefreshAsync" disabled="@_loading">
|
|
@if (_loading)
|
|
{
|
|
<span class="spinner-border spinner-border-sm me-1"></span>
|
|
}
|
|
else
|
|
{
|
|
<i class="bi bi-arrow-clockwise me-1"></i>
|
|
}
|
|
Refresh
|
|
</button>
|
|
</div>
|
|
|
|
@if (_message != null)
|
|
{
|
|
<div class="alert @(_isError ? "alert-danger" : "alert-success")">@_message</div>
|
|
}
|
|
|
|
<div class="row g-4">
|
|
<div class="col-lg-4">
|
|
<div class="card shadow-sm mb-4">
|
|
<div class="card-header fw-medium">Browse Prefix</div>
|
|
<div class="card-body">
|
|
<label class="form-label small">Folder Prefix</label>
|
|
<div class="input-group input-group-sm mb-2">
|
|
<input @bind="_prefixInput" class="form-control font-monospace"
|
|
placeholder="e.g. cards/art or cosmetics" />
|
|
<button class="btn btn-outline-secondary" @onclick="ApplyPrefixAsync" disabled="@_loading">
|
|
Open
|
|
</button>
|
|
</div>
|
|
<div class="form-text">
|
|
Leave blank to browse the bucket root.
|
|
</div>
|
|
<div class="mt-3">
|
|
<div class="small text-muted mb-2">Quick Prefixes</div>
|
|
<div class="d-flex flex-wrap gap-1">
|
|
@foreach (var prefix in _knownPrefixes)
|
|
{
|
|
var selected = string.Equals(_activePrefix.TrimEnd('/'), prefix.Key, StringComparison.OrdinalIgnoreCase);
|
|
<button class="btn btn-sm @(selected ? "btn-primary" : "btn-outline-secondary")"
|
|
@onclick="() => SelectKnownPrefixAsync(prefix.Key)">
|
|
@prefix.Label
|
|
</button>
|
|
}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card shadow-sm mb-4">
|
|
<div class="card-header fw-medium">Prefix Guide</div>
|
|
<div class="card-body">
|
|
<ul class="small mb-0 ps-3">
|
|
<li><span class="font-monospace">data</span> - client config and UI-driven data files</li>
|
|
<li><span class="font-monospace">graphics</span> - icons and UI art</li>
|
|
<li><span class="font-monospace">cardart</span> - 2D card art, front and back</li>
|
|
<li><span class="font-monospace">sfx</span> - sound effects</li>
|
|
<li><span class="font-monospace">music</span> - background music</li>
|
|
<li><span class="font-monospace">models</span> - 3D card, UI, and tile models</li>
|
|
<li><span class="font-monospace">animations</span> - 3D animation assets</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card shadow-sm mb-4">
|
|
<div class="card-header fw-medium">Upload New File</div>
|
|
<div class="card-body">
|
|
<div class="small text-muted mb-2">
|
|
Upload destination:
|
|
<span class="font-monospace">@(_activePrefixDisplay)</span>
|
|
</div>
|
|
<div class="form-check mb-2">
|
|
<input @bind="_uploadIsAiGenerated" class="form-check-input" type="checkbox" id="cdnUploadAi" />
|
|
<label class="form-check-label small" for="cdnUploadAi">AI-generated</label>
|
|
</div>
|
|
<label class="btn btn-sm btn-primary @(_uploading ? "disabled" : "")">
|
|
@if (_uploading)
|
|
{
|
|
<span class="spinner-border spinner-border-sm me-1"></span>
|
|
}
|
|
else
|
|
{
|
|
<i class="bi bi-upload me-1"></i>
|
|
}
|
|
Upload File
|
|
<InputFile style="display:none" OnChange="UploadNewAsync" />
|
|
</label>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card shadow-sm">
|
|
<div class="card-header fw-medium">Replace File</div>
|
|
<div class="card-body">
|
|
@if (_replaceTarget is null)
|
|
{
|
|
<div class="text-muted small">
|
|
Select a file from the list, then choose a replacement upload.
|
|
</div>
|
|
}
|
|
else
|
|
{
|
|
<div class="small mb-2">
|
|
<div class="text-muted">Target Key</div>
|
|
<div class="font-monospace text-break">@_replaceTarget.Key</div>
|
|
</div>
|
|
<div class="form-check mb-2">
|
|
<input @bind="_replaceIsAiGenerated" class="form-check-input" type="checkbox" id="cdnReplaceAi" />
|
|
<label class="form-check-label small" for="cdnReplaceAi">AI-generated</label>
|
|
</div>
|
|
<div class="d-flex gap-2">
|
|
<label class="btn btn-sm btn-warning @(_replacing ? "disabled" : "")">
|
|
@if (_replacing)
|
|
{
|
|
<span class="spinner-border spinner-border-sm me-1"></span>
|
|
}
|
|
else
|
|
{
|
|
<i class="bi bi-arrow-repeat me-1"></i>
|
|
}
|
|
Replace
|
|
<InputFile style="display:none" OnChange="ReplaceAsync" />
|
|
</label>
|
|
<button class="btn btn-sm btn-outline-secondary" @onclick="ClearReplaceTarget">
|
|
Clear
|
|
</button>
|
|
</div>
|
|
<div class="form-text mt-2">
|
|
Replaces the selected object at the same CDN path.
|
|
</div>
|
|
}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-lg-8">
|
|
<div class="card shadow-sm">
|
|
<div class="card-header fw-medium">
|
|
<div class="d-flex justify-content-between align-items-center mb-2">
|
|
<span>Objects <span class="badge bg-secondary ms-1">@_items.Count</span>
|
|
@if (!string.IsNullOrWhiteSpace(_search))
|
|
{
|
|
<span class="badge bg-info ms-1">@FilteredSorted().Count() shown</span>
|
|
}
|
|
</span>
|
|
<small class="text-muted font-monospace">@_activePrefixDisplay</small>
|
|
</div>
|
|
<input @bind="_search" @bind:event="oninput"
|
|
class="form-control form-control-sm font-monospace"
|
|
placeholder="Filter by key name…" />
|
|
</div>
|
|
<div class="card-body p-0">
|
|
@if (_loading && _items.Count == 0)
|
|
{
|
|
<div class="p-3 text-muted">Loading...</div>
|
|
}
|
|
else if (_items.Count == 0)
|
|
{
|
|
<div class="p-3 text-muted">No objects found for this prefix.</div>
|
|
}
|
|
else
|
|
{
|
|
<div style="max-height:65vh;overflow:auto;">
|
|
<table class="table table-sm table-hover mb-0">
|
|
<thead class="sticky-top bg-body">
|
|
<tr>
|
|
<th style="cursor:pointer" @onclick='() => SetSort("Key")'>
|
|
Key @SortIcon("Key")
|
|
</th>
|
|
<th style="width:60px">AI</th>
|
|
<th style="width:100px;cursor:pointer" @onclick='() => SetSort("Size")'>
|
|
Size @SortIcon("Size")
|
|
</th>
|
|
<th style="width:160px;cursor:pointer" @onclick='() => SetSort("Modified")'>
|
|
Modified @SortIcon("Modified")
|
|
</th>
|
|
<th style="width:260px">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
@foreach (var item in FilteredSorted())
|
|
{
|
|
var row = item;
|
|
<tr class="@(_replaceTarget?.Key == row.Key ? "table-warning" : "")">
|
|
<td class="font-monospace small text-break align-middle">@row.Key</td>
|
|
<td class="align-middle">
|
|
@if (row.IsAiGenerated)
|
|
{
|
|
<span class="badge bg-warning text-dark">AI</span>
|
|
}
|
|
else
|
|
{
|
|
<span class="text-muted small">—</span>
|
|
}
|
|
</td>
|
|
<td class="align-middle small">@FormatBytes(row.Size)</td>
|
|
<td class="align-middle text-muted small">@row.LastModified.ToString("yyyy-MM-dd HH:mm")</td>
|
|
<td class="align-middle">
|
|
<div class="d-flex flex-wrap gap-1">
|
|
@if (IsModel(row.Key))
|
|
{
|
|
<button class="btn btn-sm btn-outline-info"
|
|
@onclick="() => OpenPreview(row.PublicUrl)">
|
|
<i class="bi bi-box me-1"></i>3D
|
|
</button>
|
|
}
|
|
else
|
|
{
|
|
<a class="btn btn-sm btn-outline-primary"
|
|
href="@row.PublicUrl"
|
|
target="_blank"
|
|
rel="noopener noreferrer">
|
|
<i class="bi bi-eye me-1"></i>View
|
|
</a>
|
|
}
|
|
<button class="btn btn-sm btn-outline-secondary"
|
|
@onclick="() => CopyUrlAsync(row.PublicUrl)">
|
|
<i class="bi bi-clipboard"></i>
|
|
</button>
|
|
<button class="btn btn-sm btn-outline-warning"
|
|
@onclick="() => SelectReplaceTarget(row)">
|
|
<i class="bi bi-arrow-repeat"></i>
|
|
</button>
|
|
<button class="btn btn-sm btn-outline-danger"
|
|
disabled="@(_deletingKey == row.Key)"
|
|
@onclick="() => DeleteAsync(row)">
|
|
@if (_deletingKey == row.Key)
|
|
{
|
|
<span class="spinner-border spinner-border-sm"></span>
|
|
}
|
|
else
|
|
{
|
|
<i class="bi bi-trash"></i>
|
|
}
|
|
</button>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
}
|
|
</div>
|
|
@if (_hasMore)
|
|
{
|
|
<div class="card-footer">
|
|
<button class="btn btn-sm btn-outline-secondary" @onclick="LoadMoreAsync" disabled="@_loading">
|
|
Load More
|
|
</button>
|
|
</div>
|
|
}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- 3D Preview Modal -->
|
|
@if (_previewUrl is not null)
|
|
{
|
|
<div class="modal d-block" tabindex="-1" style="background:rgba(0,0,0,0.55)">
|
|
<div class="modal-dialog modal-lg modal-dialog-centered">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title"><i class="bi bi-box me-2"></i>3D Preview</h5>
|
|
<button type="button" class="btn-close" @onclick="ClosePreview"></button>
|
|
</div>
|
|
<div class="modal-body p-0" style="background:#f0f0f0">
|
|
<model-viewer id="mv-preview"
|
|
src="@_previewUrl"
|
|
camera-controls
|
|
auto-rotate
|
|
auto-rotate-delay="1000"
|
|
environment-image="neutral"
|
|
shadow-intensity="0.8"
|
|
style="width:100%;height:520px;background:#f5f5f5">
|
|
</model-viewer>
|
|
</div>
|
|
<div class="modal-footer py-2 flex-wrap gap-2">
|
|
<span class="text-muted small font-monospace me-auto text-break">@_previewUrl</span>
|
|
@if (_previewAnimations.Count > 0)
|
|
{
|
|
<select class="form-select form-select-sm" style="width:auto"
|
|
value="@_selectedAnimation"
|
|
@onchange="OnAnimationSelected">
|
|
<option value="">— No animation —</option>
|
|
@foreach (var name in _previewAnimations)
|
|
{
|
|
<option value="@name">@name</option>
|
|
}
|
|
</select>
|
|
}
|
|
else if (_previewUrl is not null)
|
|
{
|
|
<span class="text-muted small fst-italic">Loading animations…</span>
|
|
}
|
|
<button class="btn btn-sm btn-outline-secondary"
|
|
@onclick="() => CopyUrlAsync(_previewUrl!)">
|
|
<i class="bi bi-clipboard me-1"></i>Copy URL
|
|
</button>
|
|
<button class="btn btn-sm btn-secondary" @onclick="ClosePreview">Close</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
}
|
|
|
|
<script type="module" src="https://ajax.googleapis.com/ajax/libs/model-viewer/3.5.0/model-viewer.min.js"></script>
|
|
<script>
|
|
window.sorCdn = {
|
|
initModelViewerAnimations: function (elementId, dotnetRef) {
|
|
const el = document.getElementById(elementId);
|
|
if (!el) { dotnetRef.invokeMethodAsync('OnAnimationsLoaded', []); return; }
|
|
const report = () =>
|
|
dotnetRef.invokeMethodAsync('OnAnimationsLoaded', Array.from(el.availableAnimations ?? []));
|
|
if (el.loaded) { report(); }
|
|
else { el.addEventListener('load', report, { once: true }); }
|
|
},
|
|
setModelAnimation: function (elementId, animName) {
|
|
const el = document.getElementById(elementId);
|
|
if (!el) return;
|
|
if (animName) { el.setAttribute('animation-name', animName); el.setAttribute('autoplay', ''); }
|
|
else { el.removeAttribute('animation-name'); el.removeAttribute('autoplay'); }
|
|
}
|
|
};
|
|
</script>
|
|
|
|
@code {
|
|
private readonly List<CdnObjectRow> _items = new();
|
|
private readonly (string Key, string Label)[] _knownPrefixes =
|
|
{
|
|
("data", "data"),
|
|
("graphics", "graphics"),
|
|
("cardart", "cardart"),
|
|
("sfx", "sfx"),
|
|
("music", "music"),
|
|
("models", "models"),
|
|
("animations", "animations"),
|
|
};
|
|
|
|
private string _prefixInput = "";
|
|
private string _activePrefix = "";
|
|
private string _bucketLabel = "";
|
|
private string _continuationToken = "";
|
|
private string _deletingKey = "";
|
|
private string? _message;
|
|
private bool _isError;
|
|
private bool _loading;
|
|
private bool _uploading;
|
|
private bool _replacing;
|
|
private bool _uploadIsAiGenerated;
|
|
private bool _replaceIsAiGenerated;
|
|
private bool _hasMore;
|
|
private CdnObjectRow? _replaceTarget;
|
|
|
|
// Search / sort state
|
|
private string _search = "";
|
|
private string _sortCol = "Modified";
|
|
private bool _sortAsc = false;
|
|
|
|
// 3D preview
|
|
private string? _previewUrl;
|
|
private List<string> _previewAnimations = new();
|
|
private string _selectedAnimation = "";
|
|
private DotNetObjectReference<CdnBrowser>? _dotNetRef;
|
|
private bool _animWatcherActive;
|
|
|
|
private string _activePrefixDisplay => string.IsNullOrEmpty(_activePrefix) ? "/" : $"/{_activePrefix}";
|
|
|
|
private IEnumerable<CdnObjectRow> FilteredSorted()
|
|
{
|
|
var q = _items.AsEnumerable();
|
|
|
|
if (!string.IsNullOrWhiteSpace(_search))
|
|
q = q.Where(r => r.Key.Contains(_search, StringComparison.OrdinalIgnoreCase));
|
|
|
|
q = _sortCol switch
|
|
{
|
|
"Key" => _sortAsc ? q.OrderBy(r => r.Key) : q.OrderByDescending(r => r.Key),
|
|
"Size" => _sortAsc ? q.OrderBy(r => r.Size) : q.OrderByDescending(r => r.Size),
|
|
_ => _sortAsc ? q.OrderBy(r => r.LastModified) : q.OrderByDescending(r => r.LastModified),
|
|
};
|
|
|
|
return q;
|
|
}
|
|
|
|
private void SetSort(string col)
|
|
{
|
|
if (_sortCol == col) _sortAsc = !_sortAsc;
|
|
else { _sortCol = col; _sortAsc = true; }
|
|
}
|
|
|
|
private string SortIcon(string col)
|
|
{
|
|
if (_sortCol != col) return "⇅";
|
|
return _sortAsc ? "↑" : "↓";
|
|
}
|
|
|
|
private static bool IsModel(string key) =>
|
|
key.EndsWith(".glb", StringComparison.OrdinalIgnoreCase) ||
|
|
key.EndsWith(".gltf", StringComparison.OrdinalIgnoreCase);
|
|
|
|
private void OpenPreview(string url)
|
|
{
|
|
_previewUrl = url;
|
|
_previewAnimations.Clear();
|
|
_selectedAnimation = "";
|
|
_animWatcherActive = false;
|
|
}
|
|
|
|
private void ClosePreview()
|
|
{
|
|
_previewUrl = null;
|
|
_previewAnimations.Clear();
|
|
_selectedAnimation = "";
|
|
_animWatcherActive = false;
|
|
}
|
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
|
{
|
|
if (_previewUrl is not null && !_animWatcherActive)
|
|
{
|
|
_animWatcherActive = true;
|
|
_dotNetRef ??= DotNetObjectReference.Create(this);
|
|
await Js.InvokeVoidAsync("sorCdn.initModelViewerAnimations", "mv-preview", _dotNetRef);
|
|
}
|
|
}
|
|
|
|
[JSInvokable]
|
|
public void OnAnimationsLoaded(string[] animations)
|
|
{
|
|
_previewAnimations = animations.ToList();
|
|
_selectedAnimation = "";
|
|
StateHasChanged();
|
|
}
|
|
|
|
private async Task OnAnimationSelected(ChangeEventArgs e)
|
|
{
|
|
_selectedAnimation = e.Value?.ToString() ?? "";
|
|
await Js.InvokeVoidAsync("sorCdn.setModelAnimation", "mv-preview", _selectedAnimation);
|
|
}
|
|
|
|
public void Dispose() => _dotNetRef?.Dispose();
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
var options = CdnOptionsAccessor.Value;
|
|
_bucketLabel = $"{options.BucketName} @ {options.EndpointUrl}";
|
|
await LoadAsync(reset: true);
|
|
}
|
|
|
|
private async Task ApplyPrefixAsync()
|
|
{
|
|
_activePrefix = NormalizePrefix(_prefixInput);
|
|
_replaceTarget = null;
|
|
_search = "";
|
|
await LoadAsync(reset: true);
|
|
}
|
|
|
|
private async Task SelectKnownPrefixAsync(string prefix)
|
|
{
|
|
_prefixInput = prefix;
|
|
_activePrefix = NormalizePrefix(prefix);
|
|
_replaceTarget = null;
|
|
_search = "";
|
|
await LoadAsync(reset: true);
|
|
}
|
|
|
|
private async Task RefreshAsync()
|
|
{
|
|
await LoadAsync(reset: true);
|
|
}
|
|
|
|
private async Task LoadMoreAsync()
|
|
{
|
|
if (_loading || !_hasMore)
|
|
return;
|
|
await LoadAsync(reset: false);
|
|
}
|
|
|
|
private async Task LoadAsync(bool reset)
|
|
{
|
|
if (_loading)
|
|
return;
|
|
|
|
_loading = true;
|
|
_message = null;
|
|
|
|
try
|
|
{
|
|
if (reset)
|
|
{
|
|
_items.Clear();
|
|
_continuationToken = "";
|
|
_hasMore = false;
|
|
}
|
|
|
|
var options = CdnOptionsAccessor.Value;
|
|
var request = new ListObjectsV2Request
|
|
{
|
|
BucketName = options.BucketName,
|
|
Prefix = _activePrefix,
|
|
MaxKeys = 200,
|
|
ContinuationToken = string.IsNullOrWhiteSpace(_continuationToken) ? null : _continuationToken,
|
|
};
|
|
|
|
var response = await S3.ListObjectsV2Async(request);
|
|
var keys = response.S3Objects
|
|
.Where(o => !string.IsNullOrWhiteSpace(o.Key) && !o.Key.EndsWith('/'))
|
|
.Select(o => o.Key)
|
|
.ToList();
|
|
|
|
var aiFlags = keys.Count == 0
|
|
? new Dictionary<string, bool>(StringComparer.Ordinal)
|
|
: await Db.CdnUploadRecords
|
|
.Where(r => keys.Contains(r.ObjectKey))
|
|
.ToDictionaryAsync(r => r.ObjectKey, r => r.IsAiGenerated);
|
|
|
|
foreach (var obj in response.S3Objects)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(obj.Key) || obj.Key.EndsWith('/'))
|
|
continue;
|
|
|
|
_items.Add(new CdnObjectRow
|
|
{
|
|
Key = obj.Key,
|
|
Size = obj.Size,
|
|
LastModified = obj.LastModified.ToUniversalTime(),
|
|
PublicUrl = BuildPublicUrl(obj.Key),
|
|
IsAiGenerated = aiFlags.GetValueOrDefault(obj.Key, false),
|
|
});
|
|
}
|
|
|
|
_continuationToken = response.NextContinuationToken ?? "";
|
|
_hasMore = response.IsTruncated;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_message = $"CDN list failed: {ex.Message}";
|
|
_isError = true;
|
|
}
|
|
finally
|
|
{
|
|
_loading = false;
|
|
}
|
|
}
|
|
|
|
private async Task UploadNewAsync(InputFileChangeEventArgs e)
|
|
{
|
|
var file = e.File;
|
|
if (file is null)
|
|
return;
|
|
|
|
_uploading = true;
|
|
_message = null;
|
|
|
|
try
|
|
{
|
|
var key = $"{_activePrefix}{file.Name}";
|
|
await UploadFileToKeyAsync(key, file); // ETag not propagated for brand-new uploads (no CardAsset row yet)
|
|
await UpsertUploadRecordAsync(key, file.ContentType, _uploadIsAiGenerated);
|
|
_message = $"Uploaded {file.Name}.";
|
|
_isError = false;
|
|
await LoadAsync(reset: true);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_message = $"Upload failed: {ex.Message}";
|
|
_isError = true;
|
|
}
|
|
finally
|
|
{
|
|
_uploading = false;
|
|
}
|
|
}
|
|
|
|
private async Task ReplaceAsync(InputFileChangeEventArgs e)
|
|
{
|
|
var file = e.File;
|
|
if (file is null || _replaceTarget is null)
|
|
return;
|
|
|
|
_replacing = true;
|
|
_message = null;
|
|
|
|
try
|
|
{
|
|
var etag = await UploadFileToKeyAsync(_replaceTarget.Key, file);
|
|
await UpsertUploadRecordAsync(_replaceTarget.Key, file.ContentType, _replaceIsAiGenerated);
|
|
await PropagateAssetETagAsync(_replaceTarget.PublicUrl, etag);
|
|
_message = $"Replaced {_replaceTarget.Key}.";
|
|
_isError = false;
|
|
await LoadAsync(reset: true);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_message = $"Replace failed: {ex.Message}";
|
|
_isError = true;
|
|
}
|
|
finally
|
|
{
|
|
_replacing = false;
|
|
}
|
|
}
|
|
|
|
private async Task DeleteAsync(CdnObjectRow item)
|
|
{
|
|
_deletingKey = item.Key;
|
|
_message = null;
|
|
|
|
try
|
|
{
|
|
var options = CdnOptionsAccessor.Value;
|
|
await S3.DeleteObjectAsync(options.BucketName, item.Key);
|
|
|
|
var record = await Db.CdnUploadRecords.FirstOrDefaultAsync(r => r.ObjectKey == item.Key);
|
|
if (record is not null)
|
|
{
|
|
Db.CdnUploadRecords.Remove(record);
|
|
await Db.SaveChangesAsync();
|
|
}
|
|
|
|
_items.Remove(item);
|
|
if (_replaceTarget?.Key == item.Key)
|
|
_replaceTarget = null;
|
|
|
|
_message = $"Deleted {item.Key}.";
|
|
_isError = false;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_message = $"Delete failed: {ex.Message}";
|
|
_isError = true;
|
|
}
|
|
finally
|
|
{
|
|
_deletingKey = "";
|
|
}
|
|
}
|
|
|
|
private async Task CopyUrlAsync(string url)
|
|
{
|
|
try
|
|
{
|
|
await Js.InvokeVoidAsync("sorAdmin.copyText", url);
|
|
_message = "CDN URL copied to clipboard.";
|
|
_isError = false;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_message = $"Copy failed: {ex.Message}";
|
|
_isError = true;
|
|
}
|
|
}
|
|
|
|
private void SelectReplaceTarget(CdnObjectRow item)
|
|
{
|
|
_replaceTarget = item;
|
|
_replaceIsAiGenerated = item.IsAiGenerated;
|
|
_message = null;
|
|
}
|
|
|
|
private void ClearReplaceTarget()
|
|
{
|
|
_replaceTarget = null;
|
|
_replaceIsAiGenerated = false;
|
|
}
|
|
|
|
/// <summary>Upload <paramref name="file"/> to <paramref name="key"/>; returns the S3 ETag.</summary>
|
|
private async Task<string?> UploadFileToKeyAsync(string key, IBrowserFile file)
|
|
{
|
|
const long maxUploadBytes = 100 * 1024 * 1024;
|
|
|
|
var options = CdnOptionsAccessor.Value;
|
|
await using var stream = file.OpenReadStream(maxUploadBytes);
|
|
|
|
var request = new PutObjectRequest
|
|
{
|
|
BucketName = options.BucketName,
|
|
Key = key,
|
|
InputStream = stream,
|
|
ContentType = string.IsNullOrWhiteSpace(file.ContentType)
|
|
? "application/octet-stream"
|
|
: file.ContentType,
|
|
CannedACL = S3CannedACL.PublicRead,
|
|
DisableDefaultChecksumValidation = true,
|
|
};
|
|
|
|
var response = await S3.PutObjectAsync(request);
|
|
return response.ETag;
|
|
}
|
|
|
|
/// <summary>
|
|
/// After a replace, update ContentETag on any CardAsset whose CdnUrl matches
|
|
/// <paramref name="publicUrl"/> and touch the parent CardDefinition so the
|
|
/// definition_version trigger fires and clients pick up the change.
|
|
/// </summary>
|
|
private async Task PropagateAssetETagAsync(string publicUrl, string? etag)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(etag))
|
|
return;
|
|
|
|
var assets = await Db.Set<SOR.Database.Entities.Catalog.CardAsset>()
|
|
.Include(a => a.CardDefinition)
|
|
.Where(a => a.CdnUrl == publicUrl)
|
|
.ToListAsync();
|
|
|
|
if (assets.Count == 0)
|
|
return;
|
|
|
|
var now = DateTimeOffset.UtcNow;
|
|
foreach (var asset in assets)
|
|
{
|
|
asset.ContentETag = etag;
|
|
asset.CardDefinition.UpdatedAt = now;
|
|
}
|
|
|
|
await Db.SaveChangesAsync();
|
|
}
|
|
|
|
private async Task UpsertUploadRecordAsync(string key, string? contentType, bool isAiGenerated)
|
|
{
|
|
var record = await Db.CdnUploadRecords.FirstOrDefaultAsync(r => r.ObjectKey == key);
|
|
if (record is null)
|
|
{
|
|
record = new SOR.Database.Entities.Catalog.CdnUploadRecord
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
ObjectKey = key,
|
|
};
|
|
Db.CdnUploadRecords.Add(record);
|
|
}
|
|
|
|
record.Prefix = ExtractPrefix(key);
|
|
record.PublicUrl = BuildPublicUrl(key);
|
|
record.ContentType = string.IsNullOrWhiteSpace(contentType) ? null : contentType;
|
|
record.IsAiGenerated = isAiGenerated;
|
|
record.UploadedAt = DateTimeOffset.UtcNow;
|
|
|
|
await Db.SaveChangesAsync();
|
|
}
|
|
|
|
private string NormalizePrefix(string raw)
|
|
{
|
|
var prefix = raw.Trim().Trim('/');
|
|
if (string.IsNullOrWhiteSpace(prefix))
|
|
return string.Empty;
|
|
return prefix + "/";
|
|
}
|
|
|
|
private string BuildPublicUrl(string key)
|
|
{
|
|
var options = CdnOptionsAccessor.Value;
|
|
var baseUrl = options.PublicBaseUrl.TrimEnd('/');
|
|
var bucketSegment = options.BucketName.Trim('/');
|
|
|
|
if (baseUrl.EndsWith("/" + bucketSegment, StringComparison.OrdinalIgnoreCase))
|
|
return $"{baseUrl}/{key}";
|
|
|
|
return $"{baseUrl}/{bucketSegment}/{key}";
|
|
}
|
|
|
|
private string ExtractPrefix(string key)
|
|
{
|
|
var slash = key.LastIndexOf('/');
|
|
return slash <= 0 ? string.Empty : key[..slash];
|
|
}
|
|
|
|
private string FormatBytes(long bytes)
|
|
{
|
|
if (bytes < 1024)
|
|
return $"{bytes} B";
|
|
if (bytes < 1024 * 1024)
|
|
return $"{bytes / 1024.0:F1} KB";
|
|
if (bytes < 1024 * 1024 * 1024)
|
|
return $"{bytes / 1024.0 / 1024.0:F1} MB";
|
|
return $"{bytes / 1024.0 / 1024.0 / 1024.0:F1} GB";
|
|
}
|
|
|
|
private sealed class CdnObjectRow
|
|
{
|
|
public string Key { get; set; } = string.Empty;
|
|
public long Size { get; set; }
|
|
public DateTime LastModified { get; set; }
|
|
public string PublicUrl { get; set; } = string.Empty;
|
|
public bool IsAiGenerated { get; set; }
|
|
}
|
|
}
|