Files
sor/Server/src/SOR.Cdn/HttpCdnClient.cs
T
8d85f76180 Fix: unique CDN URLs per upload to bust browser + disk model cache
Uploading a replacement GLB with changed animation names to the same
folder/fileName path silently overwrote the S3 object but left the URL
unchanged.  ModelCache keyed its disk cache and in-memory cache by URL,
so all clients continued loading the stale model indefinitely.

Fix (CardEditor.razor):
- Asset uploads: append _yyyyMMddHHmmss timestamp to the filename stem
  before calling UploadAsync, e.g. merlock_20260316_143022.glb.
- Animation uploads: same treatment.
- Result: every upload produces a unique URL; the card asset record is
  updated to that URL; ModelCache sees a new key and fetches fresh bytes.

Fix (HttpCdnClient.cs):
- Set Cache-Control: public, max-age=31536000, immutable on uploaded
  objects.  Since keys are now unique-per-upload, aggressive caching is
  safe and avoids unnecessary re-downloads of unchanged assets.

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>
2026-03-16 12:05:28 -04:00

81 lines
2.6 KiB
C#

namespace SOR.Cdn;
using Amazon.S3;
using Amazon.S3.Model;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
/// <summary>
/// S3-compatible CDN client backed by Ceph RadosGW.
///
/// Uploads objects with public-read ACL; the public URL is built as:
/// {PublicBaseUrl}/{folder}/{fileName}
///
/// Requires ForcePathStyle=true for Ceph (virtual-hosted bucket style is not supported
/// without explicit DNS configuration).
/// </summary>
public sealed class S3CdnClient : ICdnClient
{
private readonly IAmazonS3 _s3;
private readonly CdnOptions _options;
private readonly ILogger<S3CdnClient> _logger;
public S3CdnClient(IAmazonS3 s3, IOptions<CdnOptions> options, ILogger<S3CdnClient> logger)
{
_s3 = s3;
_options = options.Value;
_logger = logger;
}
public async Task<CdnUploadResult> UploadAsync(
string folder,
string fileName,
string contentType,
Stream data,
CancellationToken ct = default)
{
var key = $"{folder.Trim('/')}/{fileName}";
try
{
var request = new PutObjectRequest
{
BucketName = _options.BucketName,
Key = key,
InputStream = data,
ContentType = contentType,
CannedACL = S3CannedACL.PublicRead,
DisableDefaultChecksumValidation = true,
};
// Immutable URLs: each upload gets a unique timestamped key, so browsers
// and CDN edges can cache aggressively without ever serving stale bytes.
request.Headers["Cache-Control"] = "public, max-age=31536000, immutable";
await _s3.PutObjectAsync(request, ct);
var publicUrl = $"{_options.PublicBaseUrl.TrimEnd('/')}/{key}";
_logger.LogInformation("CDN upload succeeded: {Url}", publicUrl);
return new CdnUploadResult(true, key, publicUrl, null);
}
catch (Exception ex)
{
_logger.LogError(ex, "CDN upload failed for {Folder}/{FileName}", folder, fileName);
return new CdnUploadResult(false, null, null, ex.Message);
}
}
public async Task<bool> DeleteAsync(string path, CancellationToken ct = default)
{
try
{
await _s3.DeleteObjectAsync(_options.BucketName, path, ct);
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "CDN delete failed for path {Path}", path);
return false;
}
}
}