Skip to content

Commit 02b958f

Browse files
committed
corrected more issues
1 parent c86a6ec commit 02b958f

File tree

40 files changed

+356
-146
lines changed

40 files changed

+356
-146
lines changed

src/Server/Coderr.Server.Api/Core/Messaging/EmailResource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class EmailResource
1313
/// </summary>
1414
/// <param name="name">CID</param>
1515
/// <param name="content">Actual content</param>
16-
public EmailResource(string name, Stream content)
16+
public EmailResource(string name, byte[] content)
1717
{
1818
if (name == null) throw new ArgumentNullException("name");
1919
if (content == null) throw new ArgumentNullException("content");
@@ -31,7 +31,7 @@ protected EmailResource()
3131
/// <summary>
3232
/// Contents of the resource. Stream must be readable.
3333
/// </summary>
34-
public Stream Content { get; set; }
34+
public byte[] Content { get; set; }
3535

3636
/// <summary>
3737
/// CID

src/Server/Coderr.Server.App/Coderr.Server.App.csproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55
<AssemblyName>Coderr.Server.App</AssemblyName>
66
<DefaultItemExcludes>$(DefaultItemExcludes);*.DotSettings;*.vsspell;CustomDictionary.xml</DefaultItemExcludes>
77
</PropertyGroup>
8+
<ItemGroup>
9+
<None Remove="Core\Accounts\CommandHandlers\Templates\ResetPassword\Template.md" />
10+
<None Remove="Modules\Messaging\Templating\Layout\logo2.png" />
11+
<None Remove="Modules\Messaging\Templating\Layout\Template.html" />
12+
</ItemGroup>
13+
<ItemGroup>
14+
<EmbeddedResource Include="Core\Accounts\CommandHandlers\Templates\ResetPassword\Template.md" />
15+
</ItemGroup>
16+
<ItemGroup>
17+
<EmbeddedResource Include="Modules\Messaging\Templating\Layout\logo2.png" />
18+
<EmbeddedResource Include="Modules\Messaging\Templating\Layout\Template.html" />
19+
</ItemGroup>
820
<ItemGroup>
921
<PackageReference Include="ColorCode" Version="1.0.1">
1022
<NoWarn>NU1701</NoWarn>

src/Server/Coderr.Server.App/Core/Accounts/CommandHandlers/RequestPasswordResetHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public async Task HandleAsync(IMessageContext context, RequestPasswordReset comm
4545
{
4646
AccountName = account.UserName,
4747
ResetLink =
48-
_baseConfig.BaseUrl + "/password/reset/" +
48+
_baseConfig.BaseUrl + "password/reset/" +
4949
account.ActivationKey
5050
},
5151
Subject = "Reset password"

src/Server/Coderr.Server.App/Modules/Messaging/Commands/SendEmailHandler.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,12 @@ public async Task HandleAsync(IMessageContext context, SendEmail command)
7373
foreach (var resource in command.EmailMessage.Resources)
7474
{
7575
var contentType = new ContentType(MimeMapping.GetMimeType(Path.GetExtension(resource.Name)));
76-
var linkedResource = new LinkedResource(resource.Name, contentType);
77-
await resource.Content.CopyToAsync(linkedResource.ContentStream);
76+
var ms = new MemoryStream(resource.Content, 0, resource.Content.Length, false);
77+
var linkedResource = new LinkedResource(ms)
78+
{
79+
ContentId = resource.Name,
80+
ContentType = contentType
81+
};
7882
av.LinkedResources.Add(linkedResource);
7983
}
8084

src/Server/Coderr.Server.App/Modules/Messaging/Commands/SendTemplateEmailHandler.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Coderr.Server.Api.Core.Messaging.Commands;
66
using Coderr.Server.App.Modules.Messaging.Templating;
77
using DotNetCqs;
8+
using Newtonsoft.Json.Linq;
89

910

1011
namespace Coderr.Server.App.Modules.Messaging.Commands
@@ -30,6 +31,7 @@ public async Task HandleAsync(IMessageContext context, SendTemplateEmail command
3031
var layout = loader.Load("Layout");
3132

3233
var template = loader.Load(command.TemplateName);
34+
3335
var html = templateParser.RunAll(template, command.Model);
3436
if (html.IndexOf("src=\"cid:", StringComparison.OrdinalIgnoreCase) == -1)
3537
html = html.Replace(@"src=""", @"src=""cid:");
@@ -49,7 +51,10 @@ public async Task HandleAsync(IMessageContext context, SendTemplateEmail command
4951

5052
foreach (var resource in template.Resources)
5153
{
52-
var linkedResource = new EmailResource(resource.Key, resource.Value);
54+
var buffer = new byte[resource.Value.Length];
55+
resource.Value.Read(buffer, 0, buffer.Length);
56+
resource.Value.Position = 0;
57+
var linkedResource = new EmailResource(resource.Key, buffer);
5358

5459
var reader = new BinaryReader(resource.Value);
5560
var dimensions = ImageHelper.GetDimensions(reader);
@@ -63,7 +68,10 @@ public async Task HandleAsync(IMessageContext context, SendTemplateEmail command
6368
}
6469
foreach (var resource in layout.Resources)
6570
{
66-
var linkedResource = new EmailResource(resource.Key, resource.Value);
71+
var buffer = new byte[resource.Value.Length];
72+
resource.Value.Read(buffer, 0, buffer.Length);
73+
resource.Value.Position = 0;
74+
var linkedResource = new EmailResource(resource.Key, buffer);
6775

6876
var reader = new BinaryReader(resource.Value);
6977
var dimensions = ImageHelper.GetDimensions(reader);
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Newtonsoft.Json.Linq;
4+
5+
namespace Coderr.Server.App.Modules.Messaging.Templating.Formatting
6+
{
7+
public class JObjectReflector
8+
{
9+
private readonly Dictionary<string, object> _items = new Dictionary<string, object>();
10+
11+
/// <summary>
12+
/// Checks if the specified type could be traversed or just added as a value.
13+
/// </summary>
14+
/// <param name="type">Type to check</param>
15+
/// <returns><c>true</c> if we should add this type as a value; <c>false</c> if we should do reflection on it.</returns>
16+
public static bool IsSimpleType(Type type)
17+
{
18+
if (type == null) throw new ArgumentNullException(nameof(type));
19+
20+
return type.IsPrimitive
21+
|| type == typeof(decimal)
22+
|| type == typeof(string)
23+
|| type == typeof(DateTime)
24+
|| type == typeof(int)
25+
|| type == typeof(DateTimeOffset)
26+
|| type == typeof(TimeSpan);
27+
}
28+
29+
public IDictionary<string, object> Reflect(JObject data)
30+
{
31+
if (data == null) throw new ArgumentNullException(nameof(data));
32+
33+
_items.Clear();
34+
foreach (var prop in data) ReflectObject(prop.Value, prop.Key);
35+
36+
return _items;
37+
}
38+
39+
private void ReflectObject(JArray data, string prefix)
40+
{
41+
var index = 0;
42+
foreach (var item in data)
43+
{
44+
var childPrefix = $"{prefix}[{index++}]";
45+
ReflectObject(item, childPrefix);
46+
}
47+
}
48+
49+
private void ReflectObject(JToken data, string prefix)
50+
{
51+
var isHandled = true;
52+
switch (data.Type)
53+
{
54+
case JTokenType.Array:
55+
ReflectObject((JArray) data, prefix);
56+
break;
57+
case JTokenType.Date:
58+
_items.Add(prefix, data.ToObject<DateTime>());
59+
break;
60+
61+
case JTokenType.Boolean:
62+
_items.Add(prefix, data.ToObject<bool>());
63+
break;
64+
case JTokenType.Float:
65+
_items.Add(prefix, data.ToObject<float>());
66+
break;
67+
case JTokenType.Guid:
68+
_items.Add(prefix, data.ToObject<Guid>());
69+
break;
70+
case JTokenType.Integer:
71+
_items.Add(prefix, data.ToObject<int>());
72+
break;
73+
case JTokenType.String:
74+
_items.Add(prefix, data.ToObject<string>());
75+
break;
76+
case JTokenType.TimeSpan:
77+
_items.Add(prefix, data.ToObject<TimeSpan>());
78+
break;
79+
case JTokenType.Uri:
80+
_items.Add(prefix, data.ToObject<string>());
81+
break;
82+
case JTokenType.Null:
83+
_items.Add(prefix, null);
84+
break;
85+
default:
86+
isHandled = false;
87+
break;
88+
}
89+
90+
if (isHandled)
91+
return;
92+
93+
if (data is JObject)
94+
foreach (var prop in (JObject) data)
95+
ReflectObject(prop.Value, $"{prefix}.{prop.Key}");
96+
}
97+
}
98+
}

src/Server/Coderr.Server.App/Modules/Messaging/Templating/Formatting/ObjectToDictionaryConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class ObjectToDictionaryConverter
1717
/// <remarks>
1818
/// Look at the class doc for an example.
1919
/// </remarks>
20-
public Dictionary<string, object> Convert(object instance)
20+
public IDictionary<string, object> Convert(object instance)
2121
{
2222
if (instance == null)
2323
throw new ArgumentNullException("instance");

src/Server/Coderr.Server.App/Modules/Messaging/Templating/Formatting/StringFormatter.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
24
using System.Diagnostics.CodeAnalysis;
35
using System.Text;
6+
using Newtonsoft.Json.Linq;
47

58
namespace Coderr.Server.App.Modules.Messaging.Templating.Formatting
69
{
@@ -26,9 +29,19 @@ public string Format(string source, params object[] arguments)
2629
var tokenizer = new Tokenizer();
2730
var tokens = tokenizer.Parse(source);
2831

32+
IDictionary<string, object> model;
2933
//Console.WriteLine(string.Join("+", tokens.Select(x => x.Name + x.Value)));
30-
var converter = new ObjectToDictionaryConverter();
31-
var model = converter.Convert(arguments[0]);
34+
if (arguments[0] is JObject)
35+
{
36+
var converter = new JObjectReflector();
37+
model = converter.Reflect((JObject)arguments[0]);
38+
}
39+
else
40+
{
41+
var converter = new ObjectToDictionaryConverter();
42+
model = converter.Convert(arguments[0]);
43+
}
44+
3245

3346
if (arguments.Length != 1)
3447
{
@@ -44,7 +57,7 @@ public string Format(string source, params object[] arguments)
4457
sb.Append('}');
4558
includeEnd = false;
4659
}
47-
else if (token.Name != null && token.Name.IndexOfAny(new[] {' ', ',', '-', '+', ':'}) > -1)
60+
else if (token.Name != null && token.Name.IndexOfAny(new[] { ' ', ',', '-', '+', ':' }) > -1)
4861
{
4962
// vars can't contain spaces.
5063
sb.Append('{');

src/Server/Coderr.Server.App/Modules/Messaging/Templating/Layout/Template.html

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,6 @@ <h6 class="collapse">{Title}</h6>
277277
<tr>
278278
<td align="center">
279279
<p>
280-
<a href="https://coderrapp.com/Account/Terms">Terms</a> |
281-
<a href="https://coderrapp.com/account/privacypolicy">Privacy</a> |
282-
<a href="https://app.coderrapp.com/account/settings">
283-
<unsubscribe>Unsubscribe</unsubscribe>
284-
</a>
285280
</p>
286281
</td>
287282
</tr>

src/Server/Coderr.Server.Infrastructure/Messaging/MessagingSerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void IMessageSerializer<string>.Serialize(object dto, out string serializedDto,
7171
try
7272
{
7373
contentType = dto.GetType().AssemblyQualifiedName;
74-
serializedDto = JsonConvert.SerializeObject(dto);
74+
serializedDto = JsonConvert.SerializeObject(dto, _settings);
7575
}
7676
catch (JsonException ex)
7777
{

0 commit comments

Comments
 (0)