DocSpace-buildtools/common/ASC.Data.Storage/Encryption/EncryptionWorker.cs

107 lines
3.8 KiB
C#
Raw Normal View History

2020-09-02 10:54:50 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2020
2020-09-16 07:54:52 +00:00
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL 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 more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 <EFBFBD> 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 <EFBFBD> 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
2020-09-02 10:54:50 +00:00
*
*/
using System;
2020-10-14 14:26:18 +00:00
using System.Linq;
2020-09-02 10:54:50 +00:00
using ASC.Common;
2020-09-30 12:23:35 +00:00
using ASC.Common.Threading;
2020-12-28 13:22:08 +00:00
using ASC.Core.Encryption;
2020-09-30 12:23:35 +00:00
2020-09-02 10:54:50 +00:00
using Microsoft.Extensions.DependencyInjection;
namespace ASC.Data.Storage.Encryption
{
2020-10-19 15:53:15 +00:00
[Singletone]
2020-09-02 10:54:50 +00:00
public class EncryptionWorker
{
private object Locker { get; }
2020-10-13 14:28:10 +00:00
private FactoryOperation FactoryOperation { get; }
2020-09-30 12:23:35 +00:00
private DistributedTaskQueue Queue { get; }
2020-09-02 10:54:50 +00:00
2020-09-30 12:23:35 +00:00
public EncryptionWorker(FactoryOperation factoryOperation, DistributedTaskQueueOptionsManager options)
2020-09-02 10:54:50 +00:00
{
Locker = new object();
2020-09-30 12:23:35 +00:00
FactoryOperation = factoryOperation;
Queue = options.Get<EncryptionOperation>();
2020-09-02 10:54:50 +00:00
}
public void Start(EncryptionSettingsProto encryptionSettings)
{
2020-09-03 11:48:36 +00:00
EncryptionOperation encryptionOperation;
2020-09-02 10:54:50 +00:00
lock (Locker)
{
2020-10-13 14:28:10 +00:00
if (Queue.GetTask<EncryptionOperation>(GetCacheId()) != null) return;
encryptionOperation = FactoryOperation.CreateOperation(encryptionSettings, GetCacheId());
2020-10-02 07:19:02 +00:00
Queue.QueueTask(encryptionOperation);
2020-09-02 10:54:50 +00:00
}
}
public void Stop()
{
2020-10-13 14:28:10 +00:00
Queue.CancelTask(GetCacheId());
2020-09-02 10:54:50 +00:00
}
2020-10-13 14:28:10 +00:00
public string GetCacheId()
2020-09-02 10:54:50 +00:00
{
return typeof(EncryptionOperation).FullName;
}
2020-10-13 14:28:10 +00:00
public double? GetEncryptionProgress()
{
2020-10-14 14:26:18 +00:00
var progress = Queue.GetTasks<EncryptionOperation>().FirstOrDefault();
return progress.Percentage;
}
2020-09-02 10:54:50 +00:00
}
2020-10-22 17:57:18 +00:00
[Singletone(Additional = typeof(FactoryOperationExtension))]
2020-09-02 10:54:50 +00:00
public class FactoryOperation
{
private IServiceProvider ServiceProvider { get; set; }
public FactoryOperation(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
}
2020-09-30 12:23:35 +00:00
public EncryptionOperation CreateOperation(EncryptionSettingsProto encryptionSettings, string id)
2020-09-02 10:54:50 +00:00
{
var item = ServiceProvider.GetService<EncryptionOperation>();
2020-09-30 12:23:35 +00:00
item.Init(encryptionSettings, id);
2020-09-02 10:54:50 +00:00
return item;
}
}
2020-10-22 17:57:18 +00:00
public class FactoryOperationExtension
2020-09-02 10:54:50 +00:00
{
2020-10-22 17:57:18 +00:00
public static void Register(DIHelper dIHelper)
2020-09-30 12:23:35 +00:00
{
dIHelper.TryAdd<EncryptionOperation>();
dIHelper.AddDistributedTaskQueueService<EncryptionOperation>(1);
2020-09-02 10:54:50 +00:00
}
}
}