Skip to content

Commit 28a01d6

Browse files
committed
Fixes: U4-5981 Umbraco 7.2 Text encoding when saving partial view template
1 parent 2f74dfe commit 28a01d6

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed

src/Umbraco.Core/IO/PhysicalFileSystem.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,8 @@ public void AddFile(string path, Stream stream, bool overrideIfExists)
100100
if (stream.CanSeek)
101101
stream.Seek(0, 0);
102102

103-
using (var memoryStream = new MemoryStream())
104-
{
105-
//Add the BOM
106-
var bom = new byte[] { 0xEF, 0xBB, 0xBF };
107-
memoryStream.Write(bom, 0, bom.Length);
108-
stream.CopyTo(memoryStream);
109-
110-
if (memoryStream.CanSeek)
111-
memoryStream.Seek(0, 0);
112-
113-
using (var destination = (Stream)File.Create(GetFullPath(fsRelativePath)))
114-
memoryStream.CopyTo(destination);
115-
}
103+
using (var destination = (Stream)File.Create(GetFullPath(fsRelativePath)))
104+
stream.CopyTo(destination);
116105
}
117106

118107
public IEnumerable<string> GetFiles(string path)

src/Umbraco.Core/Persistence/Repositories/FileRepository.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ internal virtual void PersistDeletedFolder(Folder entity)
132132

133133
protected virtual void PersistNewItem(TEntity entity)
134134
{
135-
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(entity.Content)))
135+
using (var stream = GetContentStream(entity.Content))
136136
{
137137
FileSystem.AddFile(entity.Path, stream, true);
138138
entity.CreateDate = FileSystem.GetCreated(entity.Path).UtcDateTime;
@@ -146,7 +146,7 @@ protected virtual void PersistNewItem(TEntity entity)
146146

147147
protected virtual void PersistUpdatedItem(TEntity entity)
148148
{
149-
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(entity.Content)))
149+
using (var stream = GetContentStream(entity.Content))
150150
{
151151
FileSystem.AddFile(entity.Path, stream, true);
152152
entity.CreateDate = FileSystem.GetCreated(entity.Path).UtcDateTime;
@@ -168,6 +168,16 @@ protected virtual void PersistDeletedItem(TEntity entity)
168168

169169
#endregion
170170

171+
/// <summary>
172+
/// Gets a stream that is used to write to the file
173+
/// </summary>
174+
/// <param name="content"></param>
175+
/// <returns></returns>
176+
protected virtual Stream GetContentStream(string content)
177+
{
178+
return new MemoryStream(Encoding.UTF8.GetBytes(content));
179+
}
180+
171181
protected IEnumerable<string> FindAllFiles(string path)
172182
{
173183
var list = new List<string>();

src/Umbraco.Core/Persistence/Repositories/PartialViewRepository.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
45
using System.Text;
56
using Umbraco.Core.IO;
@@ -79,5 +80,20 @@ public override IEnumerable<PartialView> GetAll(params string[] ids)
7980
}
8081
}
8182
}
83+
84+
/// <summary>
85+
/// Gets a stream that is used to write to the file
86+
/// </summary>
87+
/// <param name="content"></param>
88+
/// <returns></returns>
89+
/// <remarks>
90+
/// This ensures the stream includes a utf8 BOM
91+
/// </remarks>
92+
protected override Stream GetContentStream(string content)
93+
{
94+
var data = Encoding.UTF8.GetBytes(content);
95+
var withBom = Encoding.UTF8.GetPreamble().Concat(data).ToArray();
96+
return new MemoryStream(withBom);
97+
}
8298
}
8399
}

src/Umbraco.Core/Persistence/Repositories/TemplateRepository.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ protected override Guid NodeObjectTypeId
127127

128128
protected override void PersistNewItem(ITemplate entity)
129129
{
130-
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(entity.Content)))
130+
var data = Encoding.UTF8.GetBytes(entity.Content);
131+
var withBom = Encoding.UTF8.GetPreamble().Concat(data).ToArray();
132+
133+
using (var stream = new MemoryStream(withBom))
131134
{
132135
if (entity.GetTypeOfRenderingEngine() == RenderingEngine.Mvc)
133136
{
@@ -184,7 +187,10 @@ protected override void PersistNewItem(ITemplate entity)
184187

185188
protected override void PersistUpdatedItem(ITemplate entity)
186189
{
187-
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(entity.Content)))
190+
var data = Encoding.UTF8.GetBytes(entity.Content);
191+
var withBom = Encoding.UTF8.GetPreamble().Concat(data).ToArray();
192+
193+
using (var stream = new MemoryStream(withBom))
188194
{
189195
if (entity.GetTypeOfRenderingEngine() == RenderingEngine.Mvc)
190196
{

0 commit comments

Comments
 (0)