<PackageReference Include="Relativity.Transfer.Client" Version="7.2.7" />

FileShareService

using Relativity.Transfer.Dto; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace Relativity.Transfer { internal class FileShareService : IFileShareService { private const string _END_POINT = "/Relativity.Rest/api/Relativity.StagingExplorer.Services.StagingManager.IStagingManagerModule/FileManagementService/"; private const string _COMMAND_QUERY = "QueryAsync"; private const string _COMMAND_QUERY_PROPERTIES = "QueryPropertiesAsync"; private const string _COMMAND_GET_ITEM = "GetItemAsync"; private const string _COMMAND_GET_ITEMS = "GetItemsAsync"; private const string _COMMAND_DELETE = "DeleteAsync"; private const string _COMMAND_ITEM_EXIST = "ItemExistsAsync"; private const string _COMMAND_ITEMS_EXIST = "ItemsExistAsync"; private readonly IFileShareRestClient _fileShareRestClient; private readonly int _fileShareId; private readonly bool _throwExceptions; internal FileShareService(RelativityConnectionInfo connectionInfo, int fileShareId, ITransferLog log, int maxRetryAttempts, double timeoutSeconds, bool throwExceptions = true) { if (connectionInfo == null) throw new ArgumentNullException("connectionInfo"); if (log == null) throw new ArgumentNullException("log"); if (fileShareId <= 0) throw new ArgumentOutOfRangeException("fileShareId", fileShareId, "FileShare id should be greater than zero"); _fileShareRestClient = new FileShareRestClient(connectionInfo, log, maxRetryAttempts, timeoutSeconds); _fileShareId = fileShareId; _throwExceptions = throwExceptions; } internal FileShareService(IFileShareRestClient restClient, int fileShareId, bool throwExceptions) { if (restClient == null) throw new ArgumentNullException("restClient"); if (fileShareId <= 0) throw new ArgumentOutOfRangeException("fileShareId", fileShareId, "FileShare id should be greater than zero"); _fileShareRestClient = restClient; _fileShareId = fileShareId; _throwExceptions = throwExceptions; } public async Task<bool> ItemExistsAsync(string path, CancellationToken token) { ItemExistsRequestDto request = new ItemExistsRequestDto { FileshareId = _fileShareId, Path = path }; return (await SendFileShareMessage<ItemExistsResponseDto, ItemExistsRequestDto>(request, "ItemExistsAsync", token)).Exist; } public async Task<bool> ItemsExistAsync(IEnumerable<string> paths, CancellationToken token) { ItemsExistRequestDto request = new ItemsExistRequestDto { FileshareId = _fileShareId, Paths = paths.ToArray() }; return (await SendFileShareMessage<ItemsExistResponseDto, ItemsExistRequestDto>(request, "ItemsExistAsync", token)).Exist; } public async Task<bool> DirectoryEmptyAsync(string path, CancellationToken token) { BrowseDirectoryResponseDto response = await BrowseDirectoryAsync(path, 0, 1, 1, token).ConfigureAwait(false); return IsValidResponse(response.ErrorCode) && response.TotalCount <= 0; } public async Task<FileShareItem> GetFileShareItemAsync(string path, CancellationToken token) { GetItemRequestDto request = new GetItemRequestDto { FileshareId = _fileShareId, Path = path }; return (await SendFileShareMessage<GetItemResponseDto, GetItemRequestDto>(request, "GetItemAsync", token)).Item; } public async Task<IEnumerable<FileShareItem>> GetFileShareItemsAsync(IEnumerable<string> directories, IEnumerable<string> files, CancellationToken token) { GetItemsRequestDto request = new GetItemsRequestDto { FileshareId = _fileShareId, DirectoriesPaths = directories.ToArray(), FilesPaths = files.ToArray() }; return (await SendFileShareMessage<GetItemsResponseDto, GetItemsRequestDto>(request, "GetItemsAsync", token)).Items; } public async Task<BrowseDirectoryResponseDto> BrowseDirectoryAsync(string path, int page, int pageSize, int enumerateMax, CancellationToken token) { BrowseDirectoryRequestDto request = new BrowseDirectoryRequestDto { FileshareId = _fileShareId, Path = path, MaxItems = enumerateMax, Page = page, PageSize = pageSize }; FileShareMessageWithCancel<BrowseDirectoryRequestDto> fileShareMessageWithCancel = new FileShareMessageWithCancel<BrowseDirectoryRequestDto>(request, Guid.NewGuid(), Guid.NewGuid()); BrowseDirectoryResponseDto response = await _fileShareRestClient.RequestPostAsync<BrowseDirectoryResponseDto, BrowseDirectoryRequestDto>(fileShareMessageWithCancel, "/Relativity.Rest/api/Relativity.StagingExplorer.Services.StagingManager.IStagingManagerModule/FileManagementService/QueryAsync", token).ConfigureAwait(false); if (IsValidResponse(response.ErrorCode) || response.ErrorCode == FileShareOperationErrorCode.QueryItemsLimitExceeded) return response; if (_throwExceptions) throw new FileShareException(response.ErrorCode, response.Error); return response; } public async Task<QueryPropertiesResponseDto> GetItemAttributesAsync(string path, CancellationToken token) { QueryPropertiesRequestDto request = new QueryPropertiesRequestDto { FileshareId = _fileShareId, Path = path }; return await SendFileShareMessage<QueryPropertiesResponseDto, QueryPropertiesRequestDto>(request, "QueryPropertiesAsync", token); } public async Task DeleteDirectoriesAsync(IEnumerable<string> paths, CancellationToken token) { await DeleteAsync(paths, false, token).ConfigureAwait(false); } public async Task DeleteFilesAsync(IEnumerable<string> paths, CancellationToken token) { await DeleteAsync(paths, true, token).ConfigureAwait(false); } private async Task DeleteAsync(IEnumerable<string> paths, bool isFile, CancellationToken token) { DeleteFileRequestDto request = new DeleteFileRequestDto { SourceFileshareId = _fileShareId, CorrelationId = Guid.NewGuid(), SourcePaths = from path in paths select new FileOperationSourcePath { Path = path, IsFile = isFile } }; await _fileShareRestClient.RequestPostAsync<FileShareResponseBaseDto, DeleteFileRequestDto>(new FileShareMessage<DeleteFileRequestDto>(request), "/Relativity.Rest/api/Relativity.StagingExplorer.Services.StagingManager.IStagingManagerModule/FileManagementService/DeleteAsync", token).ConfigureAwait(false); } internal async Task<TResponse> SendFileShareMessage<TResponse, TRequest>(TRequest request, string command, CancellationToken token) where TResponse : FileShareResponseBaseDto, new { FileShareMessage<TRequest> fileShareMessage = new FileShareMessage<TRequest>(request); TResponse response = await _fileShareRestClient.RequestPostAsync<TResponse, TRequest>(fileShareMessage, "/Relativity.Rest/api/Relativity.StagingExplorer.Services.StagingManager.IStagingManagerModule/FileManagementService/" + command, token).ConfigureAwait(false); if (response == null) throw new ArgumentNullException("HTTP response is null for command \"" + command + "\" (response: TResponse)"); if (IsValidResponse(response.ErrorCode)) return response; if (_throwExceptions) throw new FileShareException(response.ErrorCode, response.Error); return response; } internal bool IsValidResponse(FileShareOperationErrorCode responseError) { return responseError == FileShareOperationErrorCode.Unknown; } internal static string StandardizePathSeparator(string path) { return path.Replace('/', '\\'); } internal string RemoveLastSeparator(string path) { return (path.Length > 1 && path.EndsWith("\\")) ? path.Remove(path.Length - 1) : path; } } }