Skip to content

Commit 1ac7768

Browse files
authored
Merge pull request #12114 from umbraco/v9/feature/move-created-packages
Move created packages out of webroot
2 parents fbb80c4 + 7e8f23e commit 1ac7768

File tree

3 files changed

+37
-48
lines changed

3 files changed

+37
-48
lines changed

src/Umbraco.Core/Constants-SystemDirectories.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,11 @@ public static class SystemDirectories
4545

4646
public const string AppPlugins = "/App_Plugins";
4747

48-
4948
[Obsolete("Use PluginIcons instead")]
5049
public static string AppPluginIcons => "/Backoffice/Icons";
5150

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

54-
public const string CreatedPackages = "/created-packages";
55-
56-
5753
public const string MvcViews = "~/Views";
5854

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

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

61+
public const string CreatedPackages = Data + "/CreatedPackages";
62+
6563
public const string Preview = Data + "/preview";
6664

6765
/// <summary>

src/Umbraco.Core/Packaging/PackagesRepository.cs

+17-17
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.IO;
66
using System.IO.Compression;
77
using System.Linq;
8-
using System.Text;
98
using System.Xml.Linq;
109
using Microsoft.Extensions.Options;
1110
using Umbraco.Cms.Core.Configuration.Models;
@@ -33,7 +32,7 @@ public class PackagesRepository : ICreatedPackagesRepository
3332
private readonly IEntityXmlSerializer _serializer;
3433
private readonly IHostingEnvironment _hostingEnvironment;
3534
private readonly string _packageRepositoryFileName;
36-
private readonly string _mediaFolderPath;
35+
private readonly string _createdPackagesFolderPath;
3736
private readonly string _packagesFolderPath;
3837
private readonly string _tempFolderPath;
3938
private readonly PackageDefinitionXmlParser _parser;
@@ -93,7 +92,7 @@ public PackagesRepository(
9392

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

9897
_parser = new PackageDefinitionXmlParser();
9998
_mediaService = mediaService;
@@ -250,15 +249,8 @@ public string ExportPackage(PackageDefinition definition)
250249
}
251250
}
252251

253-
254-
255-
var directoryName =
256-
_hostingEnvironment.MapPathWebRoot(Path.Combine(_mediaFolderPath, definition.Name.Replace(' ', '_')));
257-
258-
if (Directory.Exists(directoryName) == false)
259-
{
260-
Directory.CreateDirectory(directoryName);
261-
}
252+
var directoryName = _hostingEnvironment.MapPathContentRoot(Path.Combine(_createdPackagesFolderPath, definition.Name.Replace(' ', '_')));
253+
Directory.CreateDirectory(directoryName);
262254

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

@@ -276,14 +268,14 @@ public string ExportPackage(PackageDefinition definition)
276268
}
277269
finally
278270
{
279-
//Clean up
271+
// Clean up
280272
Directory.Delete(temporaryPath, true);
281273
}
282274
}
283275

284276
private void ValidatePackage(PackageDefinition definition)
285277
{
286-
//ensure it's valid
278+
// ensure it's valid
287279
var context = new ValidationContext(definition, serviceProvider: null, items: null);
288280
var results = new List<ValidationResult>();
289281
var isValid = Validator.TryValidateObject(definition, context, results);
@@ -732,14 +724,15 @@ private static XDocument CreateCompiledPackageXml(out XElement root)
732724
private XDocument EnsureStorage(out string packagesFile)
733725
{
734726
var packagesFolder = _hostingEnvironment.MapPathContentRoot(_packagesFolderPath);
735-
//ensure it exists
736727
Directory.CreateDirectory(packagesFolder);
737728

738729
packagesFile = _hostingEnvironment.MapPathContentRoot(CreatedPackagesFile);
739730
if (!File.Exists(packagesFile))
740731
{
741732
var xml = new XDocument(new XElement("packages"));
742733
xml.Save(packagesFile);
734+
735+
return xml;
743736
}
744737

745738
var packagesXml = XDocument.Load(packagesFile);
@@ -749,9 +742,16 @@ private XDocument EnsureStorage(out string packagesFile)
749742
public void DeleteLocalRepositoryFiles()
750743
{
751744
var packagesFile = _hostingEnvironment.MapPathContentRoot(CreatedPackagesFile);
752-
File.Delete(packagesFile);
745+
if (File.Exists(packagesFile))
746+
{
747+
File.Delete(packagesFile);
748+
}
749+
753750
var packagesFolder = _hostingEnvironment.MapPathContentRoot(_packagesFolderPath);
754-
Directory.Delete(packagesFolder);
751+
if (Directory.Exists(packagesFolder))
752+
{
753+
Directory.Delete(packagesFolder);
754+
}
755755
}
756756
}
757757
}

