Skip to content

Move created packages out of webroot #12114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/Umbraco.Core/Constants-SystemDirectories.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,11 @@ public static class SystemDirectories

public const string AppPlugins = "/App_Plugins";


[Obsolete("Use PluginIcons instead")]
public static string AppPluginIcons => "/Backoffice/Icons";

public const string PluginIcons = "/backoffice/icons";

public const string CreatedPackages = "/created-packages";


public const string MvcViews = "~/Views";

public const string PartialViews = MvcViews + "/Partials/";
Expand All @@ -62,6 +58,8 @@ public static class SystemDirectories

public const string Packages = Data + "/packages";

public const string CreatedPackages = Data + "/CreatedPackages";

public const string Preview = Data + "/preview";

/// <summary>
Expand Down
34 changes: 17 additions & 17 deletions src/Umbraco.Core/Packaging/PackagesRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core.Configuration.Models;
Expand Down Expand Up @@ -33,7 +32,7 @@ public class PackagesRepository : ICreatedPackagesRepository
private readonly IEntityXmlSerializer _serializer;
private readonly IHostingEnvironment _hostingEnvironment;
private readonly string _packageRepositoryFileName;
private readonly string _mediaFolderPath;
private readonly string _createdPackagesFolderPath;
private readonly string _packagesFolderPath;
private readonly string _tempFolderPath;
private readonly PackageDefinitionXmlParser _parser;
Expand Down Expand Up @@ -93,7 +92,7 @@ public PackagesRepository(

_tempFolderPath = tempFolderPath ?? Constants.SystemDirectories.TempData.EnsureEndsWith('/') + "PackageFiles";
_packagesFolderPath = packagesFolderPath ?? Constants.SystemDirectories.Packages;
_mediaFolderPath = mediaFolderPath ?? Path.Combine(globalSettings.Value.UmbracoMediaPhysicalRootPath, Constants.SystemDirectories.CreatedPackages);
_createdPackagesFolderPath = mediaFolderPath ?? Constants.SystemDirectories.CreatedPackages;

_parser = new PackageDefinitionXmlParser();
_mediaService = mediaService;
Expand Down Expand Up @@ -250,15 +249,8 @@ public string ExportPackage(PackageDefinition definition)
}
}



var directoryName =
_hostingEnvironment.MapPathWebRoot(Path.Combine(_mediaFolderPath, definition.Name.Replace(' ', '_')));

if (Directory.Exists(directoryName) == false)
{
Directory.CreateDirectory(directoryName);
}
var directoryName = _hostingEnvironment.MapPathContentRoot(Path.Combine(_createdPackagesFolderPath, definition.Name.Replace(' ', '_')));
Directory.CreateDirectory(directoryName);

var finalPackagePath = Path.Combine(directoryName, fileName);

Expand All @@ -276,14 +268,14 @@ public string ExportPackage(PackageDefinition definition)
}
finally
{
//Clean up
// Clean up
Directory.Delete(temporaryPath, true);
}
}

