From fd9faf5a995cb55d86d4f98184b56df77979e188 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Wed, 12 May 2021 11:58:36 +0200 Subject: [PATCH] Re-implemented various file service methods from V8 required in Deploy. --- .../Repositories/IFileRepository.cs | 13 + .../IFileWithFoldersRepository.cs | 9 + .../Repositories/IPartialViewRepository.cs | 4 +- .../Repositories/IScriptRepository.cs | 5 +- .../Repositories/IStylesheetRepository.cs | 4 +- .../Repositories/ITemplateRepository.cs | 10 +- src/Umbraco.Core/Services/IFileService.cs | 119 +++++++- .../Repositories/Implement/FileRepository.cs | 86 +++--- .../Implement/ScriptRepository.cs | 3 +- .../Implement/TemplateRepository.cs | 189 +++++++----- .../Services/Implement/FileService.cs | 280 +++++++++++++----- 11 files changed, 508 insertions(+), 214 deletions(-) create mode 100644 src/Umbraco.Core/Persistence/Repositories/IFileRepository.cs create mode 100644 src/Umbraco.Core/Persistence/Repositories/IFileWithFoldersRepository.cs diff --git a/src/Umbraco.Core/Persistence/Repositories/IFileRepository.cs b/src/Umbraco.Core/Persistence/Repositories/IFileRepository.cs new file mode 100644 index 0000000000..ce76086ed2 --- /dev/null +++ b/src/Umbraco.Core/Persistence/Repositories/IFileRepository.cs @@ -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); + } +} diff --git a/src/Umbraco.Core/Persistence/Repositories/IFileWithFoldersRepository.cs b/src/Umbraco.Core/Persistence/Repositories/IFileWithFoldersRepository.cs new file mode 100644 index 0000000000..77c2f9d40b --- /dev/null +++ b/src/Umbraco.Core/Persistence/Repositories/IFileWithFoldersRepository.cs @@ -0,0 +1,9 @@ +namespace Umbraco.Cms.Core.Persistence.Repositories +{ + public interface IFileWithFoldersRepository + { + void AddFolder(string folderPath); + + void DeleteFolder(string folderPath); + } +} diff --git a/src/Umbraco.Core/Persistence/Repositories/IPartialViewRepository.cs b/src/Umbraco.Core/Persistence/Repositories/IPartialViewRepository.cs index d1e322dd4c..a8a84079fa 100644 --- a/src/Umbraco.Core/Persistence/Repositories/IPartialViewRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/IPartialViewRepository.cs @@ -2,9 +2,7 @@ using Umbraco.Cms.Core.Models; namespace Umbraco.Cms.Core.Persistence.Repositories { - public interface IPartialViewRepository : IReadRepository, IWriteRepository + public interface IPartialViewRepository : IReadRepository, IWriteRepository, IFileRepository, IFileWithFoldersRepository { - void AddFolder(string folderPath); - void DeleteFolder(string folderPath); } } diff --git a/src/Umbraco.Core/Persistence/Repositories/IScriptRepository.cs b/src/Umbraco.Core/Persistence/Repositories/IScriptRepository.cs index d3835b8e7e..604e1da8d2 100644 --- a/src/Umbraco.Core/Persistence/Repositories/IScriptRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/IScriptRepository.cs @@ -1,10 +1,9 @@ +using System.IO; using Umbraco.Cms.Core.Models; namespace Umbraco.Cms.Core.Persistence.Repositories { - public interface IScriptRepository : IReadRepository, IWriteRepository + public interface IScriptRepository : IReadRepository, IWriteRepository, IFileRepository, IFileWithFoldersRepository { - void AddFolder(string folderPath); - void DeleteFolder(string folderPath); } } diff --git a/src/Umbraco.Core/Persistence/Repositories/IStylesheetRepository.cs b/src/Umbraco.Core/Persistence/Repositories/IStylesheetRepository.cs index ce57f2c9ae..dcdb5debe7 100644 --- a/src/Umbraco.Core/Persistence/Repositories/IStylesheetRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/IStylesheetRepository.cs @@ -2,9 +2,7 @@ using Umbraco.Cms.Core.Models; namespace Umbraco.Cms.Core.Persistence.Repositories { - public interface IStylesheetRepository : IReadRepository, IWriteRepository + public interface IStylesheetRepository : IReadRepository, IWriteRepository, IFileRepository, IFileWithFoldersRepository { - void AddFolder(string folderPath); - void DeleteFolder(string folderPath); } } diff --git a/src/Umbraco.Core/Persistence/Repositories/ITemplateRepository.cs b/src/Umbraco.Core/Persistence/Repositories/ITemplateRepository.cs index 7e6a81864c..3c9174e818 100644 --- a/src/Umbraco.Core/Persistence/Repositories/ITemplateRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/ITemplateRepository.cs @@ -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 + public interface ITemplateRepository : IReadWriteQueryRepository, IFileRepository { ITemplate Get(string alias); @@ -13,12 +12,5 @@ namespace Umbraco.Cms.Core.Persistence.Repositories IEnumerable GetChildren(int masterTemplateId); IEnumerable GetDescendants(int masterTemplateId); - - /// - /// Gets the content of a template as a stream. - /// - /// The filesystem path to the template. - /// The content of the template. - Stream GetFileContentStream(string filepath); } } diff --git a/src/Umbraco.Core/Services/IFileService.cs b/src/Umbraco.Core/Services/IFileService.cs index eea1bb6b22..5df5602bc6 100644 --- a/src/Umbraco.Core/Services/IFileService.cs +++ b/src/Umbraco.Core/Services/IFileService.cs @@ -24,6 +24,48 @@ namespace Umbraco.Cms.Core.Services Attempt SavePartialView(IPartialView partialView, int userId = Constants.Security.SuperUserId); Attempt SavePartialViewMacro(IPartialView partialView, int userId = Constants.Security.SuperUserId); + /// + /// Gets the content of a partial view as a stream. + /// + /// The filesystem path to the partial view. + /// The content of the partial view. + Stream GetPartialViewFileContentStream(string filepath); + + /// + /// Sets the content of a partial view. + /// + /// The filesystem path to the partial view. + /// The content of the partial view. + void SetPartialViewFileContent(string filepath, Stream content); + + /// + /// Gets the size of a partial view. + /// + /// The filesystem path to the partial view. + /// The size of the partial view. + long GetPartialViewFileSize(string filepath); + + /// + /// Gets the content of a macro partial view as a stream. + /// + /// The filesystem path to the macro partial view. + /// The content of the macro partial view. + Stream GetPartialViewMacroFileContentStream(string filepath); + + /// + /// Sets the content of a macro partial view. + /// + /// The filesystem path to the macro partial view. + /// The content of the macro partial view. + void SetPartialViewMacroFileContent(string filepath, Stream content); + + /// + /// Gets the size of a macro partial view. + /// + /// The filesystem path to the macro partial view. + /// The size of the macro partial view. + long GetPartialViewMacroFileSize(string filepath); + /// /// Gets a list of all objects /// @@ -51,6 +93,40 @@ namespace Umbraco.Cms.Core.Services /// Optional id of the user deleting the stylesheet void DeleteStylesheet(string path, int userId = Constants.Security.SuperUserId); + /// + /// Creates a folder for style sheets + /// + /// + /// + void CreateStyleSheetFolder(string folderPath); + + /// + /// Deletes a folder for style sheets + /// + /// + void DeleteStyleSheetFolder(string folderPath); + + /// + /// Gets the content of a stylesheet as a stream. + /// + /// The filesystem path to the stylesheet. + /// The content of the stylesheet. + Stream GetStylesheetFileContentStream(string filepath); + + /// + /// Sets the content of a stylesheet. + /// + /// The filesystem path to the stylesheet. + /// The content of the stylesheet. + void SetStylesheetFileContent(string filepath, Stream content); + + /// + /// Gets the size of a stylesheet. + /// + /// The filesystem path to the stylesheet. + /// The size of the stylesheet. + long GetStylesheetFileSize(string filepath); + /// /// Gets a object by its name /// @@ -86,17 +162,25 @@ namespace Umbraco.Cms.Core.Services void DeleteScriptFolder(string folderPath); /// - /// Creates a folder for style sheets + /// Gets the content of a script file as a stream. /// - /// - /// - void CreateStyleSheetFolder(string folderPath); + /// The filesystem path to the script. + /// The content of the script file. + Stream GetScriptFileContentStream(string filepath); /// - /// Deletes a folder for style sheets + /// Sets the content of a script file. /// - /// - void DeleteStyleSheetFolder(string folderPath); + /// The filesystem path to the script. + /// The content of the script file. + void SetScriptFileContent(string filepath, Stream content); + + /// + /// Gets the size of a script file. + /// + /// The filesystem path to the script file. + /// The size of the script file. + long GetScriptFileSize(string filepath); /// /// Gets a list of all objects @@ -172,6 +256,27 @@ namespace Umbraco.Cms.Core.Services /// Optional id of the user void SaveTemplate(IEnumerable templates, int userId = Constants.Security.SuperUserId); + /// + /// Gets the content of a template as a stream. + /// + /// The filesystem path to the template. + /// The content of the template. + Stream GetTemplateFileContentStream(string filepath); + + /// + /// Sets the content of a template. + /// + /// The filesystem path to the template. + /// The content of the template. + void SetTemplateFileContent(string filepath, Stream content); + + /// + /// Gets the size of a template. + /// + /// The filesystem path to the template. + /// The size of the template. + long GetTemplateFileSize(string filepath); + /// /// Gets the content of a macro partial view snippet as a string /// diff --git a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/FileRepository.cs b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/FileRepository.cs index 2e7e8af144..23365939e0 100644 --- a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/FileRepository.cs +++ b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/FileRepository.cs @@ -12,46 +12,35 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement internal abstract class FileRepository : IReadRepository, IWriteRepository 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 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 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 /// /// /// - 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)); /// /// Returns all files in the file system @@ -179,7 +154,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement var list = new List(); list.AddRange(FileSystem.GetFiles(path, filter)); - var directories = FileSystem.GetDirectories(path); + IEnumerable 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 { diff --git a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/ScriptRepository.cs b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/ScriptRepository.cs index 3cebe816fb..711a43f716 100644 --- a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/ScriptRepository.cs +++ b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/ScriptRepository.cs @@ -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; diff --git a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/TemplateRepository.cs b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/TemplateRepository.cs index cd42bd5dd5..37eb9fab79 100644 --- a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/TemplateRepository.cs +++ b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/TemplateRepository.cs @@ -39,39 +39,38 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement _viewHelper = new ViewHelper(_viewsFileSystem); } - protected override IRepositoryCachePolicy CreateCachePolicy() - { - return new FullDataSetRepositoryCachePolicy(GlobalIsolatedCache, ScopeAccessor, GetEntityId, /*expires:*/ false); - } + protected override IRepositoryCachePolicy CreateCachePolicy() => + new FullDataSetRepositoryCachePolicy(GlobalIsolatedCache, ScopeAccessor, GetEntityId, /*expires:*/ false); #region Overrides of RepositoryBase - 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 PerformGetAll(params int[] ids) { - var sql = GetBaseQuery(false); + Sql 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(x => x.NodeObjectType == NodeObjectTypeId); } - var dtos = Database.Fetch(sql); + List dtos = Database.Fetch(sql); - if (dtos.Count == 0) return Enumerable.Empty(); + if (dtos.Count == 0) + { + return Enumerable.Empty(); + } //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 PerformGetByQuery(IQuery query) { - var sqlClause = GetBaseQuery(false); + Sql sqlClause = GetBaseQuery(false); var translator = new SqlTranslator(sqlClause, query); - var sql = translator.Translate(); + Sql sql = translator.Translate(); - var dtos = Database.Fetch(sql); + List dtos = Database.Fetch(sql); - if (dtos.Count == 0) return Enumerable.Empty(); + if (dtos.Count == 0) + { + return Enumerable.Empty(); + } //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 GetBaseQuery(bool isCount) { - var sql = SqlContext.Sql(); + Sql 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 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) ? Convert.ToInt32(Database.Insert(nodeDto)) : Database.Update(nodeDto); + int o = Database.IsNew(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("WHERE nodeId = @Id", new { Id = entity.Id }); + TemplateDto templateDto = Database.SingleOrDefault("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 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 childIdsSql = SqlContext.Sql() .Select("nodeId,alias,parentID") .From() .InnerJoin() @@ -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(childIdsSql) + IEnumerable childIds = Database.Fetch(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,8 +442,11 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement public Stream GetFileContentStream(string filepath) { - var fs = GetFileSystem(filepath); - if (fs.FileExists(filepath) == false) return null; + IFileSystem fs = GetFileSystem(filepath); + if (fs.FileExists(filepath) == false) + { + return null; + } try { @@ -449,9 +458,28 @@ 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) + { + if (GetFileSystem(filename).FileExists(filename) == false) + { + return -1; + } + + try + { + return GetFileSystem(filename).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 +495,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 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 +514,43 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement public IEnumerable 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 parent = all.FirstOrDefault(x => x.Id == masterTemplateId); + if (parent == null) + { + return Enumerable.Empty(); + } - var children = all.Where(x => x.MasterTemplateAlias.InvariantEquals(parent.Alias)); + IEnumerable children = all.Where(x => x.MasterTemplateAlias.InvariantEquals(parent.Alias)); return children; } public IEnumerable 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(); if (masterTemplateId > 0) { - var parent = all.FirstOrDefault(x => x.Id == masterTemplateId); - if (parent == null) return Enumerable.Empty(); + ITemplate parent = all.FirstOrDefault(x => x.Id == masterTemplateId); + if (parent == null) + { + return Enumerable.Empty(); + } + //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 +563,15 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement private void AddChildren(ITemplate[] all, List 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 +589,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 +601,8 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement private bool AliasAlreadExists(ITemplate template) { - var sql = GetBaseQuery(true).Where(x => x.Alias.InvariantEquals(template.Alias) && x.NodeId != template.Id); - var count = Database.ExecuteScalar(sql); + Sql sql = GetBaseQuery(true).Where(x => x.Alias.InvariantEquals(template.Alias) && x.NodeId != template.Id); + int count = Database.ExecuteScalar(sql); return count > 0; } @@ -566,7 +610,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); } diff --git a/src/Umbraco.Infrastructure/Services/Implement/FileService.cs b/src/Umbraco.Infrastructure/Services/Implement/FileService.cs index 174adde65b..6d7d5b4490 100644 --- a/src/Umbraco.Infrastructure/Services/Implement/FileService.cs +++ b/src/Umbraco.Infrastructure/Services/Implement/FileService.cs @@ -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 /// public IEnumerable 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 /// public IStylesheet GetStylesheetByName(string name) { - using (var scope = ScopeProvider.CreateScope(autoComplete: true)) + using (IScope scope = ScopeProvider.CreateScope(autoComplete: true)) { return _stylesheetRepository.Get(name); } } /// - 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 } /// - 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 } } + /// public void CreateStyleSheetFolder(string folderPath) { - using (var scope = ScopeProvider.CreateScope()) + using (IScope scope = ScopeProvider.CreateScope()) { _stylesheetRepository.AddFolder(folderPath); scope.Complete(); } } + /// public void DeleteStyleSheetFolder(string folderPath) { - using (var scope = ScopeProvider.CreateScope()) + using (IScope scope = ScopeProvider.CreateScope()) { _stylesheetRepository.DeleteFolder(folderPath); scope.Complete(); } } + /// + public Stream GetStylesheetFileContentStream(string filepath) + { + using (IScope scope = ScopeProvider.CreateScope(autoComplete: true)) + { + return _stylesheetRepository.GetFileContentStream(filepath); + } + } + + /// + public void SetStylesheetFileContent(string filepath, Stream content) + { + using (IScope scope = ScopeProvider.CreateScope()) + { + _stylesheetRepository.SetFileContent(filepath, content); + scope.Complete(); + } + } + + /// + 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 /// public IScript GetScriptByName(string name) { - using (var scope = ScopeProvider.CreateScope(autoComplete: true)) + using (IScope scope = ScopeProvider.CreateScope(autoComplete: true)) { return _scriptRepository.Get(name); } } /// - 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 } /// - 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 } } + /// public void CreateScriptFolder(string folderPath) { - using (var scope = ScopeProvider.CreateScope()) + using (IScope scope = ScopeProvider.CreateScope()) { _scriptRepository.AddFolder(folderPath); scope.Complete(); } } + /// public void DeleteScriptFolder(string folderPath) { - using (var scope = ScopeProvider.CreateScope()) + using (IScope scope = ScopeProvider.CreateScope()) { _scriptRepository.DeleteFolder(folderPath); scope.Complete(); } } + /// + public Stream GetScriptFileContentStream(string filepath) + { + using (IScope scope = ScopeProvider.CreateScope(autoComplete: true)) + { + return _scriptRepository.GetFileContentStream(filepath); + } + } + + /// + public void SetScriptFileContent(string filepath, Stream content) + { + using (IScope scope = ScopeProvider.CreateScope()) + { + _scriptRepository.SetFileContent(filepath, content); + scope.Complete(); + } + } + + /// + 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 /// /// The template created /// - public Attempt> CreateTemplateForContentType(string contentTypeAlias, string contentTypeName, int userId = Cms.Core.Constants.Security.SuperUserId) + public Attempt> 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 /// /// /// - 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 /// An enumerable list of objects public IEnumerable 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 /// An enumerable list of objects public IEnumerable 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 /// The object matching the alias, or null. 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 /// The object matching the identifier, or null. 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 /// The object matching the identifier, or null. public ITemplate GetTemplate(Guid id) { - using (var scope = ScopeProvider.CreateScope(autoComplete: true)) + using (IScope scope = ScopeProvider.CreateScope(autoComplete: true)) { - var query = Query().Where(x => x.Key == id); + IQuery query = Query().Where(x => x.Key == id); return _templateRepository.Get(query).SingleOrDefault(); } } @@ -390,7 +451,7 @@ namespace Umbraco.Cms.Core.Services.Implement /// public IEnumerable 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 /// /// to save /// - 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 /// /// List of to save /// Optional id of the user - public void SaveTemplate(IEnumerable templates, int userId = Cms.Core.Constants.Security.SuperUserId) + public void SaveTemplate(IEnumerable 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 /// /// Alias of the to delete /// - 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 + /// + public Stream GetTemplateFileContentStream(string filepath) + { + using (IScope scope = ScopeProvider.CreateScope(autoComplete: true)) + { + return _templateRepository.GetFileContentStream(filepath); + } + } -#region Partial Views + /// + public void SetTemplateFileContent(string filepath, Stream content) + { + using (IScope scope = ScopeProvider.CreateScope()) + { + _templateRepository.SetFileContent(filepath, content); + scope.Complete(); + } + } + + /// + public long GetTemplateFileSize(string filepath) + { + using (IScope scope = ScopeProvider.CreateScope(autoComplete: true)) + { + return _templateRepository.GetFileSize(filepath); + } + } + + #endregion + + #region Partial Views public IEnumerable 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 CreatePartialView(IPartialView partialView, string snippetName = null, int userId = Cms.Core.Constants.Security.SuperUserId) - { - return CreatePartialViewMacro(partialView, PartialViewType.PartialView, snippetName, userId); - } + public Attempt CreatePartialView(IPartialView partialView, string snippetName = null, int userId = Constants.Security.SuperUserId) => + CreatePartialViewMacro(partialView, PartialViewType.PartialView, snippetName, userId); - public Attempt CreatePartialViewMacro(IPartialView partialView, string snippetName = null, int userId = Cms.Core.Constants.Security.SuperUserId) - { - return CreatePartialViewMacro(partialView, PartialViewType.PartialViewMacro, snippetName, userId); - } + public Attempt CreatePartialViewMacro(IPartialView partialView, string snippetName = null, int userId = Constants.Security.SuperUserId) => + CreatePartialViewMacro(partialView, PartialViewType.PartialViewMacro, snippetName, userId); - private Attempt CreatePartialViewMacro(IPartialView partialView, PartialViewType partialViewType, string snippetName = null, int userId = Cms.Core.Constants.Security.SuperUserId) + private Attempt 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.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 SavePartialView(IPartialView partialView, int userId = Cms.Core.Constants.Security.SuperUserId) - { - return SavePartialView(partialView, PartialViewType.PartialView, userId); - } + public Attempt SavePartialView(IPartialView partialView, int userId = Constants.Security.SuperUserId) => + SavePartialView(partialView, PartialViewType.PartialView, userId); - public Attempt SavePartialViewMacro(IPartialView partialView, int userId = Cms.Core.Constants.Security.SuperUserId) - { - return SavePartialView(partialView, PartialViewType.PartialViewMacro, userId); - } + public Attempt SavePartialViewMacro(IPartialView partialView, int userId = Constants.Security.SuperUserId) => + SavePartialView(partialView, PartialViewType.PartialViewMacro, userId); - private Attempt SavePartialView(IPartialView partialView, PartialViewType partialViewType, int userId = Cms.Core.Constants.Security.SuperUserId) + private Attempt 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 } } + /// + public Stream GetPartialViewFileContentStream(string filepath) + { + using (IScope scope = ScopeProvider.CreateScope(autoComplete: true)) + { + return _partialViewRepository.GetFileContentStream(filepath); + } + } + + /// + public void SetPartialViewFileContent(string filepath, Stream content) + { + using (IScope scope = ScopeProvider.CreateScope()) + { + _partialViewRepository.SetFileContent(filepath, content); + scope.Complete(); + } + } + + /// + public long GetPartialViewFileSize(string filepath) + { + using (IScope scope = ScopeProvider.CreateScope(autoComplete: true)) + { + return _partialViewRepository.GetFileSize(filepath); + } + } + + /// + public Stream GetPartialViewMacroFileContentStream(string filepath) + { + using (IScope scope = ScopeProvider.CreateScope(autoComplete: true)) + { + return _partialViewMacroRepository.GetFileContentStream(filepath); + } + } + + /// + public void SetPartialViewMacroFileContent(string filepath, Stream content) + { + using (IScope scope = ScopeProvider.CreateScope()) + { + _partialViewMacroRepository.SetFileContent(filepath, content); + scope.Complete(); + } + } + + /// + 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 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 }