xslt file repository and methods for getting xslt files in FileService.
This commit is contained in:
7
src/Umbraco.Core/Models/IXsltFile.cs
Normal file
7
src/Umbraco.Core/Models/IXsltFile.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Umbraco.Core.Models
|
||||
{
|
||||
public interface IXsltFile : IFile
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
32
src/Umbraco.Core/Models/XsltFile.cs
Normal file
32
src/Umbraco.Core/Models/XsltFile.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace Umbraco.Core.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a XSLT file
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[DataContract(IsReference = true)]
|
||||
public class XsltFile : File, IXsltFile
|
||||
{
|
||||
public XsltFile(string path)
|
||||
: this(path, (Func<File, string>) null)
|
||||
{ }
|
||||
|
||||
internal XsltFile(string path, Func<File, string> getFileContent)
|
||||
: base(path, getFileContent)
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the current entity has an identity, which in this case is a path/name.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Overrides the default Entity identity check.
|
||||
/// </remarks>
|
||||
public override bool HasIdentity
|
||||
{
|
||||
get { return string.IsNullOrEmpty(Path) == false; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System.IO;
|
||||
using Umbraco.Core.Models;
|
||||
|
||||
namespace Umbraco.Core.Persistence.Repositories
|
||||
{
|
||||
public interface IXsltFileRepository : IRepository<string, XsltFile>
|
||||
{
|
||||
bool ValidateXsltFile(XsltFile xsltFile);
|
||||
Stream GetFileContentStream(string filepath);
|
||||
void SetFileContent(string filepath, Stream content);
|
||||
}
|
||||
}
|
||||
135
src/Umbraco.Core/Persistence/Repositories/XsltFileRepository.cs
Normal file
135
src/Umbraco.Core/Persistence/Repositories/XsltFileRepository.cs
Normal file
@@ -0,0 +1,135 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Umbraco.Core.IO;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Core.Persistence.UnitOfWork;
|
||||
|
||||
namespace Umbraco.Core.Persistence.Repositories
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the XsltFile Repository
|
||||
/// </summary>
|
||||
internal class XsltFileRepository : FileRepository<string, XsltFile>, IXsltFileRepository
|
||||
{
|
||||
public XsltFileRepository(IUnitOfWork work, IFileSystem fileSystem)
|
||||
: base(work, fileSystem)
|
||||
{
|
||||
}
|
||||
|
||||
public override XsltFile Get(string id)
|
||||
{
|
||||
var path = FileSystem.GetRelativePath(id);
|
||||
|
||||
path = path.EnsureEndsWith(".xslt");
|
||||
|
||||
if (FileSystem.FileExists(path) == false)
|
||||
return null;
|
||||
|
||||
var created = FileSystem.GetCreated(path).UtcDateTime;
|
||||
var updated = FileSystem.GetLastModified(path).UtcDateTime;
|
||||
|
||||
var xsltFile = new XsltFile(path, file => GetFileContent(file.OriginalPath))
|
||||
{
|
||||
Key = path.EncodeAsGuid(),
|
||||
CreateDate = created,
|
||||
UpdateDate = updated,
|
||||
Id = path.GetHashCode(),
|
||||
VirtualPath = FileSystem.GetUrl(path)
|
||||
};
|
||||
|
||||
//on initial construction we don't want to have dirty properties tracked
|
||||
// http://issues.umbraco.org/issue/U4-1946
|
||||
xsltFile.ResetDirtyProperties(false);
|
||||
|
||||
return xsltFile;
|
||||
}
|
||||
|
||||
public override void AddOrUpdate(XsltFile entity)
|
||||
{
|
||||
base.AddOrUpdate(entity);
|
||||
|
||||
// ensure that from now on, content is lazy-loaded
|
||||
if (entity.GetFileContent == null)
|
||||
entity.GetFileContent = file => GetFileContent(file.OriginalPath);
|
||||
}
|
||||
|
||||
public override IEnumerable<XsltFile> GetAll(params string[] ids)
|
||||
{
|
||||
ids = ids
|
||||
.Select(x => x.EnsureEndsWith(".xslt"))
|
||||
.Distinct()
|
||||
.ToArray();
|
||||
|
||||
if (ids.Any())
|
||||
{
|
||||
foreach (var id in ids)
|
||||
{
|
||||
yield return Get(id);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var files = FindAllFiles("", "*.xslt");
|
||||
foreach (var file in files)
|
||||
{
|
||||
yield return Get(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of all <see cref="XsltFile"/> that exist at the relative path specified.
|
||||
/// </summary>
|
||||
/// <param name="rootPath">
|
||||
/// If null or not specified, will return the XSLT files at the root path relative to the IFileSystem
|
||||
/// </param>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<XsltFile> GetXsltFilesAtPath(string rootPath = null)
|
||||
{
|
||||
return FileSystem.GetFiles(rootPath ?? string.Empty, "*.xslt").Select(Get);
|
||||
}
|
||||
|
||||
private static readonly List<string> ValidExtensions = new List<string> { "xslt" };
|
||||
|
||||
public bool ValidateXsltFile(XsltFile xsltFile)
|
||||
{
|
||||
// get full path
|
||||
string fullPath;
|
||||
try
|
||||
{
|
||||
// may throw for security reasons
|
||||
fullPath = FileSystem.GetFullPath(xsltFile.Path);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// validate path and extension
|
||||
var validDir = SystemDirectories.Xslt;
|
||||
var isValidPath = IOHelper.VerifyEditPath(fullPath, validDir);
|
||||
var isValidExtension = IOHelper.VerifyFileExtension(xsltFile.Path, ValidExtensions);
|
||||
return isValidPath && isValidExtension;
|
||||
}
|
||||
|
||||
public Stream GetFileContentStream(string filepath)
|
||||
{
|
||||
if (FileSystem.FileExists(filepath) == false) return null;
|
||||
|
||||
try
|
||||
{
|
||||
return FileSystem.OpenFile(filepath);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null; // deal with race conds
|
||||
}
|
||||
}
|
||||
|
||||
public void SetFileContent(string filepath, Stream content)
|
||||
{
|
||||
FileSystem.AddFile(filepath, content, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -240,6 +240,11 @@ namespace Umbraco.Core.Persistence
|
||||
_settings.Templates);
|
||||
}
|
||||
|
||||
public virtual IXsltFileRepository CreateXsltFileRepository(IUnitOfWork uow)
|
||||
{
|
||||
return new XsltFileRepository(uow, new PhysicalFileSystem(SystemDirectories.Xslt));
|
||||
}
|
||||
|
||||
public virtual IMigrationEntryRepository CreateMigrationEntryRepository(IDatabaseUnitOfWork uow)
|
||||
{
|
||||
return new MigrationEntryRepository(
|
||||
|
||||
@@ -665,6 +665,22 @@ namespace Umbraco.Core.Services
|
||||
}
|
||||
}
|
||||
|
||||
public IXsltFile GetXsltFile(string path)
|
||||
{
|
||||
using (var repository = RepositoryFactory.CreateXsltFileRepository(_fileUowProvider.GetUnitOfWork()))
|
||||
{
|
||||
return repository.Get(path);
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<IXsltFile> GetXsltFiles(params string[] names)
|
||||
{
|
||||
using (var repository = RepositoryFactory.CreateXsltFileRepository(_fileUowProvider.GetUnitOfWork()))
|
||||
{
|
||||
return repository.GetAll(names).OrderBy(x => x.Name);
|
||||
}
|
||||
}
|
||||
|
||||
public Attempt<IPartialView> CreatePartialView(IPartialView partialView, string snippetName = null, int userId = 0)
|
||||
{
|
||||
return CreatePartialViewMacro(partialView, PartialViewType.PartialView, snippetName, userId);
|
||||
|
||||
@@ -16,6 +16,8 @@ namespace Umbraco.Core.Services
|
||||
IPartialView GetPartialView(string path);
|
||||
IPartialView GetPartialViewMacro(string path);
|
||||
IEnumerable<IPartialView> GetPartialViewMacros(params string[] names);
|
||||
IXsltFile GetXsltFile(string path);
|
||||
IEnumerable<IXsltFile> GetXsltFiles(params string[] names);
|
||||
Attempt<IPartialView> CreatePartialView(IPartialView partialView, string snippetName = null, int userId = 0);
|
||||
Attempt<IPartialView> CreatePartialViewMacro(IPartialView partialView, string snippetName = null, int userId = 0);
|
||||
bool DeletePartialView(string path, int userId = 0);
|
||||
|
||||
@@ -386,6 +386,7 @@
|
||||
<Compile Include="Models\IPublishedContentWithKey.cs" />
|
||||
<Compile Include="Models\IRedirectUrl.cs" />
|
||||
<Compile Include="Models\IServerRegistration.cs" />
|
||||
<Compile Include="Models\IXsltFile.cs" />
|
||||
<Compile Include="Models\Mapping\MappingExpressionExtensions.cs" />
|
||||
<Compile Include="Models\MigrationEntry.cs" />
|
||||
<Compile Include="Models\PartialViewType.cs" />
|
||||
@@ -414,6 +415,7 @@
|
||||
<Compile Include="Models\Rdbms\MigrationDto.cs" />
|
||||
<Compile Include="Models\Rdbms\UmbracoDeployChecksumDto.cs" />
|
||||
<Compile Include="Models\Rdbms\UmbracoDeployDependencyDto.cs" />
|
||||
<Compile Include="Models\XsltFile.cs" />
|
||||
<Compile Include="Models\TemplateOnDisk.cs" />
|
||||
<Compile Include="Models\UmbracoDomain.cs" />
|
||||
<Compile Include="Models\DoNotCloneAttribute.cs" />
|
||||
@@ -490,11 +492,13 @@
|
||||
<Compile Include="Persistence\Repositories\Interfaces\IRedirectUrlRepository.cs" />
|
||||
<Compile Include="Persistence\Repositories\Interfaces\IServerRegistrationRepository.cs" />
|
||||
<Compile Include="Persistence\Repositories\Interfaces\IDeleteMediaFilesRepository.cs" />
|
||||
<Compile Include="Persistence\Repositories\Interfaces\IXsltFileRepository.cs" />
|
||||
<Compile Include="Persistence\Repositories\Interfaces\ITaskRepository.cs" />
|
||||
<Compile Include="Persistence\Repositories\Interfaces\ITaskTypeRepository.cs" />
|
||||
<Compile Include="Persistence\Repositories\MigrationEntryRepository.cs" />
|
||||
<Compile Include="Persistence\Repositories\PublicAccessRepository.cs" />
|
||||
<Compile Include="Persistence\Repositories\RedirectUrlRepository.cs" />
|
||||
<Compile Include="Persistence\Repositories\XsltFileRepository.cs" />
|
||||
<Compile Include="Persistence\Repositories\TaskRepository.cs" />
|
||||
<Compile Include="Persistence\Repositories\TaskTypeRepository.cs" />
|
||||
<Compile Include="PropertyEditors\DecimalValidator.cs" />
|
||||
|
||||
Reference in New Issue
Block a user