Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
Updated version to 6.0.0-preview-0320
  • Loading branch information
richieedwards committed Jun 28, 2024
1 parent 2589b50 commit de3f142
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 22 deletions.
9 changes: 5 additions & 4 deletions Core/src/Umbrella.Utilities/Email/EmailAttachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Umbrella.Utilities.Email;
/// </summary>
public class EmailAttachment : IDisposable
{
private const string DefaultContentType = "application/octet-stream";
private bool _disposedValue;

/// <summary>
Expand All @@ -31,11 +32,11 @@ public class EmailAttachment : IDisposable
/// <param name="fileName">The file name.</param>
/// <param name="content">The content.</param>
/// <param name="contentType">The content type.</param>
public EmailAttachment(string fileName, Stream content, string contentType = "application/octet-stream")
public EmailAttachment(string fileName, Stream content, string? contentType)
{
FileName = fileName;
Content = content;
ContentType = contentType;
ContentType = contentType ?? DefaultContentType;
}

/// <summary>
Expand All @@ -44,11 +45,11 @@ public EmailAttachment(string fileName, Stream content, string contentType = "ap
/// <param name="fileName">The file name.</param>
/// <param name="content">The content.</param>
/// <param name="contentType">The content type.</param>
public EmailAttachment(string fileName, byte[] content, string contentType = "application/octet-stream")
public EmailAttachment(string fileName, byte[] content, string? contentType)
{
FileName = fileName;
Content = new MemoryStream(content);
ContentType = contentType;
ContentType = contentType ?? DefaultContentType;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
/// </summary>
public interface IMimeTypeUtility
{
string? GetFileExtension(string mimeType);
/// <summary>
/// Gets the file extension for the specified mime type. If the extension cannot be identified then null will be returned.
/// </summary>
/// <param name="mimeType">The mime type.</param>
/// <returns>The file extension or null if the extension cannot be identified.</returns>
string? GetFileExtension(string? mimeType);

/// <summary>
/// Gets the MIME Type of the specified filename or extension.
Expand Down
20 changes: 4 additions & 16 deletions Core/src/Umbrella.Utilities/Mime/MimeTypeUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ public MimeTypeUtility(ILogger<MimeTypeUtility> logger)
_logger = logger;
}

/// <summary>
/// Gets the MIME Type of the specified filename or extension.
/// If the extension cannot be identified then the default type of application/octet-stream will be returned.
/// </summary>
/// <param name="fileNameOrExtension">The file name or extension.</param>
/// <exception cref="UmbrellaException">Thrown if there is a problem identifying the mime type.</exception>
/// <inheritdoc />
public string GetMimeType(string fileNameOrExtension)
{
Guard.IsNotNullOrWhiteSpace(fileNameOrExtension);
Expand Down Expand Up @@ -72,19 +67,12 @@ public string GetMimeType(string fileNameOrExtension)
}
}

/// <summary>
/// Gets the file extension for the specified mime type. If the extension cannot be identified then null will be returned.
/// </summary>
/// <param name="mimeType">The mime type.</param>
/// <returns>The file extension or null if the extension cannot be identified.</returns>
/// <exception cref="UmbrellaException">Thrown if there is a problem identifying the file extension.</exception>
public string? GetFileExtension(string mimeType)
/// <inheritdoc />
public string? GetFileExtension(string? mimeType)
{
Guard.IsNotNullOrWhiteSpace(mimeType);

try
{
return MimeTypes.TryGetExtension(mimeType, out string? extension) ? extension : null;
return !string.IsNullOrWhiteSpace(mimeType) && MimeTypes.TryGetExtension(mimeType, out string? extension) ? extension : null;
}
catch (Exception exc) when (_logger.WriteError(exc, new { mimeType }))
{
Expand Down
35 changes: 35 additions & 0 deletions Core/test/Umbrella.Utilities.Test/Mime/MimeTypeUtilityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,40 @@ public void GetMimeType_FilenameWithPeriods()
Assert.Equal("image/png", mimeType);
}

[Fact]
public void GetFileExtension_Null()
{
string? fileExtension = CreateMimeTypeUtility().GetFileExtension(null);
Assert.Null(fileExtension);
}

[Fact]
public void GetFileExtension_Empty()
{
string? fileExtension = CreateMimeTypeUtility().GetFileExtension("");
Assert.Null(fileExtension);
}

[Fact]
public void GetFileExtension_Whitespace()
{
string? fileExtension = CreateMimeTypeUtility().GetFileExtension(" ");
Assert.Null(fileExtension);
}

[Fact]
public void GetFileExtension_Invalid()
{
string? fileExtension = CreateMimeTypeUtility().GetFileExtension("test");
Assert.Null(fileExtension);
}

[Fact]
public void GetFileExtension_Valid()
{
string? fileExtension = CreateMimeTypeUtility().GetFileExtension("image/png");
Assert.Equal(".png", fileExtension);
}

private static MimeTypeUtility CreateMimeTypeUtility() => new(new Mock<ILogger<MimeTypeUtility>>().Object);
}
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>6.0.0-preview-0319</Version>
<Version>6.0.0-preview-0320</Version>

<Nullable>enable</Nullable>
<WarningsAsErrors>nullable</WarningsAsErrors>
Expand Down

0 comments on commit de3f142

Please sign in to comment.