From 635d00224440a1d6f8aae68fc478ad73e7cfc0d6 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Fri, 27 Mar 2026 09:29:13 +0100 Subject: [PATCH 1/2] Ensure media directory exists before creating PhysicalFileProvider. --- src/Umbraco.Core/IO/PhysicalFileSystem.cs | 6 ++++- .../IO/PhysicalFileSystemTests.cs | 22 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Umbraco.Core/IO/PhysicalFileSystem.cs b/src/Umbraco.Core/IO/PhysicalFileSystem.cs index f829e630bc29..26d087b822b5 100644 --- a/src/Umbraco.Core/IO/PhysicalFileSystem.cs +++ b/src/Umbraco.Core/IO/PhysicalFileSystem.cs @@ -578,7 +578,11 @@ protected void WithRetry(Action action) /// Creates a file provider. /// /// The file provider. - public IFileProvider Create() => new PhysicalFileProvider(_rootPath); + public IFileProvider Create() + { + Directory.CreateDirectory(_rootPath); + return new PhysicalFileProvider(_rootPath); + } #endregion } diff --git a/tests/Umbraco.Tests.UnitTests/Umbraco.Core/IO/PhysicalFileSystemTests.cs b/tests/Umbraco.Tests.UnitTests/Umbraco.Core/IO/PhysicalFileSystemTests.cs index 7893c8a6f629..60729e4ffa85 100644 --- a/tests/Umbraco.Tests.UnitTests/Umbraco.Core/IO/PhysicalFileSystemTests.cs +++ b/tests/Umbraco.Tests.UnitTests/Umbraco.Core/IO/PhysicalFileSystemTests.cs @@ -2,6 +2,7 @@ // See LICENSE for more details. using System.Text; +using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Logging; using Moq; using NUnit.Framework; @@ -103,4 +104,25 @@ public void GetFullPathTest() // that path is invalid as it would be outside the root directory Assert.Throws(() => _fileSystem.GetFullPath("../../foo.tmp")); } + + [Test] + public void CreateFileProviderCreatesDirectoryIfMissing() + { + var nonExistentPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "FileSysTests", "NonExistent"); + if (Directory.Exists(nonExistentPath)) + { + Directory.Delete(nonExistentPath, true); + } + + var fs = new PhysicalFileSystem( + TestHelper.IOHelper, + TestHelper.GetHostingEnvironment(), + Mock.Of>(), + nonExistentPath, + "/Media/"); + + IFileProvider fileProvider = ((IFileProviderFactory)fs).Create(); + Assert.IsNotNull(fileProvider); + Assert.IsTrue(Directory.Exists(nonExistentPath)); + } } From 1c805bfbaaa84664b4085e0c660d31a748e12615 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Fri, 27 Mar 2026 09:40:58 +0100 Subject: [PATCH 2/2] Ensure file provider is disposed in test. --- .../Umbraco.Core/IO/PhysicalFileSystemTests.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/Umbraco.Tests.UnitTests/Umbraco.Core/IO/PhysicalFileSystemTests.cs b/tests/Umbraco.Tests.UnitTests/Umbraco.Core/IO/PhysicalFileSystemTests.cs index 60729e4ffa85..2476f69c0429 100644 --- a/tests/Umbraco.Tests.UnitTests/Umbraco.Core/IO/PhysicalFileSystemTests.cs +++ b/tests/Umbraco.Tests.UnitTests/Umbraco.Core/IO/PhysicalFileSystemTests.cs @@ -122,7 +122,14 @@ public void CreateFileProviderCreatesDirectoryIfMissing() "/Media/"); IFileProvider fileProvider = ((IFileProviderFactory)fs).Create(); - Assert.IsNotNull(fileProvider); - Assert.IsTrue(Directory.Exists(nonExistentPath)); + try + { + Assert.IsNotNull(fileProvider); + Assert.IsTrue(Directory.Exists(nonExistentPath)); + } + finally + { + (fileProvider as IDisposable)?.Dispose(); + } } }