Skip to content

Commit

Permalink
fixes related to K12 migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
tkrch committed Sep 5, 2024
1 parent 1246223 commit 2c66120
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task<CommandResult> Handle(MigratePageTypesCommand request, Cancell
{
var (_, ksClass) = di;

if (ksClass.ClassInheritsFromClassID is { } classInheritsFromClassId && !primaryKeyMappingContext.HasMapping<CmsClass>(c => c.ClassId, classInheritsFromClassId))
if (ksClass.ClassInheritsFromClassID is { } classInheritsFromClassId && !primaryKeyMappingContext.HasMapping<ICmsClass>(c => c.ClassID, classInheritsFromClassId))
{
// defer migration to later stage
if (ksClasses.TryDeferItem(di))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ private async Task MigratePageUrlPaths(Guid webPageItemGuid, int webPageItemId,
WebPageUrlPathGUID = contentItemCommonDataInfo.ContentItemCommonDataVersionStatus == VersionStatus.Draft
? Guid.NewGuid()
: ksPath.PageUrlPathGUID,
WebPageUrlPath = ksPath.PageUrlPathUrlPath,
WebPageUrlPath = ksPath.PageUrlPathUrlPath.TrimStart('/'),
WebPageUrlPathHash = ksPath.PageUrlPathUrlPathHash,
WebPageUrlPathWebPageItemGuid = webPageItemGuid,
WebPageUrlPathWebsiteChannelGuid = webSiteChannelGuid,
Expand Down Expand Up @@ -420,7 +420,7 @@ private async Task MigratePageUrlPaths(Guid webPageItemGuid, int webPageItemId,
WebPageUrlPathGUID = contentItemCommonDataInfo.ContentItemCommonDataVersionStatus == VersionStatus.Draft
? GuidHelper.CreateWebPageUrlPathGuid($"{ksDocument!.DocumentGUID}|{documentCulture}|{ksTree.NodeAliasPath}|DRAFT|{ksTree.NodeID}")
: GuidHelper.CreateWebPageUrlPathGuid($"{ksDocument!.DocumentGUID}|{ksTree.NodeAliasPath}|{ksTree.NodeID}"),
WebPageUrlPath = ksTree.NodeAliasPath, //ksPath.PageUrlPathUrlPath,
WebPageUrlPath = ksTree.NodeAliasPath.TrimStart('/'), //ksPath.PageUrlPathUrlPath,
// WebPageUrlPathHash = ksPath.PageUrlPathUrlPathHash,
WebPageUrlPathWebPageItemGuid = webPageItemGuid,
WebPageUrlPathWebsiteChannelGuid = webSiteChannelGuid,
Expand Down Expand Up @@ -460,13 +460,13 @@ private async Task MigratePageUrlPaths(Guid webPageItemGuid, int webPageItemId,
logger.LogTrace("Page url path common data info: CIID={ContentItemId} CLID={Language} ID={Id}", contentItemCommonDataInfo.ContentItemCommonDataContentItemID,
contentItemCommonDataInfo.ContentItemCommonDataContentLanguageID, contentItemCommonDataInfo.ContentItemCommonDataID);

string urlPath = (ksDocument switch
string urlPath = ((ksDocument switch
{
CmsDocumentK11 doc => isLinkedNode ? $"{languageInfo.ContentLanguageName}{ksTree.NodeAliasPath}" : doc.DocumentUrlPath,
CmsDocumentK12 doc => isLinkedNode ? $"{languageInfo.ContentLanguageName}{ksTree.NodeAliasPath}" : doc.DocumentUrlPath,
null => $"{languageInfo.ContentLanguageName}{ksTree.NodeAliasPath}",
_ => null
}).NullIf(string.Empty) ?? $"{ksTree.NodeAliasPath}";
}).NullIf(string.Empty) ?? $"{ksTree.NodeAliasPath}").TrimStart('/');

var webPageUrlPath = new WebPageUrlPathModel
{
Expand Down
20 changes: 10 additions & 10 deletions KVA/Migration.Toolkit.Source/Services/AssetFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public async Task<ContentItemSimplifiedModel> FromMediaFile(IMediaFile mediaFile
throw new InvalidOperationException($"Invalid media file path generated for {mediaFile} and {mediaLibrary} on {site}");
}

string mediaFilePath = Path.Combine(mediaLibraryAbsolutePath, mediaFile.FilePath);
string mediaFilePath = System.IO.Path.Combine(mediaLibraryAbsolutePath, mediaFile.FilePath);

int? createdByUserId = AdminUserHelper.MapTargetAdminUser(
mediaFile.FileCreatedByUserID,
Expand All @@ -113,7 +113,7 @@ public async Task<ContentItemSimplifiedModel> FromMediaFile(IMediaFile mediaFile
{
ContentItemGuid = translatedMediaGuid,
Identifier = GuidHelper.CreateAssetGuid(translatedMediaGuid, contentLanguageName),
Name = Path.Combine(mediaFile.FileName, mediaFile.FileExtension),
Name = System.IO.Path.Combine(mediaFile.FileName, mediaFile.FileExtension),
Extension = mediaFile.FileExtension,
Size = null,
LastModified = null,
Expand All @@ -122,7 +122,7 @@ public async Task<ContentItemSimplifiedModel> FromMediaFile(IMediaFile mediaFile
}
}));

string mediaFolder = Path.Combine(mediaLibrary.LibraryFolder, Path.GetDirectoryName(mediaFile.FilePath)!);
string mediaFolder = System.IO.Path.Combine(mediaLibrary.LibraryFolder, System.IO.Path.GetDirectoryName(mediaFile.FilePath)!);

var folder = GetAssetFolder(site);

Expand Down Expand Up @@ -163,7 +163,7 @@ public async Task<ContentItemSimplifiedModel> FromAttachment(ICmsAttachment atta
{
ContentItemGuid = translatedAttachmentGuid,
Identifier = GuidHelper.CreateAssetGuid(translatedAttachmentGuid, contentLanguageName),
Name = Path.Combine(attachment.AttachmentName, attachment.AttachmentExtension),
Name = System.IO.Path.Combine(attachment.AttachmentName, attachment.AttachmentExtension),
Extension = attachment.AttachmentExtension,
Size = null,
LastModified = attachment.AttachmentLastModified,
Expand Down Expand Up @@ -236,7 +236,7 @@ public async Task<ContentItemSimplifiedModel> FromAttachment(ICmsAttachment atta
contentFolderModels[rootKey] = rootFolder;
}

string[] pathSplit = folderPath.Split(Path.DirectorySeparatorChar);
string[] pathSplit = folderPath.Split(System.IO.Path.DirectorySeparatorChar);
ContentFolderModel? lastFolder = null;
for (int i = 0; i < pathSplit.Length; i++)
{
Expand Down Expand Up @@ -424,26 +424,26 @@ internal static ContentFolderModel GetAssetFolder(ICmsSite site)
string? cmsMediaLibrariesFolder = KenticoHelper.GetSettingsKey(modelFacade, ksSite.SiteID, "CMSMediaLibrariesFolder");
if (cmsMediaLibrariesFolder != null)
{
if (Path.IsPathRooted(cmsMediaLibrariesFolder))
if (System.IO.Path.IsPathRooted(cmsMediaLibrariesFolder))
{
sourceMediaLibraryPath = Path.Combine(cmsMediaLibrariesFolder, ksSite.SiteName, ksMediaLibrary.LibraryFolder);
sourceMediaLibraryPath = System.IO.Path.Combine(cmsMediaLibrariesFolder, ksSite.SiteName, ksMediaLibrary.LibraryFolder);
}
else
{
if (cmsMediaLibrariesFolder.StartsWith("~/"))
{
string cleared = $"{cmsMediaLibrariesFolder[2..]}".Replace("/", "\\");
sourceMediaLibraryPath = Path.Combine(toolkitConfiguration.KxCmsDirPath, cleared, ksSite.SiteName, ksMediaLibrary.LibraryFolder);
sourceMediaLibraryPath = System.IO.Path.Combine(toolkitConfiguration.KxCmsDirPath, cleared, ksSite.SiteName, ksMediaLibrary.LibraryFolder);
}
else
{
sourceMediaLibraryPath = Path.Combine(toolkitConfiguration.KxCmsDirPath, cmsMediaLibrariesFolder, ksSite.SiteName, ksMediaLibrary.LibraryFolder);
sourceMediaLibraryPath = System.IO.Path.Combine(toolkitConfiguration.KxCmsDirPath, cmsMediaLibrariesFolder, ksSite.SiteName, ksMediaLibrary.LibraryFolder);
}
}
}
else
{
sourceMediaLibraryPath = Path.Combine(toolkitConfiguration.KxCmsDirPath, ksSite.SiteName, DirMedia, ksMediaLibrary.LibraryFolder);
sourceMediaLibraryPath = System.IO.Path.Combine(toolkitConfiguration.KxCmsDirPath, ksSite.SiteName, DirMedia, ksMediaLibrary.LibraryFolder);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@

using System.Collections.Concurrent;
using System.Diagnostics;
using System.Text.RegularExpressions;
using CMS.ContentEngine.Internal;
using CMS.Core;
using CMS.Helpers;
using Kentico.Xperience.UMT.Services;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;
Expand All @@ -23,15 +19,6 @@ public class AttachmentMigratorToContentItem(
IImporter importer
) : IAttachmentMigrator
{
private static readonly Regex sanitizationRegex =
RegexHelper.GetRegex("[^-_a-z0-9]", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant);

private static readonly Regex libraryPathValidationRegex =
RegexHelper.GetRegex("^[-_a-z0-9]+$", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.CultureInvariant);


private readonly ConcurrentDictionary<(string libraryName, int siteId), int> mediaLibraryIdCache = new();

public async Task<IMigrateAttachmentResult> TryMigrateAttachmentByPath(string documentPath, string additionalPath)
{
if (string.IsNullOrWhiteSpace(documentPath))
Expand Down Expand Up @@ -108,7 +95,7 @@ public async Task<IMigrateAttachmentResult> MigrateAttachment(Guid ksAttachmentG
}
}

private bool assetFacadeInitialized = false;
private bool assetFacadeInitialized;
public async Task<IMigrateAttachmentResult> MigrateAttachment(ICmsAttachment ksAttachment, string? additionalMediaPath = null)
{
if (!assetFacadeInitialized)
Expand Down Expand Up @@ -151,7 +138,7 @@ public async Task<IMigrateAttachmentResult> MigrateAttachment(ICmsAttachment ksA
var contentLanguageRetriever = Service.Resolve<IContentLanguageRetriever>();
var defaultContentLanguage = await contentLanguageRetriever.GetDefaultContentLanguage();

var asset = await assetFacade.FromAttachment(ksAttachment, site, ksNode, [defaultContentLanguage.ContentLanguageName]);
var asset = await assetFacade.FromAttachment(ksAttachment, site, ksNode, [ksAttachmentDocument?.DocumentCulture ?? defaultContentLanguage.ContentLanguageName]);
switch (await importer.ImportAsync(asset))
{
case { Success: true }:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq.Expressions;

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

Expand Down

0 comments on commit 2c66120

Please sign in to comment.