/// <summary>
        /// Persist a stream given a blobId.
        /// </summary>
        /// <param name="blobId"></param>
        /// <param name="fileName"></param>
        /// <param name="sourceStream"></param>
        /// <returns></returns>
        private FileSystemBlobDescriptor InnerPersistOfBlob(BlobId blobId, FileNameWithExtension fileName, Stream sourceStream)
        {
            FileSystemBlobDescriptor descriptor = new FileSystemBlobDescriptor()
            {
                BlobId = blobId,
                FileNameWithExtension = fileName,
                Timestamp             = DateTime.Now,
                ContentType           = MimeTypes.GetMimeType(fileName)
            };
            string destinationFileName = GetFileNameFromBlobIdAndRemoveDuplicates(blobId, descriptor.FileNameWithExtension);

            Logger.Debug($"File {fileName} was assigned blob {blobId} and will be saved in file {destinationFileName}");

            using (var md5 = MD5.Create())
                using (var fileStream = new FileStream(destinationFileName, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    fileStream.Seek(0, SeekOrigin.Begin);
                    fileStream.SetLength(0);

                    Byte[] buffer = new Byte[8192];
                    Int32  read;
                    Int64  length = 0;
                    while ((read = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        md5.TransformBlock(buffer, 0, read, buffer, 0);
                        fileStream.Write(buffer, 0, read);
                        length += read;
                    }
                    md5.TransformFinalBlock(buffer, 0, 0);
                    descriptor.Length = length;
                    descriptor.Md5    = BitConverter.ToString(md5.Hash).Replace("-", "");
                }
            Logger.Info($"Blob {blobId} saved in file {destinationFileName} with hash {descriptor.Md5} and length {descriptor.Length}");
            return(descriptor);
        }
 public void RawStore(BlobId blobId, IBlobDescriptor descriptor)
 {
     Logger.InfoFormat("Ask for raw storage of blob {0} for file {1}", blobId, descriptor.FileNameWithExtension);
     using (var stream = descriptor.OpenRead())
     {
         InnerPersistOfBlob(blobId, descriptor.FileNameWithExtension, stream);
         FileSystemBlobDescriptor newDescriptor = new FileSystemBlobDescriptor()
         {
             BlobId                = blobId,
             ContentType           = descriptor.ContentType,
             FileNameWithExtension = descriptor.FileNameWithExtension,
             Length                = descriptor.Length,
             Md5       = descriptor.Hash.ToString(),
             Timestamp = DateTime.UtcNow,
         };
         _mongodDbFileSystemBlobDescriptorStorage.SaveDescriptor(newDescriptor);
     }
 }