Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I build an index of your "cid:"s first:</p> <pre><code>private const string CidPattern = "cid:"; private static HashSet&lt;int&gt; BuildCidIndex(string html) { var index = new HashSet&lt;int&gt;(); var pos = html.IndexOf(CidPattern, 0); while (pos &gt; 0) { var start = pos + CidPattern.Length; index.Add(start); pos = html.IndexOf(CidPattern, start); } return index; } </code></pre> <p>Then you need a replace function that replaces the cids based on your index</p> <pre><code>private static void AdjustIndex(HashSet&lt;int&gt; index, int oldPos, int byHowMuch) { var oldIndex = new List&lt;int&gt;(index); index.Clear(); foreach (var pos in oldIndex) { if (pos &lt; oldPos) index.Add(pos); else index.Add(pos + byHowMuch); } } private static bool ReplaceCid(HashSet&lt;int&gt; index, ref string html, string cid, string path) { var posToRemove = -1; foreach (var pos in index) { if (pos + cid.Length &lt; html.Length &amp;&amp; html.Substring(pos, cid.Length) == cid) { var sb = new StringBuilder(); sb.Append(html.Substring(0, pos-CidPattern.Length)); sb.Append(path); sb.Append(html.Substring(pos + cid.Length)); html = sb.ToString(); posToRemove = pos; break; } } if (posToRemove &lt; 0) return false; index.Remove(posToRemove); AdjustIndex(index, posToRemove, path.Length - (CidPattern.Length + cid.Length)); return true; } </code></pre> <p>so now, you can check your attachments</p> <pre><code>FileAttachment[] attachments = null; var index = BuildCidIndex(sHTMLCOntent); if (index.Count &gt; 0 &amp;&amp; item.Attachments.Count &gt; 0) { var basePath = Directory.GetCurrentDirectory(); attachments = new FileAttachment[item.Attachments.Count]; for (var i = 0; i &lt; item.Attachments.Count; ++i) { var type = item.Attachments[i].ContentType.ToLower(); if (!type.StartsWith("image/")) continue; type = type.Replace("image/", ""); var attachment = (FileAttachment)item.Attachments[i]; var cid = attachment.ContentId; var filename = cid + "." + type; var path = Path.Combine(basePath, filename); if(ReplaceCid(index, ref sHTMLCOntent, cid, path)) { // only load images when they have been found attachment.Load(path); attachments[i] = attachment; } } } </code></pre> <p>Additional to that: instead of calling <code>attachment.Load</code> right away, and pass the path to the image directly, you could link to another script, where you pass the <code>cid</code> as a parameter and then check back with the exchange for that image; then the process of loading the image from exchange does not block the html cid replacement and could lead to loading the page faster, since the html can send to the browser sooner. PS: Code is not tested, just so you get the idea!</p> <p><strong>EDIT</strong></p> <p>Added the missing <code>AdjustIndex</code> function.</p> <p><strong>EDIT 2</strong></p> <p>Fixed small bug in AdjustIndex</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload