Exemplo n.º 1
0
        private void Download(System.ComponentModel.DoWorkEventArgs e)
        {
            // Initializing service proxy
            var cloudProxy = new CloudServiceClient();

            try
            {
                // Initializing client-side decryption
                var clientCypher = new XTEA();

                // Getting decryption key
                clientCypher.SetKey(Encoding.ASCII.GetBytes(cloudProxy.GetKey()));

                // Initializing stream from service
                var length = cloudProxy.DownloadFile(ref _cloudFileName, out var inputStream);

                // Write stream to disk
                using (var writeStream = new FileStream(_localFilePath, FileMode.CreateNew, FileAccess.Write))
                {
                    // Initializing buffer
                    var buffer = new byte[ChunkSize];

                    // Initializing boolean determining whether it's the last chunk
                    var lastChunk = false;

                    do
                    {
                        // Read bytes from input stream
                        var bytesRead = inputStream.Read(buffer, 0, ChunkSize);
                        if (bytesRead == 0)
                        {
                            break;
                        }

                        // Check if it is the last chunk
                        if (bytesRead < ChunkSize)
                        {
                            var temp = new byte[bytesRead];
                            Array.Copy(buffer, temp, bytesRead);
                            buffer    = temp;
                            lastChunk = true;
                        }

                        // Decrypt data from the buffer
                        var decryptedBuffer = clientCypher.Decrypt(buffer);

                        // Write bytes to output stream
                        writeStream.Write(decryptedBuffer, 0, decryptedBuffer.Length);

                        // Update progress bar
                        if (lastChunk)
                        {
                            backgroundWorker.ReportProgress(100);
                        }
                        else
                        {
                            backgroundWorker.ReportProgress((int)(writeStream.Position * 100 / length));
                        }

                        // Check for cancellation
                        if (!backgroundWorker.CancellationPending)
                        {
                            continue;
                        }

                        e.Cancel = true;
                        inputStream.Dispose();
                        return;
                    } while (!lastChunk);

                    writeStream.Close();
                }

                // Deallocate stream
                inputStream.Dispose();
            }
            catch (Exception exception)
            {
                throw new ArgumentException(exception.Message);
            }
            finally
            {
                // Close service proxy
                cloudProxy.Close();
            }
        }