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 = "/Relativity.Rest/api/Relativity.StagingExplorer.Services.StagingManager.IStagingManagerModule/FileManagementService/";
private const string = "QueryAsync";
private const string = "QueryPropertiesAsync";
private const string = "GetItemAsync";
private const string = "GetItemsAsync";
private const string = "DeleteAsync";
private const string = "ItemExistsAsync";
private const string = "ItemsExistAsync";
private readonly IFileShareRestClient ;
private readonly int ;
private readonly bool ;
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> (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> (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> (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> (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>> (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> (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> (string path, CancellationToken token)
{
QueryPropertiesRequestDto request = new QueryPropertiesRequestDto {
FileshareId = _fileShareId,
Path = path
};
return await SendFileShareMessage<QueryPropertiesResponseDto, QueryPropertiesRequestDto>(request, "QueryPropertiesAsync", token);
}
public async Task (IEnumerable<string> paths, CancellationToken token)
{
await DeleteAsync(paths, false, token).ConfigureAwait(false);
}
public async Task (IEnumerable<string> paths, CancellationToken token)
{
await DeleteAsync(paths, true, token).ConfigureAwait(false);
}
private async Task (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> < , >(TRequest request, string command, CancellationToken token) where : 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 (FileShareOperationErrorCode responseError)
{
return responseError == FileShareOperationErrorCode.Unknown;
}
internal static string (string path)
{
return path.Replace('/', '\\');
}
internal string (string path)
{
return (path.Length > 1 && path.EndsWith("\\")) ? path.Remove(path.Length - 1) : path;
}
}
}