protected override Task SerializeToStreamAsync(Stream stream, TransportContext context) { var ps = new ProgressStream(stream, x => this.progress.Report(new UploadProgressInfo(x, this.totalBytesToSend))); var task = this.innerContent.CopyToAsync(ps, context); if (!this.totalBytesToSend.HasValue) task = task.Done( () => this.progress.Report(new UploadProgressInfo(ps.BytesWritten, ps.BytesWritten)), CancellationToken.None); return task; }
private async Task OnGetResponseCompleted(Task<WebResponse> task) { if (this._isCancelled) { return; } // get the response HttpWebResponse response; try { response = (HttpWebResponse)await task.ConfigureAwait(false); } catch (Exception e) { this.InvokeOnErrorHandler(ErrorMessages.WebPageLoadError); return; } if (response.StatusCode != HttpStatusCode.OK) { this.InvokeOnErrorHandler((int)response.StatusCode + " " + response.StatusDescription); return; } // response stream Stream stream; if (this.OnProgressChanged != null && response.Headers.AllKeys.Any(key => key == "Content-Length")) { var progressStream = new ProgressStream(response.GetResponseStream(), response.ContentLength); progressStream.OnProgressChanged = v => this.InvokeInUiThread(() => this.OnProgressChanged(v)); stream = progressStream; } else { stream = response.GetResponseStream(); } if (!this._isCancelled) { this.OnStreamDownloaded(stream); } }
public async Task ExportAsync( string path, T file, XmlWriterSettings settings, IProgress <float> progress, CancellationToken cancel) { if (settings == null) { settings = GetDefaultWriterSettings(); } settings.Async = true; long currentBytes = 0L; using (ProgressStream f = new ProgressStream(new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None), null, null)) using (XmlWriter r = XmlWriter.Create(f, settings)) { if (progress != null) { float length = f.Length; f.SetWriteProgress(new BasicProgress <int>(i => { currentBytes += i; progress.Report(currentBytes / length); })); } await ExportAsync(file, r, cancel); } }
private Task <Lite <DisconnectedImportEntity> > UploadDatabase() { pbUploading.Minimum = 0; pbUploading.Maximum = fi.Length; return(Task.Factory.StartNew(() => { using (FileStream fs = fi.OpenRead()) using (ProgressStream ps = new ProgressStream(fs)) { ps.ProgressChanged += (s, args) => pbUploading.Dispatcher.BeginInvoke(() => pbUploading.Value = ps.Position); UploadDatabaseResult result = transferServer.UploadDatabase(new UploadDatabaseRequest { FileName = fi.Name, Length = fi.Length, Stream = ps, Machine = DisconnectedMachineEntity.Current, User = UserHolder.Current.ToLite(), }); return result.UploadStatistics; } })); }
public FileInfo downloadFile(String fileId) { File fileToDownload = findFile(fileId); FileInfo newFile = createTempFile(fileToDownload); Endpoint p = createEndpoint(pkgInfo, fileId); using (FileStream decryptedFileStream = newFile.OpenWrite()) { for (int i = 1; i <= fileToDownload.Parts; i++) { FileInfo tmpFile = createTempFile(); using (FileStream segmentStream = tmpFile.OpenWrite()) { using (ProgressStream progressStream = new ProgressStream(segmentStream, progress, "Downloading", fileToDownload.FileSize, 0)) { DownloadSegment(progressStream, p, i); } } String dataToDecrypt = System.IO.File.ReadAllText(tmpFile.FullName); using (FileStream segmentStream = tmpFile.OpenRead()) { DecryptFile(segmentStream, decryptedFileStream); } } } return(newFile); }
/// <inheritdoc /> public HttpContent ConvertToHttpContent(Type typeToConvert, object content) { if (CanConvertToHttpContent(typeToConvert, content)) { var bitmapSource = content as BitmapSource; if (bitmapSource != null) { var httpBehaviour = HttpBehaviour.Current; var configuration = httpBehaviour.GetConfig <BitmapSourceConfiguration>(); Stream stream = new MemoryStream(); var encoder = configuration.CreateEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmapSource)); encoder.Save(stream); stream.Seek(0, SeekOrigin.Begin); // Add progress support, if this is enabled if (httpBehaviour.UseProgressStream) { var progressStream = new ProgressStream(stream) { BytesRead = (sender, eventArgs) => { httpBehaviour.UploadProgress?.Invoke((float)eventArgs.StreamPosition / eventArgs.StreamLength); } }; stream = progressStream; } var httpContent = new StreamContent(stream); httpContent.Headers.Add("Content-Type", "image/" + configuration.Format.ToString().ToLowerInvariant()); return(httpContent); } } return(null); }
private void StartDownloading() { var file = transferServer.EndExportDatabase(new DownloadDatabaseRequests { User = UserHolder.Current?.ToLite(), DownloadStatistics = currentLite }); FileTools.CreateParentDirectory(DisconnectedClient.DownloadBackupFile); pbDownloading.Minimum = 0; pbDownloading.Maximum = file.Length; Task.Factory.StartNew(() => { using (var ps = new ProgressStream(file.Stream)) { ps.ProgressChanged += (s, args) => Dispatcher.Invoke(() => pbDownloading.Value = ps.Position); using (FileStream fs = File.OpenWrite(DisconnectedClient.DownloadBackupFile)) ps.CopyTo(fs); } Dispatcher.Invoke(() => { MessageBox.Show(Window.GetWindow(this), "You have successfully downloaded a local database. \r\nThe application will turn off now.\r\nNext time you start it up, choose Run disconnected.", "Download complete", MessageBoxButton.OK); }); Environment.Exit(0); }); }
/// <summary> /// Downloads a blob to a local file asynchronously. /// </summary> /// <param name="localFile">The local file.</param> /// <exception cref="System.Exception">BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer.</exception> public ICancellableAsyncResult DownloadBlobAsync(string localFile) { lock (workingLock) { if (!working) { working = true; } else { throw new Exception("BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer."); } } // Create an async op in order to raise the events back to the client on the correct thread. asyncOp = AsyncOperationManager.CreateOperation(blob); TransferType = TransferTypeEnum.Download; fileName = localFile; this.blob.FetchAttributes(); FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read); ProgressStream pstream = new ProgressStream(fs); pstream.ProgressChanged += pstream_ProgressChanged; pstream.SetLength(this.blob.Properties.Length); this.blob.ServiceClient.ParallelOperationThreadCount = 10; asyncresult = this.blob.BeginDownloadToStream(pstream, BlobTransferCompletedCallback, new BlobTransferAsyncState(this.blob, pstream)); return(asyncresult); }
private void init(ProgressStream stream) { stream.ReadCallback = readBytes; Progress = delegate { }; }
public static RSSFile Download(string url, ProgressDelegate listener, ICancellable cancelObject) { try { Logger.Log("Downloading RSS file from {0}", url); var request = HttpWebRequest.Create(url); request.Timeout = 15000; using (var response = request.GetResponse()) { using (var stream = new ProgressStream(response.GetResponseStream(), response.ContentLength, listener, cancelObject)) { try { return(new RSSFile(stream)); } finally { stream.Close(); } } } } catch (Exception e) { Logger.Log("Caught exception: {0}", e.ToString()); return(null); } }
public FileInfo DownloadFile(string fileId) { var fileToDownload = FindFile(fileId); var newFile = CreateTempFile(fileToDownload); var p = CreateEndpoint(_pkgInfo, fileId); using var decryptedFileStream = newFile.OpenWrite(); for (var i = 1; i <= fileToDownload.Parts; i++) { var tmpFile = CreateTempFile(); using (var segmentStream = tmpFile.OpenWrite()) { using var progressStream = new ProgressStream(segmentStream, _progress, "Downloading", fileToDownload.FileSize, 0); DownloadSegment(progressStream, p, i); } using (var segmentStream = tmpFile.OpenRead()) { DecryptFile(segmentStream, decryptedFileStream); } } return(newFile); }
public void TestRead() { using (var stream = new ProgressStream(new DummyNetworkStream(), Update)) { var buffer = new byte[1024]; int expected = 517; stream.Source.Write(buffer, 0, expected); stream.Source.Position = 0; progress = 0; int n = stream.Read(buffer, 0, buffer.Length); Assert.AreEqual(expected, n, "nread"); Assert.AreEqual(expected, progress, "progress"); } using (var stream = new ProgressStream(new DummyNetworkStream(), Update)) { var buffer = new byte[1024]; int expected = 517; stream.Source.Write(buffer, 0, expected); stream.Source.Position = 0; progress = 0; int n = stream.Read(buffer, 0, buffer.Length, CancellationToken.None); Assert.AreEqual(expected, n, "nread"); Assert.AreEqual(expected, progress, "progress"); } }
public void UpdateLengthIfInputStreamGrowsAfterStartReading() { using (var stream = new MemoryStream()) { long initialLength = 100; long length = initialLength; byte[] buffer = new byte[initialLength]; stream.Write(buffer, 0, buffer.Length); using (var progress = new ProgressStream(stream)) { progress.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e) { var t = sender as ProgressStream; if (e.PropertyName == Utils.NameOf(() => t.Position)) { Assert.That(t.Position, Is.LessThanOrEqualTo(length)); Assert.That(t.Length, Is.LessThanOrEqualTo(length)); } }; progress.Read(buffer, 0, buffer.Length / 2); stream.Write(buffer, 0, buffer.Length); length = length + buffer.Length; progress.Read(buffer, 0, buffer.Length / 2); progress.Read(buffer, 0, buffer.Length / 2); progress.Read(buffer, 0, buffer.Length / 2); stream.Write(buffer, 0, buffer.Length); length = length + buffer.Length; progress.Read(buffer, 0, buffer.Length); } } }
internal static void WriteToRequest(HttpWebRequest request, ProgressStream progressStream) { log.Info("Writing data to request synchronously."); if (progressStream == null) { return; } try { log.Info("Reporting progress requested."); var buffer = new byte[CHUNK_SIZE]; using (var req = request.GetRequestStream()) { progressStream.Position = 0; int read = 0; for (int i = 0; i < progressStream.Length; i += read) { read = progressStream.Read(buffer, 0, CHUNK_SIZE); req.Write(buffer, 0, read); req.Flush(); // flushing is required or else we jump to 100% very fast } progressStream.ReportMaxValue(); } } catch (WebException ex) { HandleWebException(ex); } }
public void reports_progress_via_progress_t() { var callback = new ProgressReporter(); Mock <Stream> mockStream; var buffer = CreateStreamMock(out mockStream); using (var progressStream = new ProgressStream(mockStream.Object, callback)) { callback.CallbackCount.Should().Be(0); callback.LastPercentage.Should().Be(0); while (true) { var bytesRead = progressStream.Read(buffer, 0, buffer.Length); if (bytesRead <= 0) { break; } } } mockStream.Verify(); callback.CallbackCount.Should().BeGreaterThan(0); callback.LastPercentage.Should().Be(100f); }
public Stream GetContent(FileInfoContract source, ProgressProxy progress) { try { var result = gateway.GetContentAsync(rootName, source.Id).Result; if (!result.CanSeek) { var bufferStream = new MemoryStream(); result.CopyTo(bufferStream, MAX_BULKDOWNLOAD_SIZE); bufferStream.Seek(0, SeekOrigin.Begin); result.Dispose(); result = bufferStream; } result = new ProgressStream(result, progress); if (!string.IsNullOrEmpty(encryptionKey)) { result = result.Decrypt(encryptionKey); } return(result); } catch (AggregateException ex) when(ex.InnerExceptions.Count == 1) { throw ex.InnerExceptions[0]; } }
/// <summary> /// Write the literal to the specified stream. /// </summary> /// <remarks> /// Writes the literal to the specified stream. /// </remarks> /// <param name="stream">The stream.</param> /// <param name="cancellationToken">The cancellation token.</param> public void WriteTo(ImapStream stream, CancellationToken cancellationToken) { if (Type == ImapLiteralType.String) { var bytes = (byte[])Literal; stream.Write(bytes, 0, bytes.Length, cancellationToken); stream.Flush(cancellationToken); return; } if (Type == ImapLiteralType.MimeMessage) { var message = (MimeMessage)Literal; using (var s = new ProgressStream(stream, update)) { message.WriteTo(format, s, cancellationToken); s.Flush(cancellationToken); return; } } var literal = (Stream)Literal; var buf = new byte[4096]; int nread; while ((nread = literal.Read(buf, 0, buf.Length)) > 0) { stream.Write(buf, 0, nread, cancellationToken); } stream.Flush(cancellationToken); }
public virtual void Encrypt(IDataStore sourceFile, Stream destinationStream, EncryptionParameters encryptionParameters, AxCryptOptions options, IProgressContext progress) { if (sourceFile == null) { throw new ArgumentNullException("sourceFile"); } if (destinationStream == null) { throw new ArgumentNullException("destinationStream"); } if (encryptionParameters == null) { throw new ArgumentNullException("encryptionParameters"); } if (progress == null) { throw new ArgumentNullException("progress"); } using (Stream sourceStream = new ProgressStream(sourceFile.OpenRead(), progress)) { using (IAxCryptDocument document = New <AxCryptFactory>().CreateDocument(encryptionParameters)) { document.FileName = sourceFile.Name; document.CreationTimeUtc = sourceFile.CreationTimeUtc; document.LastAccessTimeUtc = sourceFile.LastAccessTimeUtc; document.LastWriteTimeUtc = sourceFile.LastWriteTimeUtc; document.EncryptTo(sourceStream, destinationStream, options); } } }
/// <summary> /// Get the Content-stream of the HttpContent, wrap it in ProgressStream if this is specified /// </summary> /// <param name="httpContent"></param> /// <returns>Stream from ReadAsStreamAsync eventually wrapped by ProgressStream</returns> public static async Task <Stream> GetContentStream(this HttpContent httpContent) { var contentStream = await httpContent.ReadAsStreamAsync().ConfigureAwait(false); var hasContentLength = httpContent.Headers.Any(h => h.Key.Equals("Content-Length")); if (hasContentLength) { var contentLength = int.Parse(httpContent.Headers.First(h => h.Key.Equals("Content-Length")).Value.First()); var httpBehaviour = HttpBehaviour.Current; // Add progress support, if this is enabled if (httpBehaviour.UseProgressStream && contentLength > 0) { long position = 0; var progressStream = new ProgressStream(contentStream) { BytesRead = (sender, eventArgs) => { position += eventArgs.BytesMoved; httpBehaviour.DownloadProgress?.Invoke((float)position / contentLength); } }; contentStream = progressStream; } } return(contentStream); }
public static RSSFile Download(string url, ProgressDelegate listener, ICancellable cancelObject) { try { var request = HttpWebRequest.Create(url); request.Timeout = 15000; using (var response = request.GetResponse()) { using (var stream = new ProgressStream(response.GetResponseStream(), response.ContentLength, listener, cancelObject)) { try { return(new RSSFile(stream)); } finally { stream.Close(); } } } } catch (IOException) { return(null); } catch (WebException) { return(null); } }
public void DownloadBlobAsync(ICloudBlob blob, string LocalFile) { // The class currently stores state in class level variables so calling UploadBlobAsync or DownloadBlobAsync a second time will cause problems. // A better long term solution would be to better encapsulate the state, but the current solution works for the needs of my primary client. // Throw an exception if UploadBlobAsync or DownloadBlobAsync has already been called. lock (WorkingLock) { if (!Working) { Working = true; } else { throw new Exception("BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer."); } } // Create an async op in order to raise the events back to the client on the correct thread. asyncOp = AsyncOperationManager.CreateOperation(blob); TransferType = TransferTypeEnum.Download; m_Blob = blob; m_FileName = LocalFile; m_Blob.FetchAttributes(); FileStream fs = new FileStream(m_FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read); ProgressStream pstream = new ProgressStream(fs); pstream.ProgressChanged += pstream_ProgressChanged; pstream.SetLength(m_Blob.Properties.Length); m_Blob.ServiceClient.DefaultRequestOptions.ParallelOperationThreadCount = 10; asyncresult = m_Blob.BeginDownloadToStream(pstream, BlobTransferCompletedCallback, new BlobTransferAsyncState(m_Blob, pstream)); }
/// <summary> /// Uploads a file to an Azure blob asynchronously. /// </summary> /// <param name="localFile">The local file.</param> /// <exception cref="System.Exception">BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer.</exception> public ICancellableAsyncResult UploadBlobAsync(string localFile) { lock (workingLock) { if (!working) { working = true; } else { throw new Exception("BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer."); } } // Attempt to open the file first so that we throw an exception before getting into the async work using (var fstemp = new FileStream(localFile, FileMode.Open, FileAccess.Read)) { } // Create an async op in order to raise the events back to the client on the correct thread. asyncOp = AsyncOperationManager.CreateOperation(this.blob); TransferType = TransferTypeEnum.Upload; fileName = localFile; var file = new FileInfo(fileName); long fileSize = file.Length; FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); ProgressStream pstream = new ProgressStream(fs); pstream.ProgressChanged += pstream_ProgressChanged; pstream.SetLength(fileSize); this.blob.ServiceClient.ParallelOperationThreadCount = 10; asyncresult = this.blob.BeginUploadFromStream(pstream, BlobTransferCompletedCallback, new BlobTransferAsyncState(this.blob, pstream)); return(asyncresult); }
public void PositionTest() { var mockedStream = new Mock <Stream>(); FileTransmissionEvent transmissionEvent = new FileTransmissionEvent(this.transmissionType, this.filename); transmissionEvent.TransmissionStatus += delegate(object sender, TransmissionProgressEventArgs args) { if (args.Length != null && args.Length != this.length) { this.lengthCalls++; this.length = (long)args.Length; } if (args.ActualPosition != null) { this.positionCalls++; } }; mockedStream.Setup(s => s.SetLength(It.IsAny <long>())); mockedStream.SetupProperty(s => s.Position); using (ProgressStream progress = new ProgressStream(mockedStream.Object, transmissionEvent)) { progress.SetLength(100); Assert.AreEqual(1, this.lengthCalls); Assert.AreEqual(0, this.positionCalls); progress.Position = 50; progress.Position = 50; Assert.AreEqual(1, this.positionCalls); progress.Position = 55; Assert.AreEqual(2, this.positionCalls); Assert.AreEqual(1, this.lengthCalls); } }
public static async Task Main() { Write("Enter URL:> "); var url = ReadLine(); var uri = new Uri(url); var request = HttpWebRequestExt.Create(uri) .DoNotTrack() .Accept(Any); using var response = await request .GetAsync() .ConfigureAwait(false); var contentType = Lookup(response.ContentType); WriteLine($"Status {response.StatusCode}"); WriteLine($"Content-Type {contentType.Value}"); WriteLine($"Content-Length {response.ContentLength}"); var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); var fileName = Path.Combine(desktop, PathExt.FixPath(uri.PathAndQuery.Substring(1))); var fileExt = Path.GetExtension(fileName).Substring(1); if (contentType?.PrimaryExtension is object && !contentType.Extensions.Contains(fileExt)) { fileName += "." + contentType.PrimaryExtension; } var file = new FileInfo(fileName); file.Directory.Create(); using var outStream = file.Create(); using var body = response.GetResponseStream(); using var progStream = new ProgressStream(body, response.ContentLength, new ConsoleProgress(), false); await progStream.CopyToAsync(outStream) .ConfigureAwait(false); }
/// <summary> /// Asynchronously write the literal to the specified stream. /// </summary> /// <remarks> /// Asynchronously writes the literal to the specified stream. /// </remarks> /// <param name="stream">The stream.</param> /// <param name="cancellationToken">The cancellation token.</param> public async Task WriteToAsync(ImapStream stream, CancellationToken cancellationToken) { if (Type == ImapLiteralType.String) { var bytes = (byte[])Literal; await stream.WriteAsync(bytes, 0, bytes.Length, cancellationToken).ConfigureAwait(false); await stream.FlushAsync(cancellationToken).ConfigureAwait(false); return; } //if (Type == ImapLiteralType.Stream) { // var literal = (Stream) Literal; // var buf = new byte[4096]; // int nread; // while ((nread = await literal.ReadAsync (buf, 0, buf.Length, cancellationToken).ConfigureAwait (false)) > 0) // await stream.WriteAsync (buf, 0, nread, cancellationToken).ConfigureAwait (false); // await stream.FlushAsync (cancellationToken).ConfigureAwait (false); // return; //} var message = (MimeMessage)Literal; using (var s = new ProgressStream(stream, update)) { await message.WriteToAsync(format, s, cancellationToken).ConfigureAwait(false); await s.FlushAsync(cancellationToken).ConfigureAwait(false); } }
public void reports_progress_via_action() { var callbackCount = 0; var lastPercentage = 0f; Action <ProgressStream.Progress> callback = progress => { callbackCount++; lastPercentage = progress.Percentage; Debug.WriteLine(lastPercentage); }; Mock <Stream> mockStream; var buffer = CreateStreamMock(out mockStream); using (var progressStream = new ProgressStream(mockStream.Object, callback)) { callbackCount.Should().Be(0); lastPercentage.Should().Be(0); while (true) { var bytesRead = progressStream.Read(buffer, 0, buffer.Length); if (bytesRead <= 0) { break; } } } mockStream.Verify(); callbackCount.Should().BeGreaterThan(0); lastPercentage.Should().Be(100f); }
public void Position() { var mockedStream = new Mock <Stream>(); mockedStream.Setup(s => s.SetLength(It.IsAny <long>())); mockedStream.SetupProperty(s => s.Position); using (var underTest = new ProgressStream(mockedStream.Object)) { underTest.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e) { var p = sender as ProgressStream; if (e.PropertyName == Utils.NameOf(() => p.Length)) { this.lengthCalls++; } if (e.PropertyName == Utils.NameOf(() => p.Position)) { this.positionCalls++; } }; underTest.SetLength(100); Assert.AreEqual(1, this.lengthCalls); Assert.AreEqual(0, this.positionCalls); underTest.Position = 50; underTest.Position = 50; Assert.AreEqual(1, this.positionCalls); underTest.Position = 55; Assert.AreEqual(2, this.positionCalls); Assert.AreEqual(1, this.lengthCalls); } }
public void ConstructorExtractsLengthOfGivenStream() { long length = 10; using (var underTest = new ProgressStream(Mock.Of <Stream>(s => s.Length == length))) { Assert.That(underTest.Length, Is.EqualTo(length)); } }
//public StandardResponse upload(FileInfo rawFile, String filename, String signature, long fileSize, String uploadType) public StandardResponse Upload(FileInfo rawFile, UploadFileRequest requestData, string filename, long uploadedSoFar, long fullFillSize) { using var fileStream = rawFile.OpenRead(); using var progressStream = new ProgressStream(fileStream, _progress, "Uploading", fullFillSize, uploadedSoFar); var uploadedBytes = UploadSegment(progressStream, requestData, filename, rawFile.Length); return(_response); }
public void TestCanReadWriteSeek() { using (var stream = new ProgressStream(Stream.Null, Update)) { Assert.AreEqual(Stream.Null.CanRead, stream.CanRead); Assert.AreEqual(Stream.Null.CanWrite, stream.CanWrite); Assert.IsFalse(stream.CanSeek); Assert.AreEqual(Stream.Null.CanTimeout, stream.CanTimeout); } }
public void RunJob() { var filename = Path.Combine(tmpfolder, Id + ".tbm"); try { if (!Directory.Exists(tmpfolder)) { Directory.CreateDirectory(tmpfolder); } var backuper = new BackupManager(filename, currentWebConfigPath); backuper.ProgressChanged += (o, e) => { Percentage = Math.Max(0, Math.Min((int)e.Progress / 2, 50)); }; backuper.Save(Tenant); using (var stream = new FileStream(filename, FileMode.Open)) using (var progressStream = new ProgressStream(stream)) { progressStream.OnReadProgress += (o, e) => { Percentage = Math.Max(0, Math.Min(100, 50 + e / 2)); }; var uploadname = string.Format("{0}-{1:yyyyMMdd-HHmmss}.zip", CoreContext.TenantManager.GetTenant(Tenant).TenantDomain, DateTime.UtcNow).ToLowerInvariant(); ExpireDate = DateTime.UtcNow.Add(expire); Status = GetStore().SavePrivate(string.Empty, uploadname, progressStream, ExpireDate); } IsCompleted = true; Percentage = 100; NotifyHelper.SendAboutBackupCompleted(Tenant, userId, (string)Status, ExpireDate); } catch (Exception e) { Error = e; log.Error(e); } finally { try { if (File.Exists(filename)) { File.Delete(filename); } } catch (Exception e) { log.Error(e); } } }
public void TestSeek() { using (var stream = new ProgressStream(new DummyNetworkStream(), Update)) { Assert.Throws <NotSupportedException> (() => stream.Seek(0, SeekOrigin.Begin)); Assert.Throws <NotSupportedException> (() => stream.Position = 500); Assert.AreEqual(0, stream.Position); Assert.AreEqual(0, stream.Length); } }
private long UploadFile(long parentId, string path,DoWorkEventArgs e) { if (_worker.CancellationPending){ e.Cancel = true; return -1; } ContentServiceClient contentService=new ContentServiceClient(); DocumentManagementClient docMan = new DocumentManagementClient(); FileInfo info = new FileInfo(path); _worker.ReportProgress(_counter, new {CurrentFile=info.Name}); FileAtts fileAtts = new FileAtts { CreatedDate = info.CreationTime, FileName = info.Name, FileSize = info.Length, ModifiedDate = info.LastWriteTime }; var stream = new FileStream(path,FileMode.Open,FileAccess.Read,FileShare.Read); ProgressStream pStream = new ProgressStream(stream); pStream.ProgressChanged += pStream_ProgressChanged; try { bool isVersionAdded = false; var res = docMan.GetNodeByName(ref _otAuthentication, parentId, info.Name); string contextId; if (res == null) { contextId=docMan.CreateDocumentContext(ref _otAuthentication, parentId, info.Name, null, false, null); } else { contextId = docMan.AddVersionContext(ref _otAuthentication, res.ID, null); isVersionAdded = true; } var id = contentService.UploadContent(ref _otAuthentication, contextId, fileAtts, pStream); ++_counter; _worker.ReportProgress(_counter,new {Message=isVersionAdded ? string.Format("{{{0}}} - Version {1} added.", id, info.Name) : string.Format("{{{0}}} - Document {1} added.", id, info.Name)}); } catch (Exception ex) { _worker.ReportProgress(_counter, new {Message=ex.ToString()}); } finally { contentService.Close(); docMan.Close(); stream.Close(); } return -1; }
public void UploadBlobAsync(ICloudBlob blob, string LocalFile) { // The class currently stores state in class level variables so calling UploadBlobAsync or DownloadBlobAsync a second time will cause problems. // A better long term solution would be to better encapsulate the state, but the current solution works for the needs of my primary client. // Throw an exception if UploadBlobAsync or DownloadBlobAsync has already been called. lock (WorkingLock) { if (!Working) Working = true; else throw new Exception("BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer."); } // Attempt to open the file first so that we throw an exception before getting into the async work using (FileStream fstemp = new FileStream(LocalFile, FileMode.Open, FileAccess.Read)) { } // Create an async op in order to raise the events back to the client on the correct thread. asyncOp = AsyncOperationManager.CreateOperation(blob); TransferType = TransferTypeEnum.Upload; m_Blob = blob; m_FileName = LocalFile; var file = new FileInfo(m_FileName); long fileSize = file.Length; FileStream fs = new FileStream(m_FileName, FileMode.Open, FileAccess.Read, FileShare.Read); ProgressStream pstream = new ProgressStream(fs); pstream.ProgressChanged += pstream_ProgressChanged; pstream.SetLength(fileSize); m_Blob.ServiceClient.ParallelOperationThreadCount = 10; asyncresult = m_Blob.BeginUploadFromStream(pstream, BlobTransferCompletedCallback, new BlobTransferAsyncState(m_Blob, pstream)); }
void pStream_ProgressChanged(object sender, ProgressStream.ProgressChangedEventArgs e) { _worker.ReportProgress(_counter,new {e.Length,e.BytesRead}); }
void Data (FormatOptions options, MimeMessage message, CancellationToken cancellationToken, ITransferProgress progress) { var response = SendCommand ("DATA", cancellationToken); if (response.StatusCode != SmtpStatusCode.StartMailInput) throw new SmtpCommandException (SmtpErrorCode.UnexpectedStatusCode, response.StatusCode, response.Response); if (progress != null) { var ctx = new SendContext (progress, null); using (var stream = new ProgressStream (Stream, ctx.Update)) { using (var filtered = new FilteredStream (stream)) { filtered.Add (new SmtpDataFilter ()); message.WriteTo (options, filtered, cancellationToken); filtered.Flush (); } } } else { using (var filtered = new FilteredStream (Stream)) { filtered.Add (new SmtpDataFilter ()); message.WriteTo (options, filtered, cancellationToken); filtered.Flush (); } } Stream.Write (EndData, 0, EndData.Length, cancellationToken); Stream.Flush (cancellationToken); response = Stream.ReadResponse (cancellationToken); switch (response.StatusCode) { default: throw new SmtpCommandException (SmtpErrorCode.MessageNotAccepted, response.StatusCode, response.Response); case SmtpStatusCode.AuthenticationRequired: throw new ServiceNotAuthenticatedException (response.Response); case SmtpStatusCode.Ok: OnMessageSent (new MessageSentEventArgs (message, response.Response)); break; } }
void Bdat (FormatOptions options, MimeMessage message, CancellationToken cancellationToken, ITransferProgress progress) { long size; using (var measure = new MeasuringStream ()) { message.WriteTo (options, measure, cancellationToken); size = measure.Length; } var bytes = Encoding.UTF8.GetBytes (string.Format ("BDAT {0} LAST\r\n", size)); Stream.Write (bytes, 0, bytes.Length, cancellationToken); if (progress != null) { var ctx = new SendContext (progress, size); using (var stream = new ProgressStream (Stream, ctx.Update)) { message.WriteTo (options, stream, cancellationToken); stream.Flush (cancellationToken); } } else { message.WriteTo (options, Stream, cancellationToken); Stream.Flush (cancellationToken); } var response = Stream.ReadResponse (cancellationToken); switch (response.StatusCode) { default: throw new SmtpCommandException (SmtpErrorCode.MessageNotAccepted, response.StatusCode, response.Response); case SmtpStatusCode.AuthenticationRequired: throw new ServiceNotAuthenticatedException (response.Response); case SmtpStatusCode.Ok: OnMessageSent (new MessageSentEventArgs (message, response.Response)); break; } }
/// <summary> /// Downloads a blob to a local file asynchronously. /// </summary> /// <param name="localFile">The local file.</param> /// <exception cref="System.Exception">BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer.</exception> public ICancellableAsyncResult DownloadBlobAsync(string localFile) { lock (workingLock) { if (!working) working = true; else throw new Exception("BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer."); } // Create an async op in order to raise the events back to the client on the correct thread. asyncOp = AsyncOperationManager.CreateOperation(blob); TransferType = TransferTypeEnum.Download; fileName = localFile; this.blob.FetchAttributes(); FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read); ProgressStream pstream = new ProgressStream(fs); pstream.ProgressChanged += pstream_ProgressChanged; pstream.SetLength(this.blob.Properties.Length); this.blob.ServiceClient.ParallelOperationThreadCount = 10; asyncresult = this.blob.BeginDownloadToStream(pstream, BlobTransferCompletedCallback, new BlobTransferAsyncState(this.blob, pstream)); return asyncresult; }
protected override void ProcessRecord() { ProviderInfo destinationProviderInfo; var destinationPath = SessionState.Path.GetResolvedProviderPathFromPSPath(Destination, out destinationProviderInfo); var sources = Path.Select(each => { ProviderInfo spi; var sourceFiles = SessionState.Path.GetResolvedProviderPathFromPSPath(each, out spi); return new SourceSet { ProviderInfo = spi, SourcePaths = sourceFiles.ToArray(), }; }).ToArray(); var providerInfos = sources.Select(each => each.ProviderInfo).Distinct().ToArray(); if (providerInfos.Length > 1 && providerInfos[0] == destinationProviderInfo) { Console.WriteLine("Using regular copy-item"); base.ProcessRecord(); return; } bool force = Force; // resolve where files are going to go. var destinationLocation = GetLocationResolver(destinationProviderInfo).GetLocation(destinationPath[0]); var copyOperations = ResolveSourceLocations(sources, destinationLocation).ToArray(); if (copyOperations.Length > 1 && destinationLocation.IsFile) { // source can only be a single file. throw new CoAppException("Destination file exists--multiple source files specified."); } foreach (var operation in copyOperations) { Console.WriteLine("COPY '{0}' to '{1}'", operation.Source.AbsolutePath, operation.Destination.AbsolutePath); if (!force) { if (operation.Destination.Exists) { throw new CoAppException("Destination file '{0}' exists. Must use -force to override".format(operation.Destination.AbsolutePath)); } } using (var inputStream = new ProgressStream(operation.Source.Open(FileMode.Open))) { using (var outputStream = new ProgressStream(operation.Destination.Open(FileMode.Create))) { inputStream.BytesRead += (sender, args) => {}; outputStream.BytesWritten += (sender, args) => {}; inputStream.CopyTo(outputStream); } } } Console.WriteLine("Done."); }
private void StartDownloading() { var file = transferServer.EndExportDatabase(new DownloadDatabaseRequests { User = UserEntity.Current.ToLite(), DownloadStatistics = currentLite }); FileTools.CreateParentDirectory(DisconnectedClient.DownloadBackupFile); pbDownloading.Minimum = 0; pbDownloading.Maximum = file.Length; Task.Factory.StartNew(() => { using (var ps = new ProgressStream(file.Stream)) { ps.ProgressChanged += (s, args) => Dispatcher.Invoke(() => pbDownloading.Value = ps.Position); using (FileStream fs = File.OpenWrite(DisconnectedClient.DownloadBackupFile)) ps.CopyTo(fs); } Dispatcher.Invoke(() => { MessageBox.Show(Window.GetWindow(this), "You have successfully downloaded a local database. \r\nThe application will turn off now.\r\nNext time you start it up, choose Run disconnected.", "Download complete", MessageBoxButton.OK); }); Environment.Exit(0); }); }
protected override void ProcessRecord() { ProviderInfo destinationProviderInfo; var destinationLocation = ResolveDestinationLocation(out destinationProviderInfo); var sources = Path.Select(each => { ProviderInfo spi; var sourceFiles = SessionState.Path.GetResolvedProviderPathFromPSPath(each, out spi); return new SourceSet { ProviderInfo = spi, SourcePaths = sourceFiles.ToArray(), }; }).ToArray(); var providerInfos = sources.Select(each => each.ProviderInfo).Distinct().ToArray(); if (providerInfos.Length == 1 && providerInfos[0] == destinationProviderInfo) { WriteVerbose("Using regular copy-item"); base.ProcessRecord(); return; } bool force = Force; var copyOperations = ResolveSourceLocations(sources, destinationLocation).ToArray(); if (copyOperations.Length > 1 && destinationLocation.IsFile) { // source can only be a single file. ThrowTerminatingError(new ErrorRecord(new DirectoryNotFoundException(), "0", ErrorCategory.InvalidArgument, null)); //WriteError(new ErrorRecord(new ClrPlusException("Destination file exists--multiple source files specified."), "ErrorId", ErrorCategory.InvalidArgument, null)); return; } var s = new Stopwatch(); s.Start(); for (var i = 0; i < copyOperations.Length; i++) { var operation = copyOperations[i]; WriteProgress(CreateProgressRecord(1, "Copy", "Copying item {0} of {1}".format(i, copyOperations.Length), 100 * (double)i/copyOperations.Length)); //Console.WriteLine("COPY '{0}' to '{1}'", operation.Source.AbsolutePath, operation.Destination.AbsolutePath); if (!force) { if (operation.Destination.Exists) { ThrowTerminatingError(new ErrorRecord(new ClrPlusException("Destination file '{0}' exists. Must use -force to override".format(operation.Destination.AbsolutePath)), "ErrorId", ErrorCategory.ResourceExists, null)); return; } } using (var inputStream = new ProgressStream(operation.Source.Open(FileMode.Open))) { using (var outputStream = new ProgressStream(operation.Destination.Open(FileMode.Create))) { var inputLength = inputStream.Length; inputStream.BytesRead += (sender, args) => {}; CopyOperation operation1 = operation; outputStream.BytesWritten += (sender, args) => WriteProgress(CreateProgressRecord(2, "Copy", "Copying '{0}' to '{1}'".format(operation1.Source.AbsolutePath, operation1.Destination.AbsolutePath), 100*(double)args.StreamPosition/inputLength, 1)); Task t = inputStream.CopyToAsync(outputStream, _cancellationToken.Token, false); try { t.RunSynchronously(); } catch (TaskCanceledException e) { return; } } } WriteProgress(CreateCompletedProgressRecord(2, "Copy", "Copying '{0}' to '{1}'".format(operation.Source.AbsolutePath, operation.Destination.AbsolutePath), 1)); // WriteVerbose("Copy from {0} to {1}".format(operation.Source.AbsolutePath, operation.Destination.AbsolutePath)); } WriteProgress(CreateCompletedProgressRecord(1, "Copy", "Copy finished")); s.Stop(); WriteVerbose("Completed in {0}".format(s.Elapsed)); }
void init(ProgressStream stream) { stream.ReadCallback = readBytes; Progress = delegate { }; }
ProgressStreamContent(ProgressStream stream, int bufferSize) : base(stream, bufferSize) { init(stream); }
ProgressStreamContent(ProgressStream stream) : base(stream) { init(stream); }
private Task<Lite<DisconnectedImportEntity>> UploadDatabase() { pbUploading.Minimum = 0; pbUploading.Maximum = fi.Length; return Task.Factory.StartNew(() => { using (FileStream fs = fi.OpenRead()) using (ProgressStream ps = new ProgressStream(fs)) { ps.ProgressChanged += (s, args) => pbUploading.Dispatcher.BeginInvoke(() => pbUploading.Value = ps.Position); UploadDatabaseResult result = transferServer.UploadDatabase(new UploadDatabaseRequest { FileName = fi.Name, Length = fi.Length, Stream = ps, Machine = DisconnectedMachineEntity.Current, User = UserEntity.Current.ToLite(), }); return result.UploadStatistics; } }); }
/// <summary> /// Write the literal to the specified stream. /// </summary> /// <remarks> /// Writes the literal to the specified stream. /// </remarks> /// <param name="stream">The stream.</param> /// <param name="cancellationToken">The cancellation token.</param> public void WriteTo (ImapStream stream, CancellationToken cancellationToken) { if (Type == ImapLiteralType.String) { var bytes = (byte[]) Literal; stream.Write (bytes, 0, bytes.Length, cancellationToken); stream.Flush (cancellationToken); return; } if (Type == ImapLiteralType.MimeMessage) { var message = (MimeMessage) Literal; using (var s = new ProgressStream (stream, update)) { message.WriteTo (format, s, cancellationToken); s.Flush (cancellationToken); return; } } var literal = (Stream) Literal; var buf = new byte[4096]; int nread; while ((nread = literal.Read (buf, 0, buf.Length)) > 0) stream.Write (buf, 0, nread, cancellationToken); stream.Flush (cancellationToken); }
/// <summary> /// Uploads a file to an Azure blob asynchronously. /// </summary> /// <param name="localFile">The local file.</param> /// <exception cref="System.Exception">BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer.</exception> public ICancellableAsyncResult UploadBlobAsync(string localFile) { lock (workingLock) { if (!working) working = true; else throw new Exception("BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer."); } // Attempt to open the file first so that we throw an exception before getting into the async work using (var fstemp = new FileStream(localFile, FileMode.Open, FileAccess.Read)) { } // Create an async op in order to raise the events back to the client on the correct thread. asyncOp = AsyncOperationManager.CreateOperation(this.blob); TransferType = TransferTypeEnum.Upload; fileName = localFile; var file = new FileInfo(fileName); long fileSize = file.Length; FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); ProgressStream pstream = new ProgressStream(fs); pstream.ProgressChanged += pstream_ProgressChanged; pstream.SetLength(fileSize); this.blob.ServiceClient.ParallelOperationThreadCount = 10; asyncresult = this.blob.BeginUploadFromStream(pstream, BlobTransferCompletedCallback, new BlobTransferAsyncState(this.blob, pstream)); return asyncresult; }