BlobSync.AzureOps.UploadBytes C# (CSharp) Method

UploadBytes() private method

private UploadBytes ( RemainingBytes remainingBytes, string localFilePath, string containerName, string blobName, bool testMode = false ) : List
remainingBytes BlobSync.Datatypes.RemainingBytes
localFilePath string
containerName string
blobName string
testMode bool
return List
        private List<UploadedBlock> UploadBytes(RemainingBytes remainingBytes, string localFilePath, string containerName, string blobName, bool testMode = false)
        {
            var uploadedBlockList = new List<UploadedBlock>();

            try
            {
                CloudBlockBlob blob = null;
                if (!testMode)
                {
                    var client = AzureHelper.GetCloudBlobClient();
                    var container = client.GetContainerReference(containerName);
                    container.CreateIfNotExists();
                    blob = container.GetBlockBlobReference(blobName);

                }
                var blockCount =
                    Math.Round((double)(remainingBytes.EndOffset - remainingBytes.BeginOffset + 1) /
                               (double)ConfigHelper.SignatureSize, MidpointRounding.AwayFromZero);

                using (var stream = new FileStream(localFilePath, FileMode.Open))
                {
                    for (var offset = remainingBytes.BeginOffset; offset <= remainingBytes.EndOffset; )
                    {
                        var sizeToRead = offset + ConfigHelper.SignatureSize <= remainingBytes.EndOffset
                            ? ConfigHelper.SignatureSize
                            : remainingBytes.EndOffset - offset + 1;

                        if (sizeToRead == 0)
                        {
                            var error = "";
                        }

                        // seek to the offset we need. Dont forget remaining bytes may be bigger than the signature size
                        // we want to deal with.
                        stream.Seek(offset, SeekOrigin.Begin);
                        var bytesToRead = new byte[sizeToRead];
                        var bytesRead = stream.Read(bytesToRead, 0, (int)sizeToRead);

                        var sig = CommonOps.GenerateBlockSig(bytesToRead, offset, (int)sizeToRead, 0);
                        var blockId = Convert.ToBase64String(sig.MD5Signature);
                        var isDupe = uploadedBlockList.Any(ub => ub.BlockId == blockId);

                        if (!testMode)
                        {
                            // only upload bytes IF another block hasn't already covered it.
                            // unlikely situation I think, but possibly going to happen for
                            // VM images etc where there is lots of "blank space".

                            if (!isDupe)
                            {
                                // yes, putting into memory stream is probably a waste here.
                                using (var ms = new MemoryStream(bytesToRead))
                                {
                                    var options = new BlobRequestOptions() { ServerTimeout = new TimeSpan(0, 90, 0) };
                                    blob.PutBlock(blockId, ms, null, null, options);
                                }
                            }
                        }

                        // store the block id that is associated with this byte range.
                        uploadedBlockList.Add(new UploadedBlock()
                        {
                            BlockId = blockId,
                            Offset = offset,
                            Sig = sig,
                            Size = bytesRead,
                            IsNew = true,
                            IsDuplicate = isDupe
                        });

                        offset += sizeToRead;

                    }
                }

            }
            catch (ArgumentException ex)
            {
                // probably bad container.
                Console.WriteLine("Argument Exception " + ex.ToString());
            }
            finally
            {

            }

            return uploadedBlockList;
        }