Feature: ETag-based CDN model cache invalidation
When admin replaces a model/art file via the CDN browser, the S3 ETag is now captured and stored on CardAsset.ContentETag. The ETag is included in the content sync DTO (CardAssetDto) so clients receive it on every sync. Client-side ModelCache now keys its disk cache on sha256(url + "#" + etag) instead of sha256(url), so updating a file at the same CDN path busts the disk cache automatically. A _CACHE_VER="v2" sentinel purges old-scheme .bin files on first launch after the client update. Changes: - CdnUploadResult: add string? ETag field - S3CdnClient: capture ETag from PutObjectAsync response - CardAsset: add string? ContentETag property - Migration 20260318000001_AddAssetContentETag: add content_etag text column - ContentMessages: add string? ContentETag to CardAssetDto - ContentDtoMapper: map ContentETag in CardAsset → CardAssetDto - CdnBrowser.razor: UploadFileToKeyAsync returns Task<string?> (ETag); ReplaceAsync calls PropagateAssetETagAsync which updates CardAsset rows and touches CardDefinition.UpdatedAt to trigger a version bump - ModelCache.gd: versioned disk key + cache migration + etag parameter on load_model() and evict() - game_screen.gd: pass model_asset.contentETag to load_model / retry chain 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>
This commit is contained in:
@@ -578,7 +578,7 @@ window.sorCdn = {
|
||||
try
|
||||
{
|
||||
var key = $"{_activePrefix}{file.Name}";
|
||||
await UploadFileToKeyAsync(key, file);
|
||||
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;
|
||||
@@ -606,8 +606,9 @@ window.sorCdn = {
|
||||
|
||||
try
|
||||
{
|
||||
await UploadFileToKeyAsync(_replaceTarget.Key, file);
|
||||
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);
|
||||
@@ -686,7 +687,8 @@ window.sorCdn = {
|
||||
_replaceIsAiGenerated = false;
|
||||
}
|
||||
|
||||
private async Task UploadFileToKeyAsync(string key, IBrowserFile file)
|
||||
/// <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;
|
||||
|
||||
@@ -705,7 +707,36 @@ window.sorCdn = {
|
||||
DisableDefaultChecksumValidation = true,
|
||||
};
|
||||
|
||||
await S3.PutObjectAsync(request);
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user