Skip to content

Commit 9ead1fb

Browse files
author
aden.chen
committed
Handle URLs in GetFileContentType and add null check
Improved GetFileContentType to extract the path from URLs before resolving content type, ensuring correct handling of query strings. Added a check to return an empty string if fileName is null or empty.
1 parent 14db7f1 commit 9ead1fb

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

src/Infrastructure/BotSharp.Abstraction/Files/Utilities/FileUtility.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,19 @@ public static BinaryData BuildBinaryDataFromFile(IFormFile file)
6565

6666
public static string GetFileContentType(string fileName)
6767
{
68+
if (string.IsNullOrEmpty(fileName))
69+
{
70+
return string.Empty;
71+
}
72+
73+
// For URLs (e.g. signed S3/CloudFront URLs with query strings), extract the path portion
74+
// so that FileExtensionContentTypeProvider can correctly resolve the extension.
75+
if (Uri.TryCreate(fileName, UriKind.Absolute, out var uri)
76+
&& (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps))
77+
{
78+
fileName = uri.AbsolutePath;
79+
}
80+
6881
string contentType;
6982
var provider = new FileExtensionContentTypeProvider();
7083
if (!provider.TryGetContentType(fileName, out contentType))

0 commit comments

Comments
 (0)