Merge pull request #10266 from umbraco/v9/feature/further-file-service-methods

Re-implemented various file service methods from V8 required in Deploy.
This commit is contained in:
Andy Butland
2021-05-12 22:07:25 +02:00
committed by GitHub
11 changed files with 510 additions and 215 deletions

View File

@@ -0,0 +1,13 @@
using System.IO;
namespace Umbraco.Cms.Core.Persistence.Repositories
{
public interface IFileRepository
{
Stream GetFileContentStream(string filepath);
void SetFileContent(string filepath, Stream content);
long GetFileSize(string filepath);
}
}

View File

@@ -0,0 +1,9 @@
namespace Umbraco.Cms.Core.Persistence.Repositories
{
public interface IFileWithFoldersRepository
{
void AddFolder(string folderPath);
void DeleteFolder(string folderPath);
}
}

View File

@@ -2,9 +2,7 @@ using Umbraco.Cms.Core.Models;
namespace Umbraco.Cms.Core.Persistence.Repositories
{
public interface IPartialViewRepository : IReadRepository<string, IPartialView>, IWriteRepository<IPartialView>
public interface IPartialViewRepository : IReadRepository<string, IPartialView>, IWriteRepository<IPartialView>, IFileRepository, IFileWithFoldersRepository
{
void AddFolder(string folderPath);
void DeleteFolder(string folderPath);
}
}

View File

@@ -1,10 +1,9 @@
using System.IO;
using Umbraco.Cms.Core.Models;
namespace Umbraco.Cms.Core.Persistence.Repositories
{
public interface IScriptRepository : IReadRepository<string, IScript>, IWriteRepository<IScript>
public interface IScriptRepository : IReadRepository<string, IScript>, IWriteRepository<IScript>, IFileRepository, IFileWithFoldersRepository
{
void AddFolder(string folderPath);
void DeleteFolder(string folderPath);
}
}

View File

@@ -2,9 +2,7 @@ using Umbraco.Cms.Core.Models;
namespace Umbraco.Cms.Core.Persistence.Repositories
{
public interface IStylesheetRepository : IReadRepository<string, IStylesheet>, IWriteRepository<IStylesheet>
public interface IStylesheetRepository : IReadRepository<string, IStylesheet>, IWriteRepository<IStylesheet>, IFileRepository, IFileWithFoldersRepository
{
void AddFolder(string folderPath);
void DeleteFolder(string folderPath);
}
}

View File

@@ -1,10 +1,9 @@
using System.Collections.Generic;
using System.IO;
using Umbraco.Cms.Core.Models;
namespace Umbraco.Cms.Core.Persistence.Repositories
{
public interface ITemplateRepository : IReadWriteQueryRepository<int, ITemplate>
public interface ITemplateRepository : IReadWriteQueryRepository<int, ITemplate>, IFileRepository
{
ITemplate Get(string alias);
@@ -13,12 +12,5 @@ namespace Umbraco.Cms.Core.Persistence.Repositories
IEnumerable<ITemplate> GetChildren(int masterTemplateId);
IEnumerable<ITemplate> GetDescendants(int masterTemplateId);
/// <summary>
/// Gets the content of a template as a stream.
/// </summary>
/// <param name="filepath">The filesystem path to the template.</param>
/// <returns>The content of the template.</returns>
Stream GetFileContentStream(string filepath);
}
}

View File

@@ -24,6 +24,48 @@ namespace Umbraco.Cms.Core.Services
Attempt<IPartialView> SavePartialView(IPartialView partialView, int userId = Constants.Security.SuperUserId);
Attempt<IPartialView> SavePartialViewMacro(IPartialView partialView, int userId = Constants.Security.SuperUserId);
/// <summary>
/// Gets the content of a partial view as a stream.
/// </summary>
/// <param name="filepath">The filesystem path to the partial view.</param>
/// <returns>The content of the partial view.</returns>
Stream GetPartialViewFileContentStream(string filepath);
/// <summary>
/// Sets the content of a partial view.
/// </summary>
/// <param name="filepath">The filesystem path to the partial view.</param>
/// <param name="content">The content of the partial view.</param>
void SetPartialViewFileContent(string filepath, Stream content);
/// <summary>
/// Gets the size of a partial view.
/// </summary>
/// <param name="filepath">The filesystem path to the partial view.</param>
/// <returns>The size of the partial view.</returns>
long GetPartialViewFileSize(string filepath);
/// <summary>
/// Gets the content of a macro partial view as a stream.
/// </summary>
/// <param name="filepath">The filesystem path to the macro partial view.</param>
/// <returns>The content of the macro partial view.</returns>
Stream GetPartialViewMacroFileContentStream(string filepath);
/// <summary>
/// Sets the content of a macro partial view.
/// </summary>
/// <param name="filepath">The filesystem path to the macro partial view.</param>
/// <param name="content">The content of the macro partial view.</param>
void SetPartialViewMacroFileContent(string filepath, Stream content);
/// <summary>
/// Gets the size of a macro partial view.
/// </summary>
/// <param name="filepath">The filesystem path to the macro partial view.</param>
/// <returns>The size of the macro partial view.</returns>
long GetPartialViewMacroFileSize(string filepath);
/// <summary>
/// Gets a list of all <see cref="IStylesheet"/> objects
/// </summary>
@@ -51,6 +93,40 @@ namespace Umbraco.Cms.Core.Services
/// <param name="userId">Optional id of the user deleting the stylesheet</param>
void DeleteStylesheet(string path, int userId = Constants.Security.SuperUserId);
/// <summary>
/// Creates a folder for style sheets
/// </summary>
/// <param name="folderPath"></param>
/// <returns></returns>
void CreateStyleSheetFolder(string folderPath);
/// <summary>
/// Deletes a folder for style sheets
/// </summary>
/// <param name="folderPath"></param>
void DeleteStyleSheetFolder(string folderPath);
/// <summary>
/// Gets the content of a stylesheet as a stream.
/// </summary>
/// <param name="filepath">The filesystem path to the stylesheet.</param>
/// <returns>The content of the stylesheet.</returns>
Stream GetStylesheetFileContentStream(string filepath);
/// <summary>
/// Sets the content of a stylesheet.
/// </summary>
/// <param name="filepath">The filesystem path to the stylesheet.</param>
/// <param name="content">The content of the stylesheet.</param>
void SetStylesheetFileContent(string filepath, Stream content);
/// <summary>
/// Gets the size of a stylesheet.
/// </summary>
/// <param name="filepath">The filesystem path to the stylesheet.</param>
/// <returns>The size of the stylesheet.</returns>
long GetStylesheetFileSize(string filepath);
/// <summary>
/// Gets a <see cref="IScript"/> object by its name
/// </summary>
@@ -86,17 +162,25 @@ namespace Umbraco.Cms.Core.Services
void DeleteScriptFolder(string folderPath);
/// <summary>
/// Creates a folder for style sheets
/// Gets the content of a script file as a stream.
/// </summary>
/// <param name="folderPath"></param>
/// <returns></returns>
void CreateStyleSheetFolder(string folderPath);
/// <param name="filepath">The filesystem path to the script.</param>
/// <returns>The content of the script file.</returns>
Stream GetScriptFileContentStream(string filepath);
/// <summary>
/// Deletes a folder for style sheets
/// Sets the content of a script file.
/// </summary>
/// <param name="folderPath"></param>
void DeleteStyleSheetFolder(string folderPath);
/// <param name="filepath">The filesystem path to the script.</param>
/// <param name="content">The content of the script file.</param>
void SetScriptFileContent(string filepath, Stream content);
/// <summary>
/// Gets the size of a script file.
/// </summary>
/// <param name="filepath">The filesystem path to the script file.</param>
/// <returns>The size of the script file.</returns>
long GetScriptFileSize(string filepath);
/// <summary>
/// Gets a list of all <see cref="ITemplate"/> objects
@@ -172,6 +256,27 @@ namespace Umbraco.Cms.Core.Services
/// <param name="userId">Optional id of the user</param>
void SaveTemplate(IEnumerable<ITemplate> templates, int userId = Constants.Security.SuperUserId);
/// <summary>
/// Gets the content of a template as a stream.
/// </summary>
/// <param name="filepath">The filesystem path to the template.</param>
/// <returns>The content of the template.</returns>
Stream GetTemplateFileContentStream(string filepath);
/// <summary>
/// Sets the content of a template.
/// </summary>
/// <param name="filepath">The filesystem path to the template.</param>
/// <param name="content">The content of the template.</param>
void SetTemplateFileContent(string filepath, Stream content);
/// <summary>
/// Gets the size of a template.
/// </summary>
/// <param name="filepath">The filesystem path to the template.</param>
/// <returns>The size of the template.</returns>
long GetTemplateFileSize(string filepath);
/// <summary>
/// Gets the content of a macro partial view snippet as a string
/// </summary>

