private async Task <FileTransferResponseCodeTypes> AddFileToDfsAsync(IFileTransferInformation fileTransferInformation) { var responseCode = FileTransferResponseCodeTypes.Finished; try { Cid cid; using (var fileStream = File.Open(fileTransferInformation.TempPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { cid = await _dfs.AddAsync(fileStream, fileTransferInformation.FileOutputPath).ConfigureAwait(false); } fileTransferInformation.DfsHash = cid.Encode(); Logger.Information($"Added File Name {fileTransferInformation.FileOutputPath} to DFS, Hash: {fileTransferInformation.DfsHash}"); } catch (Exception e) { Logger.Error(e, "Failed to handle file download OnSuccess {0}", fileTransferInformation.CorrelationId.Id); responseCode = FileTransferResponseCodeTypes.Failed; } finally { fileTransferInformation.Delete(); } return(responseCode); }
private async Task <FileTransferResponseCodeTypes> AddFileToDfsAsync(IFileTransferInformation fileTransferInformation) { var responseCode = FileTransferResponseCodeTypes.Finished; try { IFileSystemNode fileSystemNode; await using (var fileStream = File.Open(fileTransferInformation.TempPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { fileSystemNode = await _dfsService.UnixFsApi.AddAsync(fileStream, fileTransferInformation.FileOutputPath, new AddFileOptions { Hash = _hashProvider.HashingAlgorithm.Name }).ConfigureAwait(false); } fileTransferInformation.DfsHash = fileSystemNode.Id.Encode(); Logger.Information( $"Added File Name {fileTransferInformation.FileOutputPath} to DFS, Hash: {fileTransferInformation.DfsHash}"); } catch (Exception e) { Logger.Error(e, "Failed to handle file download OnSuccess {0}", fileTransferInformation.CorrelationId.Id); responseCode = FileTransferResponseCodeTypes.Failed; } finally { fileTransferInformation.Delete(); } return(responseCode); }
/// <summary>Called when [success] on file transfer.</summary> /// <param name="fileTransferInformation">The file transfer information.</param> private async Task OnSuccessAsync(IFileTransferInformation fileTransferInformation) { var addFileResponseCode = AddFileToDfsAsync(fileTransferInformation).ConfigureAwait(false); var message = GetResponse(fileTransferInformation, await addFileResponseCode); var protocolMessage = message.ToProtocolMessage(PeerSettings.PeerId, fileTransferInformation.CorrelationId); // Send Response var responseMessage = new MessageDto( protocolMessage, fileTransferInformation.RecipientId ); await fileTransferInformation.RecipientChannel.WriteAndFlushAsync(responseMessage).ConfigureAwait(false); }
/// <param name="fileTransferInformation">The file transfer information.</param> /// <param name="responseCode">The response code.</param> private AddFileToDfsResponse GetResponse(IFileTransferInformation fileTransferInformation, Enumeration responseCode) { Logger.Information("File transfer response code: " + responseCode); if (responseCode == FileTransferResponseCodeTypes.Successful) { Logger.Information($"Initialised file transfer, FileName: {fileTransferInformation.FileOutputPath}, Chunks: {fileTransferInformation.MaxChunk.ToString()}"); } var dfsHash = responseCode == FileTransferResponseCodeTypes.Finished ? fileTransferInformation.DfsHash : string.Empty; // Build Response var response = new AddFileToDfsResponse { ResponseCode = ByteString.CopyFrom((byte)responseCode.Id), DfsHash = dfsHash }; return(response); }