Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSOE-658: Rename MaximumSpace to MaximumSpaceBytes #79

Merged
merged 10 commits into from
Jul 21, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ public static class MediaStorageManagementExtensions
{
public static void SetMediaStorageManagementOptionsForUITest(
this OrchardCoreUITestExecutorConfiguration configuration,
long maximumSpace)
long maximumStorageQuotaBytes)
{
configuration.OrchardCoreConfiguration.BeforeAppStart +=
(_, argumentsBuilder) =>
{
argumentsBuilder
.AddWithValue(
"OrchardCore:Lombiq_Hosting_Tenants_MediaStorageManagement:Media_Storage_Management_Options:MaximumSpace",
maximumSpace);
"OrchardCore:Lombiq_Hosting_Tenants_MediaStorageManagement:MaximumStorageQuotaBytes",
maximumStorageQuotaBytes);

return Task.CompletedTask;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace Lombiq.Hosting.Tenants.MediaStorageManagement.Constants;
namespace Lombiq.Hosting.Tenants.MediaStorageManagement.Constants;

public static class MediaStorageManagementOptionsConstants
{
/// <summary>
/// Default MaximumStorageQuota in bytes representing 1GB.
/// Default maximum storage quota in bytes representing 1GB.
/// </summary>
public const long MaximumStorageQuota = 1_073_741_824;
public const long MaximumStorageQuotaBytes = 1_073_741_824;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Lombiq.Hosting.Tenants.MediaStorageManagement.Service;
using Lombiq.Hosting.Tenants.MediaStorageManagement.Service;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -16,7 +16,7 @@ public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
.HttpContext
.RequestServices
.GetRequiredService<IMediaStorageQuotaService>()
.GetRemainingMediaSpaceQuotaLeftAsync();
.GetRemainingMediaStorageQuotaBytesAsync();

var formOptions = new FormOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ actionRouteValue is nameof(AdminController.Index) &&
{
var layout = await _layoutAccessor.GetLayoutAsync();
var contentZone = layout.Zones["Footer"];
var maximumSpace = _mediaStorageQuotaService.MaxSpaceForTenantInMegabytes();
var maximumStorageQuotaMegabytes = _mediaStorageQuotaService.GetMaxStorageQuotaMegabytes();
await contentZone.AddAsync(await _shapeFactory.CreateAsync<UploadFileSizeViewModel>(
"UploadFileSize",
viewModel => viewModel.MaximumSpace = maximumSpace));
viewModel => viewModel.MaximumStorageQuotaMegabytes = maximumStorageQuotaMegabytes));
}

