fix: change upload name

This commit is contained in:
2026-09-15 22:23:54 +07:00
parent 4078bf01f4
commit 637ca51f47
11 changed files with 62 additions and 46 deletions
@@ -15,35 +15,45 @@ public class S3DocumentContentStore(
{
private string Bucket => opt.Value.Bucket;
private static string Key(Guid projectId, Guid documentId) => $"{projectId:N}/{documentId:N}.html";
public Task EnsureBucketAsync(Guid projectId, CancellationToken ct) => Task.CompletedTask;
public async Task UploadAsync(Guid projectId, Guid documentId, string contentType, Stream body, CancellationToken ct)
public static string FileNameNow(string name)
{
var slug = new string(name.Trim().ToLowerInvariant()
.Select(c => char.IsLetterOrDigit(c) ? c : '-')
.ToArray()).Trim('-');
if (slug.Length == 0)
{
slug = "document";
}
return $"{slug}_{DateTime.UtcNow:yyyyMMddHHmmss}";
}
public async Task<string> UploadAsync(Guid projectId, string fileName, string contentType, Stream body, CancellationToken ct)
{
var key = $"{projectId:N}/{FileNameNow(fileName)}.html";
try
{
await s3.PutObjectAsync(new PutObjectRequest
{
BucketName = Bucket,
Key = Key(projectId, documentId),
Key = key,
InputStream = body,
ContentType = contentType,
AutoCloseStream = false,
}, ct);
return key;
}
catch (AmazonS3Exception ex)
{
log.LogError(ex, "MinIO upload failed for document {DocumentId}", documentId);
log.LogError(ex, "MinIO upload failed for {Key}", key);
throw new StorageUnavailableException("Document upload failed", ex);
}
}
public async Task<Stream> DownloadAsync(Guid projectId, Guid documentId, CancellationToken ct)
public async Task<Stream> DownloadAsync(string storageKey, CancellationToken ct)
{
try
{
var response = await s3.GetObjectAsync(Bucket, Key(projectId, documentId), ct);
var response = await s3.GetObjectAsync(Bucket, storageKey, ct);
return response.ResponseStream;
}
catch (AmazonS3Exception ex) when (ex.StatusCode == HttpStatusCode.NotFound)
@@ -52,33 +62,20 @@ public class S3DocumentContentStore(
}
catch (AmazonS3Exception ex)
{
log.LogError(ex, "MinIO download failed for document {DocumentId}", documentId);
log.LogError(ex, "MinIO download failed for {Key}", storageKey);
throw new StorageUnavailableException("Document download failed", ex);
}
}
public async Task<bool> ExistsAsync(Guid projectId, Guid documentId, CancellationToken ct)
public async Task DeleteAsync(string storageKey, CancellationToken ct)
{
try
{
await s3.GetObjectMetadataAsync(Bucket, Key(projectId, documentId), ct);
return true;
}
catch (AmazonS3Exception ex) when (ex.StatusCode == HttpStatusCode.NotFound)
{
return false;
}
}
public async Task DeleteAsync(Guid projectId, Guid documentId, CancellationToken ct)
{
try
{
await s3.DeleteObjectAsync(Bucket, Key(projectId, documentId), ct);
await s3.DeleteObjectAsync(Bucket, storageKey, ct);
}
catch (AmazonS3Exception ex)
{
log.LogWarning(ex, "MinIO delete failed for document {DocumentId}", documentId);
log.LogWarning(ex, "MinIO delete failed for {Key}", storageKey);
}
}
}