CDN flag change
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
</div>
|
||||
|
||||
<p class="text-muted">
|
||||
All assets and animations flagged as AI-generated across the card catalog.
|
||||
All catalog assets, animation bindings, and generic CDN uploads flagged as AI-generated.
|
||||
</p>
|
||||
|
||||
@if (_loading)
|
||||
@@ -24,6 +24,54 @@
|
||||
}
|
||||
else
|
||||
{
|
||||
<!-- Generic CDN Uploads -->
|
||||
<div class="card shadow-sm mb-4">
|
||||
<div class="card-header fw-medium d-flex justify-content-between align-items-center">
|
||||
<span>
|
||||
<i class="bi bi-hdd-network me-2"></i>Generic CDN Uploads
|
||||
<span class="badge bg-warning text-dark ms-2">@_cdnUploads.Count AI-generated</span>
|
||||
</span>
|
||||
</div>
|
||||
<div class="card-body p-0">
|
||||
@if (_cdnUploads.Count == 0)
|
||||
{
|
||||
<p class="text-muted p-3 mb-0">No AI-generated generic CDN uploads.</p>
|
||||
}
|
||||
else
|
||||
{
|
||||
<table class="table table-sm table-hover mb-0">
|
||||
<thead class="table-light">
|
||||
<tr>
|
||||
<th>Prefix</th>
|
||||
<th>Object Key</th>
|
||||
<th>MIME</th>
|
||||
<th>CDN URL</th>
|
||||
<th>Uploaded</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var item in _cdnUploads)
|
||||
{
|
||||
<tr>
|
||||
<td><span class="badge bg-secondary">@(!string.IsNullOrWhiteSpace(item.Prefix) ? item.Prefix : "/")</span></td>
|
||||
<td class="font-monospace small">@item.ObjectKey</td>
|
||||
<td class="text-muted small">@(!string.IsNullOrWhiteSpace(item.ContentType) ? item.ContentType : "—")</td>
|
||||
<td>
|
||||
<a href="@item.PublicUrl" target="_blank"
|
||||
class="text-decoration-none small font-monospace">
|
||||
@TruncateUrl(item.PublicUrl)
|
||||
<i class="bi bi-box-arrow-up-right ms-1"></i>
|
||||
</a>
|
||||
</td>
|
||||
<td class="text-muted small">@item.UploadedAt.ToString("yyyy-MM-dd")</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Static Assets -->
|
||||
<div class="card shadow-sm mb-4">
|
||||
<div class="card-header fw-medium d-flex justify-content-between align-items-center">
|
||||
@@ -166,8 +214,16 @@ else
|
||||
string? ActionName,
|
||||
string? CdnUrl);
|
||||
|
||||
private record CdnUploadRow(
|
||||
string Prefix,
|
||||
string ObjectKey,
|
||||
string? ContentType,
|
||||
string PublicUrl,
|
||||
DateTimeOffset UploadedAt);
|
||||
|
||||
private List<AssetRow> _assets = new();
|
||||
private List<AnimationRow> _animations = new();
|
||||
private List<CdnUploadRow> _cdnUploads = new();
|
||||
|
||||
protected override async Task OnInitializedAsync() => await LoadAsync();
|
||||
|
||||
@@ -203,6 +259,18 @@ else
|
||||
a.AnimationCdnUrl))
|
||||
.ToListAsync();
|
||||
|
||||
_cdnUploads = await Db.CdnUploadRecords
|
||||
.Where(r => r.IsAiGenerated)
|
||||
.OrderBy(r => r.Prefix)
|
||||
.ThenBy(r => r.ObjectKey)
|
||||
.Select(r => new CdnUploadRow(
|
||||
r.Prefix,
|
||||
r.ObjectKey,
|
||||
r.ContentType,
|
||||
r.PublicUrl,
|
||||
r.UploadedAt))
|
||||
.ToListAsync();
|
||||
|
||||
_loading = false;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,11 +3,14 @@
|
||||
@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>
|
||||
|
||||
@@ -88,6 +91,10 @@
|
||||
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)
|
||||
{
|
||||
@@ -118,6 +125,10 @@
|
||||
<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)
|
||||
@@ -165,6 +176,7 @@
|
||||
<thead class="sticky-top bg-body">
|
||||
<tr>
|
||||
<th>Key</th>
|
||||
<th style="width:90px">AI</th>
|
||||
<th style="width:110px">Size</th>
|
||||
<th style="width:180px">Modified</th>
|
||||
<th style="width:270px">Actions</th>
|
||||
@@ -176,6 +188,16 @@
|
||||
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">No</span>
|
||||
}
|
||||
</td>
|
||||
<td class="align-middle">@FormatBytes(row.Size)</td>
|
||||
<td class="align-middle text-muted small">@row.LastModified.ToString("yyyy-MM-dd HH:mm UTC")</td>
|
||||
<td class="align-middle">
|
||||
@@ -251,6 +273,8 @@
|
||||
private bool _loading;
|
||||
private bool _uploading;
|
||||
private bool _replacing;
|
||||
private bool _uploadIsAiGenerated;
|
||||
private bool _replaceIsAiGenerated;
|
||||
private bool _hasMore;
|
||||
private CdnObjectRow? _replaceTarget;
|
||||
|
||||
@@ -317,6 +341,17 @@
|
||||
};
|
||||
|
||||
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('/'))
|
||||
@@ -328,6 +363,7 @@
|
||||
Size = obj.Size,
|
||||
LastModified = obj.LastModified.ToUniversalTime(),
|
||||
PublicUrl = BuildPublicUrl(obj.Key),
|
||||
IsAiGenerated = aiFlags.GetValueOrDefault(obj.Key, false),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -358,6 +394,7 @@
|
||||
{
|
||||
var key = $"{_activePrefix}{file.Name}";
|
||||
await UploadFileToKeyAsync(key, file);
|
||||
await UpsertUploadRecordAsync(key, file.ContentType, _uploadIsAiGenerated);
|
||||
_message = $"Uploaded {file.Name}.";
|
||||
_isError = false;
|
||||
await LoadAsync(reset: true);
|
||||
@@ -385,6 +422,7 @@
|
||||
try
|
||||
{
|
||||
await UploadFileToKeyAsync(_replaceTarget.Key, file);
|
||||
await UpsertUploadRecordAsync(_replaceTarget.Key, file.ContentType, _replaceIsAiGenerated);
|
||||
_message = $"Replaced {_replaceTarget.Key}.";
|
||||
_isError = false;
|
||||
await LoadAsync(reset: true);
|
||||
@@ -410,6 +448,13 @@
|
||||
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;
|
||||
@@ -446,12 +491,14 @@
|
||||
private void SelectReplaceTarget(CdnObjectRow item)
|
||||
{
|
||||
_replaceTarget = item;
|
||||
_replaceIsAiGenerated = item.IsAiGenerated;
|
||||
_message = null;
|
||||
}
|
||||
|
||||
private void ClearReplaceTarget()
|
||||
{
|
||||
_replaceTarget = null;
|
||||
_replaceIsAiGenerated = false;
|
||||
}
|
||||
|
||||
private async Task UploadFileToKeyAsync(string key, IBrowserFile file)
|
||||
@@ -476,6 +523,28 @@
|
||||
await S3.PutObjectAsync(request);
|
||||
}
|
||||
|
||||
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('/');
|
||||
@@ -486,7 +555,20 @@
|
||||
|
||||
private string BuildPublicUrl(string key)
|
||||
{
|
||||
return $"{CdnOptionsAccessor.Value.PublicBaseUrl.TrimEnd('/')}/{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)
|
||||
@@ -506,5 +588,6 @@
|
||||
public long Size { get; set; }
|
||||
public DateTime LastModified { get; set; }
|
||||
public string PublicUrl { get; set; } = string.Empty;
|
||||
public bool IsAiGenerated { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user