|
1 | 1 | using System.Diagnostics; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | +using System.Text.RegularExpressions; |
2 | 4 | using CMS.DataEngine; |
3 | 5 | using CMS.MediaLibrary; |
4 | 6 | using Microsoft.Extensions.Logging; |
| 7 | +using Migration.Tool.Common; |
5 | 8 |
|
6 | 9 | namespace Migration.Tool.KXP.Api; |
7 | 10 |
|
8 | 11 | public class KxpMediaFileFacade |
9 | 12 | { |
10 | 13 | private readonly ILogger<KxpMediaFileFacade> logger; |
| 14 | + private readonly ToolConfiguration toolConfiguration; |
11 | 15 |
|
12 | | - public KxpMediaFileFacade(ILogger<KxpMediaFileFacade> logger, KxpApiInitializer kxpApiInitializer) |
| 16 | + public KxpMediaFileFacade(ILogger<KxpMediaFileFacade> logger, KxpApiInitializer kxpApiInitializer, ToolConfiguration toolConfiguration) |
13 | 17 | { |
14 | 18 | this.logger = logger; |
| 19 | + this.toolConfiguration = toolConfiguration; |
15 | 20 | kxpApiInitializer.EnsureApiIsInitialized(); |
16 | 21 | } |
17 | 22 |
|
@@ -42,11 +47,72 @@ public void SetMediaFile(MediaFileInfo mfi, bool newInstance) |
42 | 47 | public MediaLibraryInfo GetMediaLibraryInfo(Guid mediaLibraryGuid) => MediaLibraryInfoProvider.ProviderObject.Get(mediaLibraryGuid); |
43 | 48 | #pragma warning restore CS0618 // Type or member is obsolete |
44 | 49 |
|
| 50 | + public static bool IsDirectoryNameAllowed(string name) |
| 51 | + { |
| 52 | + if (string.IsNullOrWhiteSpace(name)) |
| 53 | + { |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + if (!name.All(x => char.IsLetterOrDigit(x) || new char[] { '-', '_', System.IO.Path.DirectorySeparatorChar, System.IO.Path.AltDirectorySeparatorChar }.Contains(x))) |
| 58 | + { |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + // Reserved device names (Windows only) |
| 63 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 64 | + { |
| 65 | + string[] reserved = { |
| 66 | + "CON", "PRN", "AUX", "NUL", |
| 67 | + "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", |
| 68 | + "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" |
| 69 | + }; |
| 70 | + |
| 71 | + string upper = name.ToUpperInvariant(); |
| 72 | + if (reserved.Contains(upper) || reserved.Any(r => upper.StartsWith(r + ".", StringComparison.Ordinal))) |
| 73 | + { |
| 74 | + return false; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return true; |
| 79 | + } |
| 80 | + |
| 81 | + private readonly HashSet<string> fixedFolderPaths = []; |
| 82 | + |
45 | 83 | #pragma warning disable CS0618 // Type or member is obsolete |
46 | 84 | public void EnsureMediaFilePathExistsInLibrary(MediaFileInfo mfi, int libraryId) |
47 | 85 | #pragma warning restore CS0618 // Type or member is obsolete |
48 | 86 | { |
49 | 87 | string? librarySubDir = System.IO.Path.GetDirectoryName(mfi.FilePath); |
| 88 | + if (toolConfiguration.LegacyFlatAssetTree != true) |
| 89 | + { |
| 90 | + if (!string.IsNullOrEmpty(librarySubDir) && !IsDirectoryNameAllowed(librarySubDir)) |
| 91 | + { |
| 92 | + // Try an automatic correction - replacing stretches of whitespaces with underscore. |
| 93 | + // If that succeeds, inform user by a warning. Otherwise raise error and defer fixing the issue to user. |
| 94 | + string fixedLibrarySubdir = Regex.Replace(librarySubDir.Trim(), @"\s+", "-"); |
| 95 | + |
| 96 | + string bypassMessage = $"If you want to bypass this, use {nameof(toolConfiguration.LegacyPermissiveMediaLibrarySubfolders)} appsettings option. Please refer to README for potential sideeffects."; |
| 97 | + |
| 98 | + if (IsDirectoryNameAllowed(fixedLibrarySubdir)) |
| 99 | + { |
| 100 | + librarySubDir = fixedLibrarySubdir; |
| 101 | + mfi.FilePath = $"{librarySubDir}/{System.IO.Path.GetFileName(mfi.FilePath)}"; // Don't use Path.Combine, because it doesn't work correctly with subsequent internal CMS logic |
| 102 | + |
| 103 | + if (!fixedFolderPaths.Contains(librarySubDir)) |
| 104 | + { |
| 105 | + fixedFolderPaths.Add(librarySubDir); |
| 106 | + logger.LogWarning($"Media library subfolder '{{MediaLibrarySubfolder}}' was transformed to XbyK allowed format '{{FixedMediaLibrarySubfolder}}'. {bypassMessage}", librarySubDir, fixedLibrarySubdir); |
| 107 | + } |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + throw new InvalidOperationException($"Media library subfolder name {librarySubDir} is not in XbyK allowed format. It must contain only alphanumeric characters, underscores ('_'), hyphens ('-'), and cannot contain file names reserved by the operating system. {bypassMessage}"); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
50 | 116 | #pragma warning disable CS0618 // Type or member is obsolete |
51 | 117 | MediaLibraryInfoProvider.CreateMediaLibraryFolder(libraryId, $"{librarySubDir}"); |
52 | 118 | #pragma warning restore CS0618 // Type or member is obsolete |
|
0 commit comments