await next();
Expand Down
8 changes: 2 additions & 6 deletions Lombiq.Hosting.Tenants.MediaStorageManagement/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ With this module, you can specify how much space would you like to limit each te
```json
"OrchardCore": {
"Lombiq_Hosting_Tenants_MediaStorageManagement": {
"Media_Storage_Management_Options": {
"MaximumSpace": 2147483648
}
"MaximumStorageQuotaBytes": 2147483648
}
}
```
Expand All @@ -34,9 +32,7 @@ Tenant based configuration can be defined as the following, for more details rea
"OrchardCore": {
"TenantName": {
"Lombiq_Hosting_Tenants_MediaStorageManagement": {
"Media_Storage_Management_Options": {
"MaximumSpace": 2147483648
}
"MaximumStorageQuotaBytes": 2147483648
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Threading.Tasks;
using System.Threading.Tasks;

namespace Lombiq.Hosting.Tenants.MediaStorageManagement.Service;

Expand All @@ -8,17 +8,22 @@ namespace Lombiq.Hosting.Tenants.MediaStorageManagement.Service;
public interface IMediaStorageQuotaService
{
/// <summary>
/// Returns remaining quota space left in bytes. It is always a non-negative number, meaning the minimum value is 0.
/// Returns the remaining storage space left from the quota in bytes. It is always a non-negative number, meaning
/// the minimum value is 0.
/// </summary>
Task<long> GetRemainingMediaSpaceQuotaLeftAsync();
Task<long> GetRemainingMediaStorageQuotaBytesAsync();

/// <summary>
/// Returns the maximum quota space in bytes.
/// Returns the maximum storage space form the quota in bytes.
/// </summary>
long MaxSpaceForTenantInBytes();
long GetMaxStorageQuotaBytes();
}

public static class MediaStorageQuotaServiceExtensions
{
/// <summary>
/// Returns the maximum quota space in Megabytes.
/// Returns the maximum storage quota space in Megabytes.
/// </summary>
float MaxSpaceForTenantInMegabytes();
public static float GetMaxStorageQuotaMegabytes(this IMediaStorageQuotaService mediaStorageQuotaService) =>
mediaStorageQuotaService.GetMaxStorageQuotaBytes() / 1024f / 1024f;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Lombiq.Hosting.Tenants.MediaStorageManagement.Settings;
using Lombiq.Hosting.Tenants.MediaStorageManagement.Settings;
using Microsoft.Extensions.Options;
using OrchardCore.Media;
using System.Linq;
Expand All @@ -19,18 +19,16 @@ public MediaStorageQuotaService(
_mediaFileStore = mediaFileStore;
}

public async Task<long> GetRemainingMediaSpaceQuotaLeftAsync()
public async Task<long> GetRemainingMediaStorageQuotaBytesAsync()
{
var directoryContent = _mediaFileStore.GetDirectoryContentAsync(includeSubDirectories: true);

var listed = await directoryContent.ToListAsync();
var sumSize = listed.Where(item => item.Length > 0).Sum(item => item.Length);
var remainingSpace = MaxSpaceForTenantInBytes() - sumSize;
var sumBytes = listed.Where(item => item.Length > 0).Sum(item => item.Length);
var remainingStorageQuotaBytes = GetMaxStorageQuotaBytes() - sumBytes;

return remainingSpace < 0 ? 0 : remainingSpace;
return remainingStorageQuotaBytes < 0 ? 0 : remainingStorageQuotaBytes;
}

public long MaxSpaceForTenantInBytes() => _mediaStorageManagementOptions.MaximumStorageQuota;

public float MaxSpaceForTenantInMegabytes() => MaxSpaceForTenantInBytes() / 1024f / 1024f;
public long GetMaxStorageQuotaBytes() => _mediaStorageManagementOptions.MaximumStorageQuotaBytes;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace Lombiq.Hosting.Tenants.MediaStorageManagement.Settings;
namespace Lombiq.Hosting.Tenants.MediaStorageManagement.Settings;

public class MediaStorageManagementOptions
{
/// <summary>
/// Gets or sets the maximum storage quota for a tenant in bytes. Default is 1GB.
/// </summary>
public long MaximumStorageQuota { get; set; }
public long MaximumStorageQuotaBytes { get; set; }
}
6 changes: 4 additions & 2 deletions Lombiq.Hosting.Tenants.MediaStorageManagement/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ public class Startup : StartupBase

public override void ConfigureServices(IServiceCollection services)
{
var maximumStorageQuota =
var maximumStorageQuotaBytes =
_shellConfiguration.GetValue<long?>(
"Lombiq_Hosting_Tenants_MediaStorageManagement:MaximumStorageQuotaBytes") ??
_shellConfiguration.GetValue<long?>(
"Lombiq_Hosting_Tenants_MediaStorageManagement:Media_Storage_Management_Options:MaximumSpace");
services.Configure<MediaStorageManagementOptions>(options =>
options.MaximumStorageQuota = maximumStorageQuota ?? MaximumStorageQuota);
options.MaximumStorageQuotaBytes = maximumStorageQuotaBytes ?? MaximumStorageQuotaBytes);
Piedone marked this conversation as resolved.
Show resolved Hide resolved

services.AddScoped<IMediaStorageQuotaService, MediaStorageQuotaService>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Lombiq.Hosting.Tenants.MediaStorageManagement.ViewModels;
namespace Lombiq.Hosting.Tenants.MediaStorageManagement.ViewModels;

public class UploadFileSizeViewModel
{
public float MaximumSpace { get; set; }
public float MaximumStorageQuotaMegabytes { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ $(function () {
return;
}
model.errorMessage =
@T["You may only store {0} MB of Media files for your site, and it seems that you’d just go over that. But don’t worry! If you delete some large files, you should be able to upload new ones. Also, you can contact us for a larger quota.", Model.MaximumSpace].Json();
@T[
"You may only store {0} MB of Media files for your site, and it seems that you’d just go over that. But don’t worry! If you delete some large files, you should be able to upload new ones. Also, you can contact us for a larger quota.",
Model.MaximumStorageQuotaMegabytes].Json();
});
});
</script>
Expand Down