Exemplo n.º 1
0
            public TestContext()
            {
                // data
                UtcNow = new DateTimeOffset(2015, 1, 2, 3, 4, 5, 6, TimeSpan.Zero);
                Content = "foobar";
                Container = TestSupport.GetTestContainer();
                UploadRequest = new UploadRequest
                {
                    ConnectionString = TestSupport.ConnectionString,
                    Container = Container,
                    ContentType = "text/plain",
                    PathFormat = "testpath/{0}.txt",
                    UploadDirect = true,
                    UploadLatest = true,
                    Stream = new MemoryStream(Encoding.UTF8.GetBytes(Content)),
                    Trace = TextWriter.Null
                };
                GetLatestRequest = new GetLatestRequest
                {
                    ConnectionString = UploadRequest.ConnectionString,
                    Container = UploadRequest.Container,
                    PathFormat = UploadRequest.PathFormat,
                    Trace = TextWriter.Null
                };

                // dependencies
                SystemTime = new Mock<ISystemTime>();
                PathBuilder = new PathBuilder();

                // setup
                SystemTime.Setup(x => x.UtcNow).Returns(() => UtcNow);

                // target
                Target = new Client(SystemTime.Object, PathBuilder);
            }
Exemplo n.º 2
0
        public async Task<UriResult> GetLatestUriResultAsync(GetLatestRequest request)
        {
            var context = new CloudContext(request.ConnectionString, request.Container);

            var latestPath = _pathBuilder.GetLatest(request.PathFormat);
            var latestBlob = context.BlobContainer.GetBlockBlobReference(latestPath);

            if (!await latestBlob.ExistsAsync())
            {
                return null;
            }

            return new UriResult
            {
                Uri = latestBlob.Uri,
                ETag = latestBlob.Properties.ETag
            };
        }
Exemplo n.º 3
0
        public Uri GetLatestUri(GetLatestRequest request)
        {
            var context = new CloudContext(request.ConnectionString, request.Container);
            
            var latestPath = _pathBuilder.GetLatest(request.PathFormat);
            var latestBlob = context.BlobContainer.GetBlockBlobReference(latestPath);

            return latestBlob.Uri;
        }
Exemplo n.º 4
0
        public async Task<StreamResult> GetLatestStreamAsync(GetLatestRequest request)
        {
            var context = new CloudContext(request.ConnectionString, request.Container);

            var latestPath = _pathBuilder.GetLatest(request.PathFormat);
            var latestBlob = context.BlobContainer.GetBlockBlobReference(latestPath);

            try
            {
                var stream = await latestBlob.OpenReadAsync();
                return new StreamResult
                {
                    Stream = stream,
                    ETag = latestBlob.Properties.ETag,
                    ContentMD5 = latestBlob.Properties.ContentMD5
                };
            }
            catch (StorageException e)
            {
                if (e.RequestInformation.HttpStatusCode == 404)
                {
                    request.Trace.WriteLine($"The stream could not be found due to the following error: {e.RequestInformation.HttpStatusMessage}");
                    return null;
                }

                throw;
            }
        }
Exemplo n.º 5
0
        public async Task<UploadResult> UploadAsync(UniqueUploadRequest request)
        {
            if (!request.Stream.CanRead)
            {
                throw new ArgumentException("The provided stream must be readable.");
            }

            using (request.Stream)
            {
                // get the current
                var getLatestRequest = new GetLatestRequest
                {
                    ConnectionString = request.ConnectionString,
                    Container = request.Container,
                    PathFormat = request.PathFormat,
                    Trace = request.Trace
                };

                request.Trace.Write("Gettings the existing latest...");
                string etag = null;
                using (var currentResult = await _innerClient.GetLatestStreamAsync(getLatestRequest))
                {
                    // return nothing if the streams are equivalent
                    if (currentResult != null)
                    {
                        if (!request.Stream.CanSeek)
                        {
                            throw new ArgumentException("The provided stream must be seekable");
                        }

                        // seek to the beginning for calculating the hash
                        request.Stream.Seek(0, SeekOrigin.Begin);
                        var contentMD5 = await GetStreamContentMD5(request.Stream);
                        if (contentMD5 == currentResult.ContentMD5)
                        {
                            request.Trace.WriteLine(" exactly the same! No upload required.");
                            return null;
                        }

                        if (request.EqualsAsync != null)
                        {
                            // seek to the beginning for comparing the content
                            request.Stream.Seek(0, SeekOrigin.Begin);
                            if (await request.EqualsAsync(currentResult))
                            {
                                request.Trace.WriteLine(" equivalent! No upload required.");
                                return null;
                            }
                        }

                        etag = currentResult.ETag;

                        request.Trace.WriteLine(" different! The provided content will be uploaded.");
                    }
                    else
                    {
                        request.Trace.WriteLine(" non-existent! The provided content will be uploaded.");
                    }
                }

                // Seek to the beginning for uploading the blob.
                request.Stream.Seek(0, SeekOrigin.Begin);
                var uploadRequest = new UploadRequest
                {
                    ConnectionString = request.ConnectionString,
                    ETag = etag,
                    UseETags = request.UseETag,
                    Stream = request.Stream,
                    PathFormat = request.PathFormat,
                    Container = request.Container,
                    Trace = request.Trace,
                    UploadDirect = request.UploadDirect,
                    UploadLatest = true,
                    ContentType = request.ContentType,
                    Type = request.Type
                };

                return await _innerClient.UploadAsync(uploadRequest);
            }
        }
Exemplo n.º 6
0
            public TestContext()
            {
                // data
                UtcNow = new DateTimeOffset(2015, 1, 2, 3, 4, 5, 6, TimeSpan.Zero);
                Content = "newContent";
                Container = TestSupport.GetTestContainer();
                EqualsAsyncCalled = false;
                UniqueUploadRequest = new UniqueUploadRequest
                {
                    ConnectionString = TestSupport.ConnectionString,
                    Container = Container,
                    ContentType = "text/plain",
                    PathFormat = "testpath/{0}.txt",
                    UploadDirect = true,
                    Stream = new MemoryStream(Encoding.UTF8.GetBytes(Content)),
                    Trace = TextWriter.Null,
                    EqualsAsync = async x =>
                    {
                        EqualsAsyncCalled = true;
                        var actualContent = await new StreamReader(x.Stream).ReadLineAsync();
                        return actualContent == Content;
                    }
                };
                GetLatestRequest = new GetLatestRequest
                {
                    ConnectionString = UniqueUploadRequest.ConnectionString,
                    Container = UniqueUploadRequest.Container,
                    PathFormat = UniqueUploadRequest.PathFormat,
                    Trace = TextWriter.Null
                };

                // dependencies
                SystemTime = new Mock<ISystemTime>();
                PathBuilder = new PathBuilder();
                Client = new Client(SystemTime.Object, PathBuilder);

                // setup
                SystemTime.Setup(x => x.UtcNow).Returns(() => UtcNow);

                // target
                Target = new UniqueClient(Client);
            }