Serve Media and App_Plugins using WebRootFileProvider (and allow changing the physical media path) (#11783)

* Allow changing UmbracoMediaPath to an absolute path. Also ensure Imagesharp are handing requests outside of the wwwroot folder.

* Let UmbracoMediaUrl fallback to UmbracoMediaPath when empty

* Add FileSystemFileProvider to expose an IFileSystem as IFileProvider

* Replace IUmbracoMediaFileProvider with IFileProviderFactory implementation

* Fix issue resolving relative paths when media URL has changed

* Remove FileSystemFileProvider and require explicitly implementing IFileProviderFactory

* Update tests (UnauthorizedAccessException isn't thrown anymore for rooted files)

* Update test to use UmbracoMediaUrl

* Add UmbracoMediaPhysicalRootPath global setting

* Remove MediaFileManagerImageProvider and use composited file providers

* Move CreateFileProvider to IFileSystem extension method

* Add rooted path tests

Co-authored-by: Ronald Barendse <ronald@barend.se>
This commit is contained in:
Bjarke Berg
2022-01-06 13:35:24 +01:00
committed by GitHub
parent 84fea8f953
commit 642c216f94
24 changed files with 233 additions and 96 deletions

View File

@@ -3,14 +3,17 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Extensions;
namespace Umbraco.Cms.Core.IO
{
public interface IPhysicalFileSystem : IFileSystem {}
public class PhysicalFileSystem : IPhysicalFileSystem
public interface IPhysicalFileSystem : IFileSystem
{ }
public class PhysicalFileSystem : IPhysicalFileSystem, IFileProviderFactory
{
private readonly IIOHelper _ioHelper;
private readonly ILogger<PhysicalFileSystem> _logger;
@@ -28,7 +31,7 @@ namespace Umbraco.Cms.Core.IO
// eg "" or "/Views" or "/Media" or "/<vpath>/Media" in case of a virtual path
private readonly string _rootUrl;
public PhysicalFileSystem(IIOHelper ioHelper,IHostingEnvironment hostingEnvironment, ILogger<PhysicalFileSystem> logger, string rootPath, string rootUrl)
public PhysicalFileSystem(IIOHelper ioHelper, IHostingEnvironment hostingEnvironment, ILogger<PhysicalFileSystem> logger, string rootPath, string rootUrl)
{
_ioHelper = ioHelper ?? throw new ArgumentNullException(nameof(ioHelper));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
@@ -270,7 +273,7 @@ namespace Umbraco.Cms.Core.IO
return path.Substring(_rootUrl.Length).TrimStart(Constants.CharArrays.ForwardSlash);
// unchanged - what else?
return path;
return path.TrimStart(Constants.CharArrays.ForwardSlash);
}
/// <summary>
@@ -285,7 +288,7 @@ namespace Umbraco.Cms.Core.IO
public string GetFullPath(string path)
{
// normalize
var opath = path;
var originalPath = path;
path = EnsureDirectorySeparatorChar(path);
// FIXME: this part should go!
@@ -318,7 +321,7 @@ namespace Umbraco.Cms.Core.IO
// nothing prevents us to reach the file, security-wise, yet it is outside
// this filesystem's root - throw
throw new UnauthorizedAccessException($"File original: [{opath}] full: [{path}] is outside this filesystem's root.");
throw new UnauthorizedAccessException($"File original: [{originalPath}] full: [{path}] is outside this filesystem's root.");
}
/// <summary>
@@ -450,6 +453,9 @@ namespace Umbraco.Cms.Core.IO
}
}
/// <inheritdoc />
public IFileProvider Create() => new PhysicalFileProvider(_rootPath);
#endregion
}
}