Skip to content

Commit

Permalink
Merge pull request #5 from amitmittal/patch-2
Browse files Browse the repository at this point in the history
added support for style attribute. fix bug in image attachment.
  • Loading branch information
hermanho authored Nov 30, 2018
2 parents 3894099 + 3f0d92d commit 1f5d1ae
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/Postal.AspNetCore/HtmlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,18 @@ public static class HtmlExtensions
/// <param name="imagePathOrUrl">An image file path or URL. A file path can be relative to the web application root directory.</param>
/// <param name="alt">The content for the &lt;img alt&gt; attribute.</param>
/// <returns>An HTML &lt;img&gt; tag.</returns>
public static IHtmlContent EmbedImage(this IHtmlHelper html, string imagePathOrUrl, string alt = "")
public static IHtmlContent EmbedImage(this IHtmlHelper html, string imagePathOrUrl, string alt = "", string style = "")
{
if (string.IsNullOrWhiteSpace(imagePathOrUrl)) throw new ArgumentException("Path or URL required", "imagePathOrUrl");

if (IsFileName(imagePathOrUrl))
{
var hosting = html.ViewContext.HttpContext.RequestServices.GetService<IHostingEnvironment>();
string webRootPath = hosting.WebRootPath;
imagePathOrUrl = System.IO.Path.Combine(webRootPath, imagePathOrUrl);
imagePathOrUrl = hosting.WebRootPath + System.IO.Path.DirectorySeparatorChar + imagePathOrUrl.Replace('/', System.IO.Path.DirectorySeparatorChar).Replace('\\', System.IO.Path.DirectorySeparatorChar);
}
var imageEmbedder = (ImageEmbedder)html.ViewData[ImageEmbedder.ViewDataKey];
var resource = imageEmbedder.ReferenceImage(imagePathOrUrl);
return new HtmlString(string.Format("<img src=\"cid:{0}\" alt=\"{1}\"/>", resource.ContentId, html.Encode(alt)));
return new HtmlString(string.Format("<img src=\"cid:{0}\" alt=\"{1}\" style=\"{2}\"/>", resource.ContentId, html.Encode(alt), html.Encode(style)));
}

static bool IsFileName(string pathOrUrl)
Expand Down

0 comments on commit 1f5d1ae

Please sign in to comment.