@page "/content/cdn" @attribute [Authorize(Roles = "Admin,SuperAdmin")] @using Amazon.S3 @using Amazon.S3.Model @using Microsoft.AspNetCore.Components.Forms @using Microsoft.Extensions.Options @using SOR.Cdn @inject IAmazonS3 S3 @inject IOptions CdnOptionsAccessor @inject IJSRuntime Js CDN Browser - SOR Admin

CDN Browser

@_bucketLabel
@if (_message != null) {
@_message
}
Browse Prefix
Leave blank to browse the bucket root.
Quick Prefixes
@foreach (var prefix in _knownPrefixes) { var selected = string.Equals(_activePrefix.TrimEnd('/'), prefix.Key, StringComparison.OrdinalIgnoreCase); }
Prefix Guide
  • data - client config and UI-driven data files
  • graphics - icons and UI art
  • cardart - 2D card art, front and back
  • sfx - sound effects
  • music - background music
  • models - 3D card, UI, and tile models
  • animations - 3D animation assets
Upload New File
Upload destination: @(_activePrefixDisplay)
Replace File
@if (_replaceTarget is null) {
Select a file from the list, then choose a replacement upload.
} else {
Target Key
@_replaceTarget.Key
Replaces the selected object at the same CDN path.
}
Objects @_items.Count @_activePrefixDisplay
@if (_loading && _items.Count == 0) {
Loading...
} else if (_items.Count == 0) {
No objects found for this prefix.
} else {
@foreach (var item in _items) { var row = item; }
Key Size Modified Actions
@row.Key @FormatBytes(row.Size) @row.LastModified.ToString("yyyy-MM-dd HH:mm UTC")
View
}
@if (_hasMore) { }
@code { private readonly List _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 _hasMore; private CdnObjectRow? _replaceTarget; private string _activePrefixDisplay => string.IsNullOrEmpty(_activePrefix) ? "/" : $"/{_activePrefix}"; 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; await LoadAsync(reset: true); } private async Task SelectKnownPrefixAsync(string prefix) { _prefixInput = prefix; _activePrefix = NormalizePrefix(prefix); _replaceTarget = null; 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); 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), }); } _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); _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 { await UploadFileToKeyAsync(_replaceTarget.Key, file); _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); _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; _message = null; } private void ClearReplaceTarget() { _replaceTarget = null; } private async Task 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, }; await S3.PutObjectAsync(request); } private string NormalizePrefix(string raw) { var prefix = raw.Trim().Trim('/'); if (string.IsNullOrWhiteSpace(prefix)) return string.Empty; return prefix + "/"; } private string BuildPublicUrl(string key) { return $"{CdnOptionsAccessor.Value.PublicBaseUrl.TrimEnd('/')}/{key}"; } 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; } }