Example #1
0
        private async Task <BackblazeApiResponse <UploadedPartOutputDTO> > UploadLargeFilePartAsync(
            UploadPartUrlOutputDTO uploadUrl,
            Stream partStream,
            string destinationFilename,
            int partNumber,
            IProgress <FilePartProgress> progressData = null,
            CancellationToken cancellationToken       = default(CancellationToken)
            )
        {
            if (partStream == null)
            {
                throw new NullReferenceException("partStream");
            }

            if (!partStream.CanRead)
            {
                throw new InvalidDataException();
            }

            string fileHash = FileHasher.GetFileHash(partStream);

            partStream.Seek(0, SeekOrigin.Begin);

            var progressTimer = new System.Timers.Timer();

            if (progressData != null)
            {
                RunPartProgressMonitor(progressTimer, partStream, destinationFilename, progressData, partNumber);
            }

            using (var fileContent = new StreamContent(partStream))
            {
                fileContent.Headers.ContentLength = partStream.Length;
                fileContent.Headers.ContentType   = new MediaTypeHeaderValue("b2/x-auto");

                var request = new HttpRequestMessage
                {
                    Method     = HttpMethod.Post,
                    RequestUri = new Uri(uploadUrl.Url),
                    Headers    =
                    {
                        { "X-Bz-Part-Number",  partNumber.ToString() },
                        { "X-Bz-Content-Sha1", fileHash              },
                    },
                    Content = fileContent
                };

                request.Headers.TryAddWithoutValidation("Authorization", uploadUrl.AuthorizationToken);

                using (var result = await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken))
                {
                    string resultJson = await result.Content.ReadAsStringAsync();

                    progressTimer.Enabled = false;

                    if (!result.IsSuccessStatusCode)
                    {
                        return(new BackblazeApiResponse <UploadedPartOutputDTO>()
                        {
                            Succeeded = result.IsSuccessStatusCode,
                            Error = JsonSerializer.Deserialize <B2ErrorResponseOutputDTO>(resultJson)
                        });
                    }

                    return(new BackblazeApiResponse <UploadedPartOutputDTO>()
                    {
                        Succeeded = result.IsSuccessStatusCode,
                        Data = JsonSerializer.Deserialize <UploadedPartOutputDTO>(resultJson)
                    });
                };
            }
        }
Example #2
0
        /// <summary>
        /// Upload a file without any error recovery
        /// </summary>
        public async Task <BackblazeApiResponse <FileDataOutputDTO> > UploadSmallFileAsync(
            UploadUrlOutputDTO uploadUrl,
            string sourcePath,
            string destinationFilename,
            IProgress <FileProgress> progressData = null,
            CancellationToken cancellationToken   = default(CancellationToken)
            )
        {
            using (var fs = File.OpenRead(sourcePath))
            {
                string fileHash = FileHasher.GetFileHash(fs);

                fs.Seek(0, SeekOrigin.Begin);

                using (var fileContent = new StreamContent(fs))
                {
                    fileContent.Headers.ContentLength = fs.Length;
                    fileContent.Headers.ContentType   = new MediaTypeHeaderValue("b2/x-auto");

                    var request = new HttpRequestMessage
                    {
                        Method     = HttpMethod.Post,
                        RequestUri = new Uri(uploadUrl.Url),
                        Headers    =
                        {
                            { "X-Bz-File-Name",    destinationFilename },
                            { "X-Bz-Content-Sha1", fileHash            },
                        },
                        Content = fileContent
                    };

                    request.Headers.TryAddWithoutValidation("Authorization", uploadUrl.AuthorizationToken);

                    var progressTimer = new System.Timers.Timer();
                    if (progressData != null)
                    {
                        RunSmallUploadProgressMonitor(progressTimer, fs, destinationFilename, progressData);
                    }

                    using (var result = await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken))
                    {
                        string resultJson = await result.Content.ReadAsStringAsync();

                        progressTimer.Enabled = false;

                        if (!result.IsSuccessStatusCode)
                        {
                            return(new BackblazeApiResponse <FileDataOutputDTO>()
                            {
                                Succeeded = result.IsSuccessStatusCode,
                                Error = JsonSerializer.Deserialize <B2ErrorResponseOutputDTO>(resultJson)
                            });
                        }

                        return(new BackblazeApiResponse <FileDataOutputDTO>()
                        {
                            Succeeded = result.IsSuccessStatusCode,
                            Data = JsonSerializer.Deserialize <FileDataOutputDTO>(resultJson)
                        });
                    };
                }
            }
        }