Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 115 additions & 3 deletions src/Essentials/src/FileSystem/FileSystem.uwp.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Threading.Tasks;
using Microsoft.Maui.ApplicationModel;
using Windows.Storage;
Expand Down Expand Up @@ -84,11 +88,88 @@ Task<bool> PlatformAppPackageFileExistsAsync(string filename)

public partial class FileBase
{
// Static mapping for file extensions to MIME types
// Used as fallback when Windows StorageFile.ContentType doesn't provide correct MIME type
static readonly FrozenDictionary<string, string> ExtensionToMimeTypeMap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
// Image formats
{ ".jpg", MediaTypeNames.Image.Jpeg },
{ ".jpeg", MediaTypeNames.Image.Jpeg },
{ ".png", "image/png" },
{ ".gif", MediaTypeNames.Image.Gif },
{ ".bmp", "image/bmp" },
{ ".svg", "image/svg+xml" },
{ ".webp", "image/webp" },
{ ".tiff", MediaTypeNames.Image.Tiff },
{ ".tif", MediaTypeNames.Image.Tiff },
{ ".ico", "image/x-icon" },

// Audio formats
{ ".mp3", "audio/mpeg" },
{ ".wav", "audio/wav" },
{ ".flac", "audio/flac" },
{ ".aac", "audio/aac" },
{ ".ogg", "audio/ogg" },
{ ".wma", "audio/x-ms-wma" },

// Video formats
{ ".mp4", "video/mp4" },
{ ".avi", "video/x-msvideo" },
{ ".mov", "video/quicktime" },
{ ".wmv", "video/x-ms-wmv" },
{ ".webm", "video/webm" },
{ ".mkv", "video/x-matroska" },
{ ".flv", "video/x-flv" },

// Document formats
{ ".pdf", MediaTypeNames.Application.Pdf },
{ ".doc", "application/msword" },
{ ".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document" },
{ ".xls", "application/vnd.ms-excel" },
{ ".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" },
{ ".ppt", "application/vnd.ms-powerpoint" },
{ ".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation" },
{ ".txt", MediaTypeNames.Text.Plain },
{ ".rtf", MediaTypeNames.Application.Rtf },

// Web formats
{ ".html", MediaTypeNames.Text.Html },
{ ".htm", MediaTypeNames.Text.Html },
{ ".css", "text/css" },
{ ".js", "application/javascript" },
{ ".json", "application/json" },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MartyIX , Thanks for suggestion, I've modified the changes

{ ".xml", MediaTypeNames.Text.Xml },

// Archive formats
{ ".zip", MediaTypeNames.Application.Zip },
{ ".rar", "application/x-rar-compressed" },
{ ".7z", "application/x-7z-compressed" },
{ ".tar", "application/x-tar" },
{ ".tar.gz", "application/gzip" },
{ ".gz", "application/gzip" }
}.ToFrozenDictionary(StringComparer.OrdinalIgnoreCase);

internal FileBase(IStorageFile file)
: this(file?.Path)
{
File = file;
ContentType = file?.ContentType;

// Set the ContentType, but prefer our mapping for known problematic extensions
var fileContentType = file?.ContentType;
var extension = Path.GetExtension(file?.Path ?? "");

// If Windows provides a generic "application/octet-stream" or empty ContentType,
// try to get a more specific MIME type from our extension mapping
if (string.IsNullOrWhiteSpace(fileContentType) ||
fileContentType == "application/octet-stream")
{
var betterContentType = PlatformGetContentType(extension);
ContentType = betterContentType ?? fileContentType;
}
else
{
ContentType = fileContentType;
}
}

void PlatformInit(FileBase file)
Expand All @@ -98,8 +179,39 @@ void PlatformInit(FileBase file)

internal IStorageFile File { get; set; }

// we can't do anything here, but Windows will take care of it
string PlatformGetContentType(string extension) => null;
// Use extension mapping as fallback when Windows doesn't provide correct MIME type
string PlatformGetContentType(string extension)
{
if (string.IsNullOrWhiteSpace(extension))
return null;

// Trim whitespace and convert to lowercase for consistent matching
extension = extension.Trim().ToLowerInvariant();
if (string.IsNullOrEmpty(extension))
return null;

if (!extension.StartsWith("."))
extension = "." + extension;

// Try exact match first
if (ExtensionToMimeTypeMap.TryGetValue(extension, out var mimeType))
return mimeType;

var fileName = Path.GetFileNameWithoutExtension(extension);
if (!string.IsNullOrEmpty(fileName) && fileName.Contains('.', StringComparison.Ordinal))
{
// Try progressively longer extensions (e.g., .tar.gz, then .gz)
var parts = fileName.Split('.');
for (int i = parts.Length - 1; i >= 0; i--)
{
var compoundExtension = "." + string.Join(".", parts.Skip(i)) + extension;
if (ExtensionToMimeTypeMap.TryGetValue(compoundExtension, out mimeType))
return mimeType;
}
}

return null;
}

internal virtual Task<Stream> PlatformOpenReadAsync() =>
File.OpenStreamForReadAsync();
Expand Down
24 changes: 24 additions & 0 deletions src/Essentials/test/DeviceTests/Tests/FileSystem_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,29 @@ public async Task ValidateMIMEFormat()
File.Delete(filePath);
}
#endif

#if WINDOWS
[Theory]
[InlineData(".jpg", "image/jpeg")]
[InlineData(".JPG", "image/jpeg")]
[InlineData(".Jpg", "image/jpeg")]
[InlineData(".jPg", "image/jpeg")]
[InlineData(".jpg", "image/jpeg")]
[InlineData(".jpg ", "image/jpeg")] // Trailing space
[InlineData(" .jpg", "image/jpeg")] // Leading space
[InlineData(" .jpg ", "image/jpeg")] // Leading and trailing spaces
[InlineData(".png", "image/png")]
[InlineData(".PNG", "image/png")]
[InlineData(".tar.gz", "application/gzip")]
[InlineData(".TAR.GZ", "application/gzip")]
public async Task EnsureFileResultContentType(string extension, string expectedMimeType)
{
string filePath = Path.Combine(FileSystem.CacheDirectory, $"test{extension}");
await File.WriteAllTextAsync(filePath, $"File Content type is {expectedMimeType}");
FileResult fileResult = new FileResult(filePath);
Assert.Equal(expectedMimeType, fileResult.ContentType);
File.Delete(filePath);
}
#endif
}
}