fix: upload folder name
This commit is contained in:
@@ -62,10 +62,11 @@ public class CreateDocumentHandler(IUnitOfWork uow, IMapper mapper, IDocumentCon
|
|||||||
|
|
||||||
if (request.Type == DocumentType.Document && !string.IsNullOrEmpty(request.Content))
|
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);
|
var bytes = Encoding.UTF8.GetBytes(request.Content);
|
||||||
using (var ms = new MemoryStream(bytes))
|
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;
|
doc.ContentSize = bytes.Length;
|
||||||
|
|||||||
@@ -38,11 +38,12 @@ public class UpdateDocumentHandler(IUnitOfWork uow, IMapper mapper, IDocumentCon
|
|||||||
|
|
||||||
if (doc.Type == DocumentType.Document)
|
if (doc.Type == DocumentType.Document)
|
||||||
{
|
{
|
||||||
|
var projectName = (await uow.Projects.GetByIdAsync(doc.ProjectId, ct))?.Name ?? doc.ProjectId.ToString();
|
||||||
var previousKey = doc.StorageKey;
|
var previousKey = doc.StorageKey;
|
||||||
var bytes = Encoding.UTF8.GetBytes(request.Content ?? "");
|
var bytes = Encoding.UTF8.GetBytes(request.Content ?? "");
|
||||||
using (var ms = new MemoryStream(bytes))
|
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)
|
if (!string.IsNullOrEmpty(previousKey) && previousKey != doc.StorageKey)
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ namespace mws.backend.dotnet.application.Documents;
|
|||||||
|
|
||||||
public interface IDocumentContentStore
|
public interface IDocumentContentStore
|
||||||
{
|
{
|
||||||
Task<string> UploadAsync(Guid projectId, string fileName, string contentType, Stream body, CancellationToken ct);
|
Task<string> UploadAsync(string projectName, string fileName, string contentType, Stream body, CancellationToken ct);
|
||||||
Task<Stream> DownloadAsync(string storageKey, CancellationToken ct);
|
Task<Stream> DownloadAsync(string storageKey, CancellationToken ct);
|
||||||
Task DeleteAsync(string storageKey, CancellationToken ct);
|
Task DeleteAsync(string storageKey, CancellationToken ct);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,20 +16,21 @@ public class S3DocumentContentStore(
|
|||||||
private string Bucket => opt.Value.Bucket;
|
private string Bucket => opt.Value.Bucket;
|
||||||
|
|
||||||
public static string FileNameNow(string name)
|
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()
|
var slug = new string(name.Trim().ToLowerInvariant()
|
||||||
.Select(c => char.IsLetterOrDigit(c) ? c : '-')
|
.Select(c => char.IsLetterOrDigit(c) ? c : '-')
|
||||||
.ToArray()).Trim('-');
|
.ToArray()).Trim('-');
|
||||||
if (slug.Length == 0)
|
return slug.Length == 0 ? "document" : slug;
|
||||||
{
|
|
||||||
slug = "document";
|
|
||||||
}
|
|
||||||
return $"{slug}_{DateTime.UtcNow:yyyyMMddHHmmss}";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<string> UploadAsync(Guid projectId, string fileName, string contentType, Stream body, CancellationToken ct)
|
public async Task<string> 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
|
try
|
||||||
{
|
{
|
||||||
await s3.PutObjectAsync(new PutObjectRequest
|
await s3.PutObjectAsync(new PutObjectRequest
|
||||||
|
|||||||
Reference in New Issue
Block a user