Netcore: File systems rework (#10181)

* Allow IMediaFileSystem to be replace in the DI, or registered with inner filesystem

* Remove GetFileSystem from Filesystems

It was only used by tests.

* Make MediaFileSystem inherit from PhysicalFileSystem directly

* Remove FileSystemWrapper

* Remove inner filesystem from MediaFileSystem

* Add MediaFileManager and bare minimum to make it testable

* Remove MediaFileSystem

* Fix unit tests using MediaFileManager

* Remove IFileSystem and rely only on FileSystem

* Hide dangerous methods in FileSystems and do some cleaning

* Apply stylecop warnings to MediaFileManager

* Add FilesystemsCreator to Tests.Common

This allows you to create an instance if FileSystems with your own specified IFileSystem for testing purposes outside our own test suite.

* Allow the stylesheet filesystem to be replaced.

* Fix tests

* Don't save stylesheetWrapper in a temporary var

* refactor(FileSystems): change how stylesheet filesystem is registered

* fix(FileSystems): unable to overwrite media filesystem

SetMediaFileSystem added the MediaManager as a Singleton instead of
replacing the existing instance.

* fix(FileSystems): calling AddFileSystems replaces MediaManager

When calling AddFileSystems after SetMediaFileSystem the MediaManager
gets replaced by the default PhysicalFileSystem, so instead of calling
SetMediaFileSystem in AddFileSystems we now call TrySetMediaFileSystem
instead. This method will not replace any existing instance of the
MediaManager if there's already a MediaManager registered.

* Use SetMediaFileSystem instead of TrySet, and rename AddFilesystems to ConfigureFileSystems

Also don't call AddFileSystems again in ConfigureFilesystems

* Don't wrap CSS filesystem twice

* Add CreateShadowWrapperInternal to avoid casting

* Throw UnauthorizedAccessException isntead of InvalidOperationException

* Remove ResetShadowId

Co-authored-by: Rasmus John Pedersen <mail@rjp.dk>
This commit is contained in:
Mole
2021-04-27 09:52:17 +02:00
committed by GitHub
parent eaa46fe190
commit c9ebaadf23
63 changed files with 740 additions and 760 deletions

View File

@@ -37,7 +37,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
public class CodeFileController : BackOfficeNotificationsController
{
private readonly IHostingEnvironment _hostingEnvironment;
private readonly IFileSystems _fileSystems;
private readonly FileSystems _fileSystems;
private readonly IFileService _fileService;
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
@@ -48,7 +48,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
public CodeFileController(
IHostingEnvironment hostingEnvironment,
IFileSystems fileSystems,
FileSystems fileSystems,
IFileService fileService,
IBackOfficeSecurityAccessor backOfficeSecurityAccessor,
ILocalizedTextService localizedTextService,

View File

@@ -39,7 +39,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
[PluginController(Constants.Web.Mvc.BackOfficeApiArea)]
public class CurrentUserController : UmbracoAuthorizedJsonController
{
private readonly IMediaFileSystem _mediaFileSystem;
private readonly MediaFileManager _mediaFileManager;
private readonly ContentSettings _contentSettings;
private readonly IHostingEnvironment _hostingEnvironment;
private readonly IImageUrlGenerator _imageUrlGenerator;
@@ -54,7 +54,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
private readonly IPasswordChanger<BackOfficeIdentityUser> _passwordChanger;
public CurrentUserController(
IMediaFileSystem mediaFileSystem,
MediaFileManager mediaFileManager,
IOptions<ContentSettings> contentSettings,
IHostingEnvironment hostingEnvironment,
IImageUrlGenerator imageUrlGenerator,
@@ -68,7 +68,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
IShortStringHelper shortStringHelper,
IPasswordChanger<BackOfficeIdentityUser> passwordChanger)
{
_mediaFileSystem = mediaFileSystem;
_mediaFileManager = mediaFileManager;
_contentSettings = contentSettings.Value;
_hostingEnvironment = hostingEnvironment;
_imageUrlGenerator = imageUrlGenerator;
@@ -210,7 +210,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
public IActionResult PostSetAvatar(IList<IFormFile> file)
{
//borrow the logic from the user controller
return UsersController.PostSetAvatarInternal(file, _userService, _appCaches.RuntimeCache, _mediaFileSystem, _shortStringHelper, _contentSettings, _hostingEnvironment, _imageUrlGenerator, _backofficeSecurityAccessor.BackOfficeSecurity.GetUserId().ResultOr(0));
return UsersController.PostSetAvatarInternal(file, _userService, _appCaches.RuntimeCache, _mediaFileManager, _shortStringHelper, _contentSettings, _hostingEnvironment, _imageUrlGenerator, _backofficeSecurityAccessor.BackOfficeSecurity.GetUserId().ResultOr(0));
}
/// <summary>

View File

@@ -16,14 +16,14 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
[PluginController(Constants.Web.Mvc.BackOfficeApiArea)]
public class ImagesController : UmbracoAuthorizedApiController
{
private readonly IMediaFileSystem _mediaFileSystem;
private readonly MediaFileManager _mediaFileManager;
private readonly IImageUrlGenerator _imageUrlGenerator;
public ImagesController(
IMediaFileSystem mediaFileSystem,
MediaFileManager mediaFileManager,
IImageUrlGenerator imageUrlGenerator)
{
_mediaFileSystem = mediaFileSystem;
_mediaFileManager = mediaFileManager;
_imageUrlGenerator = imageUrlGenerator;
}
@@ -63,7 +63,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
DateTimeOffset? imageLastModified = null;
try
{
imageLastModified = _mediaFileSystem.GetLastModified(imagePath);
imageLastModified = _mediaFileManager.FileSystem.GetLastModified(imagePath);
}
catch (Exception)
{

View File

@@ -24,7 +24,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
[PluginController(Constants.Web.Mvc.BackOfficeApiArea)]
public class LogController : UmbracoAuthorizedJsonController
{
private readonly IMediaFileSystem _mediaFileSystem;
private readonly MediaFileManager _mediaFileManager;
private readonly IImageUrlGenerator _imageUrlGenerator;
private readonly IAuditService _auditService;
private readonly IUmbracoMapper _umbracoMapper;
@@ -34,7 +34,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
private readonly ISqlContext _sqlContext;
public LogController(
IMediaFileSystem mediaFileSystem,
MediaFileManager mediaFileSystem,
IImageUrlGenerator imageUrlGenerator,
IAuditService auditService,
IUmbracoMapper umbracoMapper,
@@ -43,7 +43,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
AppCaches appCaches,
ISqlContext sqlContext)
{
_mediaFileSystem = mediaFileSystem ?? throw new ArgumentNullException(nameof(mediaFileSystem));
_mediaFileManager = mediaFileSystem ?? throw new ArgumentNullException(nameof(mediaFileSystem));
_imageUrlGenerator = imageUrlGenerator ?? throw new ArgumentNullException(nameof(imageUrlGenerator));
_auditService = auditService ?? throw new ArgumentNullException(nameof(auditService));
_umbracoMapper = umbracoMapper ?? throw new ArgumentNullException(nameof(umbracoMapper));
@@ -111,7 +111,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
{
var mappedItems = items.ToList();
var userIds = mappedItems.Select(x => x.UserId).ToArray();
var userAvatars = Enumerable.ToDictionary(_userService.GetUsersById(userIds), x => x.Id, x => x.GetUserAvatarUrls(_appCaches.RuntimeCache, _mediaFileSystem, _imageUrlGenerator));
var userAvatars = Enumerable.ToDictionary(_userService.GetUsersById(userIds), x => x.Id, x => x.GetUserAvatarUrls(_appCaches.RuntimeCache, _mediaFileManager, _imageUrlGenerator));
var userNames = Enumerable.ToDictionary(_userService.GetUsersById(userIds), x => x.Id, x => x.Name);
foreach (var item in mappedItems)
{

View File

@@ -89,7 +89,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
IContentTypeBaseServiceProvider contentTypeBaseServiceProvider,
IRelationService relationService,
PropertyEditorCollection propertyEditors,
IMediaFileSystem mediaFileSystem,
MediaFileManager mediaFileManager,
IHostingEnvironment hostingEnvironment,
IImageUrlGenerator imageUrlGenerator,
IJsonSerializer serializer,
@@ -110,7 +110,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
_contentTypeBaseServiceProvider = contentTypeBaseServiceProvider;
_relationService = relationService;
_propertyEditors = propertyEditors;
_mediaFileSystem = mediaFileSystem;
_mediaFileManager = mediaFileManager;
_hostingEnvironment = hostingEnvironment;
_logger = loggerFactory.CreateLogger<MediaController>();
_imageUrlGenerator = imageUrlGenerator;
@@ -288,7 +288,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
private int[] _userStartNodes;
private readonly PropertyEditorCollection _propertyEditors;
private readonly IMediaFileSystem _mediaFileSystem;
private readonly MediaFileManager _mediaFileManager;
private readonly IHostingEnvironment _hostingEnvironment;
@@ -806,7 +806,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
await using (var stream = formFile.OpenReadStream())
{
f.SetValue(_mediaFileSystem,_shortStringHelper, _contentTypeBaseServiceProvider, _serializer, Constants.Conventions.Media.File,fileName, stream);
f.SetValue(_mediaFileManager,_shortStringHelper, _contentTypeBaseServiceProvider, _serializer, Constants.Conventions.Media.File,fileName, stream);
}

View File

@@ -52,7 +52,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
[IsCurrentUserModelFilter]
public class UsersController : UmbracoAuthorizedJsonController
{
private readonly IMediaFileSystem _mediaFileSystem;
private readonly MediaFileManager _mediaFileManager;
private readonly ContentSettings _contentSettings;
private readonly IHostingEnvironment _hostingEnvironment;
private readonly ISqlContext _sqlContext;
@@ -75,7 +75,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
private readonly ILogger<UsersController> _logger;
public UsersController(
IMediaFileSystem mediaFileSystem,
MediaFileManager mediaFileManager,
IOptions<ContentSettings> contentSettings,
IHostingEnvironment hostingEnvironment,
ISqlContext sqlContext,
@@ -96,7 +96,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
UserEditorAuthorizationHelper userEditorAuthorizationHelper,
IPasswordChanger<BackOfficeIdentityUser> passwordChanger)
{
_mediaFileSystem = mediaFileSystem;
_mediaFileManager = mediaFileManager;
_contentSettings = contentSettings.Value;
_hostingEnvironment = hostingEnvironment;
_sqlContext = sqlContext;
@@ -125,7 +125,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
/// <returns></returns>
public ActionResult<string[]> GetCurrentUserAvatarUrls()
{
var urls = _backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser.GetUserAvatarUrls(_appCaches.RuntimeCache, _mediaFileSystem, _imageUrlGenerator);
var urls = _backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser.GetUserAvatarUrls(_appCaches.RuntimeCache, _mediaFileManager, _imageUrlGenerator);
if (urls == null)
return new ValidationErrorResult("Could not access Gravatar endpoint");
@@ -136,10 +136,10 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
[Authorize(Policy = AuthorizationPolicies.AdminUserEditsRequireAdmin)]
public IActionResult PostSetAvatar(int id, IList<IFormFile> file)
{
return PostSetAvatarInternal(file, _userService, _appCaches.RuntimeCache, _mediaFileSystem, _shortStringHelper, _contentSettings, _hostingEnvironment, _imageUrlGenerator, id);
return PostSetAvatarInternal(file, _userService, _appCaches.RuntimeCache, _mediaFileManager, _shortStringHelper, _contentSettings, _hostingEnvironment, _imageUrlGenerator, id);
}
internal static IActionResult PostSetAvatarInternal(IList<IFormFile> files, IUserService userService, IAppCache cache, IMediaFileSystem mediaFileSystem, IShortStringHelper shortStringHelper, ContentSettings contentSettings, IHostingEnvironment hostingEnvironment, IImageUrlGenerator imageUrlGenerator, int id)
internal static IActionResult PostSetAvatarInternal(IList<IFormFile> files, IUserService userService, IAppCache cache, MediaFileManager mediaFileManager, IShortStringHelper shortStringHelper, ContentSettings contentSettings, IHostingEnvironment hostingEnvironment, IImageUrlGenerator imageUrlGenerator, int id)
{
if (files is null)
{
@@ -176,13 +176,13 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
using (var fs = file.OpenReadStream())
{
mediaFileSystem.AddFile(user.Avatar, fs, true);
mediaFileManager.FileSystem.AddFile(user.Avatar, fs, true);
}
userService.Save(user);
}
return new OkObjectResult(user.GetUserAvatarUrls(cache, mediaFileSystem, imageUrlGenerator));
return new OkObjectResult(user.GetUserAvatarUrls(cache, mediaFileManager, imageUrlGenerator));
}
[AppendUserModifiedHeader("id")]
@@ -212,11 +212,11 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
if (filePath.IsNullOrWhiteSpace() == false)
{
if (_mediaFileSystem.FileExists(filePath))
_mediaFileSystem.DeleteFile(filePath);
if (_mediaFileManager.FileSystem.FileExists(filePath))
_mediaFileManager.FileSystem.DeleteFile(filePath);
}
return found.GetUserAvatarUrls(_appCaches.RuntimeCache, _mediaFileSystem, _imageUrlGenerator);
return found.GetUserAvatarUrls(_appCaches.RuntimeCache, _mediaFileManager, _imageUrlGenerator);
}
/// <summary>