src/Umbraco.Infrastructure/Persistence/Repositories/Implement/CreatedPackageSchemaRepository.cs

+18-27
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class CreatedPackageSchemaRepository : ICreatedPackagesRepository
3939
private readonly IMacroService _macroService;
4040
private readonly IContentTypeService _contentTypeService;
4141
private readonly string _tempFolderPath;
42-
private readonly string _mediaFolderPath;
42+
private readonly string _createdPackagesFolderPath;
4343

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

8483
public IEnumerable<PackageDefinition> GetAll()
@@ -192,17 +191,12 @@ public bool SavePackage(PackageDefinition definition)
192191

193192
public string ExportPackage(PackageDefinition definition)
194193
{
195-
196194
// Ensure it's valid
197195
ValidatePackage(definition);
198196

199197
// Create a folder for building this package
200-
var temporaryPath =
201-
_hostingEnvironment.MapPathContentRoot(_tempFolderPath.EnsureEndsWith('/') + Guid.NewGuid());
202-
if (Directory.Exists(temporaryPath) == false)
203-
{
204-
Directory.CreateDirectory(temporaryPath);
205-
}
198+
var temporaryPath = _hostingEnvironment.MapPathContentRoot(Path.Combine(_tempFolderPath, Guid.NewGuid().ToString()));
199+
Directory.CreateDirectory(temporaryPath);
206200

207201
try
208202
{
@@ -218,8 +212,7 @@ public string ExportPackage(PackageDefinition definition)
218212
PackageTemplates(definition, root);
219213
PackageStylesheets(definition, root);
220214
PackageStaticFiles(definition.Scripts, root, "Scripts", "Script", _fileSystems.ScriptsFileSystem);
221-
PackageStaticFiles(definition.PartialViews, root, "PartialViews", "View",
222-
_fileSystems.PartialViewsFileSystem);
215+
PackageStaticFiles(definition.PartialViews, root, "PartialViews", "View", _fileSystems.PartialViewsFileSystem);
223216
PackageMacros(definition, root);
224217
PackageDictionaryItems(definition, root);
225218
PackageLanguages(definition, root);
@@ -265,27 +258,25 @@ public string ExportPackage(PackageDefinition definition)
265258
}
266259
}
267260

268-
var directoryName =
269-
_hostingEnvironment.MapPathWebRoot(
270-
Path.Combine(_mediaFolderPath, definition.Name.Replace(' ', '_')));
271-
272-
if (Directory.Exists(directoryName) == false)
273-
{
274-
Directory.CreateDirectory(directoryName);
275-
}
261+
var directoryName = _hostingEnvironment.MapPathContentRoot(Path.Combine(_createdPackagesFolderPath, definition.Name.Replace(' ', '_')));
262+
Directory.CreateDirectory(directoryName);
276263

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

279-
if (File.Exists(finalPackagePath))
266+
// Clean existing files
267+
foreach (var packagePath in new[]
280268
{
281-
File.Delete(finalPackagePath);
282-
}
283-
284-
if (File.Exists(finalPackagePath.Replace("zip", "xml")))
269+
definition.PackagePath,
270+
finalPackagePath
271+
})
285272
{
286-
File.Delete(finalPackagePath.Replace("zip", "xml"));
273+
if (File.Exists(packagePath))
274+
{
275+
File.Delete(packagePath);
276+
}
287277
}
288278

279+
// Move to final package path
289280
File.Move(tempPackagePath, finalPackagePath);
290281

291282
definition.PackagePath = finalPackagePath;

0 commit comments

Comments
 (0)