Esempio n. 1
0
        private BlobReference[] WriteBlobs(
            IReadOnlyCollection <Blob> blobs,
            Fuzzy scanFuzzy,
            Func <string, Stream> openRead)
        {
            var currentBlobReferences = _currentSnapshotId == null
                ? new Dictionary <string, BlobReference>()
                : GetSnapshot(_currentSnapshotId.Value).BlobReferences
                                        .ToDictionary(br => br.Name, br => br);

            return(blobs
                   .AsParallel()
                   .Select(blob =>
            {
                currentBlobReferences.TryGetValue(
                    blob.Name,
                    out var current);

                if (!scanFuzzy.IsMatch(blob.Name) &&
                    current != null &&
                    current.ToBlob().Equals(blob))
                {
                    return current;
                }

                // Known blobs should be encrypted using the same nonce
                var nonce = current?.Nonce
                            ?? AesGcmCrypto.GenerateNonce();

                using var stream = openRead(blob.Name);

                var blobReference = new BlobReference(
                    blob.Name,
                    blob.LastWriteTimeUtc,
                    nonce,
                    WriteContent(nonce, stream));

                _probe.StoredBlob(blobReference);

                return blobReference;
            })
                   .OrderBy(blobReference => blobReference.Name)
                   .ToArray());
        }