DocSpace-buildtools/common/ASC.Data.Storage/TenantQuotaController.cs

142 lines
5.2 KiB
C#
Raw Normal View History

2022-03-15 18:00:53 +00:00
// (c) Copyright Ascensio System SIA 2010-2022
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
// any third-party rights.
//
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
namespace ASC.Data.Storage;
2022-10-20 13:19:20 +00:00
[Transient]
2022-02-10 11:24:16 +00:00
public class TenantQuotaController : IQuotaController
2019-06-04 14:43:20 +00:00
{
2022-02-10 11:24:16 +00:00
private long CurrentSize
2019-06-04 14:43:20 +00:00
{
2022-02-10 11:24:16 +00:00
get
{
2022-02-10 11:24:16 +00:00
if (!_lazyCurrentSize.IsValueCreated)
2020-11-24 10:56:24 +00:00
{
2022-02-10 11:24:16 +00:00
return _currentSize = _lazyCurrentSize.Value;
2020-11-24 10:56:24 +00:00
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
return _currentSize;
2019-06-04 14:43:20 +00:00
}
2022-02-10 11:24:16 +00:00
set => _currentSize = value;
}
2019-06-04 14:43:20 +00:00
2022-10-20 13:19:20 +00:00
private int _tenant;
2022-02-10 11:24:16 +00:00
private readonly TenantManager _tenantManager;
2022-09-06 23:10:58 +00:00
private readonly AuthContext _authContext;
2022-09-19 19:49:41 +00:00
private readonly TenantQuotaFeatureChecker<MaxFileSizeFeature, long> _maxFileSizeChecker;
private readonly TenantQuotaFeatureChecker<MaxTotalSizeFeature, long> _maxTotalSizeChecker;
2022-10-20 13:19:20 +00:00
private Lazy<long> _lazyCurrentSize;
2022-02-10 11:24:16 +00:00
private long _currentSize;
2019-06-04 14:43:20 +00:00
2022-10-20 13:19:20 +00:00
public TenantQuotaController(TenantManager tenantManager, AuthContext authContext, TenantQuotaFeatureChecker<MaxFileSizeFeature, long> maxFileSizeChecker, TenantQuotaFeatureChecker<MaxTotalSizeFeature, long> maxTotalSizeChecker)
2022-02-10 11:24:16 +00:00
{
_tenantManager = tenantManager;
2022-09-19 19:49:41 +00:00
_maxFileSizeChecker = maxFileSizeChecker;
_maxTotalSizeChecker = maxTotalSizeChecker;
2022-10-20 13:19:20 +00:00
_authContext = authContext;
}
public void Init(int tenant)
{
_tenant = tenant;
2022-02-10 11:24:16 +00:00
_lazyCurrentSize = new Lazy<long>(() => _tenantManager.FindTenantQuotaRows(tenant)
.Where(r => UsedInQuota(r.Tag))
.Sum(r => r.Counter));
}
2022-02-10 11:06:37 +00:00
2022-02-10 11:24:16 +00:00
public void QuotaUsedAdd(string module, string domain, string dataTag, long size, bool quotaCheckFileSize = true)
{
size = Math.Abs(size);
if (UsedInQuota(dataTag))
2019-06-04 14:43:20 +00:00
{
2022-02-10 11:24:16 +00:00
QuotaUsedCheck(size, quotaCheckFileSize);
CurrentSize += size;
2019-06-04 14:43:20 +00:00
}
2022-10-20 11:38:38 +00:00
SetTenantQuotaRow(module, domain, size, dataTag, true, Guid.Empty);
2022-10-07 13:37:57 +00:00
SetTenantQuotaRow(module, domain, size, dataTag, true, _authContext.CurrentAccount.ID);
2022-02-10 11:24:16 +00:00
}
2022-02-10 11:06:37 +00:00
2022-02-10 11:24:16 +00:00
public void QuotaUsedDelete(string module, string domain, string dataTag, long size)
{
size = -Math.Abs(size);
if (UsedInQuota(dataTag))
{
CurrentSize += size;
2019-06-04 14:43:20 +00:00
}
2022-10-20 11:47:45 +00:00
SetTenantQuotaRow(module, domain, size, dataTag, true, Guid.Empty);
2022-10-07 13:37:57 +00:00
SetTenantQuotaRow(module, domain, size, dataTag, true, _authContext.CurrentAccount.ID);
2022-02-10 11:24:16 +00:00
}
public void QuotaUsedSet(string module, string domain, string dataTag, long size)
{
size = Math.Max(0, size);
if (UsedInQuota(dataTag))
2021-05-21 13:26:42 +00:00
{
2022-02-10 11:24:16 +00:00
CurrentSize += size;
2021-05-21 13:26:42 +00:00
}
2022-10-07 13:37:57 +00:00
SetTenantQuotaRow(module, domain, size, dataTag, false, Guid.Empty);
2022-02-10 11:24:16 +00:00
}
public void QuotaUsedCheck(long size)
{
QuotaUsedCheck(size, true);
}
public void QuotaUsedCheck(long size, bool quotaCheckFileSize)
{
var quota = _tenantManager.GetTenantQuota(_tenant);
if (quota != null)
2019-06-04 14:43:20 +00:00
{
2022-09-19 19:49:41 +00:00
if (quota.MaxFileSize != 0 && quotaCheckFileSize)
2019-06-04 14:43:20 +00:00
{
2022-09-19 19:49:41 +00:00
_maxFileSizeChecker.CheckAdd(_tenant, size);
2022-02-10 11:24:16 +00:00
}
2022-09-19 19:49:41 +00:00
if (quota.MaxTotalSize != 0)
2022-02-10 11:24:16 +00:00
{
2022-09-19 19:49:41 +00:00
_maxTotalSizeChecker.CheckAdd(_tenant, CurrentSize + size);
2019-06-04 14:43:20 +00:00
}
}
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-10-07 13:37:57 +00:00
private void SetTenantQuotaRow(string module, string domain, long size, string dataTag, bool exchange, Guid userId)
2022-02-10 11:24:16 +00:00
{
2022-04-14 19:42:15 +00:00
_tenantManager.SetTenantQuotaRow(
2022-10-07 13:37:57 +00:00
new TenantQuotaRow { Tenant = _tenant, Path = $"/{module}/{domain}", Counter = size, Tag = dataTag, UserId = userId, LastModified = DateTime.UtcNow },
2022-04-14 19:42:15 +00:00
exchange);
2022-09-10 15:07:50 +00:00
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
private bool UsedInQuota(string tag)
{
return !string.IsNullOrEmpty(tag) && new Guid(tag) != Guid.Empty;
}
}