FileTransferService
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace Relativity.Transfer.Http
{
internal sealed class FileTransferService : IFileTransferService, IDisposable
{
private readonly IFileSystemService fileSystemService;
private readonly RelativityConnectionInfo connectionInfo;
private readonly IUserManagerService userManagerService;
private readonly IFileIoService fileIoService;
private readonly int chunkSize;
private readonly bool createDirectories;
private bool disposed;
private readonly ITransferLog logger;
public FileTransferService(RelativityConnectionInfo connectionInfo, Workspace workspace, HttpClientConfiguration configuration, IFileSystemService fileSystemService, ITransferLog log)
{
if (connectionInfo == null)
throw new ArgumentNullException("connectionInfo");
if (configuration == null)
throw new ArgumentNullException("configuration");
if (fileSystemService == null)
throw new ArgumentNullException("fileSystemService");
if (log == null)
throw new ArgumentNullException("log");
this.connectionInfo = connectionInfo;
this.fileSystemService = fileSystemService;
HttpServiceFactory httpServiceFactory = new HttpServiceFactory(connectionInfo, configuration, log);
userManagerService = httpServiceFactory.CreateUserManagerService();
fileIoService = httpServiceFactory.CreateFileIoService(workspace);
logger = log;
chunkSize = ((configuration.HttpChunkSize > 0) ? configuration.HttpChunkSize : 102400);
createDirectories = configuration.CreateDirectories;
}
~FileTransferService()
{
Dispose(false);
}
public void Login()
{
userManagerService.Login();
}
public Task RemoveTempFile(int workspaceId, string fileGuid, CancellationToken token)
{
return fileIoService.RemoveTempFile(workspaceId, fileGuid, token);
}
[AsyncStateMachine(typeof(<UploadAsync>d__12))]
public Task<TransferPathResult> UploadAsync(TransferPath path, CancellationToken token, IProgress<LargeFileProgressEventArgs> progress)
{
<UploadAsync>d__12 stateMachine = default(<UploadAsync>d__12);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<TransferPathResult>.Create();
stateMachine.<>4__this = this;
stateMachine.path = path;
stateMachine.token = token;
stateMachine.progress = progress;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
[AsyncStateMachine(typeof(<DownloadAsync>d__13))]
public Task<TransferPathResult> DownloadAsync(TransferPath path, CancellationToken token, IProgress<LargeFileProgressEventArgs> progress)
{
<DownloadAsync>d__13 stateMachine = default(<DownloadAsync>d__13);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<TransferPathResult>.Create();
stateMachine.<>4__this = this;
stateMachine.path = path;
stateMachine.token = token;
stateMachine.progress = progress;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
[AsyncStateMachine(typeof(<GetDiskSpaceReportAsync>d__14))]
public Task<string> GetDiskSpaceReportAsync(int workspaceId, CancellationToken token)
{
<GetDiskSpaceReportAsync>d__14 stateMachine = default(<GetDiskSpaceReportAsync>d__14);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<string>.Create();
stateMachine.<>4__this = this;
stateMachine.workspaceId = workspaceId;
stateMachine.token = token;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!disposed) {
if (disposing) {
if (userManagerService != null)
userManagerService.Dispose();
if (fileIoService != null)
fileIoService.Dispose();
}
disposed = true;
}
}
private byte[] FileData(Stream sourceStream, int chunkNumber, ref int readLength)
{
byte[] array = null;
int num = 0;
try {
array = new byte[readLength + 1];
num = (readLength = sourceStream.Read(array, 0, readLength));
return array;
} catch (OverflowException) {
logger.LogInformation($"""{chunkNumber}", Array.Empty<object>());
logger.LogInformation($"""{readLength}", Array.Empty<object>());
logger.LogInformation($"""{num}", Array.Empty<object>());
logger.LogInformation((array != null) ? $"""{array.Length}" : "Buffer was not initialized.", Array.Empty<object>());
throw;
}
}
private int DetermineReadLength(Stream sourceStream)
{
int result = chunkSize;
if (sourceStream.Length < chunkSize)
result = Convert.ToInt32(sourceStream.Length);
return result;
}
private string DetermineFileName(TransferPath path)
{
if (!string.IsNullOrEmpty(path.TargetFileName))
return path.TargetFileName;
string fileName = fileSystemService.GetFileName(path.TargetPath);
if (!string.IsNullOrEmpty(fileName))
return fileName;
return Guid.NewGuid().ToString();
}
}
}