Skip to content

Commit

Permalink
dotnet format
Browse files Browse the repository at this point in the history
  • Loading branch information
tkrch committed Oct 1, 2024
1 parent 9d23bc5 commit 96cf9ab
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 54 deletions.
24 changes: 12 additions & 12 deletions KVA/Migration.Toolkit.Source/Helpers/MediaHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static class MediaHelper
{
throw new InvalidOperationException($"Cannot determine media file for link match {matchResult}");
}

var mediaLibraries = modelFacade.SelectWhere<IMediaLibrary>(
"LibraryUseDirectPathForContent = 1 AND LibraryFolder = @libraryFolder AND LibrarySiteID = @librarySiteID",
new SqlParameter("libraryFolder", libraryDir),
Expand All @@ -37,19 +37,19 @@ public static class MediaHelper
case [var mediaLibrary]:
{
string filePath = path.Replace($"/{mediaLibrary.LibraryFolder}/", "", StringComparison.InvariantCultureIgnoreCase);
return modelFacade.SelectWhere<IMediaFile>("FileLibraryID = @fileLibraryID AND FilePath = @filePath AND FileSiteID = @fileSiteID",
new SqlParameter("fileLibraryID", mediaLibrary.LibraryID),
return modelFacade.SelectWhere<IMediaFile>("FileLibraryID = @fileLibraryID AND FilePath = @filePath AND FileSiteID = @fileSiteID",
new SqlParameter("fileLibraryID", mediaLibrary.LibraryID),
new SqlParameter("filePath", filePath),
new SqlParameter("fileSiteID", linkSiteId))
.ToList() switch
{
[var mediaFile] => mediaFile,
{ Count: > 1 } => throw new InvalidOperationException($"Multiple media file were found for path {path}, site {linkSiteId} and library {libraryDir}"),
{ Count: 0 } =>
// this may happen and is valid scenaria
null,
_ => null
};
{
[var mediaFile] => mediaFile,
{ Count: > 1 } => throw new InvalidOperationException($"Multiple media file were found for path {path}, site {linkSiteId} and library {libraryDir}"),
{ Count: 0 } =>
// this may happen and is valid scenaria
null,
_ => null
};
}
case { Count: > 1 }:
{
Expand All @@ -70,7 +70,7 @@ public static class MediaHelper
}
}
}

