Skip to content

Commit

Permalink
added support for style attribute. fix bug in image attachment.
Browse files Browse the repository at this point in the history
  • Loading branch information
amitmittal authored Nov 29, 2018
1 parent 3894099 commit 3f0d92d
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 3f0d92d

Please sign in to comment.