DocSpace-buildtools/web/ASC.Web.Core/Files/DocumentServiceLicense.cs

109 lines
3.4 KiB
C#
Raw Normal View History

2021-11-16 17:40:15 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2021
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
2022-03-17 15:13:38 +00:00
namespace ASC.Web.Core.Files;
[Scope]
public class DocumentServiceLicense
2021-11-16 17:40:15 +00:00
{
2022-03-17 16:57:02 +00:00
private static readonly TimeSpan _cacheExpiration = TimeSpan.FromMinutes(15);
2022-03-17 15:13:38 +00:00
private ICache Cache { get; }
public CoreBaseSettings CoreBaseSettings { get; }
private FilesLinkUtility FilesLinkUtility { get; }
private FileUtility FileUtility { get; }
private IHttpClientFactory ClientFactory { get; }
2021-11-16 17:40:15 +00:00
2022-03-17 15:13:38 +00:00
public DocumentServiceLicense(
ICache cache,
CoreBaseSettings coreBaseSettings,
FilesLinkUtility filesLinkUtility,
FileUtility fileUtility,
IHttpClientFactory clientFactory)
{
Cache = cache;
CoreBaseSettings = coreBaseSettings;
FilesLinkUtility = filesLinkUtility;
FileUtility = fileUtility;
ClientFactory = clientFactory;
}
2022-01-31 11:35:59 +00:00
2022-03-17 15:13:38 +00:00
private Task<CommandResponse> GetDocumentServiceLicenseAsync()
{
if (!CoreBaseSettings.Standalone)
{
return Task.FromResult<CommandResponse>(null);
}
2021-11-16 17:40:15 +00:00
2022-03-17 15:13:38 +00:00
if (string.IsNullOrEmpty(FilesLinkUtility.DocServiceCommandUrl))
2021-11-16 17:40:15 +00:00
{
2022-03-17 15:13:38 +00:00
return Task.FromResult<CommandResponse>(null);
2021-11-16 17:40:15 +00:00
}
2022-03-17 15:01:39 +00:00
2022-03-17 15:13:38 +00:00
return InternalGetDocumentServiceLicenseAsync();
}
2022-03-17 15:01:39 +00:00
2022-03-17 15:13:38 +00:00
private async Task<CommandResponse> InternalGetDocumentServiceLicenseAsync()
{
var cacheKey = "DocumentServiceLicense";
var commandResponse = Cache.Get<CommandResponse>(cacheKey);
if (commandResponse == null)
{
commandResponse = await DocumentService.CommandRequestAsync(
FileUtility,
FilesLinkUtility.DocServiceCommandUrl,
DocumentService.CommandMethod.License,
null,
null,
null,
null,
FileUtility.SignatureSecret,
ClientFactory
);
2022-03-17 16:57:02 +00:00
Cache.Insert(cacheKey, commandResponse, DateTime.UtcNow.Add(_cacheExpiration));
2022-02-15 13:20:06 +00:00
}
2022-03-17 15:13:38 +00:00
return commandResponse;
}
public async Task<Dictionary<string, DateTime>> GetLicenseQuotaAsync()
{
var commandResponse = await GetDocumentServiceLicenseAsync();
if (commandResponse == null
|| commandResponse.Quota == null
|| commandResponse.Quota.Users == null)
2022-02-15 13:20:06 +00:00
{
2022-03-17 15:13:38 +00:00
return null;
}
var result = new Dictionary<string, DateTime>();
commandResponse.Quota.Users.ForEach(user => result.Add(user.UserId, user.Expire));
return result;
}
2022-03-17 15:01:39 +00:00
2022-03-17 15:13:38 +00:00
public async Task<License> GetLicenseAsync()
{
var commandResponse = await GetDocumentServiceLicenseAsync();
if (commandResponse == null)
{
return null;
}
2022-03-17 15:01:39 +00:00
2022-03-17 15:13:38 +00:00
return commandResponse.License;
}
}