public override async Task DownloadAsync(TransferSpec spec) { spec.Progress.Tries++; ConfirmSchemeSupported(spec.Uri.Scheme); using (var webClient = _webClientFactory.CreateExport()) using (SetupTransferProgress(webClient.Value, spec.Progress)) await TryDownloadAsync(spec, webClient.Value, GetTmpFile(spec)).ConfigureAwait(false); }
public override void Download(TransferSpec spec) { spec.Progress.Tries++; ConfirmSchemeSupported(spec.Uri.Scheme); spec.Progress.ResetZsyncLoopInfo(); ProcessExitResult(_zsyncLauncher.RunAndProcess(spec.Progress, new Uri(FixUrl(spec.Uri)), spec.LocalFile.ToString(), spec.CancellationToken), spec); VerifyIfNeeded(spec, spec.LocalFile); }
public override async Task UploadAsync(TransferSpec spec) { spec.Progress.Tries++; ConfirmSchemeSupported(spec.Uri.Scheme); ProcessExitResult( await _rsyncLauncher.RunAndProcessAsync(spec.Progress, spec.LocalFile.ToString(), spec.Uri.ToString()) .ConfigureAwait(false), spec); }
static void ProcessExitResult(ProcessExitResultWithOutput result, TransferSpec spec) { if (result.ExitCode == 0) return; throw new RsyncException( String.Format("Did not exit gracefully (PID: {0}, Status: {1}). {2}", result.Id, result.ExitCode, CreateTransferExceptionMessage(spec)), result.StandardOutput + result.StandardError, result.StartInfo.Arguments); }
static void ProcessExitResult(ProcessExitResultWithOutput result, TransferSpec spec) { switch (result.ExitCode) { case 0: break; case -1: throw new RsyncSoftException( String.Format("Aborted/Killed (PID: {0}, Status: {1}). {2}", result.Id, result.ExitCode, CreateTransferExceptionMessage(spec)), result.StandardOutput + result.StandardError, result.StartInfo.Arguments); case 5: throw new RsyncSoftException( String.Format("Server full (PID: {0}, Status: {1}). {2}", result.Id, result.ExitCode, CreateTransferExceptionMessage(spec)), result.StandardOutput + result.StandardError, result.StartInfo.Arguments); case 10: throw new RsyncSoftException( String.Format("Connection refused (PID: {0}, Status: {1}). {2}", result.Id, result.ExitCode, CreateTransferExceptionMessage(spec)), result.StandardOutput + result.StandardError, result.StartInfo.Arguments); case 12: throw new RsyncSoftException( String.Format("Could not retrieve file (PID: {0}, Status: {1}). {2}", result.Id, result.ExitCode, CreateTransferExceptionMessage(spec)), result.StandardOutput + result.StandardError, result.StartInfo.Arguments); case 14: throw new RsyncSoftException( String.Format("Could not retrieve file due to IPC error (PID: {0}, Status: {1}). {2}", result.Id, result.ExitCode, CreateTransferExceptionMessage(spec)), result.StandardOutput + result.StandardError, result.StartInfo.Arguments); case 23: throw new RsyncSoftException( String.Format("Could not retrieve file (PID: {0}, Status: {1}). {2}", result.Id, result.ExitCode, CreateTransferExceptionMessage(spec)), result.StandardOutput + result.StandardError, result.StartInfo.Arguments); case 24: throw new RsyncSoftException( String.Format("Could not retrieve file (PID: {0}, Status: {1}). {2}", result.Id, result.ExitCode, CreateTransferExceptionMessage(spec)), result.StandardOutput + result.StandardError, result.StartInfo.Arguments); case 30: throw new RsyncSoftException( String.Format("Could not retrieve file due to Timeout (PID: {0}, Status: {1}). {2}", result.Id, result.ExitCode, CreateTransferExceptionMessage(spec)), result.StandardOutput + result.StandardError, result.StartInfo.Arguments); case 35: throw new RsyncSoftException( String.Format("Could not retrieve file due to Timeout (PID: {0}, Status: {1}). {2}", result.Id, result.ExitCode, CreateTransferExceptionMessage(spec)), result.StandardOutput + result.StandardError, result.StartInfo.Arguments); default: throw new RsyncException( String.Format("Did not exit gracefully (PID: {0}, Status: {1}). {2}", result.Id, result.ExitCode, CreateTransferExceptionMessage(spec)), result.StandardOutput + result.StandardError, result.StartInfo.Arguments); } }
public override async Task DownloadAsync(TransferSpec spec, Action<int, double, int> progressReporting) { spec.Progress.Tries++; ConfirmSchemeSupported(spec.Uri.Scheme); var obs = new Subject<ITransferStatus2>(); var act = new Action<int, double, int>( (bytesReceived, progress, speed) => obs.OnNext(new TransferStatus2(progress, bytesReceived, speed))); //DoDownload(spec).ToObservable().Catch((ex, e2) => obs.OnError(ex)); }
protected void Wrap(Action action, TransferSpec spec) { OnStart(spec); try { action(); } catch (Exception e) { OnError(spec, e); throw; } OnFinished(spec); }
protected async Task Wrap(Func<Task> task, TransferSpec spec) { OnStart(spec); try { await task().ConfigureAwait(false); } catch (Exception e) { OnError(spec, e); throw; } OnFinished(spec); }
public override async Task DownloadAsync(TransferSpec spec) { spec.Progress.Tries++; ConfirmSchemeSupported(spec.Uri.Scheme); ProcessExitResult( await _rsyncLauncher.RunAndProcessAsync(spec.Progress, spec.Uri.ToString(), spec.LocalFile.ToString(), spec.CancellationToken) .ConfigureAwait(false), spec); VerifyIfNeeded(spec, spec.LocalFile); }
static void ProcessExitResult(ProcessExitResultWithOutput result, TransferSpec spec) { if (spec.Progress.ZsyncIncompatible) { throw new ZsyncIncompatibleException( String.Format("Zsync Incompatible (PID: {0}, Status: {1}). {2}", result.Id, result.ExitCode, CreateTransferExceptionMessage(spec)), result.StandardOutput + result.StandardError, result.StartInfo.Arguments); } switch (result.ExitCode) { case 0: break; case -1: { if (spec.Progress.ZsyncLoopCount >= 2) { throw new ZsyncLoopDetectedException( String.Format("Loop detected, aborted transfer (PID: {0}, Status: {1}). {2}", result.Id, result.ExitCode, CreateTransferExceptionMessage(spec)), result.StandardOutput + result.StandardError, result.StartInfo.Arguments); } throw new ZsyncSoftException( String.Format("Aborted/Killed (PID: {0}, Status: {1}). {2}", result.Id, result.ExitCode, CreateTransferExceptionMessage(spec)), result.StandardOutput + result.StandardError, result.StartInfo.Arguments); } case 1: throw new ZsyncSoftException( String.Format( "Could not retrieve file due to protocol error (not a zsync file?) (PID: {0}, Status: {1}). {2}", result.Id, result.ExitCode, CreateTransferExceptionMessage(spec)), result.StandardOutput + result.StandardError, result.StartInfo.Arguments); case 2: throw new ZsyncSoftException( String.Format("Connection reset (PID: {0}, Status: {1}). {2}", result.Id, result.ExitCode, CreateTransferExceptionMessage(spec)), result.StandardOutput + result.StandardError, result.StartInfo.Arguments); case 3: throw new ZsyncSoftException( String.Format("Could not retrieve file (PID: {0}, Status: {1}). {2}", result.Id, result.ExitCode, CreateTransferExceptionMessage(spec)), result.StandardOutput + result.StandardError, result.StartInfo.Arguments); default: throw new ZsyncException( String.Format("Did not exit gracefully (PID: {0}, Status: {1}). {2}", result.Id, result.ExitCode, CreateTransferExceptionMessage(spec)), result.StandardOutput + result.StandardError, result.StartInfo.Arguments); } }
static void GenerateDownloadException(TransferSpec spec, WebException e) { // TODO: Or should we rather abstract this away into the downloader exceptions instead? var r = e.Response as HttpWebResponse; if (r != null) { throw new HttpDownloadException(e.Message + ". " + CreateTransferExceptionMessage(spec), e) { StatusCode = r.StatusCode }; } var r2 = e.Response as FtpWebResponse; if (r2 != null) { throw new FtpDownloadException(e.Message + ". " + CreateTransferExceptionMessage(spec), e) { StatusCode = r2.StatusCode }; } throw new DownloadException(e.Message + ". " + CreateTransferExceptionMessage(spec), e); }
static async Task TryUploadAsync(TransferSpec spec, IWebClient webClient) { try { if (!string.IsNullOrWhiteSpace(spec.Uri.UserInfo)) webClient.SetAuthInfo(spec.Uri); using (webClient.HandleCancellationToken(spec)) await webClient.UploadFileTaskAsync(spec.Uri, spec.LocalFile.ToString()).ConfigureAwait(false); } catch (OperationCanceledException e) { throw CreateTimeoutException(spec, e); } catch (WebException e) { var cancelledEx = e.InnerException as OperationCanceledException; if (cancelledEx != null) throw CreateTimeoutException(spec, cancelledEx); if (e.Status == WebExceptionStatus.RequestCanceled) throw CreateTimeoutException(spec, e); GenerateUploadException(spec, e); } }
async Task TryDownloadAsync(TransferSpec spec, IWebClient webClient, IAbsoluteFilePath tmpFile) { try { tmpFile.RemoveReadonlyWhenExists(); if (!string.IsNullOrWhiteSpace(spec.Uri.UserInfo)) webClient.SetAuthInfo(spec.Uri); using (webClient.HandleCancellationToken(spec)) await webClient.DownloadFileTaskAsync(spec.Uri, tmpFile.ToString()).ConfigureAwait(false); VerifyIfNeeded(spec, tmpFile); _fileOps.Move(tmpFile, spec.LocalFile); } catch (OperationCanceledException e) { _fileOps.DeleteIfExists(tmpFile.ToString()); throw CreateTimeoutException(spec, e); } catch (WebException e) { _fileOps.DeleteIfExists(tmpFile.ToString()); var cancelledEx = e.InnerException as OperationCanceledException; if (cancelledEx != null) throw CreateTimeoutException(spec, cancelledEx); if (e.Status == WebExceptionStatus.RequestCanceled) throw CreateTimeoutException(spec, e); GenerateDownloadException(spec, e); } }
protected abstract void OnFinished(TransferSpec spec);
static IAbsoluteFilePath GetPathFromUri(TransferSpec spec) { return spec.Uri.LocalPath.ToAbsoluteFilePath(); // spec.Uri.IsAbsoluteUri ? spec.Uri.AbsolutePath : spec.Uri.ToString(); }
public override Task UploadAsync(TransferSpec spec) { spec.Progress.Tries++; ConfirmSchemeSupported(spec.Uri.Scheme); return _fileCopy.CopyFileAsync(spec.LocalFile, GetPathFromUri(spec)); }
public override void Upload(TransferSpec spec) { spec.Progress.Tries++; ConfirmSchemeSupported(spec.Uri.Scheme); _fileCopy.CopyFile(spec.LocalFile, GetPathFromUri(spec)); }
protected override void OnError(TransferSpec spec, Exception e) { this.Logger().Error("Failed upload of {0} to {1} ({2})", spec.LocalFile, spec.Uri, e.Message); }
protected override void OnFinished(TransferSpec spec) { this.Logger().Info("Finished upload of {0} to {1}", spec.LocalFile, spec.Uri); }
public override async Task DownloadAsync(TransferSpec spec) { spec.Progress.Tries++; ConfirmSchemeSupported(spec.Uri.Scheme); await _fileCopy.CopyFileAsync(GetPathFromUri(spec), spec.LocalFile).ConfigureAwait(false); VerifyIfNeeded(spec, spec.LocalFile); }
static DownloadException CreateTimeoutException(TransferSpec spec, Exception e) { return new DownloadSoftException("", null, null, new TimeoutException( String.Format("The operation timedout in {0}. {1}", timeout, CreateTransferExceptionMessage(spec)), e)); }
protected abstract void OnStart(TransferSpec spec);
static IAbsoluteFilePath GetTmpFile(TransferSpec spec) { return (spec.LocalFile + ".sixtmp").ToAbsoluteFilePath(); }
public override void Download(TransferSpec spec) { spec.Progress.Tries++; // Wrapping in Task.Run could solve deadlock issues Task.Run(() => DownloadAsync(spec)).WaitAndUnwrapException(); }
protected static string CreateTransferExceptionMessage(TransferSpec spec) { return String.Format("After {0}% ({1}B) for {2}", spec.Progress.Progress, spec.Progress.FileSizeTransfered, spec.Uri.AuthlessUri()); }
public abstract void Upload(TransferSpec spec);
public override void Upload(TransferSpec spec) { spec.Progress.Tries++; ConfirmSchemeSupported(spec.Uri.Scheme); ProcessExitResult( _rsyncLauncher.RunAndProcess(spec.Progress, spec.LocalFile.ToString(), spec.Uri.ToString()), spec); }
protected abstract void OnError(TransferSpec spec, Exception exception);
public abstract Task UploadAsync(TransferSpec spec);
public override void Download(TransferSpec spec) { spec.Progress.Tries++; ConfirmSchemeSupported(spec.Uri.Scheme); _fileCopy.CopyFile(GetPathFromUri(spec), spec.LocalFile); VerifyIfNeeded(spec, spec.LocalFile); }