public static ICmsAttachment? GetAttachment(MatchMediaLinkResult matchResult, ModelFacade modelFacade) =>
modelFacade.SelectWhere<ICmsAttachment>("AttachmentSiteID = @attachmentSiteID AND AttachmentGUID = @attachmentGUID",
new SqlParameter("attachmentSiteID", matchResult.LinkSiteId),
Expand Down
1 change: 0 additions & 1 deletion KVA/Migration.Toolkit.Source/KsCoreDiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

using Migration.Toolkit.Common;
using Migration.Toolkit.Common.Abstractions;
using Migration.Toolkit.Common.Helpers;
using Migration.Toolkit.Common.MigrationProtocol;
using Migration.Toolkit.Common.Services;
using Migration.Toolkit.Common.Services.BulkCopy;
Expand Down
11 changes: 7 additions & 4 deletions KVA/Migration.Toolkit.Source/Mappers/ContentItemMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ ICmsSite site
{
switch (result)
{
case { LinkKind: MediaLinkKind.Guid or MediaLinkKind.DirectMediaPath, MediaKind: MediaKind.MediaFile}:
case { LinkKind: MediaLinkKind.Guid or MediaLinkKind.DirectMediaPath, MediaKind: MediaKind.MediaFile }:
{
var mediaFile = MediaHelper.GetMediaFile(result, modelFacade);
if (mediaFile is null)
Expand All @@ -666,17 +666,20 @@ ICmsSite site
{
return original;
}
await attachmentMigrator.MigrateAttachment(attachment);
string? culture = null;
if (attachment.AttachmentDocumentID is {} attachmentDocumentId)
if (attachment.AttachmentDocumentID is { } attachmentDocumentId)
{
culture = modelFacade.SelectById<ICmsDocument>(attachmentDocumentId)?.DocumentCulture;
}
return assetFacade.GetAssetUri(attachment, culture);
}
default:
break;
}
return original;
Expand Down
10 changes: 5 additions & 5 deletions KVA/Migration.Toolkit.Source/Services/AssetFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ internal static ContentFolderModel GetAssetFolder(ICmsSite site)
{
string? cmsMediaLibrariesFolder = KenticoHelper.GetSettingsKey(modelFacade, ksSite.SiteID, "CMSMediaLibrariesFolder");
bool cmsUseMediaLibrariesSiteFolder = !"false".Equals(KenticoHelper.GetSettingsKey(modelFacade, ksSite.SiteID, "CMSUseMediaLibrariesSiteFolder"), StringComparison.InvariantCultureIgnoreCase);

string? sourceMediaLibraryPath = null;
if (!toolkitConfiguration.MigrateOnlyMediaFileInfo.GetValueOrDefault(true) &&
!string.IsNullOrWhiteSpace(toolkitConfiguration.KxCmsDirPath))
Expand All @@ -448,7 +448,7 @@ internal static ContentFolderModel GetAssetFolder(ICmsSite site)
if (Path.IsPathRooted(cmsMediaLibrariesFolder))
{
pathParts.Add(cmsMediaLibrariesFolder);
if(cmsUseMediaLibrariesSiteFolder)
if (cmsUseMediaLibrariesSiteFolder)
{
pathParts.Add(ksSite.SiteName);
}
Expand All @@ -462,7 +462,7 @@ internal static ContentFolderModel GetAssetFolder(ICmsSite site)
string cleared = $"{cmsMediaLibrariesFolder[2..]}".Replace("/", "\\");
pathParts.Add(toolkitConfiguration.KxCmsDirPath);
pathParts.Add(cleared);
if(cmsUseMediaLibrariesSiteFolder)
if (cmsUseMediaLibrariesSiteFolder)
{
pathParts.Add(ksSite.SiteName);
}
Expand All @@ -473,7 +473,7 @@ internal static ContentFolderModel GetAssetFolder(ICmsSite site)
{
pathParts.Add(toolkitConfiguration.KxCmsDirPath);
pathParts.Add(cmsMediaLibrariesFolder);
if(cmsUseMediaLibrariesSiteFolder)
if (cmsUseMediaLibrariesSiteFolder)
{
pathParts.Add(ksSite.SiteName);
}
Expand All @@ -485,7 +485,7 @@ internal static ContentFolderModel GetAssetFolder(ICmsSite site)
else
{
pathParts.Add(toolkitConfiguration.KxCmsDirPath);
if(cmsUseMediaLibrariesSiteFolder)
if (cmsUseMediaLibrariesSiteFolder)
{
pathParts.Add(ksSite.SiteName);
}
Expand Down
2 changes: 0 additions & 2 deletions KVA/Migration.Toolkit.Source/Services/MediaFileMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public class MediaFileMigrator(
IProtocol protocol
) : IMediaFileMigrator, IDisposable
{
private const string DirMedia = "media";

private KxpContext kxpContext = kxpContextFactory.CreateDbContext();

public void Dispose() => kxpContext.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ public MediaLinkService Create()
var cmsMediaLibrariesFolder = modelFacade
.SelectWhere<ICmsSettingsKey>("KeyName = N'CMSMediaLibrariesFolder'").Select(x => (x.SiteID, x.KeyValue))
.ToImmutableList();

var cmsUseMediaLibrariesSiteFolder = modelFacade
.SelectWhere<ICmsSettingsKey>("KeyName = N'CMSUseMediaLibrariesSiteFolder'").Select(x => (x.SiteID, x.KeyValue))
.ToImmutableList();

var mediaLibraries = modelFacade.SelectAll<IMediaLibrary>()
.GroupBy(x => x.LibrarySiteID)
.ToDictionary(x => x.Key, x => new HashSet<string>(x.Select(l => l.LibraryFolder)));

instance = new MediaLinkService(
sites,
cmsMediaLibrariesFolder,
Expand Down
8 changes: 4 additions & 4 deletions Migration.Toolkit.Common/Helpers/HtmlProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class HtmlProcessor
public HtmlProcessor(string html, MediaLinkService mediaLinkService)
{
this.mediaLinkService = mediaLinkService;

var doc = new HtmlDocument();
doc.LoadHtml(html);
this.html = html;
Expand All @@ -23,7 +23,7 @@ public IEnumerable<MatchMediaLinkResult> GetImages(int currentSiteId)
{
foreach (var imgNode in document.DocumentNode.SelectNodes("//img[@src]"))
{
if (imgNode?.Attributes["src"].Value is {} src)
if (imgNode?.Attributes["src"].Value is { } src)
{
yield return mediaLinkService.MatchMediaLink(src, currentSiteId);
}
Expand All @@ -35,7 +35,7 @@ public async Task<string> ProcessHtml(int currentSiteId, Func<MatchMediaLinkResu
bool anythingChanged = false;
foreach (var imgNode in document.DocumentNode.SelectNodes("//img[@src]") ?? Enumerable.Empty<HtmlNode>())
{
if (imgNode?.Attributes["src"].Value is {} src)
if (imgNode?.Attributes["src"].Value is { } src)
{
var matchedLink = mediaLinkService.MatchMediaLink(src, currentSiteId);

Expand All @@ -47,7 +47,7 @@ public async Task<string> ProcessHtml(int currentSiteId, Func<MatchMediaLinkResu
{ Success: true, MediaKind: MediaKind.Attachment, LinkKind: MediaLinkKind.DirectMediaPath } => throw new InvalidOperationException($"Invalid image link encountered: {matchedLink}"),
_ => imgNode.Attributes["src"].Value
};

anythingChanged = true;
}
}
Expand Down
32 changes: 16 additions & 16 deletions Migration.Toolkit.Common/Helpers/MediaLinkService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ public enum MediaLinkKind

public class MediaLinkService(
ImmutableList<(int siteId, string siteName, string siteLiveSiteUrl)> sites,
ImmutableList<(int? siteId, string? value)> cmsMediaLibrariesFolder,
ImmutableList<(int? siteId, string? value)> cmsMediaLibrariesFolder,
ImmutableList<(int? siteId, string? value)> cmsUseMediaLibrariesSiteFolder,
Dictionary<int, HashSet<string>> siteLibraryNames
)
{
{
public MatchMediaLinkResult MatchMediaLink(string? linkStr, int currentSiteId)
{
if (string.IsNullOrEmpty(linkStr))
{
return MatchMediaLinkResult.None;
}

string link = linkStr.TrimStart(['~']);

Guid? mediaId = null;
Expand Down Expand Up @@ -64,9 +64,9 @@ public MatchMediaLinkResult MatchMediaLink(string? linkStr, int currentSiteId)
path = link;
}
}

int inspectionIndex = 0;

// assuming that link is from current site
int? linkSiteId = currentSiteId;
if (uri != null)
Expand All @@ -89,7 +89,7 @@ public MatchMediaLinkResult MatchMediaLink(string? linkStr, int currentSiteId)

string[] spl = path.Split('/', StringSplitOptions.RemoveEmptyEntries);
var site = sites.FirstOrDefault(s => s.siteId == linkSiteId);

// match site sub path
if (site != default && uri is not null)
{
Expand All @@ -98,13 +98,13 @@ public MatchMediaLinkResult MatchMediaLink(string? linkStr, int currentSiteId)
if (uri.LocalPath.StartsWith(siteLiveSiteUri.LocalPath, StringComparison.InvariantCultureIgnoreCase))
{
inspectionIndex += subPathLength;
}
}
}

// custom lib dir + custom global media dir + subdir: /CustomMediaFolder/CDWCLN2/Subdir1/unnamed.jpg
// custom lib dir + custom global media dir: /CustomMediaFolder/CDWCLN2/84693449_B.png
// standard site dir: /MTExtensibilityTests/media/MediaWithDirectPath/84693449_B.png

string? globalMediaLibraryFolder = cmsMediaLibrariesFolder.OrderBy(x => x.siteId ?? -1).FirstOrDefault(x => x.siteId == linkSiteId || x.siteId == null).value;
if (!string.IsNullOrWhiteSpace(globalMediaLibraryFolder))
{
Expand All @@ -116,7 +116,7 @@ public MatchMediaLinkResult MatchMediaLink(string? linkStr, int currentSiteId)
inspectionIndex++;
}
}

bool siteMediaFolder = "True".Equals(cmsUseMediaLibrariesSiteFolder.OrderBy(x => x.siteId ?? -1).FirstOrDefault(x => x.siteId == linkSiteId || x.siteId == null).value, StringComparison.InvariantCultureIgnoreCase);
if (siteMediaFolder)
{
Expand All @@ -128,27 +128,27 @@ public MatchMediaLinkResult MatchMediaLink(string? linkStr, int currentSiteId)
mediaKind = MediaKind.MediaFile;
mediaLinkKind = MediaLinkKind.DirectMediaPath;
inspectionIndex++;
}
}
}
}

if ("media".Equals(spl[inspectionIndex], StringComparison.InvariantCultureIgnoreCase))
{
mediaKind = MediaKind.MediaFile;
mediaLinkKind = MediaLinkKind.DirectMediaPath;
inspectionIndex++;
}

if (mediaLinkKind is MediaLinkKind.DirectMediaPath)
{
// try match libreary name
if(linkSiteId is {} lsid && siteLibraryNames.TryGetValue(lsid, out var libraryNames) && libraryNames.Contains(spl[inspectionIndex]))
if (linkSiteId is { } lsid && siteLibraryNames.TryGetValue(lsid, out var libraryNames) && libraryNames.Contains(spl[inspectionIndex]))
{
return new MatchMediaLinkResult(true, mediaLinkKind, mediaKind, $"/{string.Join("/", spl[inspectionIndex..])}", null, linkSiteId, spl[inspectionIndex]);
return new MatchMediaLinkResult(true, mediaLinkKind, mediaKind, $"/{string.Join("/", spl[inspectionIndex..])}", null, linkSiteId, spl[inspectionIndex]);
}
else
{
return new MatchMediaLinkResult(true, mediaLinkKind, mediaKind, $"/{string.Join("/", spl[inspectionIndex..])}", null, linkSiteId, null);
return new MatchMediaLinkResult(true, mediaLinkKind, mediaKind, $"/{string.Join("/", spl[inspectionIndex..])}", null, linkSiteId, null);
}
}

Expand Down
6 changes: 3 additions & 3 deletions Migration.Toolkit.Tests/HtmlProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void TestHtmlFragment1()
{2, new (["MediaWithDirectPath"],StringComparer.InvariantCultureIgnoreCase)},
}
);

var processor = new HtmlProcessor(HtmlFragmentSample1, mediaLinkService);

var actual = processor.GetImages(2).ToArray();
Expand Down Expand Up @@ -75,15 +75,15 @@ public void TestHtmlFragment1()
Assert.Equal(MediaKind.MediaFile, r.MediaKind);
Assert.Null(r.LibraryDir);
Assert.Equal(new Guid("f393c1be-9b65-47bb-a246-f00f1e8b1ce0"), r.MediaGuid);
},
},
r =>
{
Assert.True(r.Success);
Assert.Equal(MediaLinkKind.Guid, r.LinkKind);
Assert.Equal(MediaKind.Attachment, r.MediaKind);
Assert.Null(r.LibraryDir);
Assert.Equal(new Guid("7c0fa1fd-f099-4516-8818-538bf8128244"), r.MediaGuid);
},
},
r =>
{
Assert.True(r.Success);
Expand Down
10 changes: 5 additions & 5 deletions Migration.Toolkit.Tests/MediaHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void ParseMediaLinkDirectPath()
Assert.Equal(1, a.LinkSiteId);
Assert.Null(a.LibraryDir);
}

// * Site 2: ~/getmedia/CCEAD0F0-E2BF-459B-814A-36699E5C773E/myfile.jpg
{
var a = mediaLinkService.MatchMediaLink("~/getmedia/CCEAD0F0-E2BF-459B-814A-36699E5C773E/myfile.jpg", 2);
Expand Down Expand Up @@ -177,7 +177,7 @@ public void ParseMediaLinkDirectPath()
Assert.Equal(2, a.LinkSiteId);
Assert.Equal("MediaLibraryS2", a.LibraryDir);
}

// * Site 3: ~/getmedia/CCEAD0F0-E2BF-459B-814A-36699E5C773E/myfile.jpg
{
var a = mediaLinkService.MatchMediaLink("~/getmedia/CCEAD0F0-E2BF-459B-814A-36699E5C773E/myfile.jpg", 3);
Expand Down Expand Up @@ -222,7 +222,7 @@ public void ParseMediaLinkDirectPath()
Assert.Equal(3, a.LinkSiteId);
Assert.Equal("MediaLibraryS3", a.LibraryDir);
}

// * Site 4: ~/getmedia/CCEAD0F0-E2BF-459B-814A-36699E5C773E/myfile.jpg
{
var a = mediaLinkService.MatchMediaLink("~/getmedia/CCEAD0F0-E2BF-459B-814A-36699E5C773E/myfile.jpg", 4);
Expand Down Expand Up @@ -267,7 +267,7 @@ public void ParseMediaLinkDirectPath()
Assert.Equal(4, a.LinkSiteId);
Assert.Equal("MediaLibraryS4", a.LibraryDir);
}

// refs from other site
// * Site 2: http://localhost:5004/Site4MediaFolder/Site4/media/MediaLibraryS4/myfile.jpg
{
Expand All @@ -280,7 +280,7 @@ public void ParseMediaLinkDirectPath()
Assert.Equal(4, a.LinkSiteId);
Assert.Equal("MediaLibraryS4", a.LibraryDir);
}

// {sitepath}/{SiteName}/media/{LibraryFolder}/{LibrarySubFolder}/{FileName}
{
var a = mediaLinkService.MatchMediaLink("http://localhost:5003/Site3/media/MediaLibraryS3/myfile.jpg", 3);
Expand Down

0 comments on commit 96cf9ab

Please sign in to comment.