2021-12-21 14:02:49 +01:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.IO.Compression;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using NPoco;
|
|
|
|
|
using Umbraco.Cms.Core;
|
|
|
|
|
using Umbraco.Cms.Core.Configuration.Models;
|
|
|
|
|
using Umbraco.Cms.Core.Hosting;
|
|
|
|
|
using Umbraco.Cms.Core.IO;
|
|
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
using Umbraco.Cms.Core.Packaging;
|
|
|
|
|
using Umbraco.Cms.Core.Services;
|
|
|
|
|
using Umbraco.Cms.Infrastructure.Persistence.Dtos;
|
|
|
|
|
using Umbraco.Extensions;
|
|
|
|
|
using File = System.IO.File;
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
|
|
|
|
|
{
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public class CreatedPackageSchemaRepository : ICreatedPackagesRepository
|
|
|
|
|
{
|
|
|
|
|
private readonly PackageDefinitionXmlParser _xmlParser;
|
2022-02-24 09:24:56 +01:00
|
|
|
private readonly IUmbracoDatabase? _umbracoDatabase;
|
2021-12-21 14:02:49 +01:00
|
|
|
private readonly IHostingEnvironment _hostingEnvironment;
|
|
|
|
|
private readonly FileSystems _fileSystems;
|
|
|
|
|
private readonly IEntityXmlSerializer _serializer;
|
|
|
|
|
private readonly IDataTypeService _dataTypeService;
|
|
|
|
|
private readonly ILocalizationService _localizationService;
|
|
|
|
|
private readonly IFileService _fileService;
|
|
|
|
|
private readonly IMediaService _mediaService;
|
|
|
|
|
private readonly IMediaTypeService _mediaTypeService;
|
|
|
|
|
private readonly IContentService _contentService;
|
|
|
|
|
private readonly MediaFileManager _mediaFileManager;
|
|
|
|
|
private readonly IMacroService _macroService;
|
|
|
|
|
private readonly IContentTypeService _contentTypeService;
|
|
|
|
|
private readonly string _tempFolderPath;
|
2022-03-09 11:06:30 +01:00
|
|
|
private readonly string _createdPackagesFolderPath;
|
2021-12-21 14:02:49 +01:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="CreatedPackageSchemaRepository"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public CreatedPackageSchemaRepository(
|
2022-01-23 15:26:17 +01:00
|
|
|
IUmbracoDatabaseFactory umbracoDatabaseFactory,
|
2021-12-21 14:02:49 +01:00
|
|
|
IHostingEnvironment hostingEnvironment,
|
|
|
|
|
IOptions<GlobalSettings> globalSettings,
|
|
|
|
|
FileSystems fileSystems,
|
|
|
|
|
IEntityXmlSerializer serializer,
|
|
|
|
|
IDataTypeService dataTypeService,
|
|
|
|
|
ILocalizationService localizationService,
|
|
|
|
|
IFileService fileService,
|
|
|
|
|
IMediaService mediaService,
|
|
|
|
|
IMediaTypeService mediaTypeService,
|
|
|
|
|
IContentService contentService,
|
|
|
|
|
MediaFileManager mediaFileManager,
|
|
|
|
|
IMacroService macroService,
|
|
|
|
|
IContentTypeService contentTypeService,
|
2022-02-24 09:24:56 +01:00
|
|
|
string? mediaFolderPath = null,
|
|
|
|
|
string? tempFolderPath = null)
|
2021-12-21 14:02:49 +01:00
|
|
|
{
|
2022-01-23 15:26:17 +01:00
|
|
|
_umbracoDatabase = umbracoDatabaseFactory.CreateDatabase();
|
2021-12-21 14:02:49 +01:00
|
|
|
_hostingEnvironment = hostingEnvironment;
|
|
|
|
|
_fileSystems = fileSystems;
|
|
|
|
|
_serializer = serializer;
|
|
|
|
|
_dataTypeService = dataTypeService;
|
|
|
|
|
_localizationService = localizationService;
|
|
|
|
|
_fileService = fileService;
|
|
|
|
|
_mediaService = mediaService;
|
|
|
|
|
_mediaTypeService = mediaTypeService;
|
|
|
|
|
_contentService = contentService;
|
|
|
|
|
_mediaFileManager = mediaFileManager;
|
|
|
|
|
_macroService = macroService;
|
|
|
|
|
_contentTypeService = contentTypeService;
|
|
|
|
|
_xmlParser = new PackageDefinitionXmlParser();
|
2022-03-09 11:06:30 +01:00
|
|
|
_createdPackagesFolderPath = mediaFolderPath ?? Constants.SystemDirectories.CreatedPackages;
|
|
|
|
|
_tempFolderPath = tempFolderPath ?? Constants.SystemDirectories.TempData + "/PackageFiles";
|
2021-12-21 14:02:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<PackageDefinition> GetAll()
|
|
|
|
|
{
|
2022-02-24 09:24:56 +01:00
|
|
|
Sql<ISqlContext> query = new Sql<ISqlContext>(_umbracoDatabase!.SqlContext)
|
2021-12-21 14:02:49 +01:00
|
|
|
.Select<CreatedPackageSchemaDto>()
|
|
|
|
|
.From<CreatedPackageSchemaDto>()
|
|
|
|
|
.OrderBy<CreatedPackageSchemaDto>(x => x.Id);
|
|
|
|
|
|
|
|
|
|
var packageDefinitions = new List<PackageDefinition>();
|
|
|
|
|
|
|
|
|
|
List<CreatedPackageSchemaDto> xmlSchemas = _umbracoDatabase.Fetch<CreatedPackageSchemaDto>(query);
|
|
|
|
|
foreach (CreatedPackageSchemaDto packageSchema in xmlSchemas)
|
|
|
|
|
{
|
|
|
|
|
var packageDefinition = _xmlParser.ToPackageDefinition(XElement.Parse(packageSchema.Value));
|
2022-02-24 09:24:56 +01:00
|
|
|
if (packageDefinition is not null)
|
|
|
|
|
{
|
|
|
|
|
packageDefinition.Id = packageSchema.Id;
|
|
|
|
|
packageDefinition.Name = packageSchema.Name;
|
|
|
|
|
packageDefinition.PackageId = packageSchema.PackageId;
|
|
|
|
|
packageDefinitions.Add(packageDefinition);
|
|
|
|
|
}
|
2021-12-21 14:02:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return packageDefinitions;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 09:24:56 +01:00
|
|
|
public PackageDefinition? GetById(int id)
|
2021-12-21 14:02:49 +01:00
|
|
|
{
|
2022-02-24 09:24:56 +01:00
|
|
|
Sql<ISqlContext> query = new Sql<ISqlContext>(_umbracoDatabase!.SqlContext)
|
2021-12-21 14:02:49 +01:00
|
|
|
.Select<CreatedPackageSchemaDto>()
|
|
|
|
|
.From<CreatedPackageSchemaDto>()
|
|
|
|
|
.Where<CreatedPackageSchemaDto>(x => x.Id == id);
|
|
|
|
|
List<CreatedPackageSchemaDto> schemaDtos = _umbracoDatabase.Fetch<CreatedPackageSchemaDto>(query);
|
|
|
|
|
|
|
|
|
|
if (schemaDtos.IsCollectionEmpty())
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var packageSchema = schemaDtos.First();
|
|
|
|
|
var packageDefinition = _xmlParser.ToPackageDefinition(XElement.Parse(packageSchema.Value));
|
2022-02-24 09:24:56 +01:00
|
|
|
if (packageDefinition is not null)
|
|
|
|
|
{
|
|
|
|
|
packageDefinition.Id = packageSchema.Id;
|
|
|
|
|
packageDefinition.Name = packageSchema.Name;
|
|
|
|
|
packageDefinition.PackageId = packageSchema.PackageId;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 14:02:49 +01:00
|
|
|
return packageDefinition;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Delete(int id)
|
|
|
|
|
{
|
|
|
|
|
// Delete package snapshot
|
|
|
|
|
var packageDef = GetById(id);
|
2022-02-24 09:24:56 +01:00
|
|
|
if (File.Exists(packageDef?.PackagePath))
|
2021-12-21 14:02:49 +01:00
|
|
|
{
|
|
|
|
|
File.Delete(packageDef.PackagePath);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 09:24:56 +01:00
|
|
|
Sql<ISqlContext> query = new Sql<ISqlContext>(_umbracoDatabase!.SqlContext)
|
2021-12-21 14:02:49 +01:00
|
|
|
.Delete<CreatedPackageSchemaDto>()
|
|
|
|
|
.Where<CreatedPackageSchemaDto>(x => x.Id == id);
|
|
|
|
|
|
v10 SQLite support + distributed locking abstractions (#11922)
* Created Persistence.SQLite project skeleton.
* SQLite database initialization
* Various changes and hacks to make things work.
* WIP integration tests
* Fix thread safety tests
* Fix tests that relied on tie breaker sorting.
Spent a fair amount of time looking for a less lazy fix but gave up.
* Convert right join to left join ContentTypeRepository.PerformGetByQuery
SQLite doesn't support right join
* Fix test Can_Generate_Delete_SubQuery_Statement
Worth noting that NPoco.DatabaseTypes.SQLiteDatabaseType doesn't override
EscapeSqlIdentifier so NPoco will escape with [].
SQLite docs say > "A keyword enclosed in square brackets is an identifier.
This is not standard SQL.
This quoting mechanism is used by MS Access and SQL Server and is
included in SQLite for compatibility."
Also could have updated SqliteSyntaxProvider to match npoco but
decided against it.
* Fixes for paginated custom order by
* Fix tests broken by lack of unique indexes.
* Fix SqlServerTableByTableTest tests.
These tests didn't actually do anything as the tables already exist so schema creator just returned.
Did however point out that the default implementation for DoesTableExist just returns false so added a default naive implementation.
* Fix ValidateLoginSession - SelectTop must come later
* dry up database cleanup
* Fix up db migration tests.
We can't drop pk in sqlite without recreating table.
Test looks to be testing that add column works as intended which we can test.
* Prevent schema creation errors.
* SQLite ignore lock tests, WAL back on.
* Fix package schema tests
* Fix NPocoFetchTests - case sensitivity not under test
* Fix AdvancedMigrationTests (where possible)
Migrations probably need a good look later.
Maybe nuke old migrations and only support moving to v10 from v9.
If we do that can do some cleanup.
* Cleanup test database configuration
* Run integration tests against SQLite on build agent.
* Drop MS.Data.SQLite
System.Data.SQLite was quicker to roll out due to more CLR type mapping
* YAML
* Skip Umbraco.Tests.Integration.SqlCe
* Drop SqlServerTableByTable tests.
Until this week they did nothing anyway as they with NewSchemaPerTest
so the tests all passed as CreateTable was no op (already exists).
Also all of the tables are created in an empty database by SchemaValidationTest.cs
DatabaseSchemaCreation_Produces_DatabaseSchemaResult_With_Zero_Errors
* Might aswell run against macOS also.
* Copy azure pipelines task header layout
* Delete SQLCe projects
* Remove SQL CE specific code.
* Remove SQL CE NuSpec, template params, build script setup
* Delete umbraco-netcore-only.sln
* Add SkipTests solution configuration and use for codeql
* Remove reference to deleted nuspec file.
* Refactor ConnectionStrings WRT DataDirectory placeholder & ProviderName.
At this point you can try out SQLite support by setting the following
in appsettings.json and then completing the install process.
"ConnectionStrings": {
"umbracoDbDSN": "Data Source=|DataDirectory|/umbraco.sqlite",
"umbracoDbDSN_ProviderName": "System.Data.SQLite"
},
Not currently possible via installer UI without provider name pre-set in
configuration.
* Switch to Microsoft.Data.Sqlite
Some gross hacks but will be good to find out if this works
with apple silicon.
* Enable selection of SQLite via installer UI (also quick install)
* Remove SqlServerDbProviderFactoryCreator to cleanup a TODO
* Move SQL Server support to its own class library
* Add persistence dependencies to Umbraco.CMS metapackage
* Bugfix packages delete query
Created invalid query for SQLite.
* Try out cypress tests Linux + SQLite
* Prevent cypress test artifact upload failure on attempt 2+
* LocalDb bugfixes
* Drop redundant enum
* Move SqlClient constant
* Misc whitespace
* Remove IsSqlCe extension (TODO: drop non 9->10 migrations later).
* Umbraco.Persistence.* -> Umbraco.Cms.Persistence.*
* Display quick install defaults and per provider default database name.
* Misc remove old comment
* little re-arrange
* Remove almost all usages of IsSqlite extension.
* visual adjustments
* Custom Database Configuration is last step and should then say Install.
* use text instead of disabled inputs
* move legend, rename to Install
* Update SqlMainDomLock to work without distributed locks.
* Added IDistributedLockingMechanism interface and in memory impl.
* Drop locking from ISqlSyntaxProvider & wire up scope to abstraction.
* Added SqlServerDistributedLockingMechanism
* Move distributed locking interfaces and exceptions to Core + xmldocs.
* Fix tests, Misc cleanup, Add SQL distributed locking integration tests
* Provide mechanism to specify DistributedLockingMechanism in config
(even if added by composer)
* Nomplementation -> NoImplementation
* Fix misleading comment
* Integration tests use SqlServerDistributedLockingMechanism when possible
* Handle up-gradable locks SqlServerDistributedLockingMechanism.
TODO: InMemoryDistributedLockingMechanism.
Note: Nuked SqlServerDistributedLockingMechanismTests, will still sleep
at night.
Is covered by Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.LockTests
* Make tests pass for InMemoryDistributedLockingMechanism, pretty hacky.
* Tweak constraints on WithCollectionBuilder so i can drop bad constructor
* Added SqliteDistributedLockingMechanism
* Dropped InMemoryDistributedMechanism + magic
InMemoryDistributedMechanism was pretty rubbish and now we have
a decent implementation for SQLite as we no longer block readers
see 8d1f42b.
Also drop the CollectionBuilder setup, instead do the same as we do
for syntax providers etc, it's more automagical so we never require an
explicit selection although we are allowing for it.
However keeping the optional IUmbracoBuilder constructor param for
CollectionBuilders as it's extremely useful.
* Fix quick install "" database name.
* Hide Database Configuration section when a connection string is pre-set.
Doesn't seem worth it to extract db name from connection string.
* Ensure wal test 2+
* Fix logging inconsistencies.
* Ensure in transaction when obtaining locks + no-op the SQLite read lock.
There's no point in running the query just to make a single test pass.
* Fix installer database display names
* Allow SQLite shared cache without losing deferred transactions
* Opt into shared cache for new SQLite databases + fix filename
* Fix misc inconsistency in .gitignore
* Prefer our interceptor interface
* Restore DEBUG_DATABASES code OnConnectionOpened in case it's used.
* Back to private cache.
* Added retry strategy for SQLite + refactor out SQL server specific stuff
* Fix SQL server tests.
* Misc - Orphaned comment, incorrect casing.
* InMemory SQLite test database & turn shared cache back on everywhere.
Co-authored-by: Niels Lyngsø <niels.lyngso@gmail.com>
2022-03-11 16:14:20 +00:00
|
|
|
_umbracoDatabase.Execute(query);
|
2021-12-21 14:02:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SavePackage(PackageDefinition definition)
|
|
|
|
|
{
|
|
|
|
|
if (definition == null)
|
|
|
|
|
{
|
|
|
|
|
throw new NullReferenceException("PackageDefinition cannot be null when saving");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (definition.Name == null || string.IsNullOrEmpty(definition.Name) || definition.PackagePath == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensure it's valid
|
|
|
|
|
ValidatePackage(definition);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (definition.Id == default)
|
|
|
|
|
{
|
|
|
|
|
// Create dto from definition
|
|
|
|
|
var dto = new CreatedPackageSchemaDto()
|
|
|
|
|
{
|
|
|
|
|
Name = definition.Name,
|
|
|
|
|
Value = _xmlParser.ToXml(definition).ToString(),
|
|
|
|
|
UpdateDate = DateTime.Now,
|
|
|
|
|
PackageId = Guid.NewGuid()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Set the ids, we have to save in database first to get the Id
|
2022-03-16 14:39:28 +01:00
|
|
|
_umbracoDatabase!.Insert(dto);
|
v10 SQLite support + distributed locking abstractions (#11922)
* Created Persistence.SQLite project skeleton.
* SQLite database initialization
* Various changes and hacks to make things work.
* WIP integration tests
* Fix thread safety tests
* Fix tests that relied on tie breaker sorting.
Spent a fair amount of time looking for a less lazy fix but gave up.
* Convert right join to left join ContentTypeRepository.PerformGetByQuery
SQLite doesn't support right join
* Fix test Can_Generate_Delete_SubQuery_Statement
Worth noting that NPoco.DatabaseTypes.SQLiteDatabaseType doesn't override
EscapeSqlIdentifier so NPoco will escape with [].
SQLite docs say > "A keyword enclosed in square brackets is an identifier.
This is not standard SQL.
This quoting mechanism is used by MS Access and SQL Server and is
included in SQLite for compatibility."
Also could have updated SqliteSyntaxProvider to match npoco but
decided against it.
* Fixes for paginated custom order by
* Fix tests broken by lack of unique indexes.
* Fix SqlServerTableByTableTest tests.
These tests didn't actually do anything as the tables already exist so schema creator just returned.
Did however point out that the default implementation for DoesTableExist just returns false so added a default naive implementation.
* Fix ValidateLoginSession - SelectTop must come later
* dry up database cleanup
* Fix up db migration tests.
We can't drop pk in sqlite without recreating table.
Test looks to be testing that add column works as intended which we can test.
* Prevent schema creation errors.
* SQLite ignore lock tests, WAL back on.
* Fix package schema tests
* Fix NPocoFetchTests - case sensitivity not under test
* Fix AdvancedMigrationTests (where possible)
Migrations probably need a good look later.
Maybe nuke old migrations and only support moving to v10 from v9.
If we do that can do some cleanup.
* Cleanup test database configuration
* Run integration tests against SQLite on build agent.
* Drop MS.Data.SQLite
System.Data.SQLite was quicker to roll out due to more CLR type mapping
* YAML
* Skip Umbraco.Tests.Integration.SqlCe
* Drop SqlServerTableByTable tests.
Until this week they did nothing anyway as they with NewSchemaPerTest
so the tests all passed as CreateTable was no op (already exists).
Also all of the tables are created in an empty database by SchemaValidationTest.cs
DatabaseSchemaCreation_Produces_DatabaseSchemaResult_With_Zero_Errors
* Might aswell run against macOS also.
* Copy azure pipelines task header layout
* Delete SQLCe projects
* Remove SQL CE specific code.
* Remove SQL CE NuSpec, template params, build script setup
* Delete umbraco-netcore-only.sln
* Add SkipTests solution configuration and use for codeql
* Remove reference to deleted nuspec file.
* Refactor ConnectionStrings WRT DataDirectory placeholder & ProviderName.
At this point you can try out SQLite support by setting the following
in appsettings.json and then completing the install process.
"ConnectionStrings": {
"umbracoDbDSN": "Data Source=|DataDirectory|/umbraco.sqlite",
"umbracoDbDSN_ProviderName": "System.Data.SQLite"
},
Not currently possible via installer UI without provider name pre-set in
configuration.
* Switch to Microsoft.Data.Sqlite
Some gross hacks but will be good to find out if this works
with apple silicon.
* Enable selection of SQLite via installer UI (also quick install)
* Remove SqlServerDbProviderFactoryCreator to cleanup a TODO
* Move SQL Server support to its own class library
* Add persistence dependencies to Umbraco.CMS metapackage
* Bugfix packages delete query
Created invalid query for SQLite.
* Try out cypress tests Linux + SQLite
* Prevent cypress test artifact upload failure on attempt 2+
* LocalDb bugfixes
* Drop redundant enum
* Move SqlClient constant
* Misc whitespace
* Remove IsSqlCe extension (TODO: drop non 9->10 migrations later).
* Umbraco.Persistence.* -> Umbraco.Cms.Persistence.*
* Display quick install defaults and per provider default database name.
* Misc remove old comment
* little re-arrange
* Remove almost all usages of IsSqlite extension.
* visual adjustments
* Custom Database Configuration is last step and should then say Install.
* use text instead of disabled inputs
* move legend, rename to Install
* Update SqlMainDomLock to work without distributed locks.
* Added IDistributedLockingMechanism interface and in memory impl.
* Drop locking from ISqlSyntaxProvider & wire up scope to abstraction.
* Added SqlServerDistributedLockingMechanism
* Move distributed locking interfaces and exceptions to Core + xmldocs.
* Fix tests, Misc cleanup, Add SQL distributed locking integration tests
* Provide mechanism to specify DistributedLockingMechanism in config
(even if added by composer)
* Nomplementation -> NoImplementation
* Fix misleading comment
* Integration tests use SqlServerDistributedLockingMechanism when possible
* Handle up-gradable locks SqlServerDistributedLockingMechanism.
TODO: InMemoryDistributedLockingMechanism.
Note: Nuked SqlServerDistributedLockingMechanismTests, will still sleep
at night.
Is covered by Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.LockTests
* Make tests pass for InMemoryDistributedLockingMechanism, pretty hacky.
* Tweak constraints on WithCollectionBuilder so i can drop bad constructor
* Added SqliteDistributedLockingMechanism
* Dropped InMemoryDistributedMechanism + magic
InMemoryDistributedMechanism was pretty rubbish and now we have
a decent implementation for SQLite as we no longer block readers
see 8d1f42b.
Also drop the CollectionBuilder setup, instead do the same as we do
for syntax providers etc, it's more automagical so we never require an
explicit selection although we are allowing for it.
However keeping the optional IUmbracoBuilder constructor param for
CollectionBuilders as it's extremely useful.
* Fix quick install "" database name.
* Hide Database Configuration section when a connection string is pre-set.
Doesn't seem worth it to extract db name from connection string.
* Ensure wal test 2+
* Fix logging inconsistencies.
* Ensure in transaction when obtaining locks + no-op the SQLite read lock.
There's no point in running the query just to make a single test pass.
* Fix installer database display names
* Allow SQLite shared cache without losing deferred transactions
* Opt into shared cache for new SQLite databases + fix filename
* Fix misc inconsistency in .gitignore
* Prefer our interceptor interface
* Restore DEBUG_DATABASES code OnConnectionOpened in case it's used.
* Back to private cache.
* Added retry strategy for SQLite + refactor out SQL server specific stuff
* Fix SQL server tests.
* Misc - Orphaned comment, incorrect casing.
* InMemory SQLite test database & turn shared cache back on everywhere.
Co-authored-by: Niels Lyngsø <niels.lyngso@gmail.com>
2022-03-11 16:14:20 +00:00
|
|
|
definition.Id = dto.Id;
|
2021-12-21 14:02:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Save snapshot locally, we do this to the updated packagePath
|
|
|
|
|
ExportPackage(definition);
|
|
|
|
|
// Create dto from definition
|
|
|
|
|
var updatedDto = new CreatedPackageSchemaDto()
|
|
|
|
|
{
|
|
|
|
|
Name = definition.Name,
|
|
|
|
|
Value = _xmlParser.ToXml(definition).ToString(),
|
|
|
|
|
Id = definition.Id,
|
|
|
|
|
PackageId = definition.PackageId,
|
|
|
|
|
UpdateDate = DateTime.Now
|
|
|
|
|
};
|
2022-02-24 09:24:56 +01:00
|
|
|
_umbracoDatabase?.Update(updatedDto);
|
2021-12-21 14:02:49 +01:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string ExportPackage(PackageDefinition definition)
|
|
|
|
|
{
|
|
|
|
|
// Ensure it's valid
|
|
|
|
|
ValidatePackage(definition);
|
|
|
|
|
|
|
|
|
|
// Create a folder for building this package
|
2022-03-09 11:06:30 +01:00
|
|
|
var temporaryPath = _hostingEnvironment.MapPathContentRoot(Path.Combine(_tempFolderPath, Guid.NewGuid().ToString()));
|
|
|
|
|
Directory.CreateDirectory(temporaryPath);
|
2021-12-21 14:02:49 +01:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Init package file
|
|
|
|
|
XDocument compiledPackageXml = CreateCompiledPackageXml(out XElement root);
|
|
|
|
|
|
|
|
|
|
// Info section
|
|
|
|
|
root.Add(GetPackageInfoXml(definition));
|
|
|
|
|
|
|
|
|
|
PackageDocumentsAndTags(definition, root);
|
|
|
|
|
PackageDocumentTypes(definition, root);
|
|
|
|
|
PackageMediaTypes(definition, root);
|
|
|
|
|
PackageTemplates(definition, root);
|
|
|
|
|
PackageStylesheets(definition, root);
|
2022-02-24 09:24:56 +01:00
|
|
|
PackageStaticFiles(definition.Scripts, root, "Scripts", "Script", _fileSystems.ScriptsFileSystem!);
|
2022-03-16 14:39:28 +01:00
|
|
|
PackageStaticFiles(definition.PartialViews, root, "PartialViews", "View", _fileSystems.PartialViewsFileSystem!);
|
2021-12-21 14:02:49 +01:00
|
|
|
PackageMacros(definition, root);
|
|
|
|
|
PackageDictionaryItems(definition, root);
|
|
|
|
|
PackageLanguages(definition, root);
|
|
|
|
|
PackageDataTypes(definition, root);
|
|
|
|
|
Dictionary<string, Stream> mediaFiles = PackageMedia(definition, root);
|
|
|
|
|
|
|
|
|
|
string fileName;
|
|
|
|
|
string tempPackagePath;
|
|
|
|
|
if (mediaFiles.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
fileName = "package.zip";
|
|
|
|
|
tempPackagePath = Path.Combine(temporaryPath, fileName);
|
|
|
|
|
using (FileStream fileStream = File.OpenWrite(tempPackagePath))
|
|
|
|
|
using (var archive = new ZipArchive(fileStream, ZipArchiveMode.Create, true))
|
|
|
|
|
{
|
|
|
|
|
ZipArchiveEntry packageXmlEntry = archive.CreateEntry("package.xml");
|
|
|
|
|
using (Stream entryStream = packageXmlEntry.Open())
|
|
|
|
|
{
|
|
|
|
|
compiledPackageXml.Save(entryStream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (KeyValuePair<string, Stream> mediaFile in mediaFiles)
|
|
|
|
|
{
|
|
|
|
|
var entryPath = $"media{mediaFile.Key.EnsureStartsWith('/')}";
|
|
|
|
|
ZipArchiveEntry mediaEntry = archive.CreateEntry(entryPath);
|
|
|
|
|
using (Stream entryStream = mediaEntry.Open())
|
|
|
|
|
using (mediaFile.Value)
|
|
|
|
|
{
|
|
|
|
|
mediaFile.Value.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
mediaFile.Value.CopyTo(entryStream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
fileName = "package.xml";
|
|
|
|
|
tempPackagePath = Path.Combine(temporaryPath, fileName);
|
|
|
|
|
|
|
|
|
|
using (FileStream fileStream = File.OpenWrite(tempPackagePath))
|
|
|
|
|
{
|
|
|
|
|
compiledPackageXml.Save(fileStream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-09 11:06:30 +01:00
|
|
|
var directoryName = _hostingEnvironment.MapPathContentRoot(Path.Combine(_createdPackagesFolderPath, definition.Name.Replace(' ', '_')));
|
|
|
|
|
Directory.CreateDirectory(directoryName);
|
2021-12-21 14:02:49 +01:00
|
|
|
|
|
|
|
|
var finalPackagePath = Path.Combine(directoryName, fileName);
|
|
|
|
|
|
2022-03-09 11:38:52 +01:00
|
|
|
// Clean existing files
|
2022-03-09 11:06:30 +01:00
|
|
|
foreach (var packagePath in new[]
|
2021-12-21 14:02:49 +01:00
|
|
|
{
|
2022-03-09 11:38:52 +01:00
|
|
|
definition.PackagePath,
|
|
|
|
|
finalPackagePath
|
2022-03-09 11:06:30 +01:00
|
|
|
})
|
2021-12-21 14:02:49 +01:00
|
|
|
{
|
2022-03-09 11:06:30 +01:00
|
|
|
if (File.Exists(packagePath))
|
|
|
|
|
{
|
|
|
|
|
File.Delete(packagePath);
|
|
|
|
|
}
|
2021-12-21 14:02:49 +01:00
|
|
|
}
|
|
|
|
|
|
2022-03-09 11:06:30 +01:00
|
|
|
// Move to final package path
|
2021-12-21 14:02:49 +01:00
|
|
|
File.Move(tempPackagePath, finalPackagePath);
|
|
|
|
|
|
|
|
|
|
definition.PackagePath = finalPackagePath;
|
|
|
|
|
|
|
|
|
|
return finalPackagePath;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
// Clean up
|
|
|
|
|
Directory.Delete(temporaryPath, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private XDocument CreateCompiledPackageXml(out XElement root)
|
|
|
|
|
{
|
|
|
|
|
root = new XElement("umbPackage");
|
|
|
|
|
var compiledPackageXml = new XDocument(root);
|
|
|
|
|
return compiledPackageXml;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ValidatePackage(PackageDefinition definition)
|
|
|
|
|
{
|
|
|
|
|
// 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);
|
|
|
|
|
if (!isValid)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Validation failed, there is invalid data on the model: " +
|
|
|
|
|
string.Join(", ", results.Select(x => x.ErrorMessage)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PackageDataTypes(PackageDefinition definition, XContainer root)
|
|
|
|
|
{
|
|
|
|
|
var dataTypes = new XElement("DataTypes");
|
|
|
|
|
foreach (var dtId in definition.DataTypes)
|
|
|
|
|
{
|
|
|
|
|
if (!int.TryParse(dtId, NumberStyles.Integer, CultureInfo.InvariantCulture, out var outInt))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 09:24:56 +01:00
|
|
|
IDataType? dataType = _dataTypeService.GetDataType(outInt);
|
2021-12-21 14:02:49 +01:00
|
|
|
if (dataType == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dataTypes.Add(_serializer.Serialize(dataType));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root.Add(dataTypes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PackageLanguages(PackageDefinition definition, XContainer root)
|
|
|
|
|
{
|
|
|
|
|
var languages = new XElement("Languages");
|
|
|
|
|
foreach (var langId in definition.Languages)
|
|
|
|
|
{
|
|
|
|
|
if (!int.TryParse(langId, NumberStyles.Integer, CultureInfo.InvariantCulture, out var outInt))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 09:24:56 +01:00
|
|
|
ILanguage? lang = _localizationService.GetLanguageById(outInt);
|
2021-12-21 14:02:49 +01:00
|
|
|
if (lang == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
languages.Add(_serializer.Serialize(lang));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root.Add(languages);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PackageDictionaryItems(PackageDefinition definition, XContainer root)
|
|
|
|
|
{
|
|
|
|
|
var rootDictionaryItems = new XElement("DictionaryItems");
|
|
|
|
|
var items = new Dictionary<Guid, (IDictionaryItem dictionaryItem, XElement serializedDictionaryValue)>();
|
|
|
|
|
|
|
|
|
|
foreach (var dictionaryId in definition.DictionaryItems)
|
|
|
|
|
{
|
|
|
|
|
if (!int.TryParse(dictionaryId, NumberStyles.Integer, CultureInfo.InvariantCulture, out var outInt))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 09:24:56 +01:00
|
|
|
IDictionaryItem? di = _localizationService.GetDictionaryItemById(outInt);
|
2021-12-21 14:02:49 +01:00
|
|
|
|
|
|
|
|
if (di == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
items[di.Key] = (di, _serializer.Serialize(di, false));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// organize them in hierarchy ...
|
|
|
|
|
var itemCount = items.Count;
|
|
|
|
|
var processed = new Dictionary<Guid, XElement>();
|
|
|
|
|
while (processed.Count < itemCount)
|
|
|
|
|
{
|
|
|
|
|
foreach (Guid key in items.Keys.ToList())
|
|
|
|
|
{
|
|
|
|
|
(IDictionaryItem dictionaryItem, XElement serializedDictionaryValue) = items[key];
|
|
|
|
|
|
|
|
|
|
if (!dictionaryItem.ParentId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
// if it has no parent, its definitely just at the root
|
|
|
|
|
AppendDictionaryElement(rootDictionaryItems, items, processed, key, serializedDictionaryValue);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (processed.ContainsKey(dictionaryItem.ParentId.Value))
|
|
|
|
|
{
|
|
|
|
|
// we've processed this parent element already so we can just append this xml child to it
|
|
|
|
|
AppendDictionaryElement(processed[dictionaryItem.ParentId.Value], items, processed, key,
|
|
|
|
|
serializedDictionaryValue);
|
|
|
|
|
}
|
|
|
|
|
else if (items.ContainsKey(dictionaryItem.ParentId.Value))
|
|
|
|
|
{
|
|
|
|
|
// we know the parent exists in the dictionary but
|
|
|
|
|
// we haven't processed it yet so we'll leave it for the next loop
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// in this case, the parent of this item doesn't exist in our collection, we have no
|
|
|
|
|
// choice but to add it to the root.
|
|
|
|
|
AppendDictionaryElement(rootDictionaryItems, items, processed, key,
|
|
|
|
|
serializedDictionaryValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root.Add(rootDictionaryItems);
|
|
|
|
|
|
|
|
|
|
static void AppendDictionaryElement(XElement rootDictionaryItems,
|
|
|
|
|
Dictionary<Guid, (IDictionaryItem dictionaryItem, XElement serializedDictionaryValue)> items,
|
|
|
|
|
Dictionary<Guid, XElement> processed, Guid key, XElement serializedDictionaryValue)
|
|
|
|
|
{
|
|
|
|
|
// track it
|
|
|
|
|
processed.Add(key, serializedDictionaryValue);
|
|
|
|
|
|
|
|
|
|
// append it
|
|
|
|
|
rootDictionaryItems.Add(serializedDictionaryValue);
|
|
|
|
|
|
|
|
|
|
// remove it so its not re-processed
|
|
|
|
|
items.Remove(key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PackageMacros(PackageDefinition definition, XContainer root)
|
|
|
|
|
{
|
|
|
|
|
var packagedMacros = new List<IMacro>();
|
|
|
|
|
var macros = new XElement("Macros");
|
|
|
|
|
foreach (var macroId in definition.Macros)
|
|
|
|
|
{
|
|
|
|
|
if (!int.TryParse(macroId, NumberStyles.Integer, CultureInfo.InvariantCulture, out int outInt))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 09:24:56 +01:00
|
|
|
XElement? macroXml = GetMacroXml(outInt, out IMacro? macro);
|
|
|
|
|
if (macroXml is null)
|
2021-12-21 14:02:49 +01:00
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macros.Add(macroXml);
|
2022-02-24 09:24:56 +01:00
|
|
|
packagedMacros.Add(macro!);
|
2021-12-21 14:02:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root.Add(macros);
|
|
|
|
|
|
|
|
|
|
// Get the partial views for macros and package those (exclude views outside of the default directory, e.g. App_Plugins\*\Views)
|
|
|
|
|
IEnumerable<string> views = packagedMacros
|
|
|
|
|
.Where(x => x.MacroSource.StartsWith(Constants.SystemDirectories.MacroPartials))
|
|
|
|
|
.Select(x =>
|
|
|
|
|
x.MacroSource.Substring(Constants.SystemDirectories.MacroPartials.Length).Replace('/', '\\'));
|
2022-02-24 09:24:56 +01:00
|
|
|
PackageStaticFiles(views, root, "MacroPartialViews", "View", _fileSystems.MacroPartialsFileSystem!);
|
2021-12-21 14:02:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PackageStylesheets(PackageDefinition definition, XContainer root)
|
|
|
|
|
{
|
|
|
|
|
var stylesheetsXml = new XElement("Stylesheets");
|
|
|
|
|
foreach (var stylesheet in definition.Stylesheets)
|
|
|
|
|
{
|
|
|
|
|
if (stylesheet.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 09:24:56 +01:00
|
|
|
XElement? xml = GetStylesheetXml(stylesheet, true);
|
2021-12-21 14:02:49 +01:00
|
|
|
if (xml != null)
|
|
|
|
|
{
|
|
|
|
|
stylesheetsXml.Add(xml);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root.Add(stylesheetsXml);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PackageStaticFiles(
|
|
|
|
|
IEnumerable<string> filePaths,
|
|
|
|
|
XContainer root,
|
|
|
|
|
string containerName,
|
|
|
|
|
string elementName,
|
|
|
|
|
IFileSystem fileSystem)
|
|
|
|
|
{
|
|
|
|
|
var scriptsXml = new XElement(containerName);
|
|
|
|
|
foreach (var file in filePaths)
|
|
|
|
|
{
|
|
|
|
|
if (file.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!fileSystem.FileExists(file))
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("No file found with path " + file);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 09:24:56 +01:00
|
|
|
using Stream? stream = fileSystem.OpenFile(file);
|
|
|
|
|
if (stream is not null)
|
2021-12-21 14:02:49 +01:00
|
|
|
{
|
2022-02-24 09:24:56 +01:00
|
|
|
using (var reader = new StreamReader(stream))
|
|
|
|
|
{
|
|
|
|
|
var fileContents = reader.ReadToEnd();
|
|
|
|
|
scriptsXml.Add(
|
|
|
|
|
new XElement(
|
|
|
|
|
elementName,
|
|
|
|
|
new XAttribute("path", file),
|
|
|
|
|
new XCData(fileContents)));
|
|
|
|
|
}
|
2021-12-21 14:02:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root.Add(scriptsXml);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PackageTemplates(PackageDefinition definition, XContainer root)
|
|
|
|
|
{
|
|
|
|
|
var templatesXml = new XElement("Templates");
|
|
|
|
|
foreach (var templateId in definition.Templates)
|
|
|
|
|
{
|
|
|
|
|
if (!int.TryParse(templateId, NumberStyles.Integer, CultureInfo.InvariantCulture, out var outInt))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 09:24:56 +01:00
|
|
|
ITemplate? template = _fileService.GetTemplate(outInt);
|
2021-12-21 14:02:49 +01:00
|
|
|
if (template == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
templatesXml.Add(_serializer.Serialize(template));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root.Add(templatesXml);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PackageDocumentTypes(PackageDefinition definition, XContainer root)
|
|
|
|
|
{
|
|
|
|
|
var contentTypes = new HashSet<IContentType>();
|
|
|
|
|
var docTypesXml = new XElement("DocumentTypes");
|
|
|
|
|
foreach (var dtId in definition.DocumentTypes)
|
|
|
|
|
{
|
|
|
|
|
if (!int.TryParse(dtId, NumberStyles.Integer, CultureInfo.InvariantCulture, out var outInt))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 09:24:56 +01:00
|
|
|
IContentType? contentType = _contentTypeService.Get(outInt);
|
2021-12-21 14:02:49 +01:00
|
|
|
if (contentType == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AddDocumentType(contentType, contentTypes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (IContentType contentType in contentTypes)
|
|
|
|
|
{
|
|
|
|
|
docTypesXml.Add(_serializer.Serialize(contentType));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root.Add(docTypesXml);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PackageMediaTypes(PackageDefinition definition, XContainer root)
|
|
|
|
|
{
|
|
|
|
|
var mediaTypes = new HashSet<IMediaType>();
|
|
|
|
|
var mediaTypesXml = new XElement("MediaTypes");
|
|
|
|
|
foreach (var mediaTypeId in definition.MediaTypes)
|
|
|
|
|
{
|
|
|
|
|
if (!int.TryParse(mediaTypeId, NumberStyles.Integer, CultureInfo.InvariantCulture, out var outInt))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 09:24:56 +01:00
|
|
|
IMediaType? mediaType = _mediaTypeService.Get(outInt);
|
2021-12-21 14:02:49 +01:00
|
|
|
if (mediaType == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AddMediaType(mediaType, mediaTypes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (IMediaType mediaType in mediaTypes)
|
|
|
|
|
{
|
|
|
|
|
mediaTypesXml.Add(_serializer.Serialize(mediaType));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root.Add(mediaTypesXml);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PackageDocumentsAndTags(PackageDefinition definition, XContainer root)
|
|
|
|
|
{
|
|
|
|
|
// Documents and tags
|
|
|
|
|
if (string.IsNullOrEmpty(definition.ContentNodeId) == false && int.TryParse(definition.ContentNodeId,
|
|
|
|
|
NumberStyles.Integer, CultureInfo.InvariantCulture, out var contentNodeId))
|
|
|
|
|
{
|
|
|
|
|
if (contentNodeId > 0)
|
|
|
|
|
{
|
|
|
|
|
// load content from umbraco.
|
2022-02-24 09:24:56 +01:00
|
|
|
IContent? content = _contentService.GetById(contentNodeId);
|
2021-12-21 14:02:49 +01:00
|
|
|
if (content != null)
|
|
|
|
|
{
|
|
|
|
|
var contentXml = definition.ContentLoadChildNodes
|
|
|
|
|
? content.ToDeepXml(_serializer)
|
|
|
|
|
: content.ToXml(_serializer);
|
|
|
|
|
|
|
|
|
|
// Create the Documents/DocumentSet node
|
|
|
|
|
|
|
|
|
|
root.Add(
|
|
|
|
|
new XElement(
|
|
|
|
|
"Documents",
|
|
|
|
|
new XElement(
|
|
|
|
|
"DocumentSet",
|
|
|
|
|
new XAttribute("importMode", "root"),
|
|
|
|
|
contentXml)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Dictionary<string, Stream> PackageMedia(PackageDefinition definition, XElement root)
|
|
|
|
|
{
|
|
|
|
|
var mediaStreams = new Dictionary<string, Stream>();
|
|
|
|
|
|
|
|
|
|
// callback that occurs on each serialized media item
|
|
|
|
|
void OnSerializedMedia(IMedia media, XElement xmlMedia)
|
|
|
|
|
{
|
|
|
|
|
// get the media file path and store that separately in the XML.
|
|
|
|
|
// the media file path is different from the URL and is specifically
|
|
|
|
|
// extracted using the property editor for this media file and the current media file system.
|
2022-02-24 09:24:56 +01:00
|
|
|
Stream? mediaStream = _mediaFileManager.GetFile(media, out var mediaFilePath);
|
2021-12-21 14:02:49 +01:00
|
|
|
if (mediaStream != null)
|
|
|
|
|
{
|
2022-02-24 09:24:56 +01:00
|
|
|
xmlMedia.Add(new XAttribute("mediaFilePath", mediaFilePath!));
|
2021-12-21 14:02:49 +01:00
|
|
|
|
|
|
|
|
// add the stream to our outgoing stream
|
2022-02-24 09:24:56 +01:00
|
|
|
mediaStreams.Add(mediaFilePath!, mediaStream);
|
2021-12-21 14:02:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEnumerable<IMedia> medias = _mediaService.GetByIds(definition.MediaUdis);
|
|
|
|
|
|
|
|
|
|
var mediaXml = new XElement(
|
|
|
|
|
"MediaItems",
|
|
|
|
|
medias.Select(media =>
|
|
|
|
|
{
|
|
|
|
|
XElement serializedMedia = _serializer.Serialize(
|
|
|
|
|
media,
|
|
|
|
|
definition.MediaLoadChildNodes,
|
|
|
|
|
OnSerializedMedia);
|
|
|
|
|
|
|
|
|
|
return new XElement("MediaSet", serializedMedia);
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
root.Add(mediaXml);
|
|
|
|
|
|
|
|
|
|
return mediaStreams;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a macros xml node
|
|
|
|
|
/// </summary>
|
2022-02-24 09:24:56 +01:00
|
|
|
private XElement? GetMacroXml(int macroId, out IMacro? macro)
|
2021-12-21 14:02:49 +01:00
|
|
|
{
|
|
|
|
|
macro = _macroService.GetById(macroId);
|
|
|
|
|
if (macro == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
XElement xml = _serializer.Serialize(macro);
|
|
|
|
|
return xml;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts a umbraco stylesheet to a package xml node
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The path of the stylesheet.</param>
|
|
|
|
|
/// <param name="includeProperties">if set to <c>true</c> [include properties].</param>
|
2022-02-24 09:24:56 +01:00
|
|
|
private XElement? GetStylesheetXml(string path, bool includeProperties)
|
2021-12-21 14:02:49 +01:00
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(path))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Value cannot be null or whitespace.", nameof(path));
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 09:24:56 +01:00
|
|
|
IStylesheet? stylesheet = _fileService.GetStylesheet(path);
|
2021-12-21 14:02:49 +01:00
|
|
|
if (stylesheet == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _serializer.Serialize(stylesheet, includeProperties);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AddDocumentType(IContentType dt, HashSet<IContentType> dtl)
|
|
|
|
|
{
|
|
|
|
|
if (dt.ParentId > 0)
|
|
|
|
|
{
|
2022-02-24 09:24:56 +01:00
|
|
|
IContentType? parent = _contentTypeService.Get(dt.ParentId);
|
2021-12-21 14:02:49 +01:00
|
|
|
if (parent != null)
|
|
|
|
|
{
|
|
|
|
|
AddDocumentType(parent, dtl);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!dtl.Contains(dt))
|
|
|
|
|
{
|
|
|
|
|
dtl.Add(dt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AddMediaType(IMediaType mediaType, HashSet<IMediaType> mediaTypes)
|
|
|
|
|
{
|
|
|
|
|
if (mediaType.ParentId > 0)
|
|
|
|
|
{
|
2022-02-24 09:24:56 +01:00
|
|
|
IMediaType? parent = _mediaTypeService.Get(mediaType.ParentId);
|
2021-12-21 14:02:49 +01:00
|
|
|
if (parent != null)
|
|
|
|
|
{
|
|
|
|
|
AddMediaType(parent, mediaTypes);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!mediaTypes.Contains(mediaType))
|
|
|
|
|
{
|
|
|
|
|
mediaTypes.Add(mediaType);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static XElement GetPackageInfoXml(PackageDefinition definition)
|
|
|
|
|
{
|
|
|
|
|
var info = new XElement("info");
|
|
|
|
|
|
|
|
|
|
// Package info
|
|
|
|
|
var package = new XElement("package");
|
|
|
|
|
package.Add(new XElement("name", definition.Name));
|
|
|
|
|
info.Add(package);
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|