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"); } }
private void HandleScreenCapture() { // NOTE: at this point we assume that this panel is visible for screen capture this.logger.Verbose("Taking screen capture for {0}", this.Config.PrettyName); try { byte[] imageBytes = this.TakeScreenshot(); if (imageBytes.Length == 0) { this.logger.Warn($"Something wrong with capture for panel {this.Config.PrettyName}, image capture has zero bytes!"); this.CleanupCaptureRun(success: false); return; } string name = Guid.NewGuid().ToString() + ".pngx"; var elapsedTime = DateTime.UtcNow - new DateTime(1970, 1, 1); var payload = new PNGXPayload() { Author = this.Config.Author, BirthtimeMs = (long)elapsedTime.TotalMilliseconds, Data = imageBytes, Name = this.Config.Name, Url = this.Config.Url }; this.capturePublisher.Send(payload, name, (status, message) => { this.logger.Verbose("Capture publish complete for {0}, status: {1}, message: {2}", this.Config.PrettyName, status, message); }); this.CleanupCaptureRun(success: true); } catch (InvalidOperationException ex) { this.logger.Error("Could not capture visual for panel {0}, got error {1}", this.Config.PrettyName, ex.ToString()); this.CleanupCaptureRun(success: false); } catch (Exception ex) { this.logger.Error("Could not capture visual for panel {0}, got unexpected error {1}", this.Config.PrettyName, ex.ToString()); this.CleanupCaptureRun(success: false); throw ex; } }
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()}"); } }