Пример #1
0
        private IEnumerable <KeyValuePair <string, string> > MultiPartUpload(IMultiPartIDUploadInfo multiPartIDUploadInfo, Stream stream, long streamSize)
        {
            var result = new List <KeyValuePair <string, string> >();

            if (multiPartIDUploadInfo.PartSize <= 0)
            {
                System.Diagnostics.Debug.Assert(multiPartIDUploadInfo.PartUploadInfos.Count() == 1);
                UploadPart(multiPartIDUploadInfo.HttpMethod, stream, streamSize, multiPartIDUploadInfo.PartUploadInfos.First(), "");
            }
            else
            {
                int writePartIndex = 0;

                CopyUtils.CopyAsync(
                    async(buffer, cancellationToken) =>
                {
                    return(await CopyUtils.FillBufferAsync(buffer,
                                                           async(buf, offset, ct) =>
                    {
                        return await stream.ReadAsync(buf, offset, (int)multiPartIDUploadInfo.PartSize - offset);
                    },
                                                           cancellationToken
                                                           ));
                },
                    (buffer, size, cancellationToken) =>
                {
                    var partUploadInfo = multiPartIDUploadInfo.PartUploadInfos.ElementAt(writePartIndex);

                    var uploadPartResult = UploadPart(multiPartIDUploadInfo.HttpMethod, new MemoryStream(buffer), size, partUploadInfo, multiPartIDUploadInfo.MultiPartUploadResultHeaderName);

                    result.Add(new KeyValuePair <string, string>(partUploadInfo.Identifier, uploadPartResult));

                    writePartIndex++;

                    return(Task.CompletedTask);
                },
                    (int)multiPartIDUploadInfo.PartSize,
                    CancellationToken.None
                    )
                .Wait();

                //foreach (var partUploadInfo in multiPartIDUploadInfo.PartUploadInfos)
                //{
                //    var uploadPartResult = UploadPart(multiPartIDUploadInfo.HttpMethod, stream, multiPartIDUploadInfo.PartSize, partUploadInfo, multiPartIDUploadInfo.MultiPartUploadResultHeaderName);
                //    yield return new KeyValuePair<string, string>(partUploadInfo.Identifier, uploadPartResult);
                //}
            }

            return(result);
        }
 public DocumentMultiPartIDUploadInfo(IMultiPartIDUploadInfo multiPartIDUploadInfo, string documentID)
 {
     MultiPartIDUploadInfo = multiPartIDUploadInfo;
     DocumentID            = documentID;
 }