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

82 lines
2.9 KiB
C#
Raw Normal View History

2021-11-16 17:40:15 +00:00
namespace ASC.Web.Core.Files
{
[Scope]
public class DocumentServiceLicense
{
private static readonly TimeSpan CACHE_EXPIRATION = TimeSpan.FromMinutes(15);
private ICache Cache { get; }
public CoreBaseSettings CoreBaseSettings { get; }
private FilesLinkUtility FilesLinkUtility { get; }
private FileUtility FileUtility { get; }
2022-01-31 11:35:59 +00:00
private IHttpClientFactory ClientFactory { get; }
2021-11-16 17:40:15 +00:00
public DocumentServiceLicense(
ICache cache,
CoreBaseSettings coreBaseSettings,
FilesLinkUtility filesLinkUtility,
2022-01-31 11:35:59 +00:00
FileUtility fileUtility,
IHttpClientFactory clientFactory)
2021-11-16 17:40:15 +00:00
{
Cache = cache;
CoreBaseSettings = coreBaseSettings;
FilesLinkUtility = filesLinkUtility;
FileUtility = fileUtility;
2022-01-31 11:35:59 +00:00
ClientFactory = clientFactory;
2021-11-16 17:40:15 +00:00
}
2022-02-15 13:20:06 +00:00
private Task<CommandResponse> GetDocumentServiceLicenseAsync()
2021-11-16 17:40:15 +00:00
{
2022-02-15 13:20:06 +00:00
if (!CoreBaseSettings.Standalone) return Task.FromResult<CommandResponse>(null);
if (string.IsNullOrEmpty(FilesLinkUtility.DocServiceCommandUrl)) return Task.FromResult<CommandResponse>(null);
2021-11-16 17:40:15 +00:00
2022-02-15 13:20:06 +00:00
return InternalGetDocumentServiceLicenseAsync();
}
private async Task<CommandResponse> InternalGetDocumentServiceLicenseAsync()
{
2021-11-16 17:40:15 +00:00
var cacheKey = "DocumentServiceLicense";
var commandResponse = Cache.Get<CommandResponse>(cacheKey);
if (commandResponse == null)
{
commandResponse = await DocumentService.CommandRequestAsync(
2021-11-16 17:40:15 +00:00
FileUtility,
FilesLinkUtility.DocServiceCommandUrl,
DocumentService.CommandMethod.License,
null,
null,
null,
null,
2022-01-31 11:35:59 +00:00
FileUtility.SignatureSecret,
ClientFactory
);
2021-11-16 17:40:15 +00:00
Cache.Insert(cacheKey, commandResponse, DateTime.UtcNow.Add(CACHE_EXPIRATION));
}
return commandResponse;
}
public async Task<Dictionary<string, DateTime>> GetLicenseQuotaAsync()
2021-11-16 17:40:15 +00:00
{
var commandResponse = await GetDocumentServiceLicenseAsync();
2021-11-16 17:40:15 +00:00
if (commandResponse == null
|| commandResponse.Quota == null
|| commandResponse.Quota.Users == null)
return null;
var result = new Dictionary<string, DateTime>();
commandResponse.Quota.Users.ForEach(user => result.Add(user.UserId, user.Expire));
return result;
}
public async Task<License> GetLicenseAsync()
2021-11-16 17:40:15 +00:00
{
var commandResponse = await GetDocumentServiceLicenseAsync();
2021-11-16 17:40:15 +00:00
if (commandResponse == null)
return null;
return commandResponse.License;
}
}
}