Skip to content

Commit

Permalink
Merge pull request #1774 from umbraco/temp-9586-async-content-xml-save
Browse files Browse the repository at this point in the history
Removes async from filestream instance
  • Loading branch information
Shazwazza committed Mar 2, 2017
2 parents 15150f9 + 7e8f6d3 commit dca5a26
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 39 deletions.
26 changes: 0 additions & 26 deletions src/Umbraco.Core/XmlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,6 @@ namespace Umbraco.Core
/// </summary>
internal static class XmlExtensions
{
/// <summary>
/// Saves the xml document async
/// </summary>
/// <param name="xdoc"></param>
/// <param name="filename"></param>
/// <returns></returns>
public static async Task SaveAsync(this XmlDocument xdoc, string filename)
{
if (xdoc.DocumentElement == null)
throw new XmlException("Cannot save xml document, there is no root element");

using (var fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Read, bufferSize: 4096, useAsync: true))
using (var xmlWriter = XmlWriter.Create(fs, new XmlWriterSettings
{
Async = true,
Encoding = Encoding.UTF8,
Indent = true
}))
{
//NOTE: There are no nice methods to write it async, only flushing it async. We
// could implement this ourselves but it'd be a very manual process.
xdoc.WriteTo(xmlWriter);
await xmlWriter.FlushAsync().ConfigureAwait(false);
}
}

public static bool HasAttribute(this XmlAttributeCollection attributes, string attributeName)
{
return attributes.Cast<XmlAttribute>().Any(x => x.Name == attributeName);
Expand Down
3 changes: 1 addition & 2 deletions src/Umbraco.Web/umbraco.presentation/content.cs
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,6 @@ private DateTime XmlFileLastWriteTime
internal void SaveXmlToFile()
{
LogHelper.Info<content>("Save Xml to file...");

try
{
var xml = _xmlContent; // capture (atomic + volatile), immutable anyway
Expand All @@ -861,7 +860,7 @@ internal void SaveXmlToFile()
Directory.CreateDirectory(directoryName);

// save
using (var fs = new FileStream(_xmlFileName, FileMode.Create, FileAccess.Write, FileShare.Read, bufferSize: 4096, useAsync: true))
using (var fs = new FileStream(_xmlFileName, FileMode.Create, FileAccess.Write, FileShare.Read))
{
SaveXmlToStream(xml, fs);
}
Expand Down
23 changes: 12 additions & 11 deletions src/Umbraco.Web/umbraco.presentation/template.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,20 @@ public string AlternateMasterPageFile(string templateFolder)
string originalPath = IOHelper.MapPath(VirtualPathUtility.ToAbsolute(MasterPageFile));
string copyPath = IOHelper.MapPath(VirtualPathUtility.ToAbsolute(path));

FileStream fs = new FileStream(originalPath, FileMode.Open, FileAccess.ReadWrite);
StreamReader f = new StreamReader(fs);
String newfile = f.ReadToEnd();
f.Close();
fs.Close();

newfile = newfile.Replace("MasterPageFile=\"~/masterpages/", "MasterPageFile=\"");
string newFile;
using (var fs = new FileStream(originalPath, FileMode.Open, FileAccess.ReadWrite))
using (var f = new StreamReader(fs))
{
newFile = f.ReadToEnd();
}

fs = new FileStream(copyPath, FileMode.Create, FileAccess.Write);
newFile = newFile.Replace("MasterPageFile=\"~/masterpages/", "MasterPageFile=\"");

StreamWriter replacement = new StreamWriter(fs);
replacement.Write(newfile);
replacement.Close();
using (var fs = new FileStream(copyPath, FileMode.Create, FileAccess.Write))
using (var replacement = new StreamWriter(fs))
{
replacement.Write(newFile);
}
}

return path;
Expand Down

0 comments on commit dca5a26

Please sign in to comment.