View File

@@ -12,46 +12,35 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
internal abstract class FileRepository<TId, TEntity> : IReadRepository<TId, TEntity>, IWriteRepository<TEntity>
where TEntity : IFile
{
protected FileRepository(IFileSystem fileSystem)
{
FileSystem = fileSystem;
}
protected FileRepository(IFileSystem fileSystem) => FileSystem = fileSystem;
protected IFileSystem FileSystem { get; }
public virtual void AddFolder(string folderPath)
{
PersistNewItem(new Folder(folderPath));
}
public virtual void AddFolder(string folderPath) => PersistNewItem(new Folder(folderPath));
public virtual void DeleteFolder(string folderPath)
{
PersistDeletedItem(new Folder(folderPath));
}
public virtual void DeleteFolder(string folderPath) => PersistDeletedItem(new Folder(folderPath));
#region Implementation of IRepository<TId,TEntity>
public virtual void Save(TEntity entity)
{
if (FileSystem.FileExists(entity.OriginalPath) == false)
{
PersistNewItem(entity);
}
else
{
PersistUpdatedItem(entity);
}
}
public virtual void Delete(TEntity entity)
{
PersistDeletedItem(entity);
}
public virtual void Delete(TEntity entity) => PersistDeletedItem(entity);
public abstract TEntity Get(TId id);
public abstract IEnumerable<TEntity> GetMany(params TId[] ids);
public virtual bool Exists(TId id)
{
return FileSystem.FileExists(id.ToString());
}
public virtual bool Exists(TId id) => FileSystem.FileExists(id.ToString());
#endregion
@@ -60,8 +49,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
public void PersistNewItem(IEntity entity)
{
//special case for folder
var folder = entity as Folder;
if (folder != null)
if (entity is Folder folder)
{
PersistNewFolder(folder);
}
@@ -71,16 +59,12 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
}
}
public void PersistUpdatedItem(IEntity entity)
{
PersistUpdatedItem((TEntity)entity);
}
public void PersistUpdatedItem(IEntity entity) => PersistUpdatedItem((TEntity)entity);
public void PersistDeletedItem(IEntity entity)
{
//special case for folder
var folder = entity as Folder;
if (folder != null)
if (entity is Folder folder)
{
PersistDeletedFolder(folder);
}
@@ -92,21 +76,15 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
#endregion
internal virtual void PersistNewFolder(Folder entity)
{
FileSystem.CreateFolder(entity.Path);
}
internal virtual void PersistNewFolder(Folder entity) => FileSystem.CreateFolder(entity.Path);
internal virtual void PersistDeletedFolder(Folder entity)
{
FileSystem.DeleteDirectory(entity.Path);
}
internal virtual void PersistDeletedFolder(Folder entity) => FileSystem.DeleteDirectory(entity.Path);
#region Abstract IUnitOfWorkRepository Methods
protected virtual void PersistNewItem(TEntity entity)
{
using (var stream = GetContentStream(entity.Content))
using (Stream stream = GetContentStream(entity.Content))
{
FileSystem.AddFile(entity.Path, stream, true);
entity.CreateDate = FileSystem.GetCreated(entity.Path).UtcDateTime;
@@ -120,7 +98,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
protected virtual void PersistUpdatedItem(TEntity entity)
{
using (var stream = GetContentStream(entity.Content))
using (Stream stream = GetContentStream(entity.Content))
{
FileSystem.AddFile(entity.Path, stream, true);
entity.CreateDate = FileSystem.GetCreated(entity.Path).UtcDateTime;
@@ -156,10 +134,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
protected virtual Stream GetContentStream(string content)
{
return new MemoryStream(Encoding.UTF8.GetBytes(content));
}
protected virtual Stream GetContentStream(string content) => new MemoryStream(Encoding.UTF8.GetBytes(content));
/// <summary>
/// Returns all files in the file system
@@ -179,7 +154,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
var list = new List<string>();
list.AddRange(FileSystem.GetFiles(path, filter));
var directories = FileSystem.GetDirectories(path);
IEnumerable<string> directories = FileSystem.GetDirectories(path);
foreach (var directory in directories)
{
list.AddRange(FindAllFiles(directory, filter));
@@ -191,11 +166,13 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
protected string GetFileContent(string filename)
{
if (FileSystem.FileExists(filename) == false)
{
return null;
}
try
{
using (var stream = FileSystem.OpenFile(filename))
using (Stream stream = FileSystem.OpenFile(filename))
using (var reader = new StreamReader(stream, Encoding.UTF8, true))
{
return reader.ReadToEnd();
@@ -207,10 +184,31 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
}
}
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);
public long GetFileSize(string filename)
{
if (FileSystem.FileExists(filename) == false)
{
return -1;
}
try
{

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Models;

View File

@@ -39,39 +39,38 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
_viewHelper = new ViewHelper(_viewsFileSystem);
}
protected override IRepositoryCachePolicy<ITemplate, int> CreateCachePolicy()
{
return new FullDataSetRepositoryCachePolicy<ITemplate, int>(GlobalIsolatedCache, ScopeAccessor, GetEntityId, /*expires:*/ false);
}
protected override IRepositoryCachePolicy<ITemplate, int> CreateCachePolicy() =>
new FullDataSetRepositoryCachePolicy<ITemplate, int>(GlobalIsolatedCache, ScopeAccessor, GetEntityId, /*expires:*/ false);
#region Overrides of RepositoryBase<int,ITemplate>
protected override ITemplate PerformGet(int id)
{
protected override ITemplate PerformGet(int id) =>
//use the underlying GetAll which will force cache all templates
return base.GetMany().FirstOrDefault(x => x.Id == id);
}
base.GetMany().FirstOrDefault(x => x.Id == id);
protected override IEnumerable<ITemplate> PerformGetAll(params int[] ids)
{
var sql = GetBaseQuery(false);
Sql<ISqlContext> sql = GetBaseQuery(false);
if (ids.Any())
{
sql.Where("umbracoNode.id in (@ids)", new { ids = ids });
sql.Where("umbracoNode.id in (@ids)", new { ids });
}
else
{
sql.Where<NodeDto>(x => x.NodeObjectType == NodeObjectTypeId);
}
var dtos = Database.Fetch<TemplateDto>(sql);
List<TemplateDto> dtos = Database.Fetch<TemplateDto>(sql);
if (dtos.Count == 0) return Enumerable.Empty<ITemplate>();
if (dtos.Count == 0)
{
return Enumerable.Empty<ITemplate>();
}
//look up the simple template definitions that have a master template assigned, this is used
// later to populate the template item's properties
var childIds = (ids.Any()
IUmbracoEntity[] childIds = (ids.Any()
? GetAxisDefinitions(dtos.ToArray())
: dtos.Select(x => new EntitySlim
{
@@ -85,17 +84,20 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
protected override IEnumerable<ITemplate> PerformGetByQuery(IQuery<ITemplate> query)
{
var sqlClause = GetBaseQuery(false);
Sql<ISqlContext> sqlClause = GetBaseQuery(false);
var translator = new SqlTranslator<ITemplate>(sqlClause, query);
var sql = translator.Translate();
Sql<ISqlContext> sql = translator.Translate();
var dtos = Database.Fetch<TemplateDto>(sql);
List<TemplateDto> dtos = Database.Fetch<TemplateDto>(sql);
if (dtos.Count == 0) return Enumerable.Empty<ITemplate>();
if (dtos.Count == 0)
{
return Enumerable.Empty<ITemplate>();
}
//look up the simple template definitions that have a master template assigned, this is used
// later to populate the template item's properties
var childIds = GetAxisDefinitions(dtos.ToArray()).ToArray();
IUmbracoEntity[] childIds = GetAxisDefinitions(dtos.ToArray()).ToArray();
return dtos.Select(d => MapFromDto(d, childIds));
}
@@ -106,7 +108,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
protected override Sql<ISqlContext> GetBaseQuery(bool isCount)
{
var sql = SqlContext.Sql();
Sql<ISqlContext> sql = SqlContext.Sql();
sql = isCount
? sql.SelectCount()
@@ -121,10 +123,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
return sql;
}
protected override string GetBaseWhereClause()
{
return Cms.Core.Constants.DatabaseSchema.Tables.Node + ".id = @id";
}
protected override string GetBaseWhereClause() => Cms.Core.Constants.DatabaseSchema.Tables.Node + ".id = @id";
protected override IEnumerable<string> GetDeleteClauses()
{
@@ -150,15 +149,15 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
var template = (Template)entity;
template.AddingEntity();
var dto = TemplateFactory.BuildDto(template, NodeObjectTypeId, template.Id);
TemplateDto dto = TemplateFactory.BuildDto(template, NodeObjectTypeId, template.Id);
//Create the (base) node data - umbracoNode
var nodeDto = dto.NodeDto;
NodeDto nodeDto = dto.NodeDto;
nodeDto.Path = "-1," + dto.NodeDto.NodeId;
var o = Database.IsNew<NodeDto>(nodeDto) ? Convert.ToInt32(Database.Insert(nodeDto)) : Database.Update(nodeDto);
int o = Database.IsNew<NodeDto>(nodeDto) ? Convert.ToInt32(Database.Insert(nodeDto)) : Database.Update(nodeDto);
//Update with new correct path
var parent = Get(template.MasterTemplateId.Value);
ITemplate parent = Get(template.MasterTemplateId.Value);
if (parent != null)
{
nodeDto.Path = string.Concat(parent.Path, ",", nodeDto.NodeId);
@@ -184,7 +183,9 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
// ensure that from now on, content is lazy-loaded
if (template.GetFileContent == null)
{
template.GetFileContent = file => GetFileContent((Template) file, false);
}
}
protected override void PersistUpdatedItem(ITemplate entity)
@@ -192,11 +193,11 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
EnsureValidAlias(entity);
//store the changed alias if there is one for use with updating files later
var originalAlias = entity.Alias;
string originalAlias = entity.Alias;
if (entity.IsPropertyDirty("Alias"))
{
//we need to check what it currently is before saving and remove that file
var current = Get(entity.Id);
ITemplate current = Get(entity.Id);
originalAlias = current.Alias;
}
@@ -204,7 +205,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
if (entity.IsPropertyDirty("MasterTemplateId"))
{
var parent = Get(template.MasterTemplateId.Value);
ITemplate parent = Get(template.MasterTemplateId.Value);
if (parent != null)
{
entity.Path = string.Concat(parent.Path, ",", entity.Id);
@@ -218,17 +219,16 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
}
//Get TemplateDto from db to get the Primary key of the entity
var templateDto = Database.SingleOrDefault<TemplateDto>("WHERE nodeId = @Id", new { Id = entity.Id });
TemplateDto templateDto = Database.SingleOrDefault<TemplateDto>("WHERE nodeId = @Id", new { entity.Id });
//Save updated entity to db
template.UpdateDate = DateTime.Now;
var dto = TemplateFactory.BuildDto(template, NodeObjectTypeId, templateDto.PrimaryKey);
TemplateDto dto = TemplateFactory.BuildDto(template, NodeObjectTypeId, templateDto.PrimaryKey);
Database.Update(dto.NodeDto);
Database.Update(dto);
//re-update if this is a master template, since it could have changed!
var axisDefs = GetAxisDefinitions(dto);
IEnumerable<IUmbracoEntity> axisDefs = GetAxisDefinitions(dto);
template.IsMasterTemplate = axisDefs.Any(x => x.ParentId == dto.NodeId);
//now do the file work
@@ -238,15 +238,16 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
// ensure that from now on, content is lazy-loaded
if (template.GetFileContent == null)
{
template.GetFileContent = file => GetFileContent((Template) file, false);
}
}
private void SaveFile(Template template, string originalAlias = null)
{
string content;
var templateOnDisk = template as TemplateOnDisk;
if (templateOnDisk != null && templateOnDisk.IsOnDisk)
if (template is TemplateOnDisk templateOnDisk && templateOnDisk.IsOnDisk)
{
// if "template on disk" load content from disk
content = _viewHelper.GetFileContents(template);
@@ -266,7 +267,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
protected override void PersistDeletedItem(ITemplate entity)
{
var deletes = GetDeleteClauses().ToArray();
string[] deletes = GetDeleteClauses().ToArray();
var descendants = GetDescendants(entity.Id).ToList();
@@ -274,21 +275,21 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
descendants.Reverse();
//delete the hierarchy
foreach (var descendant in descendants)
foreach (ITemplate descendant in descendants)
{
foreach (var delete in deletes)
foreach (string delete in deletes)
{
Database.Execute(delete, new { id = GetEntityId(descendant) });
}
}
//now we can delete this one
foreach (var delete in deletes)
foreach (string delete in deletes)
{
Database.Execute(delete, new { id = GetEntityId(entity) });
}
var viewName = string.Concat(entity.Alias, ".cshtml");
string viewName = string.Concat(entity.Alias, ".cshtml");
_viewsFileSystem.DeleteFile(viewName);
entity.DeleteDate = DateTime.Now;
@@ -300,7 +301,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
{
//look up the simple template definitions that have a master template assigned, this is used
// later to populate the template item's properties
var childIdsSql = SqlContext.Sql()
Sql<ISqlContext> childIdsSql = SqlContext.Sql()
.Select("nodeId,alias,parentID")
.From<TemplateDto>()
.InnerJoin<NodeDto>()
@@ -309,7 +310,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
.Where("umbracoNode." + SqlContext.SqlSyntax.GetQuotedColumnName("id") + " IN (@parentIds) OR umbracoNode.parentID IN (@childIds)",
new {parentIds = templates.Select(x => x.NodeDto.ParentId), childIds = templates.Select(x => x.NodeId)});
var childIds = Database.Fetch<dynamic>(childIdsSql)
IEnumerable<EntitySlim> childIds = Database.Fetch<dynamic>(childIdsSql)
.Select(x => new EntitySlim
{
Id = x.nodeId,
@@ -330,11 +331,11 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
private ITemplate MapFromDto(TemplateDto dto, IUmbracoEntity[] axisDefinitions)
{
var template = TemplateFactory.BuildEntity(_shortStringHelper, dto, axisDefinitions, file => GetFileContent((Template) file, false));
Template template = TemplateFactory.BuildEntity(_shortStringHelper, dto, axisDefinitions, file => GetFileContent((Template) file, false));
if (dto.NodeDto.ParentId > 0)
{
var masterTemplate = axisDefinitions.FirstOrDefault(x => x.Id == dto.NodeDto.ParentId);
IUmbracoEntity masterTemplate = axisDefinitions.FirstOrDefault(x => x.Id == dto.NodeDto.ParentId);
if (masterTemplate != null)
{
template.MasterTemplateAlias = masterTemplate.Name;
@@ -354,7 +355,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
private void SetVirtualPath(ITemplate template)
{
var path = template.OriginalPath;
string path = template.OriginalPath;
if (string.IsNullOrWhiteSpace(path))
{
// we need to discover the path
@@ -382,16 +383,21 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
private string GetFileContent(ITemplate template, bool init)
{
var path = template.OriginalPath;
string path = template.OriginalPath;
if (string.IsNullOrWhiteSpace(path))
{
// we need to discover the path
path = string.Concat(template.Alias, ".cshtml");
if (_viewsFileSystem.FileExists(path))
{
return GetFileContent(template, _viewsFileSystem, path, init);
}
path = string.Concat(template.Alias, ".vbhtml");
if (_viewsFileSystem.FileExists(path))
{
return GetFileContent(template, _viewsFileSystem, path, init);
}
}
else
{
@@ -427,7 +433,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
private string GetFileContent(IFileSystem fs, string filename)
{
using (var stream = fs.OpenFile(filename))
using (Stream stream = fs.OpenFile(filename))
using (var reader = new StreamReader(stream, Encoding.UTF8, true))
{
return reader.ReadToEnd();
@@ -436,12 +442,15 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
public Stream GetFileContentStream(string filepath)
{
var fs = GetFileSystem(filepath);
if (fs.FileExists(filepath) == false) return null;
IFileSystem fileSystem = GetFileSystem(filepath);
if (fileSystem.FileExists(filepath) == false)
{
return null;
}
try
{
return GetFileSystem(filepath).OpenFile(filepath);
return fileSystem.OpenFile(filepath);
}
catch
{
@@ -449,9 +458,29 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
}
}
public void SetFileContent(string filepath, Stream content) => GetFileSystem(filepath).AddFile(filepath, content, true);
public long GetFileSize(string filename)
{
IFileSystem fileSystem = GetFileSystem(filename);
if (fileSystem.FileExists(filename) == false)
{
return -1;
}
try
{
return fileSystem.GetSize(filename);
}
catch
{
return -1; // deal with race conds
}
}
private IFileSystem GetFileSystem(string filepath)
{
var ext = Path.GetExtension(filepath);
string ext = Path.GetExtension(filepath);
IFileSystem fs;
switch (ext)
{
@@ -467,17 +496,17 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
#region Implementation of ITemplateRepository
public ITemplate Get(string alias)
{
return GetAll(alias).FirstOrDefault();
}
public ITemplate Get(string alias) => GetAll(alias).FirstOrDefault();
public IEnumerable<ITemplate> GetAll(params string[] aliases)
{
//We must call the base (normal) GetAll method
// which is cached. This is a specialized method and unfortunately with the params[] it
// overlaps with the normal GetAll method.
if (aliases.Any() == false) return base.GetMany();
if (aliases.Any() == false)
{
return base.GetMany();
}
//return from base.GetAll, this is all cached
return base.GetMany().Where(x => aliases.InvariantContains(x.Alias));
@@ -486,33 +515,43 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
public IEnumerable<ITemplate> GetChildren(int masterTemplateId)
{
//return from base.GetAll, this is all cached
var all = base.GetMany().ToArray();
ITemplate[] all = base.GetMany().ToArray();
if (masterTemplateId <= 0) return all.Where(x => x.MasterTemplateAlias.IsNullOrWhiteSpace());
if (masterTemplateId <= 0)
{
return all.Where(x => x.MasterTemplateAlias.IsNullOrWhiteSpace());
}
var parent = all.FirstOrDefault(x => x.Id == masterTemplateId);
if (parent == null) return Enumerable.Empty<ITemplate>();
ITemplate parent = all.FirstOrDefault(x => x.Id == masterTemplateId);
if (parent == null)
{
return Enumerable.Empty<ITemplate>();
}
var children = all.Where(x => x.MasterTemplateAlias.InvariantEquals(parent.Alias));
IEnumerable<ITemplate> children = all.Where(x => x.MasterTemplateAlias.InvariantEquals(parent.Alias));
return children;
}
public IEnumerable<ITemplate> GetDescendants(int masterTemplateId)
{
//return from base.GetAll, this is all cached
var all = base.GetMany().ToArray();
ITemplate[] all = base.GetMany().ToArray();
var descendants = new List<ITemplate>();
if (masterTemplateId > 0)
{
var parent = all.FirstOrDefault(x => x.Id == masterTemplateId);
if (parent == null) return Enumerable.Empty<ITemplate>();
ITemplate parent = all.FirstOrDefault(x => x.Id == masterTemplateId);
if (parent == null)
{
return Enumerable.Empty<ITemplate>();
}
//recursively add all children with a level
AddChildren(all, descendants, parent.Alias);
}
else
{
descendants.AddRange(all.Where(x => x.MasterTemplateAlias.IsNullOrWhiteSpace()));
foreach (var parent in descendants)
foreach (ITemplate parent in descendants)
{
//recursively add all children with a level
AddChildren(all, descendants, parent.Alias);
@@ -525,11 +564,15 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
private void AddChildren(ITemplate[] all, List<ITemplate> descendants, string masterAlias)
{
var c = all.Where(x => x.MasterTemplateAlias.InvariantEquals(masterAlias)).ToArray();
ITemplate[] c = all.Where(x => x.MasterTemplateAlias.InvariantEquals(masterAlias)).ToArray();
descendants.AddRange(c);
if (c.Any() == false) return;
if (c.Any() == false)
{
return;
}
//recurse through all children
foreach (var child in c)
foreach (ITemplate child in c)
{
AddChildren(all, descendants, child.Alias);
}
@@ -547,7 +590,9 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
template.Alias = template.Alias.ToCleanString(_shortStringHelper, CleanStringType.UnderscoreAlias);
if (template.Alias.Length > 100)
{
template.Alias = template.Alias.Substring(0, 95);
}
if (AliasAlreadExists(template))
{
@@ -557,8 +602,8 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
private bool AliasAlreadExists(ITemplate template)
{
var sql = GetBaseQuery(true).Where<TemplateDto>(x => x.Alias.InvariantEquals(template.Alias) && x.NodeId != template.Id);
var count = Database.ExecuteScalar<int>(sql);
Sql<ISqlContext> sql = GetBaseQuery(true).Where<TemplateDto>(x => x.Alias.InvariantEquals(template.Alias) && x.NodeId != template.Id);
int count = Database.ExecuteScalar<int>(sql);
return count > 0;
}
@@ -566,7 +611,10 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
{
// TODO: This is ported from the old data layer... pretty crap way of doing this but it works for now.
if (AliasAlreadExists(template))
{
return template.Alias + attempts;
}
attempts++;
return EnsureUniqueAlias(template, attempts);
}

View File

@@ -10,6 +10,7 @@ using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Persistence.Querying;
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Core.Strings;
@@ -57,7 +58,7 @@ namespace Umbraco.Cms.Core.Services.Implement
/// <inheritdoc />
public IEnumerable<IStylesheet> GetStylesheets(params string[] names)
{
using (var scope = ScopeProvider.CreateScope(autoComplete: true))
using (IScope scope = ScopeProvider.CreateScope(autoComplete: true))
{
return _stylesheetRepository.GetMany(names);
}
@@ -66,14 +67,14 @@ namespace Umbraco.Cms.Core.Services.Implement
/// <inheritdoc />
public IStylesheet GetStylesheetByName(string name)
{
using (var scope = ScopeProvider.CreateScope(autoComplete: true))
using (IScope scope = ScopeProvider.CreateScope(autoComplete: true))
{
return _stylesheetRepository.Get(name);
}
}
/// <inheritdoc />
public void SaveStylesheet(IStylesheet stylesheet, int userId = Cms.Core.Constants.Security.SuperUserId)
public void SaveStylesheet(IStylesheet stylesheet, int userId = Constants.Security.SuperUserId)
{
using (IScope scope = ScopeProvider.CreateScope())
{
@@ -95,7 +96,7 @@ namespace Umbraco.Cms.Core.Services.Implement
}
/// <inheritdoc />
public void DeleteStylesheet(string path, int userId = Cms.Core.Constants.Security.SuperUserId)
public void DeleteStylesheet(string path, int userId = Constants.Security.SuperUserId)
{
using (IScope scope = ScopeProvider.CreateScope())
{
@@ -123,24 +124,54 @@ namespace Umbraco.Cms.Core.Services.Implement
}
}
/// <inheritdoc />
public void CreateStyleSheetFolder(string folderPath)
{
using (var scope = ScopeProvider.CreateScope())
using (IScope scope = ScopeProvider.CreateScope())
{
_stylesheetRepository.AddFolder(folderPath);
scope.Complete();
}
}
/// <inheritdoc />
public void DeleteStyleSheetFolder(string folderPath)
{
using (var scope = ScopeProvider.CreateScope())
using (IScope scope = ScopeProvider.CreateScope())
{
_stylesheetRepository.DeleteFolder(folderPath);
scope.Complete();
}
}
/// <inheritdoc />
public Stream GetStylesheetFileContentStream(string filepath)
{
using (IScope scope = ScopeProvider.CreateScope(autoComplete: true))
{
return _stylesheetRepository.GetFileContentStream(filepath);
}
}
/// <inheritdoc />
public void SetStylesheetFileContent(string filepath, Stream content)
{
using (IScope scope = ScopeProvider.CreateScope())
{
_stylesheetRepository.SetFileContent(filepath, content);
scope.Complete();
}
}
/// <inheritdoc />
public long GetStylesheetFileSize(string filepath)
{
using (IScope scope = ScopeProvider.CreateScope(autoComplete: true))
{
return _stylesheetRepository.GetFileSize(filepath);
}
}
#endregion
#region Scripts
@@ -148,14 +179,14 @@ namespace Umbraco.Cms.Core.Services.Implement
/// <inheritdoc />
public IScript GetScriptByName(string name)
{
using (var scope = ScopeProvider.CreateScope(autoComplete: true))
using (IScope scope = ScopeProvider.CreateScope(autoComplete: true))
{
return _scriptRepository.Get(name);
}
}
/// <inheritdoc />
public void SaveScript(IScript script, int userId = Cms.Core.Constants.Security.SuperUserId)
public void SaveScript(IScript script, int userId = Constants.Security.SuperUserId)
{
using (IScope scope = ScopeProvider.CreateScope())
{
@@ -176,7 +207,7 @@ namespace Umbraco.Cms.Core.Services.Implement
}
/// <inheritdoc />
public void DeleteScript(string path, int userId = Cms.Core.Constants.Security.SuperUserId)
public void DeleteScript(string path, int userId = Constants.Security.SuperUserId)
{
using (IScope scope = ScopeProvider.CreateScope())
{
@@ -203,24 +234,54 @@ namespace Umbraco.Cms.Core.Services.Implement
}
}
/// <inheritdoc />
public void CreateScriptFolder(string folderPath)
{
using (var scope = ScopeProvider.CreateScope())
using (IScope scope = ScopeProvider.CreateScope())
{
_scriptRepository.AddFolder(folderPath);
scope.Complete();
}
}
/// <inheritdoc />
public void DeleteScriptFolder(string folderPath)
{
using (var scope = ScopeProvider.CreateScope())
using (IScope scope = ScopeProvider.CreateScope())
{
_scriptRepository.DeleteFolder(folderPath);
scope.Complete();
}
}
/// <inheritdoc />
public Stream GetScriptFileContentStream(string filepath)
{
using (IScope scope = ScopeProvider.CreateScope(autoComplete: true))
{
return _scriptRepository.GetFileContentStream(filepath);
}
}
/// <inheritdoc />
public void SetScriptFileContent(string filepath, Stream content)
{
using (IScope scope = ScopeProvider.CreateScope())
{
_scriptRepository.SetFileContent(filepath, content);
scope.Complete();
}
}
/// <inheritdoc />
public long GetScriptFileSize(string filepath)
{
using (IScope scope = ScopeProvider.CreateScope(autoComplete: true))
{
return _scriptRepository.GetFileSize(filepath);
}
}
#endregion
#region Templates
@@ -234,7 +295,7 @@ namespace Umbraco.Cms.Core.Services.Implement
/// <returns>
/// The template created
/// </returns>
public Attempt<OperationResult<OperationResultType, ITemplate>> CreateTemplateForContentType(string contentTypeAlias, string contentTypeName, int userId = Cms.Core.Constants.Security.SuperUserId)
public Attempt<OperationResult<OperationResultType, ITemplate>> CreateTemplateForContentType(string contentTypeAlias, string contentTypeName, int userId = Constants.Security.SuperUserId)
{
var template = new Template(_shortStringHelper, contentTypeName,
//NOTE: We are NOT passing in the content type alias here, we want to use it's name since we don't
@@ -286,7 +347,7 @@ namespace Umbraco.Cms.Core.Services.Implement
/// <param name="masterTemplate"></param>
/// <param name="userId"></param>
/// <returns></returns>
public ITemplate CreateTemplateWithIdentity(string name, string alias, string content, ITemplate masterTemplate = null, int userId = Cms.Core.Constants.Security.SuperUserId)
public ITemplate CreateTemplateWithIdentity(string name, string alias, string content, ITemplate masterTemplate = null, int userId = Constants.Security.SuperUserId)
{
if (name == null)
{
@@ -325,7 +386,7 @@ namespace Umbraco.Cms.Core.Services.Implement
/// <returns>An enumerable list of <see cref="ITemplate"/> objects</returns>
public IEnumerable<ITemplate> GetTemplates(params string[] aliases)
{
using (var scope = ScopeProvider.CreateScope(autoComplete: true))
using (IScope scope = ScopeProvider.CreateScope(autoComplete: true))
{
return _templateRepository.GetAll(aliases).OrderBy(x => x.Name);
}
@@ -337,7 +398,7 @@ namespace Umbraco.Cms.Core.Services.Implement
/// <returns>An enumerable list of <see cref="ITemplate"/> objects</returns>
public IEnumerable<ITemplate> GetTemplates(int masterTemplateId)
{
using (var scope = ScopeProvider.CreateScope(autoComplete: true))
using (IScope scope = ScopeProvider.CreateScope(autoComplete: true))
{
return _templateRepository.GetChildren(masterTemplateId).OrderBy(x => x.Name);
}
@@ -350,7 +411,7 @@ namespace Umbraco.Cms.Core.Services.Implement
/// <returns>The <see cref="ITemplate"/> object matching the alias, or null.</returns>
public ITemplate GetTemplate(string alias)
{
using (var scope = ScopeProvider.CreateScope(autoComplete: true))
using (IScope scope = ScopeProvider.CreateScope(autoComplete: true))
{
return _templateRepository.Get(alias);
}
@@ -363,7 +424,7 @@ namespace Umbraco.Cms.Core.Services.Implement
/// <returns>The <see cref="ITemplate"/> object matching the identifier, or null.</returns>
public ITemplate GetTemplate(int id)
{
using (var scope = ScopeProvider.CreateScope(autoComplete: true))
using (IScope scope = ScopeProvider.CreateScope(autoComplete: true))
{
return _templateRepository.Get(id);
}
@@ -376,9 +437,9 @@ namespace Umbraco.Cms.Core.Services.Implement
/// <returns>The <see cref="ITemplate"/> object matching the identifier, or null.</returns>
public ITemplate GetTemplate(Guid id)
{
using (var scope = ScopeProvider.CreateScope(autoComplete: true))
using (IScope scope = ScopeProvider.CreateScope(autoComplete: true))
{
var query = Query<ITemplate>().Where(x => x.Key == id);
IQuery<ITemplate> query = Query<ITemplate>().Where(x => x.Key == id);
return _templateRepository.Get(query).SingleOrDefault();
}
}
@@ -390,7 +451,7 @@ namespace Umbraco.Cms.Core.Services.Implement
/// <returns></returns>
public IEnumerable<ITemplate> GetTemplateDescendants(int masterTemplateId)
{
using (var scope = ScopeProvider.CreateScope(autoComplete: true))
using (IScope scope = ScopeProvider.CreateScope(autoComplete: true))
{
return _templateRepository.GetDescendants(masterTemplateId);
}
@@ -401,7 +462,7 @@ namespace Umbraco.Cms.Core.Services.Implement
/// </summary>
/// <param name="template"><see cref="Template"/> to save</param>
/// <param name="userId"></param>
public void SaveTemplate(ITemplate template, int userId = Cms.Core.Constants.Security.SuperUserId)
public void SaveTemplate(ITemplate template, int userId = Constants.Security.SuperUserId)
{
if (template == null)
{
@@ -438,7 +499,7 @@ namespace Umbraco.Cms.Core.Services.Implement
/// </summary>
/// <param name="templates">List of <see cref="Template"/> to save</param>
/// <param name="userId">Optional id of the user</param>
public void SaveTemplate(IEnumerable<ITemplate> templates, int userId = Cms.Core.Constants.Security.SuperUserId)
public void SaveTemplate(IEnumerable<ITemplate> templates, int userId = Constants.Security.SuperUserId)
{
ITemplate[] templatesA = templates.ToArray();
using (IScope scope = ScopeProvider.CreateScope())
@@ -468,7 +529,7 @@ namespace Umbraco.Cms.Core.Services.Implement
/// </summary>
/// <param name="alias">Alias of the <see cref="ITemplate"/> to delete</param>
/// <param name="userId"></param>
public void DeleteTemplate(string alias, int userId = Cms.Core.Constants.Security.SuperUserId)
public void DeleteTemplate(string alias, int userId = Constants.Security.SuperUserId)
{
using (IScope scope = ScopeProvider.CreateScope())
{
@@ -499,22 +560,58 @@ namespace Umbraco.Cms.Core.Services.Implement
private string GetViewContent(string fileName)
{
if (fileName.IsNullOrWhiteSpace())
{
throw new ArgumentNullException(nameof(fileName));
}
if (!fileName.EndsWith(".cshtml"))
{
fileName = $"{fileName}.cshtml";
}
Stream fs = _templateRepository.GetFileContentStream(fileName);
if (fs == null)
{
return null;
}
var fs = _templateRepository.GetFileContentStream(fileName);
if (fs == null) return null;
using (var view = new StreamReader(fs))
{
return view.ReadToEnd().Trim();
}
}
#endregion
/// <inheritdoc />
public Stream GetTemplateFileContentStream(string filepath)
{
using (IScope scope = ScopeProvider.CreateScope(autoComplete: true))
{
return _templateRepository.GetFileContentStream(filepath);
}
}
#region Partial Views
/// <inheritdoc />
public void SetTemplateFileContent(string filepath, Stream content)
{
using (IScope scope = ScopeProvider.CreateScope())
{
_templateRepository.SetFileContent(filepath, content);
scope.Complete();
}
}
/// <inheritdoc />
public long GetTemplateFileSize(string filepath)
{
using (IScope scope = ScopeProvider.CreateScope(autoComplete: true))
{
return _templateRepository.GetFileSize(filepath);
}
}
#endregion
#region Partial Views
public IEnumerable<string> GetPartialViewSnippetNames(params string[] filterNames)
{
@@ -534,7 +631,7 @@ namespace Umbraco.Cms.Core.Services.Implement
public void DeletePartialViewFolder(string folderPath)
{
using (var scope = ScopeProvider.CreateScope())
using (IScope scope = ScopeProvider.CreateScope())
{
_partialViewRepository.DeleteFolder(folderPath);
scope.Complete();
@@ -543,7 +640,7 @@ namespace Umbraco.Cms.Core.Services.Implement
public void DeletePartialViewMacroFolder(string folderPath)
{
using (var scope = ScopeProvider.CreateScope())
using (IScope scope = ScopeProvider.CreateScope())
{
_partialViewMacroRepository.DeleteFolder(folderPath);
scope.Complete();
@@ -552,7 +649,7 @@ namespace Umbraco.Cms.Core.Services.Implement
public IPartialView GetPartialView(string path)
{
using (var scope = ScopeProvider.CreateScope(autoComplete: true))
using (IScope scope = ScopeProvider.CreateScope(autoComplete: true))
{
return _partialViewRepository.Get(path);
}
@@ -560,23 +657,19 @@ namespace Umbraco.Cms.Core.Services.Implement
public IPartialView GetPartialViewMacro(string path)
{
using (var scope = ScopeProvider.CreateScope(autoComplete: true))
using (IScope scope = ScopeProvider.CreateScope(autoComplete: true))
{
return _partialViewMacroRepository.Get(path);
}
}
public Attempt<IPartialView> CreatePartialView(IPartialView partialView, string snippetName = null, int userId = Cms.Core.Constants.Security.SuperUserId)
{
return CreatePartialViewMacro(partialView, PartialViewType.PartialView, snippetName, userId);
}
public Attempt<IPartialView> CreatePartialView(IPartialView partialView, string snippetName = null, int userId = Constants.Security.SuperUserId) =>
CreatePartialViewMacro(partialView, PartialViewType.PartialView, snippetName, userId);
public Attempt<IPartialView> CreatePartialViewMacro(IPartialView partialView, string snippetName = null, int userId = Cms.Core.Constants.Security.SuperUserId)
{
return CreatePartialViewMacro(partialView, PartialViewType.PartialViewMacro, snippetName, userId);
}
public Attempt<IPartialView> CreatePartialViewMacro(IPartialView partialView, string snippetName = null, int userId = Constants.Security.SuperUserId) =>
CreatePartialViewMacro(partialView, PartialViewType.PartialViewMacro, snippetName, userId);
private Attempt<IPartialView> CreatePartialViewMacro(IPartialView partialView, PartialViewType partialViewType, string snippetName = null, int userId = Cms.Core.Constants.Security.SuperUserId)
private Attempt<IPartialView> CreatePartialViewMacro(IPartialView partialView, PartialViewType partialViewType, string snippetName = null, int userId = Constants.Security.SuperUserId)
{
string partialViewHeader;
switch (partialViewType)
@@ -646,17 +739,13 @@ namespace Umbraco.Cms.Core.Services.Implement
return Attempt<IPartialView>.Succeed(partialView);
}
public bool DeletePartialView(string path, int userId = Cms.Core.Constants.Security.SuperUserId)
{
return DeletePartialViewMacro(path, PartialViewType.PartialView, userId);
}
public bool DeletePartialView(string path, int userId = Constants.Security.SuperUserId) =>
DeletePartialViewMacro(path, PartialViewType.PartialView, userId);
public bool DeletePartialViewMacro(string path, int userId = Cms.Core.Constants.Security.SuperUserId)
{
return DeletePartialViewMacro(path, PartialViewType.PartialViewMacro, userId);
}
public bool DeletePartialViewMacro(string path, int userId = Constants.Security.SuperUserId) =>
DeletePartialViewMacro(path, PartialViewType.PartialViewMacro, userId);
private bool DeletePartialViewMacro(string path, PartialViewType partialViewType, int userId = Cms.Core.Constants.Security.SuperUserId)
private bool DeletePartialViewMacro(string path, PartialViewType partialViewType, int userId = Constants.Security.SuperUserId)
{
using (IScope scope = ScopeProvider.CreateScope())
{
@@ -686,17 +775,13 @@ namespace Umbraco.Cms.Core.Services.Implement
return true;
}
public Attempt<IPartialView> SavePartialView(IPartialView partialView, int userId = Cms.Core.Constants.Security.SuperUserId)
{
return SavePartialView(partialView, PartialViewType.PartialView, userId);
}
public Attempt<IPartialView> SavePartialView(IPartialView partialView, int userId = Constants.Security.SuperUserId) =>
SavePartialView(partialView, PartialViewType.PartialView, userId);
public Attempt<IPartialView> SavePartialViewMacro(IPartialView partialView, int userId = Cms.Core.Constants.Security.SuperUserId)
{
return SavePartialView(partialView, PartialViewType.PartialViewMacro, userId);
}
public Attempt<IPartialView> SavePartialViewMacro(IPartialView partialView, int userId = Constants.Security.SuperUserId) =>
SavePartialView(partialView, PartialViewType.PartialViewMacro, userId);
private Attempt<IPartialView> SavePartialView(IPartialView partialView, PartialViewType partialViewType, int userId = Cms.Core.Constants.Security.SuperUserId)
private Attempt<IPartialView> SavePartialView(IPartialView partialView, PartialViewType partialViewType, int userId = Constants.Security.SuperUserId)
{
using (IScope scope = ScopeProvider.CreateScope())
{
@@ -741,7 +826,7 @@ namespace Umbraco.Cms.Core.Services.Implement
public void CreatePartialViewFolder(string folderPath)
{
using (var scope = ScopeProvider.CreateScope())
using (IScope scope = ScopeProvider.CreateScope())
{
_partialViewRepository.AddFolder(folderPath);
scope.Complete();
@@ -750,7 +835,7 @@ namespace Umbraco.Cms.Core.Services.Implement
public void CreatePartialViewMacroFolder(string folderPath)
{
using (var scope = ScopeProvider.CreateScope())
using (IScope scope = ScopeProvider.CreateScope())
{
_partialViewMacroRepository.AddFolder(folderPath);
scope.Complete();
@@ -770,24 +855,76 @@ namespace Umbraco.Cms.Core.Services.Implement
}
}
/// <inheritdoc />
public Stream GetPartialViewFileContentStream(string filepath)
{
using (IScope scope = ScopeProvider.CreateScope(autoComplete: true))
{
return _partialViewRepository.GetFileContentStream(filepath);
}
}
/// <inheritdoc />
public void SetPartialViewFileContent(string filepath, Stream content)
{
using (IScope scope = ScopeProvider.CreateScope())
{
_partialViewRepository.SetFileContent(filepath, content);
scope.Complete();
}
}
/// <inheritdoc />
public long GetPartialViewFileSize(string filepath)
{
using (IScope scope = ScopeProvider.CreateScope(autoComplete: true))
{
return _partialViewRepository.GetFileSize(filepath);
}
}
/// <inheritdoc />
public Stream GetPartialViewMacroFileContentStream(string filepath)
{
using (IScope scope = ScopeProvider.CreateScope(autoComplete: true))
{
return _partialViewMacroRepository.GetFileContentStream(filepath);
}
}
/// <inheritdoc />
public void SetPartialViewMacroFileContent(string filepath, Stream content)
{
using (IScope scope = ScopeProvider.CreateScope())
{
_partialViewMacroRepository.SetFileContent(filepath, content);
scope.Complete();
}
}
/// <inheritdoc />
public long GetPartialViewMacroFileSize(string filepath)
{
using (IScope scope = ScopeProvider.CreateScope(autoComplete: true))
{
return _partialViewMacroRepository.GetFileSize(filepath);
}
}
#endregion
#region Snippets
public string GetPartialViewSnippetContent(string snippetName)
{
return GetPartialViewMacroSnippetContent(snippetName, PartialViewType.PartialView);
}
public string GetPartialViewSnippetContent(string snippetName) => GetPartialViewMacroSnippetContent(snippetName, PartialViewType.PartialView);
public string GetPartialViewMacroSnippetContent(string snippetName)
{
return GetPartialViewMacroSnippetContent(snippetName, PartialViewType.PartialViewMacro);
}
public string GetPartialViewMacroSnippetContent(string snippetName) => GetPartialViewMacroSnippetContent(snippetName, PartialViewType.PartialViewMacro);
private string GetPartialViewMacroSnippetContent(string snippetName, PartialViewType partialViewType)
{
if (snippetName.IsNullOrWhiteSpace())
{
throw new ArgumentNullException(nameof(snippetName));
}
string partialViewHeader;
switch (partialViewType)
@@ -803,7 +940,7 @@ namespace Umbraco.Cms.Core.Services.Implement
}
// Try and get the snippet path
var snippetPathAttempt = TryGetSnippetPath(snippetName);
Attempt<string> snippetPathAttempt = TryGetSnippetPath(snippetName);
if (snippetPathAttempt.Success == false)
{
throw new InvalidOperationException("Could not load snippet with name " + snippetName);
@@ -831,10 +968,7 @@ namespace Umbraco.Cms.Core.Services.Implement
#endregion
private void Audit(AuditType type, int userId, int objectId, string entityType)
{
_auditRepository.Save(new AuditItem(objectId, type, userId, entityType));
}
private void Audit(AuditType type, int userId, int objectId, string entityType) => _auditRepository.Save(new AuditItem(objectId, type, userId, entityType));
// TODO: Method to change name and/or alias of view template
}