diff --git a/application/Documents/Commands/CreateDocument.cs b/application/Documents/Commands/CreateDocument.cs index 37f6105..fc4f068 100644 --- a/application/Documents/Commands/CreateDocument.cs +++ b/application/Documents/Commands/CreateDocument.cs @@ -62,10 +62,11 @@ public class CreateDocumentHandler(IUnitOfWork uow, IMapper mapper, IDocumentCon if (request.Type == DocumentType.Document && !string.IsNullOrEmpty(request.Content)) { + var projectName = (await uow.Projects.GetByIdAsync(doc.ProjectId, ct))?.Name ?? doc.ProjectId.ToString(); var bytes = Encoding.UTF8.GetBytes(request.Content); using (var ms = new MemoryStream(bytes)) { - doc.StorageKey = await contentStore.UploadAsync(doc.ProjectId, doc.Title, "text/html; charset=utf-8", ms, ct); + doc.StorageKey = await contentStore.UploadAsync(projectName, doc.Title, "text/html; charset=utf-8", ms, ct); } doc.ContentSize = bytes.Length; diff --git a/application/Documents/Commands/UpdateDocument.cs b/application/Documents/Commands/UpdateDocument.cs index fd69471..5858f15 100644 --- a/application/Documents/Commands/UpdateDocument.cs +++ b/application/Documents/Commands/UpdateDocument.cs @@ -38,11 +38,12 @@ public class UpdateDocumentHandler(IUnitOfWork uow, IMapper mapper, IDocumentCon if (doc.Type == DocumentType.Document) { + var projectName = (await uow.Projects.GetByIdAsync(doc.ProjectId, ct))?.Name ?? doc.ProjectId.ToString(); var previousKey = doc.StorageKey; var bytes = Encoding.UTF8.GetBytes(request.Content ?? ""); using (var ms = new MemoryStream(bytes)) { - doc.StorageKey = await contentStore.UploadAsync(doc.ProjectId, doc.Title, "text/html; charset=utf-8", ms, ct); + doc.StorageKey = await contentStore.UploadAsync(projectName, doc.Title, "text/html; charset=utf-8", ms, ct); } if (!string.IsNullOrEmpty(previousKey) && previousKey != doc.StorageKey) diff --git a/application/Documents/IDocumentContentStore.cs b/application/Documents/IDocumentContentStore.cs index 7cf67d2..05b3150 100644 --- a/application/Documents/IDocumentContentStore.cs +++ b/application/Documents/IDocumentContentStore.cs @@ -2,7 +2,7 @@ namespace mws.backend.dotnet.application.Documents; public interface IDocumentContentStore { - Task UploadAsync(Guid projectId, string fileName, string contentType, Stream body, CancellationToken ct); + Task UploadAsync(string projectName, string fileName, string contentType, Stream body, CancellationToken ct); Task DownloadAsync(string storageKey, CancellationToken ct); Task DeleteAsync(string storageKey, CancellationToken ct); } diff --git a/infrastructure/Storage/S3DocumentContentStore.cs b/infrastructure/Storage/S3DocumentContentStore.cs index b425b43..a35786c 100644 --- a/infrastructure/Storage/S3DocumentContentStore.cs +++ b/infrastructure/Storage/S3DocumentContentStore.cs @@ -16,20 +16,21 @@ public class S3DocumentContentStore( private string Bucket => opt.Value.Bucket; public static string FileNameNow(string name) + { + return $"{Slug(name)}_{DateTime.UtcNow:yyyyMMddHHmmss}"; + } + + private static string Slug(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}"; + return slug.Length == 0 ? "document" : slug; } - public async Task UploadAsync(Guid projectId, string fileName, string contentType, Stream body, CancellationToken ct) + public async Task UploadAsync(string projectName, string fileName, string contentType, Stream body, CancellationToken ct) { - var key = $"{projectId:N}/{FileNameNow(fileName)}.html"; + var key = $"{Slug(projectName)}/{FileNameNow(fileName)}.html"; try { await s3.PutObjectAsync(new PutObjectRequest