Skip to content

Commit ba7ad22

Browse files
umbracotrdtimgauntTim Gaunt
authored
Cherry-pick #21: Format the returned XML because facebook limits the length of one row (#22)
* Format the returned XML because facebook limits the length of one row (#21) Co-authored-by: Tim Gaunt <[email protected]> * minor changes to code format and bump version * switch to synchronous Flush because XmlTextWriter.FlushAsync throws NotImplementedException --------- Co-authored-by: Tim Gaunt <[email protected]> Co-authored-by: Tim Gaunt <[email protected]>
1 parent 6c64a09 commit ba7ad22

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

src/Umbraco.Commerce.ProductFeeds.Web/Apis/Public/ProductFeedController.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ public ProductFeedController(
2323
public async Task<IActionResult> Xml(string path)
2424
{
2525
ProductFeedSettingReadModel? feedSettings = await _feedConfigService
26-
.FindSettingAsync(new FindSettingParams
27-
{
28-
FeedRelativePath = path,
29-
})
26+
.FindSettingAsync(new FindSettingParams { FeedRelativePath = path })
3027
.ConfigureAwait(true);
3128
if (feedSettings == null)
3229
{
@@ -36,7 +33,8 @@ public async Task<IActionResult> Xml(string path)
3633
IProductFeedGeneratorService feedGenerator = _feedGeneratorFactory.GetGenerator(feedSettings.FeedType);
3734
XmlDocument feed = feedGenerator.GenerateFeed(feedSettings);
3835

39-
return Content(feed.OuterXml, "text/xml");
36+
var result = new XmlActionResult(feed) { Formatting = Formatting.Indented };
37+
return result;
4038
}
4139
}
4240
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Text;
3+
using System.Threading.Tasks;
4+
using System.Xml;
5+
using Microsoft.AspNetCore.Http;
6+
using Microsoft.AspNetCore.Mvc;
7+
8+
namespace Umbraco.Commerce.ProductFeeds.Apis.Public;
9+
10+
public sealed class XmlActionResult : IActionResult
11+
{
12+
private static readonly Serilog.ILogger _logger = Serilog.Log.Logger.ForContext(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
13+
14+
private readonly XmlDocument _document;
15+
16+
public Formatting Formatting { get; set; } = Formatting.None;
17+
public string MimeType { get; set; } = "text/xml";
18+
19+
public XmlActionResult(XmlDocument document)
20+
{
21+
ArgumentNullException.ThrowIfNull(document);
22+
_document = document;
23+
}
24+
25+
public async Task ExecuteResultAsync(ActionContext context)
26+
{
27+
context.HttpContext.Response.Clear();
28+
context.HttpContext.Response.ContentType = MimeType;
29+
30+
try
31+
{
32+
using (var writer = new XmlTextWriter(context.HttpContext.Response.Body, Encoding.UTF8))
33+
{
34+
writer.Formatting = Formatting;
35+
_document.WriteContentTo(writer);
36+
writer.Flush();
37+
}
38+
}
39+
catch (Exception ex)
40+
{
41+
_logger.Error(ex, "Exception generating the XML response");
42+
}
43+
}
44+
}

src/Umbraco.Commerce.ProductFeeds.Web/Umbraco.Commerce.ProductFeeds.Web.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<Title>Umbraco Commerce Product Feeds Web</Title>
55
<Description>Contains apis</Description>
66
<Nullable>enable</Nullable>
7-
<NoWarn>IDE0007,CA2007</NoWarn>
7+
<NoWarn>IDE0007;CA2007</NoWarn>
88
<StaticWebAssetBasePath>App_Plugins/umbracocommerceproductfeeds</StaticWebAssetBasePath>
99
</PropertyGroup>
1010

version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
3-
"version": "14.1.1",
3+
"version": "14.1.2",
44
"assemblyVersion": {
55
"precision": "build"
66
},

0 commit comments

Comments
 (0)