remove-FileOperationsManagerHelper: first version

This commit is contained in:
SuhorukovAnton 2020-10-09 17:13:44 +03:00
parent 43a085eb18
commit 72fbf6b81e
2 changed files with 62 additions and 127 deletions

View File

@ -115,15 +115,16 @@ namespace ASC.Web.Files.Services.WCFService
private DocumentServiceConnector DocumentServiceConnector { get; }
private FileSharing FileSharing { get; }
private NotifyClient NotifyClient { get; }
private FileOperationsManagerHelper FileOperationsManagerHelper { get; }
private UrlShortener UrlShortener { get; }
private IServiceProvider ServiceProvider { get; }
private FileSharingAceHelper<T> FileSharingAceHelper { get; }
private ApiContext ApiContext { get; }
private ConsumerFactory ConsumerFactory { get; }
public EncryptionKeyPairHelper EncryptionKeyPairHelper { get; }
public SettingsManager SettingsManager { get; }
public ILog Logger { get; set; }
private EncryptionKeyPairHelper EncryptionKeyPairHelper { get; }
private SettingsManager SettingsManager { get; }
private FileOperationsManager FileOperationsManager { get; }
private TenantManager TenantManager { get; }
private ILog Logger { get; set; }
public FileStorageService(
Global global,
@ -159,14 +160,15 @@ namespace ASC.Web.Files.Services.WCFService
DocumentServiceConnector documentServiceConnector,
FileSharing fileSharing,
NotifyClient notifyClient,
FileOperationsManagerHelper fileOperationsManagerHelper,
UrlShortener urlShortener,
IServiceProvider serviceProvider,
FileSharingAceHelper<T> fileSharingAceHelper,
ApiContext apiContext,
ConsumerFactory consumerFactory,
EncryptionKeyPairHelper encryptionKeyPairHelper,
SettingsManager settingsManager)
SettingsManager settingsManager,
FileOperationsManager fileOperationsManager,
TenantManager tenantManager)
{
Global = global;
GlobalStore = globalStore;
@ -200,7 +202,6 @@ namespace ASC.Web.Files.Services.WCFService
DocumentServiceConnector = documentServiceConnector;
FileSharing = fileSharing;
NotifyClient = notifyClient;
FileOperationsManagerHelper = fileOperationsManagerHelper;
UrlShortener = urlShortener;
ServiceProvider = serviceProvider;
FileSharingAceHelper = fileSharingAceHelper;
@ -208,7 +209,9 @@ namespace ASC.Web.Files.Services.WCFService
ConsumerFactory = consumerFactory;
EncryptionKeyPairHelper = encryptionKeyPairHelper;
SettingsManager = settingsManager;
Logger = optionMonitor.Get("ASC.Files");
Logger = optionMonitor.Get("ASC.Files");
FileOperationsManager = fileOperationsManager;
TenantManager = tenantManager;
}
public Folder<T> GetFolder(T folderId)
@ -1121,8 +1124,7 @@ namespace ASC.Web.Files.Services.WCFService
public ItemList<FileOperationResult> MarkAsRead(IEnumerable<JsonElement> foldersId, IEnumerable<JsonElement> filesId)
{
if (!foldersId.Any() && !filesId.Any()) return GetTasksStatuses();
return FileOperationsManagerHelper.MarkAsRead(foldersId, filesId);
return FileOperationsManager.MarkAsRead(AuthContext.CurrentAccount.ID, TenantManager.GetCurrentTenant(), foldersId, filesId);
}
public ItemList<ThirdPartyParams> GetThirdParty()
@ -1313,21 +1315,21 @@ namespace ASC.Web.Files.Services.WCFService
{
ErrorIf(!AuthContext.IsAuthenticated, FilesCommonResource.ErrorMassage_SecurityException);
return FileOperationsManagerHelper.GetOperationResults();
return FileOperationsManager.GetOperationResults(AuthContext.CurrentAccount.ID);
}
public ItemList<FileOperationResult> TerminateTasks()
{
ErrorIf(!AuthContext.IsAuthenticated, FilesCommonResource.ErrorMassage_SecurityException);
return FileOperationsManagerHelper.CancelOperations();
return FileOperationsManager.CancelOperations(AuthContext.CurrentAccount.ID);
}
public ItemList<FileOperationResult> BulkDownload(Dictionary<JsonElement, string> folders, Dictionary<JsonElement, string> files)
{
ErrorIf(!folders.Any() && !files.Any(), FilesCommonResource.ErrorMassage_BadRequest);
return FileOperationsManagerHelper.Download(folders, files, GetHttpHeaders());
return FileOperationsManager.Download(AuthContext.CurrentAccount.ID, TenantManager.CurrentTenant, folders, files, GetHttpHeaders());
}
@ -1430,29 +1432,29 @@ namespace ASC.Web.Files.Services.WCFService
{
ItemList<FileOperationResult> result;
if (foldersId.Any() || filesId.Any())
{
result = FileOperationsManagerHelper.MoveOrCopy(foldersId, filesId, destFolderId, ic, resolve, !deleteAfter, GetHttpHeaders());
{
result = FileOperationsManager.MoveOrCopy(AuthContext.CurrentAccount.ID, TenantManager.CurrentTenant, foldersId, filesId, destFolderId, ic, resolve, !deleteAfter, GetHttpHeaders());
}
else
{
result = FileOperationsManagerHelper.GetOperationResults();
result = FileOperationsManager.GetOperationResults(AuthContext.CurrentAccount.ID);
}
return result;
}
public ItemList<FileOperationResult> DeleteFile(string action, T fileId, bool ignoreException = false, bool deleteAfter = false, bool immediately = false)
{
return FileOperationsManagerHelper.DeleteFile(fileId, ignoreException, !deleteAfter, immediately, GetHttpHeaders());
{
return FileOperationsManager.Delete(AuthContext.CurrentAccount.ID, TenantManager.CurrentTenant, new List<T>(), new List<T>() { fileId }, ignoreException, !deleteAfter, immediately, GetHttpHeaders());
}
public ItemList<FileOperationResult> DeleteFolder(string action, T folderId, bool ignoreException = false, bool deleteAfter = false, bool immediately = false)
{
return FileOperationsManagerHelper.DeleteFolder(folderId, ignoreException, !deleteAfter, immediately, GetHttpHeaders());
{
return FileOperationsManager.Delete(AuthContext.CurrentAccount.ID, TenantManager.CurrentTenant, new List<T>() { folderId }, new List<T>(), ignoreException, !deleteAfter, immediately, GetHttpHeaders());
}
public ItemList<FileOperationResult> DeleteItems(string action, List<JsonElement> files, List<JsonElement> folders, bool ignoreException = false, bool deleteAfter = false, bool immediately = false)
{
return FileOperationsManagerHelper.Delete(folders, files, ignoreException, !deleteAfter, immediately, GetHttpHeaders());
{
return FileOperationsManager.Delete(AuthContext.CurrentAccount.ID, TenantManager.CurrentTenant, folders, files, ignoreException, !deleteAfter, immediately, GetHttpHeaders());
}
public ItemList<FileOperationResult> EmptyTrash()
@ -1462,8 +1464,8 @@ namespace ASC.Web.Files.Services.WCFService
var trashId = folderDao.GetFolderIDTrash(true);
var foldersId = folderDao.GetFolders(trashId).Select(f => f.ID).ToList();
var filesId = fileDao.GetFiles(trashId).ToList();
return FileOperationsManagerHelper.Delete(foldersId, filesId, false, true, false, GetHttpHeaders());
return FileOperationsManager.Delete(AuthContext.CurrentAccount.ID, TenantManager.CurrentTenant, foldersId, filesId, false, true, false, GetHttpHeaders());
}
public ItemList<FileOperationResult> CheckConversion(ItemList<ItemList<string>> filesInfoJSON)
@ -2303,7 +2305,8 @@ namespace ASC.Web.Files.Services.WCFService
.AddDocumentServiceTrackerHelperService()
.AddSocketManagerService()
.AddFileOperationsManagerHelperService()
.AddFileSharingAceHelperService();
.AddFileSharingAceHelperService()
.AddTenantManagerService();
}
return services;

View File

@ -33,6 +33,7 @@ using System.Text.Json;
using ASC.Common;
using ASC.Common.Threading;
using ASC.Core;
using ASC.Core.Tenants;
using ASC.Files.Core.Resources;
using Microsoft.Extensions.Primitives;
@ -51,7 +52,7 @@ namespace ASC.Web.Files.Services.WCFService.FileOperations
ServiceProvider = serviceProvider;
}
public ItemList<FileOperationResult> GetOperationResults(AuthContext authContext)
public ItemList<FileOperationResult> GetOperationResults(Guid userId)
{
var operations = tasks.GetTasks();
var processlist = Process.GetProcesses();
@ -63,7 +64,7 @@ namespace ASC.Web.Files.Services.WCFService.FileOperations
tasks.RemoveTask(o.Id);
}
operations = operations.Where(t => t.GetProperty<Guid>(FileOperation.OWNER) == authContext.CurrentAccount.ID);
operations = operations.Where(t => t.GetProperty<Guid>(FileOperation.OWNER) == userId);
foreach (var o in operations.Where(o => o.Status > DistributedTaskStatus.Running))
{
o.SetProperty(FileOperation.PROGRESS, 100);
@ -88,33 +89,32 @@ namespace ASC.Web.Files.Services.WCFService.FileOperations
return new ItemList<FileOperationResult>(results);
}
public ItemList<FileOperationResult> CancelOperations(AuthContext authContext)
public ItemList<FileOperationResult> CancelOperations(Guid userId)
{
var operations = tasks.GetTasks()
.Where(t => t.GetProperty<Guid>(FileOperation.OWNER) == authContext.CurrentAccount.ID);
.Where(t => t.GetProperty<Guid>(FileOperation.OWNER) == userId);
foreach (var o in operations)
{
tasks.CancelTask(o.Id);
}
return GetOperationResults(authContext);
return GetOperationResults(userId);
}
public ItemList<FileOperationResult> MarkAsRead(AuthContext authContext, TenantManager tenantManager, IEnumerable<JsonElement> folderIds, IEnumerable<JsonElement> fileIds)
public ItemList<FileOperationResult> MarkAsRead(Guid userId, Tenant tenant, IEnumerable<JsonElement> folderIds, IEnumerable<JsonElement> fileIds)
{
var tenant = tenantManager.GetCurrentTenant();
var op1 = new FileMarkAsReadOperation<int>(ServiceProvider, new FileMarkAsReadOperationData<int>(folderIds.Where(r => r.ValueKind == JsonValueKind.Number).Select(r => r.GetInt32()), fileIds.Where(r => r.ValueKind == JsonValueKind.Number).Select(r => r.GetInt32()), tenant));
var op2 = new FileMarkAsReadOperation<string>(ServiceProvider, new FileMarkAsReadOperationData<string>(folderIds.Where(r => r.ValueKind == JsonValueKind.String).Select(r => r.GetString()), fileIds.Where(r => r.ValueKind == JsonValueKind.String).Select(r => r.GetString()), tenant));
var op = new FileMarkAsReadOperation(ServiceProvider, op2, op1);
return QueueTask(authContext, op);
return QueueTask(userId, op);
}
public ItemList<FileOperationResult> Download(AuthContext authContext, TenantManager tenantManager, Dictionary<JsonElement, string> folders, Dictionary<JsonElement, string> files, IDictionary<string, StringValues> headers)
public ItemList<FileOperationResult> Download(Guid userId, Tenant tenant, Dictionary<JsonElement, string> folders, Dictionary<JsonElement, string> files, IDictionary<string, StringValues> headers)
{
var operations = tasks.GetTasks()
.Where(t => t.GetProperty<Guid>(FileOperation.OWNER) == authContext.CurrentAccount.ID)
.Where(t => t.GetProperty<Guid>(FileOperation.OWNER) == userId)
.Where(t => t.GetProperty<FileOperationType>(FileOperation.OPERATION_TYPE) == FileOperationType.Download);
if (operations.Any(o => o.Status <= DistributedTaskStatus.Running))
@ -122,112 +122,48 @@ namespace ASC.Web.Files.Services.WCFService.FileOperations
throw new InvalidOperationException(FilesCommonResource.ErrorMassage_ManyDownloads);
}
var tenant = tenantManager.GetCurrentTenant();
var op1 = new FileDownloadOperation<int>(ServiceProvider, new FileDownloadOperationData<int>(folders.Where(r => r.Key.ValueKind == JsonValueKind.Number).ToDictionary(r => r.Key.GetInt32(), r => r.Value), files.Where(r => r.Key.ValueKind == JsonValueKind.Number).ToDictionary(r => r.Key.GetInt32(), r => r.Value), tenant, headers), false);
var op2 = new FileDownloadOperation<string>(ServiceProvider, new FileDownloadOperationData<string>(folders.Where(r => r.Key.ValueKind == JsonValueKind.String).ToDictionary(r => r.Key.GetString(), r => r.Value), files.Where(r => r.Key.ValueKind == JsonValueKind.String).ToDictionary(r => r.Key.GetString(), r => r.Value), tenant, headers), false);
var op = new FileDownloadOperation(ServiceProvider, op2, op1);
return QueueTask(authContext, op);
return QueueTask(userId, op);
}
public ItemList<FileOperationResult> MoveOrCopy(AuthContext authContext, TenantManager tenantManager, IEnumerable<JsonElement> folders, IEnumerable<JsonElement> files, JsonElement destFolderId, bool copy, FileConflictResolveType resolveType, bool holdResult, IDictionary<string, StringValues> headers)
public ItemList<FileOperationResult> MoveOrCopy(Guid userId, Tenant tenant, IEnumerable<JsonElement> folders, IEnumerable<JsonElement> files, JsonElement destFolderId, bool copy, FileConflictResolveType resolveType, bool holdResult, IDictionary<string, StringValues> headers)
{
var tenant = tenantManager.GetCurrentTenant();
var op1 = new FileMoveCopyOperation<int>(ServiceProvider, new FileMoveCopyOperationData<int>(folders.Where(r => r.ValueKind == JsonValueKind.Number).Select(r => r.GetInt32()), files.Where(r => r.ValueKind == JsonValueKind.Number).Select(r => r.GetInt32()), tenant, destFolderId, copy, resolveType, holdResult, headers));
var op2 = new FileMoveCopyOperation<string>(ServiceProvider, new FileMoveCopyOperationData<string>(folders.Where(r => r.ValueKind == JsonValueKind.String).Select(r => r.GetString()), files.Where(r => r.ValueKind == JsonValueKind.String).Select(r => r.GetString()), tenant, destFolderId, copy, resolveType, holdResult, headers));
var op = new FileMoveCopyOperation(ServiceProvider, op2, op1);
return QueueTask(authContext, op);
return QueueTask(userId, op);
}
public ItemList<FileOperationResult> Delete<T>(AuthContext authContext, TenantManager tenantManager, IEnumerable<T> folders, IEnumerable<T> files, bool ignoreException, bool holdResult, bool immediately, IDictionary<string, StringValues> headers)
public ItemList<FileOperationResult> Delete<T>(Guid userId, Tenant tenant, IEnumerable<T> folders, IEnumerable<T> files, bool ignoreException, bool holdResult, bool immediately, IDictionary<string, StringValues> headers)
{
var op = new FileDeleteOperation<T>(ServiceProvider, new FileDeleteOperationData<T>(folders, files, tenantManager.GetCurrentTenant(), holdResult, ignoreException, immediately, headers));
return QueueTask(authContext, op);
var op = new FileDeleteOperation<T>(ServiceProvider, new FileDeleteOperationData<T>(folders, files, tenant, holdResult, ignoreException, immediately, headers));
return QueueTask(userId, op);
}
public ItemList<FileOperationResult> Delete(AuthContext authContext, TenantManager tenantManager, IEnumerable<JsonElement> folders, IEnumerable<JsonElement> files, bool ignoreException, bool holdResult, bool immediately, IDictionary<string, StringValues> headers)
public ItemList<FileOperationResult> Delete(Guid userId, TenantManager tenantManager, IEnumerable<JsonElement> folders, IEnumerable<JsonElement> files, bool ignoreException, bool holdResult, bool immediately, IDictionary<string, StringValues> headers)
{
var op1 = new FileDeleteOperation<int>(ServiceProvider, new FileDeleteOperationData<int>(folders.Where(r => r.ValueKind == JsonValueKind.Number).Select(r => r.GetInt32()), files.Where(r => r.ValueKind == JsonValueKind.Number).Select(r => r.GetInt32()), tenantManager.GetCurrentTenant(), holdResult, ignoreException, immediately, headers));
var op2 = new FileDeleteOperation<string>(ServiceProvider, new FileDeleteOperationData<string>(folders.Where(r => r.ValueKind == JsonValueKind.String).Select(r => r.GetString()), files.Where(r => r.ValueKind == JsonValueKind.String).Select(r => r.GetString()), tenantManager.GetCurrentTenant(), holdResult, ignoreException, immediately, headers));
var op = new FileDeleteOperation(ServiceProvider, op2, op1);
return QueueTask(authContext, op);
return QueueTask(userId, op);
}
private ItemList<FileOperationResult> QueueTask(AuthContext authContext, FileOperation op)
private ItemList<FileOperationResult> QueueTask(Guid userId, FileOperation op)
{
tasks.QueueTask(op.RunJob, op.GetDistributedTask());
return GetOperationResults(authContext);
return GetOperationResults(userId);
}
private ItemList<FileOperationResult> QueueTask<T, TId>(AuthContext authContext, FileOperation<T, TId> op) where T : FileOperationData<TId>
private ItemList<FileOperationResult> QueueTask<T, TId>(Guid userId, FileOperation<T, TId> op) where T : FileOperationData<TId>
{
tasks.QueueTask(op.RunJob, op.GetDistributedTask());
return GetOperationResults(authContext);
}
}
public class FileOperationsManagerHelper
{
private FileOperationsManager FileOperationsManager { get; }
private AuthContext AuthContext { get; }
private TenantManager TenantManager { get; }
public FileOperationsManagerHelper(
FileOperationsManager fileOperationsManager,
AuthContext authContext,
TenantManager tenantManager)
{
FileOperationsManager = fileOperationsManager;
AuthContext = authContext;
TenantManager = tenantManager;
}
public ItemList<FileOperationResult> GetOperationResults()
{
return FileOperationsManager.GetOperationResults(AuthContext);
}
public ItemList<FileOperationResult> CancelOperations()
{
return FileOperationsManager.CancelOperations(AuthContext);
}
public ItemList<FileOperationResult> MarkAsRead(IEnumerable<JsonElement> folderIds, IEnumerable<JsonElement> fileIds)
{
return FileOperationsManager.MarkAsRead(AuthContext, TenantManager, folderIds, fileIds);
}
public ItemList<FileOperationResult> Download(Dictionary<JsonElement, string> folders, Dictionary<JsonElement, string> files, IDictionary<string, StringValues> headers)
{
return FileOperationsManager.Download(AuthContext, TenantManager, folders, files, headers);
}
public ItemList<FileOperationResult> MoveOrCopy(IEnumerable<JsonElement> folders, IEnumerable<JsonElement> files, JsonElement destFolderId, bool copy, FileConflictResolveType resolveType, bool holdResult, IDictionary<string, StringValues> headers)
{
return FileOperationsManager.MoveOrCopy(AuthContext, TenantManager, folders, files, destFolderId, copy, resolveType, holdResult, headers);
}
public ItemList<FileOperationResult> Delete(List<JsonElement> folders, List<JsonElement> files, bool ignoreException, bool holdResult, bool immediately, IDictionary<string, StringValues> headers)
{
return FileOperationsManager.Delete(AuthContext, TenantManager, folders, files, ignoreException, holdResult, immediately, headers);
}
public ItemList<FileOperationResult> Delete<T>(List<T> folders, List<T> files, bool ignoreException, bool holdResult, bool immediately, IDictionary<string, StringValues> headers)
{
return FileOperationsManager.Delete(AuthContext, TenantManager, folders, files, ignoreException, holdResult, immediately, headers);
}
public ItemList<FileOperationResult> DeleteFile<T>(T file, bool ignoreException, bool holdResult, bool immediately, IDictionary<string, StringValues> headers)
{
return Delete(new List<T>(), new List<T>() { file }, ignoreException, holdResult, immediately, headers);
}
public ItemList<FileOperationResult> DeleteFolder<T>(T folder, bool ignoreException, bool holdResult, bool immediately, IDictionary<string, StringValues> headers)
{
return Delete(new List<T>() { folder }, new List<T>(), ignoreException, holdResult, immediately, headers);
return GetOperationResults(userId);
}
}
@ -235,23 +171,19 @@ namespace ASC.Web.Files.Services.WCFService.FileOperations
{
public static DIHelper AddFileOperationsManagerHelperService(this DIHelper services)
{
if (services.TryAddScoped<FileOperationsManagerHelper>())
{
services.TryAddSingleton<DistributedTaskCacheNotify>();
services.TryAddSingleton<FileOperationsManager>();
services.TryAddScoped<FileDeleteOperationScope>();
services.TryAddScoped<FileMarkAsReadOperationScope>();
services.TryAddScoped<FileMoveCopyOperationScope>();
services.TryAddScoped<FileOperationScope>();
services.TryAddScoped<FileDownloadOperationScope>();
services.TryAddSingleton<DistributedTaskCacheNotify>();
services.TryAddSingleton<FileOperationsManager>();
services.TryAddScoped<FileDeleteOperationScope>();
services.TryAddScoped<FileMarkAsReadOperationScope>();
services.TryAddScoped<FileMoveCopyOperationScope>();
services.TryAddScoped<FileOperationScope>();
services.TryAddScoped<FileDownloadOperationScope>();
return services
.AddAuthContextService()
.AddTenantManagerService()
;
}
return services;
return services
.AddAuthContextService()
.AddTenantManagerService()
;
}
}
}