diff --git a/Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI/Extensions/MediaStorageManagementExtensions.cs b/Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI/Extensions/MediaStorageManagementExtensions.cs new file mode 100644 index 00000000..88ee7d8c --- /dev/null +++ b/Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI/Extensions/MediaStorageManagementExtensions.cs @@ -0,0 +1,37 @@ +using Lombiq.Tests.UI.Services; +using System; +using System.Linq; +using System.Threading.Tasks; + +namespace Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI.Extensions; + +public static class MediaStorageManagementExtensions +{ + public static void SetMediaStorageManagementOptionsForUITest( + this OrchardCoreUITestExecutorConfiguration configuration, + long maximumSpace) + { + configuration.OrchardCoreConfiguration.BeforeAppStart += + (_, argumentsBuilder) => + { + argumentsBuilder + .AddWithValue( + "OrchardCore:Lombiq_Hosting_Tenants_MediaStorageManagement:Media_Storage_Management_Options:MaximumSpace", + maximumSpace); + + return Task.CompletedTask; + }; + + configuration.AssertBrowserLog = + logEntries => + { + // By default, apart from some commonly known exceptions, the browser log should be empty. However, + // Media Storage Quota feature causes a 400 on upload if the file is over the limit, so we need to make + // sure not to fail on that. + var messagesWithoutToggle = logEntries.Where(logEntry => + !logEntry.Message.ContainsOrdinalIgnoreCase( + @"/Admin/Media/Upload - Failed to load resource: the server responded with a status of 400")); + OrchardCoreUITestExecutorConfiguration.AssertBrowserLogIsEmpty(messagesWithoutToggle); + }; + } +} diff --git a/Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI/Extensions/TestCaseUITestContextExtensions.cs b/Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI/Extensions/TestCaseUITestContextExtensions.cs new file mode 100644 index 00000000..66445c10 --- /dev/null +++ b/Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI/Extensions/TestCaseUITestContextExtensions.cs @@ -0,0 +1,25 @@ +using Lombiq.Tests.UI.Extensions; +using Lombiq.Tests.UI.Services; +using OpenQA.Selenium; +using Shouldly; +using System.Threading.Tasks; + +namespace Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI.Extensions; + +public static class TestCaseUITestContextExtensions +{ + public static async Task TestMediaStorageManagementBehaviorAsync(this UITestContext context) + { + await context.SignInDirectlyAsync(); + + await context.GoToAdminRelativeUrlAsync("/Media"); + + context.UploadSamplePngByIdOfAnyVisibility("fileupload"); // #spell-check-ignore-line + + // Workaround for pending uploads, until you make an action the page is stuck on "Uploads Pending". + context.WaitForPageLoad(); + await context.ClickReliablyOnAsync(By.CssSelector("body")); + await context.ClickReliablyOnAsync(By.CssSelector(".text-danger")); + context.Get(By.CssSelector(".error-message")).Text.ShouldContain("Error: You may only store"); + } +} diff --git a/Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI/License.md b/Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI/License.md new file mode 100644 index 00000000..4add1069 --- /dev/null +++ b/Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI/License.md @@ -0,0 +1,13 @@ +Copyright © 2023, [Lombiq Technologies Ltd.](https://lombiq.com) + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +- Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI/Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI.csproj b/Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI/Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI.csproj new file mode 100644 index 00000000..94b6ecd3 --- /dev/null +++ b/Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI/Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI.csproj @@ -0,0 +1,39 @@ + + + + net6.0 + $(DefaultItemExcludes);.git*;node_modules\** + + + + Lombiq Hosting - Tenants Media Storage Management - UI Test Extensions + Lombiq Technologies + Copyright © 2023, Lombiq Technologies Ltd. + Extension methods that test various features in Lombiq Hosting - Tenants Media Storage Management, with the help of Lombiq UI Testing Toolbox for Orchard Core. See the project website for detailed documentation. + NuGetIcon.png + OrchardCore;Lombiq;AspNetCore;Tenants;LombiqHostingSuite;Shouldly;xUnit;UITesting;Testing + https://github.com/Lombiq/Hosting-Tenants + https://github.com/Lombiq/Hosting-Tenants/blob/dev/Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI/Readme.md + License.md + + + + + + + + + + + + + + + + + + + + + + diff --git a/Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI/NuGetIcon.png b/Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI/NuGetIcon.png new file mode 100644 index 00000000..162a0050 Binary files /dev/null and b/Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI/NuGetIcon.png differ diff --git a/Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI/Readme.md b/Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI/Readme.md new file mode 100644 index 00000000..d585569b --- /dev/null +++ b/Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI/Readme.md @@ -0,0 +1,13 @@ +# Lombiq Hosting - Tenants Media Storage Management - UI Test Extensions + +## About + +Extension methods that test various features in Lombiq Hosting - Tenants Media Storage Management, with the help of [Lombiq UI Testing Toolbox for Orchard Core](https://github.com/Lombiq/UI-Testing-Toolbox). + +Call these from a UI test project to verify the module's basic features; as seen in [Open-Source Orchard Core Extensions](https://github.com/Lombiq/Open-Source-Orchard-Core-Extensions). + +## Contributing and support + +Bug reports, feature requests, comments, questions, code contributions and love letters are warmly welcome. You can send them to us via GitHub issues and pull requests. Please adhere to our [open-source guidelines](https://lombiq.com/open-source-guidelines) while doing so. + +This project is developed by [Lombiq Technologies](https://lombiq.com/). Commercial-grade support is available through Lombiq. diff --git a/Lombiq.Hosting.Tenants.MediaStorageManagement/Constants/FeatureNames.cs b/Lombiq.Hosting.Tenants.MediaStorageManagement/Constants/FeatureNames.cs new file mode 100644 index 00000000..1a4c0645 --- /dev/null +++ b/Lombiq.Hosting.Tenants.MediaStorageManagement/Constants/FeatureNames.cs @@ -0,0 +1,6 @@ +namespace Lombiq.Hosting.Tenants.MediaStorageManagement.Constants; + +public static class FeatureNames +{ + public const string MediaStorageManagement = "Lombiq.Hosting.Tenants.MediaStorageManagement"; +} diff --git a/Lombiq.Hosting.Tenants.MediaStorageManagement/Constants/MediaStorageManagementOptionsConstants.cs b/Lombiq.Hosting.Tenants.MediaStorageManagement/Constants/MediaStorageManagementOptionsConstants.cs new file mode 100644 index 00000000..f649fd6f --- /dev/null +++ b/Lombiq.Hosting.Tenants.MediaStorageManagement/Constants/MediaStorageManagementOptionsConstants.cs @@ -0,0 +1,9 @@ +namespace Lombiq.Hosting.Tenants.MediaStorageManagement.Constants; + +public static class MediaStorageManagementOptionsConstants +{ + /// + /// Default MaximumStorageQuota in bytes representing 1GB. + /// + public const long MaximumStorageQuota = 1_073_741_824; +} diff --git a/Lombiq.Hosting.Tenants.MediaStorageManagement/Filters/MediaStorageQuotaActionFilter.cs b/Lombiq.Hosting.Tenants.MediaStorageManagement/Filters/MediaStorageQuotaActionFilter.cs new file mode 100644 index 00000000..2c79e219 --- /dev/null +++ b/Lombiq.Hosting.Tenants.MediaStorageManagement/Filters/MediaStorageQuotaActionFilter.cs @@ -0,0 +1,34 @@ +using Lombiq.Hosting.Tenants.MediaStorageManagement.Service; +using Microsoft.AspNetCore.Http.Features; +using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.Extensions.DependencyInjection; +using System.Threading.Tasks; + +namespace Lombiq.Hosting.Tenants.MediaStorageManagement.Filters; + +public class MediaStorageQuotaActionFilter : IAsyncAuthorizationFilter, IOrderedFilter +{ + public int Order => 950; // Should be above the InternalMediaSizeFilter (900) to override its value. + + public async Task OnAuthorizationAsync(AuthorizationFilterContext context) + { + var maxFileSize = await context + .HttpContext + .RequestServices + .GetRequiredService() + .GetRemainingMediaSpaceQuotaLeftAsync(); + + var formOptions = new FormOptions + { + MultipartBodyLengthLimit = maxFileSize, + }; + + context.HttpContext.Features.Set(new FormFeature(context.HttpContext.Request, formOptions)); + + var maxRequestBodySizeFeature = context.HttpContext.Features.Get(); + if (maxRequestBodySizeFeature is { IsReadOnly: false }) + { + maxRequestBodySizeFeature.MaxRequestBodySize = maxFileSize; + } + } +} diff --git a/Lombiq.Hosting.Tenants.MediaStorageManagement/Filters/UploadFileSizeShapeFilter.cs b/Lombiq.Hosting.Tenants.MediaStorageManagement/Filters/UploadFileSizeShapeFilter.cs new file mode 100644 index 00000000..e4571ab9 --- /dev/null +++ b/Lombiq.Hosting.Tenants.MediaStorageManagement/Filters/UploadFileSizeShapeFilter.cs @@ -0,0 +1,56 @@ +using Lombiq.Hosting.Tenants.MediaStorageManagement.Service; +using Lombiq.Hosting.Tenants.MediaStorageManagement.ViewModels; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; +using OrchardCore.DisplayManagement; +using OrchardCore.DisplayManagement.Layout; +using OrchardCore.Media.Controllers; +using OrchardCore.Mvc.Core.Utilities; +using System.Threading.Tasks; + +namespace Lombiq.Hosting.Tenants.MediaStorageManagement.Filters; + +public class UploadFileSizeShapeFilter : IAsyncResultFilter +{ + private readonly IShapeFactory _shapeFactory; + private readonly ILayoutAccessor _layoutAccessor; + private readonly IMediaStorageQuotaService _mediaStorageQuotaService; + + public UploadFileSizeShapeFilter( + IShapeFactory shapeFactory, + ILayoutAccessor layoutAccessor, + IMediaStorageQuotaService mediaStorageQuotaService) + { + _shapeFactory = shapeFactory; + _layoutAccessor = layoutAccessor; + _mediaStorageQuotaService = mediaStorageQuotaService; + } + + public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) + { + if (!context.IsAdmin()) + { + await next(); + return; + } + + var actionRouteController = context.ActionDescriptor.RouteValues["Controller"]; + var actionRouteArea = context.ActionDescriptor.RouteValues["Area"]; + var actionRouteValue = context.ActionDescriptor.RouteValues["Action"]; + + if (actionRouteController == typeof(AdminController).ControllerName() && + actionRouteArea == $"{nameof(OrchardCore)}.{nameof(OrchardCore.Media)}" && + actionRouteValue is nameof(AdminController.Index) && + context.Result is ViewResult) + { + var layout = await _layoutAccessor.GetLayoutAsync(); + var contentZone = layout.Zones["Footer"]; + var maximumSpace = _mediaStorageQuotaService.MaxSpaceForTenantInMegabytes(); + await contentZone.AddAsync(await _shapeFactory.CreateAsync( + "UploadFileSize", + viewModel => viewModel.MaximumSpace = maximumSpace)); + } + + await next(); + } +} diff --git a/Lombiq.Hosting.Tenants.MediaStorageManagement/License.md b/Lombiq.Hosting.Tenants.MediaStorageManagement/License.md new file mode 100644 index 00000000..4add1069 --- /dev/null +++ b/Lombiq.Hosting.Tenants.MediaStorageManagement/License.md @@ -0,0 +1,13 @@ +Copyright © 2023, [Lombiq Technologies Ltd.](https://lombiq.com) + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +- Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Lombiq.Hosting.Tenants.MediaStorageManagement/Lombiq.Hosting.Tenants.MediaStorageManagement.csproj b/Lombiq.Hosting.Tenants.MediaStorageManagement/Lombiq.Hosting.Tenants.MediaStorageManagement.csproj new file mode 100644 index 00000000..e6d3a4b2 --- /dev/null +++ b/Lombiq.Hosting.Tenants.MediaStorageManagement/Lombiq.Hosting.Tenants.MediaStorageManagement.csproj @@ -0,0 +1,52 @@ + + + + net6.0 + true + $(DefaultItemExcludes);.git*;node_modules\**;Tests\** + + + + Lombiq Hosting - Tenants Media Storage Management for Orchard Core + Lombiq Technologies + Copyright © 2023, Lombiq Technologies Ltd. + Lombiq Hosting - Tenants Media Storage Management for Orchard Core: With the help of this module, you can set restrictions regarding maximum media space per tenant. See the project website for detailed documentation. + NuGetIcon.png + OrchardCore;Lombiq;AspNetCore;Multitenancy;SaaS + https://github.com/Lombiq/Hosting-Tenants + https://github.com/Lombiq/Hosting-Tenants/blob/dev/Lombiq.Hosting.Tenants.MediaStorageManagement/Readme.md + License.md + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Lombiq.Hosting.Tenants.MediaStorageManagement/Manifest.cs b/Lombiq.Hosting.Tenants.MediaStorageManagement/Manifest.cs new file mode 100644 index 00000000..15602175 --- /dev/null +++ b/Lombiq.Hosting.Tenants.MediaStorageManagement/Manifest.cs @@ -0,0 +1,24 @@ +using OrchardCore.Modules.Manifest; +using static Lombiq.Hosting.Tenants.MediaStorageManagement.Constants.FeatureNames; + +[assembly: Module( + Name = "Lombiq Hosting - Tenants Media Storage Management", + Author = "Lombiq Technologies", + Website = "https://github.com/Lombiq/Hosting-Tenants", + Version = "0.0.1", + Description = "Ability to configure storage quota for tenants." +)] + +[assembly: Feature( + Id = MediaStorageManagement, + Name = "Lombiq Hosting - Tenants Media Storage Management - Quota Management", + Description = "Ability to configure storage quota for tenants.", + Category = "Hosting", + IsAlwaysEnabled = true, + Dependencies = new[] + { + "OrchardCore.Media", + "OrchardCore.DisplayManagement", + "OrchardCore.ResourceManagement", + } +)] diff --git a/Lombiq.Hosting.Tenants.MediaStorageManagement/NuGetIcon.png b/Lombiq.Hosting.Tenants.MediaStorageManagement/NuGetIcon.png new file mode 100644 index 00000000..162a0050 Binary files /dev/null and b/Lombiq.Hosting.Tenants.MediaStorageManagement/NuGetIcon.png differ diff --git a/Lombiq.Hosting.Tenants.MediaStorageManagement/Readme.md b/Lombiq.Hosting.Tenants.MediaStorageManagement/Readme.md new file mode 100644 index 00000000..d10e1b0f --- /dev/null +++ b/Lombiq.Hosting.Tenants.MediaStorageManagement/Readme.md @@ -0,0 +1,55 @@ +# Lombiq Hosting - Tenants Media Storage Management for Orchard Core + +[![Lombiq.Hosting.Tenants.MediaStorageManagement NuGet](https://img.shields.io/nuget/v/Lombiq.Hosting.Tenants.MediaStorageManagement?label=Lombiq.Hosting.Tenants.MediaStorageManagement)](https://www.nuget.org/packages/Lombiq.Hosting.Tenants.MediaStorageManagement/) + +## About + +With the help of this module, you can set restrictions regarding maximum media space per tenant. + +Do you want to quickly try out this project and see it in action? Check it out in our [Open-Source Orchard Core Extensions](https://github.com/Lombiq/Open-Source-Orchard-Core-Extensions) full Orchard Core solution and also see our other useful Orchard Core-related open-source projects! + +## Documentation + +This module currently contains one feature: + +- `Lombiq.Hosting.Tenants.MediaStorageManagement` + +### `Lombiq.Hosting.Tenants.MediaStorageManagement` + +With this module, you can specify how much space would you like to limit each tenant's storage space. The default is 1GB. If you want to set it to 2GB e.g. you can do it in bytes as an environment variable or in `appsettings.json` as follows: + +```json +"OrchardCore": { + "Lombiq_Hosting_Tenants_MediaStorageManagement": { + "Media_Storage_Management_Options": { + "MaximumSpace": 2147483648 + } + } +} +``` + +Tenant based configuration can be defined as the following, for more details read the [Orchard Core documentation](https://docs.orchardcore.net/en/main/docs/reference/core/Configuration/#tenant-postconfiguration). + +```json +"OrchardCore": { + "TenantName": { + "Lombiq_Hosting_Tenants_MediaStorageManagement": { + "Media_Storage_Management_Options": { + "MaximumSpace": 2147483648 + } + } + } +} +``` + +## Dependencies + +This module has the following dependencies: + +- [Lombiq Helpful Libraries for Orchard Core](https://github.com/Lombiq/Helpful-Libraries) + +## Contributing and support + +Bug reports, feature requests, comments, questions, code contributions and love letters are warmly welcome. You can send them to us via GitHub issues and pull requests. Please adhere to our [open-source guidelines](https://lombiq.com/open-source-guidelines) while doing so. + +This project is developed by [Lombiq Technologies](https://lombiq.com/). Commercial-grade support is available through Lombiq. diff --git a/Lombiq.Hosting.Tenants.MediaStorageManagement/Service/IMediaStorageQuotaService.cs b/Lombiq.Hosting.Tenants.MediaStorageManagement/Service/IMediaStorageQuotaService.cs new file mode 100644 index 00000000..3ce70fa7 --- /dev/null +++ b/Lombiq.Hosting.Tenants.MediaStorageManagement/Service/IMediaStorageQuotaService.cs @@ -0,0 +1,24 @@ +using System.Threading.Tasks; + +namespace Lombiq.Hosting.Tenants.MediaStorageManagement.Service; + +/// +/// Storage quota related calculations. +/// +public interface IMediaStorageQuotaService +{ + /// + /// Returns remaining quota space left in bytes. + /// + Task GetRemainingMediaSpaceQuotaLeftAsync(); + + /// + /// Returns the maximum quota space in bytes. + /// + long MaxSpaceForTenantInBytes(); + + /// + /// Returns the maximum quota space in Megabytes. + /// + float MaxSpaceForTenantInMegabytes(); +} diff --git a/Lombiq.Hosting.Tenants.MediaStorageManagement/Service/MediaStorageQuotaActionFilterConvention.cs b/Lombiq.Hosting.Tenants.MediaStorageManagement/Service/MediaStorageQuotaActionFilterConvention.cs new file mode 100644 index 00000000..73a46555 --- /dev/null +++ b/Lombiq.Hosting.Tenants.MediaStorageManagement/Service/MediaStorageQuotaActionFilterConvention.cs @@ -0,0 +1,22 @@ +using Lombiq.Hosting.Tenants.MediaStorageManagement.Filters; +using Microsoft.AspNetCore.Mvc.ApplicationModels; +using OrchardCore.Media.Controllers; +using OrchardCore.Mvc.Core.Utilities; +using System.Linq; + +namespace Lombiq.Hosting.Tenants.MediaStorageManagement.Service; + +public class MediaStorageQuotaActionFilterConvention : IApplicationModelConvention +{ + public void Apply(ApplicationModel application) + { + var targetController = application.Controllers.FirstOrDefault(controller => + controller.ControllerName == typeof(AdminController).ControllerName() && + controller.Actions.Any(action => action.ActionName == nameof(AdminController.Upload))); + + var targetAction = targetController?.Actions.FirstOrDefault(action => + action.ActionName == nameof(AdminController.Upload)); + + targetAction?.Filters.Add(new MediaStorageQuotaActionFilter()); + } +} diff --git a/Lombiq.Hosting.Tenants.MediaStorageManagement/Service/MediaStorageQuotaService.cs b/Lombiq.Hosting.Tenants.MediaStorageManagement/Service/MediaStorageQuotaService.cs new file mode 100644 index 00000000..b519044a --- /dev/null +++ b/Lombiq.Hosting.Tenants.MediaStorageManagement/Service/MediaStorageQuotaService.cs @@ -0,0 +1,34 @@ +using Lombiq.Hosting.Tenants.MediaStorageManagement.Settings; +using Microsoft.Extensions.Options; +using OrchardCore.Media; +using System.Linq; +using System.Threading.Tasks; + +namespace Lombiq.Hosting.Tenants.MediaStorageManagement.Service; + +public class MediaStorageQuotaService : IMediaStorageQuotaService +{ + private readonly MediaStorageManagementOptions _mediaStorageManagementOptions; + private readonly IMediaFileStore _mediaFileStore; + + public MediaStorageQuotaService( + IOptions mediaStorageManagementOptions, + IMediaFileStore mediaFileStore) + { + _mediaStorageManagementOptions = mediaStorageManagementOptions.Value; + _mediaFileStore = mediaFileStore; + } + + public async Task GetRemainingMediaSpaceQuotaLeftAsync() + { + var directoryContent = _mediaFileStore.GetDirectoryContentAsync(includeSubDirectories: true); + + var listed = await directoryContent.ToListAsync(); + var sumSize = listed.Where(item => item.Length > 0).Sum(item => item.Length); + return MaxSpaceForTenantInBytes() - sumSize; + } + + public long MaxSpaceForTenantInBytes() => _mediaStorageManagementOptions.MaximumStorageQuota; + + public float MaxSpaceForTenantInMegabytes() => MaxSpaceForTenantInBytes() / 1024f / 1024f; +} diff --git a/Lombiq.Hosting.Tenants.MediaStorageManagement/Settings/MediaStorageManagementOptions.cs b/Lombiq.Hosting.Tenants.MediaStorageManagement/Settings/MediaStorageManagementOptions.cs new file mode 100644 index 00000000..fbe068fe --- /dev/null +++ b/Lombiq.Hosting.Tenants.MediaStorageManagement/Settings/MediaStorageManagementOptions.cs @@ -0,0 +1,9 @@ +namespace Lombiq.Hosting.Tenants.MediaStorageManagement.Settings; + +public class MediaStorageManagementOptions +{ + /// + /// Gets or sets the maximum storage quota for a tenant in bytes. Default is 1GB. + /// + public long MaximumStorageQuota { get; set; } +} diff --git a/Lombiq.Hosting.Tenants.MediaStorageManagement/Startup.cs b/Lombiq.Hosting.Tenants.MediaStorageManagement/Startup.cs new file mode 100644 index 00000000..830224cb --- /dev/null +++ b/Lombiq.Hosting.Tenants.MediaStorageManagement/Startup.cs @@ -0,0 +1,35 @@ +using Lombiq.Hosting.Tenants.MediaStorageManagement.Filters; +using Lombiq.Hosting.Tenants.MediaStorageManagement.Service; +using Lombiq.Hosting.Tenants.MediaStorageManagement.Settings; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using OrchardCore.Environment.Shell.Configuration; +using OrchardCore.Modules; +using static Lombiq.Hosting.Tenants.MediaStorageManagement.Constants.MediaStorageManagementOptionsConstants; + +namespace Lombiq.Hosting.Tenants.MediaStorageManagement; + +public class Startup : StartupBase +{ + private readonly IShellConfiguration _shellConfiguration; + + public Startup(IShellConfiguration shellConfiguration) => _shellConfiguration = shellConfiguration; + + public override void ConfigureServices(IServiceCollection services) + { + var maximumStorageQuota = + _shellConfiguration.GetValue( + "Lombiq_Hosting_Tenants_MediaStorageManagement:Media_Storage_Management_Options:MaximumSpace"); + services.Configure(options => + options.MaximumStorageQuota = maximumStorageQuota ?? MaximumStorageQuota); + + services.AddScoped(); + + services.Configure(options => + { + options.Filters.Add(typeof(UploadFileSizeShapeFilter)); + options.Conventions.Add(new MediaStorageQuotaActionFilterConvention()); + }); + } +} diff --git a/Lombiq.Hosting.Tenants.MediaStorageManagement/ViewModels/UploadFileSizeViewModel.cs b/Lombiq.Hosting.Tenants.MediaStorageManagement/ViewModels/UploadFileSizeViewModel.cs new file mode 100644 index 00000000..faa0af3b --- /dev/null +++ b/Lombiq.Hosting.Tenants.MediaStorageManagement/ViewModels/UploadFileSizeViewModel.cs @@ -0,0 +1,6 @@ +namespace Lombiq.Hosting.Tenants.MediaStorageManagement.ViewModels; + +public class UploadFileSizeViewModel +{ + public float MaximumSpace { get; set; } +} diff --git a/Lombiq.Hosting.Tenants.MediaStorageManagement/Views/UploadFileSize.cshtml b/Lombiq.Hosting.Tenants.MediaStorageManagement/Views/UploadFileSize.cshtml new file mode 100644 index 00000000..21a159ea --- /dev/null +++ b/Lombiq.Hosting.Tenants.MediaStorageManagement/Views/UploadFileSize.cshtml @@ -0,0 +1,28 @@ +@using Microsoft.AspNetCore.Mvc.Localization +@using Lombiq.Hosting.Tenants.MediaStorageManagement.ViewModels + +@model UploadFileSizeViewModel + + + + diff --git a/Lombiq.Hosting.Tenants.MediaStorageManagement/Views/_ViewImports.cshtml b/Lombiq.Hosting.Tenants.MediaStorageManagement/Views/_ViewImports.cshtml new file mode 100644 index 00000000..1bef89f1 --- /dev/null +++ b/Lombiq.Hosting.Tenants.MediaStorageManagement/Views/_ViewImports.cshtml @@ -0,0 +1,3 @@ +@inherits OrchardCore.DisplayManagement.Razor.RazorPage + +@addTagHelper *, OrchardCore.ResourceManagement diff --git a/Lombiq.Hosting.Tenants.sln b/Lombiq.Hosting.Tenants.sln index 027f2eb8..9f7daaaf 100644 --- a/Lombiq.Hosting.Tenants.sln +++ b/Lombiq.Hosting.Tenants.sln @@ -15,6 +15,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lombiq.Hosting.Tenants.Idle EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lombiq.Hosting.Tenants.FeaturesGuard.Tests.UI", "Lombiq.Hosting.Tenants.FeaturesGuard.Tests.UI\Lombiq.Hosting.Tenants.FeaturesGuard.Tests.UI.csproj", "{BC3DE841-E400-40C5-99C1-96B9299C9B04}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lombiq.Hosting.Tenants.MediaStorageManagement", "Lombiq.Hosting.Tenants.MediaStorageManagement\Lombiq.Hosting.Tenants.MediaStorageManagement.csproj", "{EFF031F1-7464-428E-9343-B1173529AC54}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI", "Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI\Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI.csproj", "{4BE4E5C8-7DB6-49D2-855E-7220B039DDF4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +49,14 @@ Global {BC3DE841-E400-40C5-99C1-96B9299C9B04}.Debug|Any CPU.Build.0 = Debug|Any CPU {BC3DE841-E400-40C5-99C1-96B9299C9B04}.Release|Any CPU.ActiveCfg = Release|Any CPU {BC3DE841-E400-40C5-99C1-96B9299C9B04}.Release|Any CPU.Build.0 = Release|Any CPU + {EFF031F1-7464-428E-9343-B1173529AC54}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EFF031F1-7464-428E-9343-B1173529AC54}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EFF031F1-7464-428E-9343-B1173529AC54}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EFF031F1-7464-428E-9343-B1173529AC54}.Release|Any CPU.Build.0 = Release|Any CPU + {4BE4E5C8-7DB6-49D2-855E-7220B039DDF4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4BE4E5C8-7DB6-49D2-855E-7220B039DDF4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4BE4E5C8-7DB6-49D2-855E-7220B039DDF4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4BE4E5C8-7DB6-49D2-855E-7220B039DDF4}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Readme.md b/Readme.md index 45c9e360..c7fde6d0 100644 --- a/Readme.md +++ b/Readme.md @@ -1,6 +1,6 @@ # Lombiq Hosting - Tenants for Orchard Core -[![Lombiq.Hosting.Tenants.Admin.Login NuGet](https://img.shields.io/nuget/v/Lombiq.Hosting.Tenants.Admin.Login?label=Lombiq.Hosting.Tenants.Admin.Login)](https://www.nuget.org/packages/Lombiq.Hosting.Tenants.Admin.Login/) [![Lombiq.Hosting.Tenants.FeaturesGuard NuGet](https://img.shields.io/nuget/v/Lombiq.Hosting.Tenants.FeaturesGuard?label=Lombiq.Hosting.Tenants.FeaturesGuard)](https://www.nuget.org/packages/Lombiq.Hosting.Tenants.FeaturesGuard/) [![Lombiq.Hosting.Tenants.FeaturesGuard.Tests.UI NuGet](https://img.shields.io/nuget/v/Lombiq.Hosting.Tenants.FeaturesGuard.Tests.UI?label=Lombiq.Hosting.Tenants.FeaturesGuard.Tests.UI)](https://www.nuget.org/packages/Lombiq.Hosting.Tenants.FeaturesGuard.Tests.UI/) [![Lombiq.Hosting.Tenants.IdleTenantManagement NuGet](https://img.shields.io/nuget/v/Lombiq.Hosting.Tenants.IdleTenantManagement?label=Lombiq.Hosting.Tenants.IdleTenantManagement)](https://www.nuget.org/packages/Lombiq.Hosting.Tenants.IdleTenantManagement/) [![Lombiq.Hosting.Tenants.IdleTenantManagement.Tests.UI NuGet](https://img.shields.io/nuget/v/Lombiq.Hosting.Tenants.IdleTenantManagement.Tests.UI?label=Lombiq.Hosting.Tenants.IdleTenantManagement.Tests.UI)](https://www.nuget.org/packages/Lombiq.Hosting.Tenants.IdleTenantManagement.Tests.UI/) [![Lombiq.Hosting.Tenants.Management NuGet](https://img.shields.io/nuget/v/Lombiq.Hosting.Tenants.Management?label=Lombiq.Hosting.Tenants.Management)](https://www.nuget.org/packages/Lombiq.Hosting.Tenants.Management/) +[![Lombiq.Hosting.Tenants.Admin.Login NuGet](https://img.shields.io/nuget/v/Lombiq.Hosting.Tenants.Admin.Login?label=Lombiq.Hosting.Tenants.Admin.Login)](https://www.nuget.org/packages/Lombiq.Hosting.Tenants.Admin.Login/) [![Lombiq.Hosting.Tenants.FeaturesGuard NuGet](https://img.shields.io/nuget/v/Lombiq.Hosting.Tenants.FeaturesGuard?label=Lombiq.Hosting.Tenants.FeaturesGuard)](https://www.nuget.org/packages/Lombiq.Hosting.Tenants.FeaturesGuard/) [![Lombiq.Hosting.Tenants.FeaturesGuard.Tests.UI NuGet](https://img.shields.io/nuget/v/Lombiq.Hosting.Tenants.FeaturesGuard.Tests.UI?label=Lombiq.Hosting.Tenants.FeaturesGuard.Tests.UI)](https://www.nuget.org/packages/Lombiq.Hosting.Tenants.FeaturesGuard.Tests.UI/) [![Lombiq.Hosting.Tenants.IdleTenantManagement NuGet](https://img.shields.io/nuget/v/Lombiq.Hosting.Tenants.IdleTenantManagement?label=Lombiq.Hosting.Tenants.IdleTenantManagement)](https://www.nuget.org/packages/Lombiq.Hosting.Tenants.IdleTenantManagement/) [![Lombiq.Hosting.Tenants.IdleTenantManagement.Tests.UI NuGet](https://img.shields.io/nuget/v/Lombiq.Hosting.Tenants.IdleTenantManagement.Tests.UI?label=Lombiq.Hosting.Tenants.IdleTenantManagement.Tests.UI)](https://www.nuget.org/packages/Lombiq.Hosting.Tenants.IdleTenantManagement.Tests.UI/) [![Lombiq.Hosting.Tenants.Management NuGet](https://img.shields.io/nuget/v/Lombiq.Hosting.Tenants.Management?label=Lombiq.Hosting.Tenants.Management)](https://www.nuget.org/packages/Lombiq.Hosting.Tenants.Management/) [![Lombiq.Hosting.Tenants.MediaStorageManagement NuGet](https://img.shields.io/nuget/v/Lombiq.Hosting.Tenants.MediaStorageManagement?label=Lombiq.Hosting.Tenants.MediaStorageManagement)](https://www.nuget.org/packages/Lombiq.Hosting.Tenants.MediaStorageManagement/) [![Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI NuGet](https://img.shields.io/nuget/v/Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI?label=Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI)](https://www.nuget.org/packages/Lombiq.Hosting.Tenants.MediaStorageManagement.Tests.UI/) ## About @@ -18,6 +18,7 @@ The project consists of the following independent modules: - [Lombiq Hosting - Tenants Admin Login](Lombiq.Hosting.Tenants.Admin.Login/Readme.md) - [Lombiq Hosting - Tenants Management](Lombiq.Hosting.Tenants.Management/Readme.md) - [Lombiq Hosting - Tenants Idle Tenant Management](Lombiq.Hosting.Tenants.IdleTenantManagement/Readme.md) +- [Lombiq Hosting - Tenants Media Storage Management](Lombiq.Hosting.Tenants.MediaStorageManagement/Readme.md) ## Contributing and support