Example #1
0
        public async Task <string> ChunkedUploadAppend(long mediaId, Stream media, long sendSize, long actualSize, long totalLength, int index, IProgress <UploadReport> progressReceiver)
        {
            long dataLength = media.Length;

            progressReceiver?.Report(UploadReport.CreateBegin(dataLength));

            var result = await this.ChunkedUploadAppendImpl(mediaId, media, sendSize, actualSize, totalLength, index, progressReceiver).ConfigureAwait(false);

            progressReceiver?.Report(UploadReport.CreateCompleted(dataLength));

            return(result);
        }
Example #2
0
        public async Task <MediaResponse> Upload(Stream contentStream, long[] additionalOwners = null, IProgress <UploadReport> progressReceiver = null)
        {
            long contentLength = contentStream.Length;

            progressReceiver?.Report(UploadReport.CreateBegin(contentLength));

            var bondary = OAuthHelper.GenerateNonce();

            using var content = new MultipartFormDataContent(bondary);

            if (additionalOwners?.Length > 0)
            {
                var joinedAdditionalOwners = string.Join(",", additionalOwners);
                content.Add(new StringContent(joinedAdditionalOwners), "additional_owners");
            }

            content.Add(new ProgressableUploadContent(contentStream, progressReceiver: progressReceiver), "media");

            var result = await this.Api.ApiPostRequestAsync <MediaResponse>(_apiEndpoint, content);

            progressReceiver?.Report(UploadReport.CreateCompleted(contentLength));

            return(result);
        }
Example #3
0
        /// <param name="uploadData"></param>
        /// <param name="mediaType"></param>
        /// <param name="additionalOwners"></param>
        /// <param name="progressReceiver"></param>
        /// <param name="segmentSize"></param>
        /// <returns></returns>
        public async Task <MediaResponse> ChunkedUpload(Stream uploadData, string mediaType, long[] additionalOwners = null,
                                                        IProgress <UploadReport> progressReceiver = null, int segmentSize = 1024 * 1024 * 1)
        {
            if (segmentSize <= 0)
            {
                throw new ArgumentException($"{nameof(segmentSize)}には0より大きい値を指定してください。");
            }

            if (uploadData.CanSeek)
            {
                uploadData.Position = 0;
            }

            long fileLength = uploadData.Length;

            if (fileLength <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(uploadData));
            }

            // INITコマンド
            var initResposne = await this.ChunkedUploadInit(fileLength, mediaType, additionalOwners)
                               .ConfigureAwait(false);

            long mediaId = initResposne.MediaId;

            // APPENDコマンド
            // アップロード開始
            progressReceiver?.Report(UploadReport.CreateBegin(fileLength));

            long chunkSize  = segmentSize;
            long actualSize = 0;
            int  index      = 0;

            while (actualSize < fileLength)
            {
                if (fileLength < (actualSize + chunkSize))
                {
                    chunkSize = fileLength - actualSize;
                }

                var res = await this.ChunkedUploadAppendImpl(mediaId, uploadData, chunkSize, actualSize, fileLength, index, progressReceiver)
                          .ConfigureAwait(false);

                if (!string.IsNullOrEmpty(res))
                {
                    throw new TwitterException(res);
                }

                actualSize += chunkSize;
                ++index;
            }

            // FINALIZEコマンド
            var response = await this.ChunkedUploadFinalize(mediaId)
                           .ConfigureAwait(false);

            progressReceiver?.Report(UploadReport.CreateCompleted(fileLength));

            return(response);
        }