示例#1
0
        public async Task <MetaData> FilesAsync(string path, Stream targetStream, string rev = null, string asTeamMember = null)
        {
            MetaData            fileMetadata = null;
            HttpResponseMessage restResponse = await _requestExecuter.Execute(() => _requestGenerator.Files(_options.Root, path, rev, asTeamMember)).ConfigureAwait(false);

            await _requestExecuter.CheckForError(restResponse, false).ConfigureAwait(false);

            long?length = restResponse.Content.Headers.ContentLength;

            if (length == null)
            {
                IEnumerable <string> metadatas;
                if (restResponse.Headers.TryGetValues("x-dropbox-metadata", out metadatas))
                {
                    string metadata = metadatas.FirstOrDefault();
                    if (metadata != null)
                    {
                        fileMetadata = JsonConvert.DeserializeObject <MetaData>(metadata);
                        length       = fileMetadata.bytes;
                    }
                }
            }
            string etag = "";
            IEnumerable <string> etags;

            if (restResponse.Headers.TryGetValues("etag", out etags))
            {
                etag = etags.FirstOrDefault();
            }


            long?read = 0;

            do
            {
                long?from = read;
                long?to   = read + _options.ChunkSize;
                if (to > length)
                {
                    to = length;
                }

                restResponse =
                    await
                    _requestExecuter.Execute(() => _requestGenerator.FilesRange(_options.Root, path, from.Value, to.Value - 1, etag, rev, asTeamMember)).ConfigureAwait(false);

                await restResponse.Content.CopyToAsync(targetStream).ConfigureAwait(false);

                read += restResponse.Content.Headers.ContentLength;

                if (read >= length)
                {
                    break;
                }
            } while (restResponse.StatusCode == HttpStatusCode.PartialContent);

            return(fileMetadata);
        }
示例#2
0
        public async Task <MetaData> FilesAsync(string path, Stream targetStream, IProgress <MyTaskProgressReport> progress = null, string rev = null, string asTeamMember = null)
        {
            MetaData fileMetadata = null;

            using (var restResponse = await _requestExecuter.Execute(() => _requestGenerator.Files(_options.Root, path, rev, asTeamMember)).ConfigureAwait(false))
            {
                await _requestExecuter.CheckForError(restResponse, false).ConfigureAwait(false);

                long?length = restResponse.Content.Headers.ContentLength;
                if (length == null)
                {
                    IEnumerable <string> metadatas;
                    if (restResponse.Headers.TryGetValues("x-dropbox-metadata", out metadatas))
                    {
                        string metadata = metadatas.FirstOrDefault();
                        if (metadata != null)
                        {
                            fileMetadata = JsonConvert.DeserializeObject <MetaData>(metadata);
                            length       = fileMetadata.bytes;
                        }
                    }
                }
                string etag = "";
                IEnumerable <string> etags;
                if (restResponse.Headers.TryGetValues("etag", out etags))
                {
                    etag = etags.FirstOrDefault();
                }
                targetStream.SetLength((long)length);
                long?read    = 0;
                bool hasMore = true;
                do
                {
                    long?from = read;
                    long?to   = read + _options.ChunkSize;
                    if (to > length)
                    {
                        to = length;
                    }
                    if (from >= length)
                    {
                        break;
                    }

                    using (var restResponse2 = await _requestExecuter.Execute(() => _requestGenerator.FilesRange(_options.Root, path, from.Value, to.Value - 1, etag, rev, asTeamMember)).ConfigureAwait(false))
                    {
                        await restResponse2.Content.CopyToAsync(targetStream).ConfigureAwait(false);

                        read += restResponse2.Content.Headers.ContentLength;

                        if (progress != null)
                        {
                            progress.Report(new MyTaskProgressReport {
                                CurrentProgressAmount = read, TotalProgressAmount = length
                            });
                        }

                        if (read >= length)
                        {
                            hasMore = false;
                        }
                        else if (restResponse2.StatusCode == HttpStatusCode.OK)
                        {
                            hasMore = false;
                        }
                    }
                } while (hasMore);
            }
            return(fileMetadata);
        }