public void Send(PNGXPayload payload, string fileName, PublishCompleteHandler complete) { try { var rawPayload = payload.Serialize(); using (var client = this.clientFactory.Create()) using (var stream = new MemoryStream(rawPayload)) { client.Connect(); this.EnsureFreeSpace(client); client.UploadFile( stream, Path.Combine(this.config.SFTPPublishPath, fileName), length => { if (length == (ulong)rawPayload.Length) { complete(PublishCompletionStatus.Success, $"Successfully uploaded {fileName}"); } }); } } catch (Exception ex) { complete(PublishCompletionStatus.Failure, $"Failed to upload file: {fileName}, got error: {ex.ToString()}"); } }
// TODO: we should have an enqueue which writes the image to disk for publishing. // and a disk watcher that then enqueues and gets the connection and performs the publishing off // of the UI thread. This will allow capture to write to a share and a separate instance on another // machine doing the publishing. public void Send(PNGXPayload pngPayload, string fileName, PublishCompleteHandler complete) { var completionCount = 2; PublishCompletionStatus status = PublishCompletionStatus.None; var startTime = DateTime.UtcNow; void decrementAndTrySendCompletion(PublishCompletionStatus s, string message) { this.logger.Verbose(message); status = s == PublishCompletionStatus.Failure || status == PublishCompletionStatus.Failure ? PublishCompletionStatus.Failure : s != PublishCompletionStatus.None ? s : status; if (--completionCount < 1) { var elapsedTime = DateTime.UtcNow - startTime; var totalMbBytesSent = ((double)pngPayload.Data.Length / (1024 * 1024)).ToString("0.###"); complete(status, $"Final publish status is {status}, time elapsed: {elapsedTime.TotalSeconds} seconds, image size {totalMbBytesSent}Mb, last message: {message}"); } } void safeDecrementAndTrySendCompletion(PublishCompletionStatus s, string message) { // the underlying publishers can execute completion on threads other than the compute bound UI thread. // to be safe we always dispatch back to the UI thread. App.Current.Dispatcher.Invoke(() => decrementAndTrySendCompletion(s, message)); } if (this.EnableDiskPublishing) { this.disk.Send( pngPayload, fileName, (s, message) => safeDecrementAndTrySendCompletion(s, $"Completed writing file: {fileName}, status: {s}, message: {message}")); } else { decrementAndTrySendCompletion(PublishCompletionStatus.None, "decrementing logger completion count, disk publishing is disabled"); } if (this.EnableFtpPublishing) { this.sftp.Send( pngPayload, fileName, (s, message) => safeDecrementAndTrySendCompletion(s, $"Completed sending {fileName} over SFTP, status: {s}, message: {message}")); } else { decrementAndTrySendCompletion(PublishCompletionStatus.None, "decrementing logger completion count, ftp publishing is disabled"); } }
public void Send(PNGXPayload payload, string fileName, PublishCompleteHandler complete) { if (complete == null) { throw new ArgumentNullException(nameof(complete)); } var fullPath = Path.Combine(this.config.DiskPath, fileName); try { File.WriteAllBytes(fullPath, payload.Serialize()); complete(PublishCompletionStatus.Success, $"Completed writing file {fileName} with success"); } catch (System.IO.IOException ex) { complete(PublishCompletionStatus.Failure, $"failed writing file {fileName} got error {ex.ToString()}"); } }