Exemple #1
0
        async Task <byte[]> _downloadChunkBasic(byte[] indexId, uint replication)
        {
            var locator = generator.DeriveLocator(indexId, replication);

            byte[] data;
            try
            {
                data = await withServiceFromPool(serviceUsage.Down, async svc =>
                {
                    var res = svc.GetBody(locator);
                    return(await Task.FromResult(res));
                });
            }
            catch (Exception ex)
            {
                throw new ServiceException($"service failed when downloading index '{indexId.ToHexString()}' with r = {replication}", ex);
            }

            if (data == null)
            {
                throw new FileNotFoundException($"data not found for segment with index '{indexId.ToHexString()}' with r = {replication}");
            }
            byte[] decrypted = null;
            try
            {
                decrypted = encryption.Decrypt(data, locator);
            }
            catch (Exception ex)
            {
                throw new InvalidDataException($"data invalid for segment with index '{indexId.ToHexString()}' with r = {replication}", ex);
            }
            var unpacked = OverallSegment.FromByteArray(decrypted);

            return(unpacked.Data.GetDecompressed());
        }
Exemple #2
0
        async Task <UploadResult> _uploadChunkBasic(byte[] bytes, byte[] indexId, uint replication)
        {
            var locator    = this.generator.DeriveLocator(indexId, replication);
            var compressed = bytes.GetCompressed();
            var packed     = new OverallSegment {
                Data = compressed
            };

            packed.AddPadding();
            var encrypted = encryption.Encrypt(packed.ToByteArray(), locator);

            try
            {
                var ok = await withServiceFromPool(serviceUsage.Up, async svc =>
                {
                    var randomSubject = Cryptography.GetRandomBytes(32).ToHexString();
                    var res           = svc.Upload(new Chunk(locator, randomSubject, encrypted));
                    return(await Task.FromResult(res));
                });

                return(new UploadResult {
                    OK = ok, CompressedData = compressed
                });
            }
            catch (Exception ex)
            {
                throw new ServiceException($"service failed when uploading index '{indexId.ToHexString()}' with r = {replication}", ex);
            }
        }