using System.Diagnostics.CodeAnalysis; using System.Text; namespace Houston.Model.Documents; /// /// Full S3 object key of a customer document, including the customer/organization prefix. /// public readonly record struct DocumentKey(OrgSlug Org, DocumentType? Type, DocumentName Name) { public static bool TryFromS3Key(string key, [NotNullWhen(true)] out DocumentKey? result) { result = null; if (key.EndsWith('/')) return false; var parts = key.Split('/', StringSplitOptions.RemoveEmptyEntries); if (parts.Contains("..")) return false; result = parts switch { [var org, var name] when OrgSlug.TryFromFolderName(org, out var slug) => new(slug.Value, null, new(name)), [var org, var type, var name] when OrgSlug.TryFromFolderName(org, out var slug) => new(slug.Value, DocumentTypeExtensions.ParseStorageFolderName(type), new(name)), _ => null, }; return result is not null; } public override string ToString() { StringBuilder result = new(); result.Append(Org.ToString()); if (Type is { } type) result.Append(type.StorageFolderName).Append('/'); result.Append(Name); return result.ToString(); } }