private void ValidatePackage(PackageDefinition definition)
{
//ensure it's valid
// ensure it's valid
var context = new ValidationContext(definition, serviceProvider: null, items: null);
var results = new List<ValidationResult>();
var isValid = Validator.TryValidateObject(definition, context, results);
Expand Down Expand Up @@ -732,14 +724,15 @@ private static XDocument CreateCompiledPackageXml(out XElement root)
private XDocument EnsureStorage(out string packagesFile)
{
var packagesFolder = _hostingEnvironment.MapPathContentRoot(_packagesFolderPath);
//ensure it exists
Directory.CreateDirectory(packagesFolder);

packagesFile = _hostingEnvironment.MapPathContentRoot(CreatedPackagesFile);
if (!File.Exists(packagesFile))
{
var xml = new XDocument(new XElement("packages"));
xml.Save(packagesFile);

return xml;
}

var packagesXml = XDocument.Load(packagesFile);
Expand All @@ -749,9 +742,16 @@ private XDocument EnsureStorage(out string packagesFile)
public void DeleteLocalRepositoryFiles()
{
var packagesFile = _hostingEnvironment.MapPathContentRoot(CreatedPackagesFile);
File.Delete(packagesFile);
if (File.Exists(packagesFile))
{
File.Delete(packagesFile);
}

var packagesFolder = _hostingEnvironment.MapPathContentRoot(_packagesFolderPath);
Directory.Delete(packagesFolder);
if (Directory.Exists(packagesFolder))
{
Directory.Delete(packagesFolder);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class CreatedPackageSchemaRepository : ICreatedPackagesRepository
private readonly IMacroService _macroService;
private readonly IContentTypeService _contentTypeService;
private readonly string _tempFolderPath;
private readonly string _mediaFolderPath;
private readonly string _createdPackagesFolderPath;

/// <summary>
/// Initializes a new instance of the <see cref="CreatedPackageSchemaRepository"/> class.
Expand Down Expand Up @@ -76,9 +76,8 @@ public CreatedPackageSchemaRepository(
_macroService = macroService;
_contentTypeService = contentTypeService;
_xmlParser = new PackageDefinitionXmlParser();
_mediaFolderPath = mediaFolderPath ?? Path.Combine(globalSettings.Value.UmbracoMediaPhysicalRootPath, Constants.SystemDirectories.CreatedPackages);
_tempFolderPath =
tempFolderPath ?? Constants.SystemDirectories.TempData.EnsureEndsWith('/') + "PackageFiles";
_createdPackagesFolderPath = mediaFolderPath ?? Constants.SystemDirectories.CreatedPackages;
_tempFolderPath = tempFolderPath ?? Constants.SystemDirectories.TempData + "/PackageFiles";
}

public IEnumerable<PackageDefinition> GetAll()
Expand Down Expand Up @@ -192,17 +191,12 @@ public bool SavePackage(PackageDefinition definition)

public string ExportPackage(PackageDefinition definition)
{

// Ensure it's valid
ValidatePackage(definition);

// Create a folder for building this package
var temporaryPath =
_hostingEnvironment.MapPathContentRoot(_tempFolderPath.EnsureEndsWith('/') + Guid.NewGuid());
if (Directory.Exists(temporaryPath) == false)
{
Directory.CreateDirectory(temporaryPath);
}
var temporaryPath = _hostingEnvironment.MapPathContentRoot(Path.Combine(_tempFolderPath, Guid.NewGuid().ToString()));
Directory.CreateDirectory(temporaryPath);

try
{
Expand All @@ -218,8 +212,7 @@ public string ExportPackage(PackageDefinition definition)
PackageTemplates(definition, root);
PackageStylesheets(definition, root);
PackageStaticFiles(definition.Scripts, root, "Scripts", "Script", _fileSystems.ScriptsFileSystem);
PackageStaticFiles(definition.PartialViews, root, "PartialViews", "View",
_fileSystems.PartialViewsFileSystem);
PackageStaticFiles(definition.PartialViews, root, "PartialViews", "View", _fileSystems.PartialViewsFileSystem);
PackageMacros(definition, root);
PackageDictionaryItems(definition, root);
PackageLanguages(definition, root);
Expand Down Expand Up @@ -265,27 +258,25 @@ public string ExportPackage(PackageDefinition definition)
}
}

var directoryName =
_hostingEnvironment.MapPathWebRoot(
Path.Combine(_mediaFolderPath, definition.Name.Replace(' ', '_')));

if (Directory.Exists(directoryName) == false)
{
Directory.CreateDirectory(directoryName);
}
var directoryName = _hostingEnvironment.MapPathContentRoot(Path.Combine(_createdPackagesFolderPath, definition.Name.Replace(' ', '_')));
Directory.CreateDirectory(directoryName);

var finalPackagePath = Path.Combine(directoryName, fileName);

if (File.Exists(finalPackagePath))
// Clean existing files
foreach (var packagePath in new[]
{
File.Delete(finalPackagePath);
}

if (File.Exists(finalPackagePath.Replace("zip", "xml")))
definition.PackagePath,
finalPackagePath
})
{
File.Delete(finalPackagePath.Replace("zip", "xml"));
if (File.Exists(packagePath))
{
File.Delete(packagePath);
}
}

// Move to final package path
File.Move(tempPackagePath, finalPackagePath);

definition.PackagePath = finalPackagePath;
Expand Down