commite0aa430d4cAuthor: Paul Johnson <pmj@umbraco.com> Date: Thu May 19 10:00:57 2022 +0100 Fix typo in pipeline yaml commit2ec450f2d6Author: Paul Johnson <pmj@umbraco.com> Date: Thu May 19 09:14:47 2022 +0100 Fix yaml conditions commitc2d548039aAuthor: Paul Johnson <pmj@umbraco.com> Date: Thu May 19 09:02:50 2022 +0100 Azure pipeline refactor (#12428) * Skip symbols for Umbraco.Templates * Resolve some test issues + Fixed whitespace dependant tests to pass regardless of build OS vs run OS. + Snap dictionary tests were failing when Configuration was release + Removed hardcoded baseUrl from one of the acceptance tests * Move docfx setup to ./build and fix * Update UI docs title * Added dockerfile that can be used when running the acceptance tests. * Take explicit dependency on System.Security.Cryptography.Pkcs * Refactor ci/cd pipeline commitee8359af75Author: Mole <nikolajlauridsen@protonmail.ch> Date: Thu May 19 09:57:21 2022 +0200 V10: Reintroduce appsettings-schema.json (#12416) * Reintroduce language files tests (#12367) * Reintroducing language files tests * Fix casing * Update tests/Umbraco.Tests.UnitTests/Umbraco.Core/EmbeddedResources/LanguageXmlTests.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Change Umbraco.Cms and Umbraco.Templates nuspecs to csproj * Remove Umbraco.Templates from VerifyNuGet step * Remove duplicate and unnecessary properties * Generate json schema on build * Add targets file * Gitignore auto generated appsettings schema * Fix build not copying file * Use the new path in appsettings * Update copy message * Build json schema as release * Update json schema options Otherwise just running the project will put the file in the wrong place * Generate schema if it doesn't exist in Web.Ui * Update json schema options Otherwise just running the project will put the file in the wrong place Co-authored-by: Elitsa Marinovska <21998037+elit0451@users.noreply.github.com> Co-authored-by: Ronald Barendse <ronald@barend.se> commitdd617ede80Author: Ronald Barendse <ronald@barend.se> Date: Thu May 19 09:51:11 2022 +0200 v10: Change Umbraco.Cms and Umbraco.Templates nuspecs to csproj (#12413) * Reintroduce language files tests (#12367) * Reintroducing language files tests * Fix casing * Update tests/Umbraco.Tests.UnitTests/Umbraco.Core/EmbeddedResources/LanguageXmlTests.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Change Umbraco.Cms and Umbraco.Templates nuspecs to csproj * Remove Umbraco.Templates from VerifyNuGet step * Remove duplicate and unnecessary properties Co-authored-by: Elitsa Marinovska <21998037+elit0451@users.noreply.github.com> Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> commitb83216876fAuthor: Ronald Barendse <ronald@barend.se> Date: Thu May 19 08:36:04 2022 +0200 v10: Project template database/connection string improvements (#12407) * Add new connection-string-provider-name parameter * Use template value forms to correctly encode JSON values * Add new development-database-type parameter * Update package template and fix App_Plugins directory rename * Remove conflicting short parameter name * Lowercase framework parameter to align with MS templates * Cleanup default template settings * Write unattended install parameters when either connection string or development database is set * Include RootNamespace in UmbracoPackage template * Update Umbraco specific gitignore rules * Revert "Lowercase framework parameter to align with MS templates" This reverts commit 22de389272a7e119df569ec2e54190265f6d0ae0. * Add exclude-gitignore parameter * Update template schemas * Add minimal-gitignore parameter commite40049dcf1Author: Mole <nikolajlauridsen@protonmail.ch> Date: Wed May 18 15:22:51 2022 +0200 Fix domain for invariant content nodes (#12405) Co-authored-by: Elitsa Marinovska <elm@umbraco.dk> commita3692b887aAuthor: Elitsa Marinovska <21998037+elit0451@users.noreply.github.com> Date: Mon May 9 11:42:10 2022 +0200 Use SnippetCollection to when working with snippets (#12355) * Introducing a new Snippet type * Adding a SnippetCollection and SnippetCollectionBuilder * Using snippetCollection to get the snippets instead of fileService * Fixed fetching the correct content * Make ISnippet non-discoverable * Split the SnippetCollection into PartialViewSnippetCollection and PartialViewMacroSnippetCollection * Update CodeFileController to use the 2 snippet collections * Display the names with Empty.cshtml on top * Remove merging embedded snippets with custom snippets from ~\Umbraco.Web.UI\umbraco\PartialViewMacros\Templates folder for the Partial View Collection * Fix naming * Fix another naming * Cleanup + Use base items Co-authored-by: Bjarke Berg <mail@bergmania.dk> (cherry picked from commit9326cc5fc6) commit4f48a4937bAuthor: patrickdemooij9 <patrickdemooij98@hotmail.com> Date: Fri Oct 8 11:18:00 2021 +0200 Cherry picked from4c08b44684commit4fdbfee597Author: Bjarne Fyrstenborg <bjarne_fyrstenborg@hotmail.com> Date: Wed May 11 00:51:37 2022 +0200 Show nicer overlay when clicking block card for deleted element type (#12140) * Show nicer overlay when clicking block card for deleted element type * Cleanup * Remove stop-scrolling container * Use flex-start instead on start * Remove legacy flexbox fallback * Remove unnecessary hack * Use standard gap property instead * Localization of message * Fix translation * End sentence with a dot (cherry picked from commitebb1dc21a9) commit3856bf8288Author: Henk Jan Pluim <henkjan.pluim@greenchoice.nl> Date: Mon Apr 25 10:02:06 2022 +0200 #fix 12254 return emptyresult (cherry picked from commit7993d19c1b) commit7087c3d9f6Author: Ronald Barendse <ronald@barend.se> Date: Tue May 17 12:59:01 2022 +0200 v10: Use ForceCreateDatabase during unattended install and extend GetUmbracoConnectionString extension methods (#12397) * Add extension methods to get the Umbraco connection string/provider name from configuration * Added tests for configuration extension methods. * Fix issue with InstallMissingDatabase and ForceCreateDatabase * Fix comments * Revert casing change in GenerateConnectionString * Re-add AddOptions (without config binding) to fix test * Update src/Umbraco.Core/Configuration/Models/ConnectionStrings.cs Co-authored-by: Ronald Barendse <ronald@barend.se> * Update src/Umbraco.Core/Configuration/Models/ConnectionStrings.cs * Update src/Umbraco.Infrastructure/Runtime/RuntimeState.cs * Whitespace and documentation updates * Add DatabaseProviderMetadataExtensions * Filter before ordering * Replace DataDirectory placeholder when setting connection string Co-authored-by: Andy Butland <abutland73@gmail.com> Co-authored-by: Bjarke Berg <mail@bergmania.dk> (cherry picked from commit8e6e262c7f) commite90bf26577Author: Ronald Barendse <ronald@barend.se> Date: Tue May 17 07:33:54 2022 +0200 v10: Support System.Data.SqlClient provider name (#12408) * Add support for System.Data.SqlClient provider name * Only update connection string when required (cherry picked from commite82bcb1b76) commitfd0637c96dAuthor: Elitsa Marinovska <21998037+elit0451@users.noreply.github.com> Date: Mon May 16 14:23:59 2022 +0200 Reintroduce language files tests (#12367) * Reintroducing language files tests * Fix casing * Update tests/Umbraco.Tests.UnitTests/Umbraco.Core/EmbeddedResources/LanguageXmlTests.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> (cherry picked from commit2ed71a64ec)
762 lines
35 KiB
C#
762 lines
35 KiB
C#
using System.Net;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Options;
|
|
using Umbraco.Cms.Core;
|
|
using Umbraco.Cms.Core.Configuration.Models;
|
|
using Umbraco.Cms.Core.Hosting;
|
|
using Umbraco.Cms.Core.IO;
|
|
using Umbraco.Cms.Core.Mapping;
|
|
using Umbraco.Cms.Core.Models;
|
|
using Umbraco.Cms.Core.Models.ContentEditing;
|
|
using Umbraco.Cms.Core.Security;
|
|
using Umbraco.Cms.Core.Services;
|
|
using Umbraco.Cms.Core.Snippets;
|
|
using Umbraco.Cms.Core.Strings;
|
|
using Umbraco.Cms.Core.Strings.Css;
|
|
using Umbraco.Cms.Web.BackOffice.Filters;
|
|
using Umbraco.Cms.Web.BackOffice.Trees;
|
|
using Umbraco.Cms.Web.Common.ActionsResults;
|
|
using Umbraco.Cms.Web.Common.Attributes;
|
|
using Umbraco.Cms.Web.Common.Authorization;
|
|
using Umbraco.Cms.Web.Common.DependencyInjection;
|
|
using Umbraco.Extensions;
|
|
using Constants = Umbraco.Cms.Core.Constants;
|
|
using Stylesheet = Umbraco.Cms.Core.Models.Stylesheet;
|
|
using StylesheetRule = Umbraco.Cms.Core.Models.ContentEditing.StylesheetRule;
|
|
|
|
namespace Umbraco.Cms.Web.BackOffice.Controllers
|
|
{
|
|
// TODO: Put some exception filters in our webapi to return 404 instead of 500 when we throw ArgumentNullException
|
|
// ref: https://www.exceptionnotfound.net/the-asp-net-web-api-exception-handling-pipeline-a-guided-tour/
|
|
[PluginController(Constants.Web.Mvc.BackOfficeApiArea)]
|
|
//[PrefixlessBodyModelValidator]
|
|
[Authorize(Policy = AuthorizationPolicies.SectionAccessSettings)]
|
|
public class CodeFileController : BackOfficeNotificationsController
|
|
{
|
|
private readonly IHostingEnvironment _hostingEnvironment;
|
|
private readonly FileSystems _fileSystems;
|
|
private readonly IFileService _fileService;
|
|
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
|
|
|
|
private readonly ILocalizedTextService _localizedTextService;
|
|
private readonly IUmbracoMapper _umbracoMapper;
|
|
private readonly IShortStringHelper _shortStringHelper;
|
|
private readonly GlobalSettings _globalSettings;
|
|
private readonly PartialViewSnippetCollection _partialViewSnippetCollection;
|
|
private readonly PartialViewMacroSnippetCollection _partialViewMacroSnippetCollection;
|
|
|
|
[ActivatorUtilitiesConstructor]
|
|
public CodeFileController(
|
|
IHostingEnvironment hostingEnvironment,
|
|
FileSystems fileSystems,
|
|
IFileService fileService,
|
|
IBackOfficeSecurityAccessor backOfficeSecurityAccessor,
|
|
ILocalizedTextService localizedTextService,
|
|
IUmbracoMapper umbracoMapper,
|
|
IShortStringHelper shortStringHelper,
|
|
IOptionsSnapshot<GlobalSettings> globalSettings,
|
|
PartialViewSnippetCollection partialViewSnippetCollection,
|
|
PartialViewMacroSnippetCollection partialViewMacroSnippetCollection)
|
|
{
|
|
_hostingEnvironment = hostingEnvironment;
|
|
_fileSystems = fileSystems;
|
|
_fileService = fileService;
|
|
_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
|
|
_localizedTextService = localizedTextService;
|
|
_umbracoMapper = umbracoMapper;
|
|
_shortStringHelper = shortStringHelper;
|
|
_globalSettings = globalSettings.Value;
|
|
_partialViewSnippetCollection = partialViewSnippetCollection;
|
|
_partialViewMacroSnippetCollection = partialViewMacroSnippetCollection;
|
|
}
|
|
|
|
[Obsolete("Use ctor will all params. Scheduled for removal in V12.")]
|
|
public CodeFileController(
|
|
IHostingEnvironment hostingEnvironment,
|
|
FileSystems fileSystems,
|
|
IFileService fileService,
|
|
IBackOfficeSecurityAccessor backOfficeSecurityAccessor,
|
|
ILocalizedTextService localizedTextService,
|
|
IUmbracoMapper umbracoMapper,
|
|
IShortStringHelper shortStringHelper,
|
|
IOptionsSnapshot<GlobalSettings> globalSettings) : this(
|
|
hostingEnvironment,
|
|
fileSystems,
|
|
fileService,
|
|
backOfficeSecurityAccessor,
|
|
localizedTextService,
|
|
umbracoMapper,
|
|
shortStringHelper,
|
|
globalSettings,
|
|
StaticServiceProvider.Instance.GetRequiredService<PartialViewSnippetCollection>(),
|
|
StaticServiceProvider.Instance.GetRequiredService<PartialViewMacroSnippetCollection>())
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Used to create a brand new file
|
|
/// </summary>
|
|
/// <param name="type">This is a string but will be 'scripts' 'partialViews', 'partialViewMacros'</param>
|
|
/// <param name="display"></param>
|
|
/// <returns>Will return a simple 200 if file creation succeeds</returns>
|
|
[ValidationFilter]
|
|
public ActionResult<CodeFileDisplay> PostCreate(string type, CodeFileDisplay display)
|
|
{
|
|
if (display == null) throw new ArgumentNullException("display");
|
|
if (string.IsNullOrWhiteSpace(type)) throw new ArgumentException("Value cannot be null or whitespace.", "type");
|
|
|
|
var currentUser = _backOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser;
|
|
switch (type)
|
|
{
|
|
case Constants.Trees.PartialViews:
|
|
var view = new PartialView(PartialViewType.PartialView, display.VirtualPath ?? string.Empty);
|
|
view.Content = display.Content;
|
|
var result = _fileService.CreatePartialView(view, display.Snippet, currentUser?.Id);
|
|
if (result.Success)
|
|
{
|
|
return Ok();
|
|
}
|
|
else
|
|
{
|
|
return ValidationProblem(result.Exception?.Message);
|
|
}
|
|
|
|
case Constants.Trees.PartialViewMacros:
|
|
var viewMacro = new PartialView(PartialViewType.PartialViewMacro, display.VirtualPath ?? string.Empty);
|
|
viewMacro.Content = display.Content;
|
|
var resultMacro = _fileService.CreatePartialViewMacro(viewMacro, display.Snippet, currentUser?.Id);
|
|
if (resultMacro.Success)
|
|
return Ok();
|
|
else
|
|
return ValidationProblem(resultMacro.Exception?.Message);
|
|
|
|
case Constants.Trees.Scripts:
|
|
var script = new Script(display.VirtualPath ?? string.Empty);
|
|
_fileService.SaveScript(script, currentUser?.Id);
|
|
return Ok();
|
|
|
|
default:
|
|
return NotFound();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Used to create a container/folder in 'partialViews', 'partialViewMacros', 'scripts' or 'stylesheets'
|
|
/// </summary>
|
|
/// <param name="type">'partialViews', 'partialViewMacros' or 'scripts'</param>
|
|
/// <param name="parentId">The virtual path of the parent.</param>
|
|
/// <param name="name">The name of the container/folder</param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public ActionResult<CodeFileDisplay> PostCreateContainer(string type, string parentId, string name)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(type)) throw new ArgumentException("Value cannot be null or whitespace.", "type");
|
|
if (string.IsNullOrWhiteSpace(parentId)) throw new ArgumentException("Value cannot be null or whitespace.", "parentId");
|
|
if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException("Value cannot be null or whitespace.", "name");
|
|
if (name.ContainsAny(Path.GetInvalidPathChars())) {
|
|
return ValidationProblem(_localizedTextService.Localize("codefile", "createFolderIllegalChars"));
|
|
}
|
|
|
|
// if the parentId is root (-1) then we just need an empty string as we are
|
|
// creating the path below and we don't want -1 in the path
|
|
if (parentId == Constants.System.RootString)
|
|
{
|
|
parentId = string.Empty;
|
|
}
|
|
|
|
name = System.Web.HttpUtility.UrlDecode(name);
|
|
|
|
if (parentId.IsNullOrWhiteSpace() == false)
|
|
{
|
|
parentId = System.Web.HttpUtility.UrlDecode(parentId);
|
|
name = parentId.EnsureEndsWith("/") + name;
|
|
}
|
|
|
|
var virtualPath = string.Empty;
|
|
switch (type)
|
|
{
|
|
case Constants.Trees.PartialViews:
|
|
virtualPath = NormalizeVirtualPath(name, Constants.SystemDirectories.PartialViews);
|
|
_fileService.CreatePartialViewFolder(virtualPath);
|
|
break;
|
|
case Constants.Trees.PartialViewMacros:
|
|
virtualPath = NormalizeVirtualPath(name, Constants.SystemDirectories.MacroPartials);
|
|
_fileService.CreatePartialViewMacroFolder(virtualPath);
|
|
break;
|
|
case Constants.Trees.Scripts:
|
|
virtualPath = NormalizeVirtualPath(name, _globalSettings.UmbracoScriptsPath);
|
|
_fileService.CreateScriptFolder(virtualPath);
|
|
break;
|
|
case Constants.Trees.Stylesheets:
|
|
virtualPath = NormalizeVirtualPath(name, _globalSettings.UmbracoCssPath);
|
|
_fileService.CreateStyleSheetFolder(virtualPath);
|
|
break;
|
|
|
|
}
|
|
|
|
return new CodeFileDisplay
|
|
{
|
|
VirtualPath = virtualPath,
|
|
Path = Url.GetTreePathFromFilePath(virtualPath)
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Used to get a specific file from disk via the FileService
|
|
/// </summary>
|
|
/// <param name="type">This is a string but will be 'scripts' 'partialViews', 'partialViewMacros' or 'stylesheets'</param>
|
|
/// <param name="virtualPath">The filename or URL encoded path of the file to open</param>
|
|
/// <returns>The file and its contents from the virtualPath</returns>
|
|
public ActionResult<CodeFileDisplay?> GetByPath(string type, string virtualPath)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(type)) throw new ArgumentException("Value cannot be null or whitespace.", "type");
|
|
if (string.IsNullOrWhiteSpace(virtualPath)) throw new ArgumentException("Value cannot be null or whitespace.", "virtualPath");
|
|
|
|
virtualPath = System.Web.HttpUtility.UrlDecode(virtualPath);
|
|
|
|
switch (type)
|
|
{
|
|
case Constants.Trees.PartialViews:
|
|
var view = _fileService.GetPartialView(virtualPath);
|
|
if (view != null)
|
|
{
|
|
var display = _umbracoMapper.Map<IPartialView, CodeFileDisplay>(view);
|
|
|
|
if (display is not null)
|
|
{
|
|
display.FileType = Constants.Trees.PartialViews;
|
|
display.Path = Url.GetTreePathFromFilePath(view.Path);
|
|
display.Id = System.Web.HttpUtility.UrlEncode(view.Path);
|
|
}
|
|
|
|
return display;
|
|
}
|
|
|
|
break;
|
|
case Constants.Trees.PartialViewMacros:
|
|
var viewMacro = _fileService.GetPartialViewMacro(virtualPath);
|
|
if (viewMacro != null)
|
|
{
|
|
var display = _umbracoMapper.Map<IPartialView, CodeFileDisplay>(viewMacro);
|
|
|
|
if (display is not null)
|
|
{
|
|
display.FileType = Constants.Trees.PartialViewMacros;
|
|
display.Path = Url.GetTreePathFromFilePath(viewMacro.Path);
|
|
display.Id = System.Web.HttpUtility.UrlEncode(viewMacro.Path);
|
|
}
|
|
|
|
return display;
|
|
}
|
|
break;
|
|
case Constants.Trees.Scripts:
|
|
var script = _fileService.GetScript(virtualPath);
|
|
if (script != null)
|
|
{
|
|
var display = _umbracoMapper.Map<IScript, CodeFileDisplay>(script);
|
|
|
|
if (display is not null)
|
|
{
|
|
display.FileType = Constants.Trees.Scripts;
|
|
display.Path = Url.GetTreePathFromFilePath(script.Path);
|
|
display.Id = System.Web.HttpUtility.UrlEncode(script.Path);
|
|
}
|
|
|
|
return display;
|
|
}
|
|
break;
|
|
case Constants.Trees.Stylesheets:
|
|
var stylesheet = _fileService.GetStylesheet(virtualPath);
|
|
if (stylesheet != null)
|
|
{
|
|
var display = _umbracoMapper.Map<IStylesheet, CodeFileDisplay>(stylesheet);
|
|
|
|
if (display is not null)
|
|
{
|
|
display.FileType = Constants.Trees.Stylesheets;
|
|
display.Path = Url.GetTreePathFromFilePath(stylesheet.Path);
|
|
display.Id = System.Web.HttpUtility.UrlEncode(stylesheet.Path);
|
|
}
|
|
|
|
return display;
|
|
}
|
|
break;
|
|
}
|
|
|
|
return NotFound();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Used to get a list of available templates/snippets to base a new Partial View or Partial View Macro from
|
|
/// </summary>
|
|
/// <param name="type">This is a string but will be 'partialViews', 'partialViewMacros'</param>
|
|
/// <returns>Returns a list of <see cref="SnippetDisplay"/> if a correct type is sent</returns>
|
|
public ActionResult<IEnumerable<SnippetDisplay>> GetSnippets(string type)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(type)) throw new ArgumentException("Value cannot be null or whitespace.", "type");
|
|
|
|
IEnumerable<string> snippets;
|
|
switch (type)
|
|
{
|
|
case Constants.Trees.PartialViews:
|
|
snippets = _partialViewSnippetCollection.GetNames();
|
|
break;
|
|
case Constants.Trees.PartialViewMacros:
|
|
snippets = _partialViewMacroSnippetCollection.GetNames();
|
|
break;
|
|
default:
|
|
return NotFound();
|
|
}
|
|
|
|
return snippets.Select(snippet => new SnippetDisplay() { Name = snippet.SplitPascalCasing(_shortStringHelper).ToFirstUpperInvariant(), FileName = snippet }).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Used to scaffold the json object for the editors for 'scripts', 'partialViews', 'partialViewMacros' and 'stylesheets'
|
|
/// </summary>
|
|
/// <param name="type">This is a string but will be 'scripts' 'partialViews', 'partialViewMacros' or 'stylesheets'</param>
|
|
/// <param name="id"></param>
|
|
/// <param name="snippetName"></param>
|
|
/// <returns></returns>
|
|
public ActionResult<CodeFileDisplay?> GetScaffold(string type, string id, string? snippetName = null)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(type)) throw new ArgumentException("Value cannot be null or whitespace.", "type");
|
|
if (string.IsNullOrWhiteSpace(id)) throw new ArgumentException("Value cannot be null or whitespace.", "id");
|
|
|
|
CodeFileDisplay? codeFileDisplay;
|
|
|
|
switch (type)
|
|
{
|
|
case Constants.Trees.PartialViews:
|
|
codeFileDisplay = _umbracoMapper.Map<IPartialView, CodeFileDisplay>(new PartialView(PartialViewType.PartialView, string.Empty));
|
|
if (codeFileDisplay is not null)
|
|
{
|
|
codeFileDisplay.VirtualPath = Constants.SystemDirectories.PartialViews;
|
|
if (snippetName.IsNullOrWhiteSpace() == false)
|
|
{
|
|
codeFileDisplay.Content = _partialViewSnippetCollection.GetContentFromName(snippetName!);
|
|
}
|
|
}
|
|
|
|
break;
|
|
case Constants.Trees.PartialViewMacros:
|
|
codeFileDisplay = _umbracoMapper.Map<IPartialView, CodeFileDisplay>(new PartialView(PartialViewType.PartialViewMacro, string.Empty));
|
|
if (codeFileDisplay is not null)
|
|
{
|
|
codeFileDisplay.VirtualPath = Constants.SystemDirectories.MacroPartials;
|
|
if (snippetName.IsNullOrWhiteSpace() == false)
|
|
{
|
|
codeFileDisplay.Content = _partialViewMacroSnippetCollection.GetContentFromName(snippetName!);
|
|
}
|
|
}
|
|
|
|
break;
|
|
case Constants.Trees.Scripts:
|
|
codeFileDisplay = _umbracoMapper.Map<Script, CodeFileDisplay>(new Script(string.Empty));
|
|
if (codeFileDisplay is not null)
|
|
{
|
|
codeFileDisplay.VirtualPath = _globalSettings.UmbracoScriptsPath;
|
|
}
|
|
|
|
break;
|
|
case Constants.Trees.Stylesheets:
|
|
codeFileDisplay = _umbracoMapper.Map<Stylesheet, CodeFileDisplay>(new Stylesheet(string.Empty));
|
|
if (codeFileDisplay is not null)
|
|
{
|
|
codeFileDisplay.VirtualPath = _globalSettings.UmbracoCssPath;
|
|
}
|
|
|
|
break;
|
|
default:
|
|
return new UmbracoProblemResult("Unsupported editortype", HttpStatusCode.BadRequest);
|
|
}
|
|
|
|
if (codeFileDisplay is null)
|
|
{
|
|
return codeFileDisplay;
|
|
}
|
|
// Make sure that the root virtual path ends with '/'
|
|
codeFileDisplay.VirtualPath = codeFileDisplay.VirtualPath?.EnsureEndsWith("/");
|
|
|
|
if (id != Constants.System.RootString)
|
|
{
|
|
codeFileDisplay.VirtualPath += id.TrimStart(Constants.CharArrays.ForwardSlash).EnsureEndsWith("/");
|
|
//if it's not new then it will have a path, otherwise it won't
|
|
codeFileDisplay.Path = Url.GetTreePathFromFilePath(id);
|
|
}
|
|
|
|
codeFileDisplay.VirtualPath = codeFileDisplay.VirtualPath?.TrimStart("~");
|
|
codeFileDisplay.FileType = type;
|
|
return codeFileDisplay;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Used to delete a specific file from disk via the FileService
|
|
/// </summary>
|
|
/// <param name="type">This is a string but will be 'scripts' 'partialViews', 'partialViewMacros' or 'stylesheets'</param>
|
|
/// <param name="virtualPath">The filename or URL encoded path of the file to delete</param>
|
|
/// <returns>Will return a simple 200 if file deletion succeeds</returns>
|
|
[HttpDelete]
|
|
[HttpPost]
|
|
public IActionResult Delete(string type, string virtualPath)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(type)) throw new ArgumentException("Value cannot be null or whitespace.", "type");
|
|
if (string.IsNullOrWhiteSpace(virtualPath)) throw new ArgumentException("Value cannot be null or whitespace.", "virtualPath");
|
|
|
|
virtualPath = System.Web.HttpUtility.UrlDecode(virtualPath);
|
|
var currentUser = _backOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser;
|
|
switch (type)
|
|
{
|
|
case Constants.Trees.PartialViews:
|
|
if (IsDirectory(_hostingEnvironment.MapPathContentRoot(Path.Combine(Constants.SystemDirectories.PartialViews, virtualPath))))
|
|
{
|
|
_fileService.DeletePartialViewFolder(virtualPath);
|
|
return Ok();
|
|
}
|
|
if (_fileService.DeletePartialView(virtualPath, currentUser?.Id))
|
|
{
|
|
return Ok();
|
|
}
|
|
return new UmbracoProblemResult("No Partial View or folder found with the specified path", HttpStatusCode.NotFound);
|
|
|
|
case Constants.Trees.PartialViewMacros:
|
|
if (IsDirectory(_hostingEnvironment.MapPathContentRoot(Path.Combine(Constants.SystemDirectories.MacroPartials, virtualPath))))
|
|
{
|
|
_fileService.DeletePartialViewMacroFolder(virtualPath);
|
|
return Ok();
|
|
}
|
|
if (_fileService.DeletePartialViewMacro(virtualPath, currentUser?.Id))
|
|
{
|
|
return Ok();
|
|
}
|
|
return new UmbracoProblemResult("No Partial View Macro or folder found with the specified path", HttpStatusCode.NotFound);
|
|
|
|
case Constants.Trees.Scripts:
|
|
if (IsDirectory(_hostingEnvironment.MapPathWebRoot(Path.Combine(_globalSettings.UmbracoScriptsPath, virtualPath))))
|
|
{
|
|
_fileService.DeleteScriptFolder(virtualPath);
|
|
return Ok();
|
|
}
|
|
if (_fileService.GetScript(virtualPath) != null)
|
|
{
|
|
_fileService.DeleteScript(virtualPath, currentUser?.Id);
|
|
return Ok();
|
|
}
|
|
return new UmbracoProblemResult("No Script or folder found with the specified path", HttpStatusCode.NotFound);
|
|
case Constants.Trees.Stylesheets:
|
|
if (IsDirectory(_hostingEnvironment.MapPathWebRoot(Path.Combine(_globalSettings.UmbracoCssPath, virtualPath))))
|
|
{
|
|
_fileService.DeleteStyleSheetFolder(virtualPath);
|
|
return Ok();
|
|
}
|
|
if (_fileService.GetStylesheet(virtualPath) != null)
|
|
{
|
|
_fileService.DeleteStylesheet(virtualPath, currentUser?.Id);
|
|
return Ok();
|
|
}
|
|
return new UmbracoProblemResult("No Stylesheet found with the specified path", HttpStatusCode.NotFound);
|
|
default:
|
|
return NotFound();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Used to create or update a 'partialview', 'partialviewmacro', 'script' or 'stylesheets' file
|
|
/// </summary>
|
|
/// <param name="display"></param>
|
|
/// <returns>The updated CodeFileDisplay model</returns>
|
|
public ActionResult<CodeFileDisplay> PostSave(CodeFileDisplay display)
|
|
{
|
|
if (display == null) throw new ArgumentNullException("display");
|
|
|
|
TryValidateModel(display);
|
|
if (ModelState.IsValid == false)
|
|
{
|
|
return ValidationProblem(ModelState);
|
|
}
|
|
|
|
switch (display.FileType)
|
|
{
|
|
case Constants.Trees.PartialViews:
|
|
var partialViewResult = CreateOrUpdatePartialView(display);
|
|
if (partialViewResult.Success)
|
|
{
|
|
display = _umbracoMapper.Map(partialViewResult.Result, display);
|
|
display.Path = Url.GetTreePathFromFilePath(partialViewResult.Result?.Path);
|
|
display.Id = System.Web.HttpUtility.UrlEncode(partialViewResult.Result?.Path);
|
|
return display;
|
|
}
|
|
|
|
display.AddErrorNotification(
|
|
_localizedTextService.Localize("speechBubbles", "partialViewErrorHeader"),
|
|
_localizedTextService.Localize("speechBubbles", "partialViewErrorText"));
|
|
break;
|
|
|
|
case Constants.Trees.PartialViewMacros:
|
|
var partialViewMacroResult = CreateOrUpdatePartialViewMacro(display);
|
|
if (partialViewMacroResult.Success)
|
|
{
|
|
display = _umbracoMapper.Map(partialViewMacroResult.Result, display);
|
|
display.Path = Url.GetTreePathFromFilePath(partialViewMacroResult.Result?.Path);
|
|
display.Id = System.Web.HttpUtility.UrlEncode(partialViewMacroResult.Result?.Path);
|
|
return display;
|
|
}
|
|
|
|
display.AddErrorNotification(
|
|
_localizedTextService.Localize("speechBubbles", "partialViewErrorHeader"),
|
|
_localizedTextService.Localize("speechBubbles", "partialViewErrorText"));
|
|
break;
|
|
|
|
case Constants.Trees.Scripts:
|
|
|
|
var scriptResult = CreateOrUpdateScript(display);
|
|
display = _umbracoMapper.Map(scriptResult, display);
|
|
display.Path = Url.GetTreePathFromFilePath(scriptResult?.Path);
|
|
display.Id = System.Web.HttpUtility.UrlEncode(scriptResult?.Path);
|
|
return display;
|
|
|
|
//display.AddErrorNotification(
|
|
// _localizedTextService.Localize("speechBubbles/partialViewErrorHeader"),
|
|
// _localizedTextService.Localize("speechBubbles/partialViewErrorText"));
|
|
|
|
case Constants.Trees.Stylesheets:
|
|
|
|
var stylesheetResult = CreateOrUpdateStylesheet(display);
|
|
display = _umbracoMapper.Map(stylesheetResult, display);
|
|
display.Path = Url.GetTreePathFromFilePath(stylesheetResult?.Path);
|
|
display.Id = System.Web.HttpUtility.UrlEncode(stylesheetResult?.Path);
|
|
return display;
|
|
|
|
default:
|
|
return NotFound();
|
|
}
|
|
|
|
return display;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extracts "umbraco style rules" from a style sheet
|
|
/// </summary>
|
|
/// <param name="data">The style sheet data</param>
|
|
/// <returns>The style rules</returns>
|
|
public StylesheetRule[]? PostExtractStylesheetRules(StylesheetData data)
|
|
{
|
|
if (data.Content.IsNullOrWhiteSpace())
|
|
{
|
|
return new StylesheetRule[0];
|
|
}
|
|
|
|
return StylesheetHelper.ParseRules(data.Content)?.Select(rule => new StylesheetRule
|
|
{
|
|
Name = rule.Name,
|
|
Selector = rule.Selector,
|
|
Styles = rule.Styles
|
|
}).ToArray();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a style sheet from CSS and style rules
|
|
/// </summary>
|
|
/// <param name="data">The style sheet data</param>
|
|
/// <returns>The style sheet combined from the CSS and the rules</returns>
|
|
/// <remarks>
|
|
/// Any "umbraco style rules" in the CSS will be removed and replaced with the rules passed in <see cref="data"/>
|
|
/// </remarks>
|
|
public string? PostInterpolateStylesheetRules(StylesheetData data)
|
|
{
|
|
// first remove all existing rules
|
|
var existingRules = data.Content.IsNullOrWhiteSpace()
|
|
? new Cms.Core.Strings.Css.StylesheetRule[0]
|
|
: StylesheetHelper.ParseRules(data.Content).ToArray();
|
|
foreach (var rule in existingRules)
|
|
{
|
|
data.Content = StylesheetHelper.ReplaceRule(data.Content, rule.Name, null);
|
|
}
|
|
|
|
data.Content = data.Content?.TrimEnd(Constants.CharArrays.LineFeedCarriageReturn);
|
|
|
|
// now add all the posted rules
|
|
if (data.Rules != null && data.Rules.Any())
|
|
{
|
|
foreach (var rule in data.Rules)
|
|
{
|
|
data.Content = StylesheetHelper.AppendRule(data.Content, new Cms.Core.Strings.Css.StylesheetRule
|
|
{
|
|
Name = rule.Name,
|
|
Selector = rule.Selector,
|
|
Styles = rule.Styles
|
|
});
|
|
}
|
|
|
|
data.Content += Environment.NewLine;
|
|
}
|
|
|
|
return data.Content;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create or Update a Script
|
|
/// </summary>
|
|
/// <param name="display"></param>
|
|
/// <returns></returns>
|
|
/// <remarks>
|
|
/// It's important to note that Scripts are DIFFERENT from cshtml files since scripts use IFileSystem and cshtml files
|
|
/// use a normal file system because they must exist on a real file system for ASP.NET to work.
|
|
/// </remarks>
|
|
private IScript? CreateOrUpdateScript(CodeFileDisplay display)
|
|
{
|
|
return CreateOrUpdateFile(display, ".js", _fileSystems.ScriptsFileSystem,
|
|
name => _fileService.GetScript(name),
|
|
(script, userId) => _fileService.SaveScript(script, userId),
|
|
name => new Script(name ?? string.Empty));
|
|
}
|
|
|
|
private IStylesheet? CreateOrUpdateStylesheet(CodeFileDisplay display)
|
|
{
|
|
return CreateOrUpdateFile(display, ".css", _fileSystems.StylesheetsFileSystem,
|
|
name => _fileService.GetStylesheet(name),
|
|
(stylesheet, userId) => _fileService.SaveStylesheet(stylesheet, userId),
|
|
name => new Stylesheet(name ?? string.Empty)
|
|
);
|
|
}
|
|
|
|
private T CreateOrUpdateFile<T>(CodeFileDisplay display, string extension, IFileSystem? fileSystem,
|
|
Func<string?, T> getFileByName, Action<T, int?> saveFile, Func<string?, T> createFile) where T : IFile?
|
|
{
|
|
//must always end with the correct extension
|
|
display.Name = EnsureCorrectFileExtension(display.Name, extension);
|
|
|
|
var virtualPath = display.VirtualPath ?? string.Empty;
|
|
// this is all weird, should be using relative paths everywhere!
|
|
var relPath = fileSystem?.GetRelativePath(virtualPath);
|
|
|
|
if (relPath?.EndsWith(extension) == false)
|
|
{
|
|
//this would typically mean it's new
|
|
relPath = relPath.IsNullOrWhiteSpace()
|
|
? relPath + display.Name
|
|
: relPath.EnsureEndsWith('/') + display.Name;
|
|
}
|
|
var currentUser = _backOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser;
|
|
var file = getFileByName(relPath);
|
|
if (file != null)
|
|
{
|
|
// might need to find the path
|
|
var orgPath = file.Name is null ? string.Empty : file.OriginalPath.Substring(0, file.OriginalPath.IndexOf(file.Name));
|
|
file.Path = orgPath + display.Name;
|
|
|
|
file.Content = display.Content;
|
|
//try/catch? since this doesn't return an Attempt?
|
|
saveFile(file, currentUser?.Id);
|
|
}
|
|
else
|
|
{
|
|
file = createFile(relPath);
|
|
if (file is not null)
|
|
{
|
|
file.Content = display.Content;
|
|
}
|
|
|
|
saveFile(file, currentUser?.Id);
|
|
}
|
|
|
|
return file;
|
|
}
|
|
|
|
private Attempt<IPartialView?> CreateOrUpdatePartialView(CodeFileDisplay display)
|
|
{
|
|
return CreateOrUpdatePartialView(display, Constants.SystemDirectories.PartialViews,
|
|
_fileService.GetPartialView, _fileService.SavePartialView, _fileService.CreatePartialView);
|
|
}
|
|
|
|
private Attempt<IPartialView?> CreateOrUpdatePartialViewMacro(CodeFileDisplay display)
|
|
{
|
|
return CreateOrUpdatePartialView(display, Constants.SystemDirectories.MacroPartials,
|
|
_fileService.GetPartialViewMacro, _fileService.SavePartialViewMacro, _fileService.CreatePartialViewMacro);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Helper method to take care of persisting partial views or partial view macros - so we're not duplicating the same logic
|
|
/// </summary>
|
|
/// <param name="display"></param>
|
|
/// <param name="systemDirectory"></param>
|
|
/// <param name="getView"></param>
|
|
/// <param name="saveView"></param>
|
|
/// <param name="createView"></param>
|
|
/// <returns></returns>
|
|
private Attempt<IPartialView?> CreateOrUpdatePartialView(
|
|
CodeFileDisplay display, string systemDirectory,
|
|
Func<string, IPartialView?> getView,
|
|
Func<IPartialView, int?, Attempt<IPartialView?>> saveView,
|
|
Func<IPartialView, string?, int?, Attempt<IPartialView?>> createView)
|
|
{
|
|
//must always end with the correct extension
|
|
display.Name = EnsureCorrectFileExtension(display.Name, ".cshtml");
|
|
|
|
Attempt<IPartialView?> partialViewResult;
|
|
var currentUser = _backOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser;
|
|
|
|
var virtualPath = NormalizeVirtualPath(display.VirtualPath, systemDirectory);
|
|
var view = getView(virtualPath);
|
|
if (view != null)
|
|
{
|
|
// might need to find the path
|
|
var orgPath = view.OriginalPath.Substring(0, view.OriginalPath.IndexOf(view.Name ?? string.Empty));
|
|
view.Path = orgPath + display.Name;
|
|
|
|
view.Content = display.Content;
|
|
partialViewResult = saveView(view, currentUser?.Id);
|
|
}
|
|
else
|
|
{
|
|
view = new PartialView(PartialViewType.PartialView, virtualPath + display.Name);
|
|
view.Content = display.Content;
|
|
partialViewResult = createView(view, display.Snippet, currentUser?.Id);
|
|
}
|
|
|
|
return partialViewResult;
|
|
}
|
|
|
|
private string NormalizeVirtualPath(string? virtualPath, string systemDirectory)
|
|
{
|
|
if (virtualPath.IsNullOrWhiteSpace())
|
|
return string.Empty;
|
|
|
|
systemDirectory = systemDirectory.TrimStart("~");
|
|
systemDirectory = systemDirectory.Replace('\\', '/');
|
|
virtualPath = virtualPath!.TrimStart("~");
|
|
virtualPath = virtualPath.Replace('\\', '/');
|
|
virtualPath = virtualPath.ReplaceFirst(systemDirectory, string.Empty);
|
|
|
|
return virtualPath;
|
|
}
|
|
|
|
private string? EnsureCorrectFileExtension(string? value, string extension)
|
|
{
|
|
if (value?.EndsWith(extension) == false)
|
|
value += extension;
|
|
|
|
return value;
|
|
}
|
|
|
|
private bool IsDirectory(string path)
|
|
{
|
|
var dirInfo = new DirectoryInfo(path);
|
|
|
|
// If you turn off indexing in Windows this will have the attribute:
|
|
// `FileAttributes.Directory | FileAttributes.NotContentIndexed`
|
|
return (dirInfo.Attributes & FileAttributes.Directory) != 0;
|
|
}
|
|
|
|
// this is an internal class for passing stylesheet data from the client to the controller while editing
|
|
public class StylesheetData
|
|
{
|
|
public string? Content { get; set; }
|
|
|
|
public StylesheetRule[]? Rules { get; set; }
|
|
}
|
|
}
|
|
}
|