Exemplo n.º 1
0
        private BackblazeB2ActionResult <BackblazeB2UploadMultipartFileResult> FinishUploadingLargeFile(
            string fileId,
            IEnumerable <BackblazeB2ActionResult <UploadFilePartResponse> > uploadResponses
            )
        {
            if (uploadResponses.Any(t => t.HasErrors) || Interlocked.Read(ref _totalNumberOfChunks) != uploadResponses.Count())
            {
                IEnumerable <BackblazeB2ActionErrorDetails> errors = from uploadResponse in uploadResponses
                                                                     where uploadResponse.HasErrors
                                                                     from error in uploadResponse.Errors
                                                                     select error;

                if (errors.Any() == false)
                {
                    errors = new[]
                    {
                        new BackblazeB2ActionErrorDetails
                        {
                            Code    = "INTERNAL_UPLOAD_ERROR",
                            Message = "Unable to upload all file chunks. Most likely due to all upload consumer threads failing",
                            Status  = -1,
                        },
                    };
                }

                return(new BackblazeB2ActionResult <BackblazeB2UploadMultipartFileResult>(Maybe <BackblazeB2UploadMultipartFileResult> .Nothing, errors));
            }

            IList <string> sha1Hashes = (from uploadResponse in uploadResponses
                                         let uploadResponseValue = uploadResponse.MaybeResult.Value
                                                                   orderby uploadResponseValue.PartNumber ascending
                                                                   select uploadResponseValue.ContentSHA1).ToList();

            FinishLargeFileRequest finishLargeFileRequest = new FinishLargeFileRequest
            {
                FileID         = fileId,
                FilePartHashes = sha1Hashes,
            };
            string serializedFileRequest = JsonConvert.SerializeObject(finishLargeFileRequest);

            byte[] requestBytes = Encoding.UTF8.GetBytes(serializedFileRequest);

            HttpWebRequest webRequest = GetHttpWebRequest(_authorizationSession.APIURL + FinishLargeFileURL);

            webRequest.ContentLength = requestBytes.Length;
            webRequest.Method        = "POST";
            webRequest.Headers.Add("Authorization", _authorizationSession.AuthorizationToken);

            BackblazeB2ActionResult <BackblazeB2UploadMultipartFileResult> response =
                SendWebRequestAndDeserialize(webRequest, requestBytes);

            response.MaybeResult.Do(r => r.FileHashes = sha1Hashes);
            return(response);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts file parts that have been uploaded into a single file.
        /// </summary>
        /// <param name="fileId">The file id of the large file to finish.</param>
        /// <param name="sha1Parts">A list of hex SHA1 checksums for the parts of the large file.</param>
        /// <exception cref="AuthenticationException">Thrown when authentication fails.</exception>
        /// <exception cref="ApiException">Thrown when an error occurs during client operation.</exception>
        async Task <IApiResults <UploadFileResponse> > IStorageParts.FinishLargeFileAsync(string fileId, List <string> sha1Parts)
        {
            var request = new FinishLargeFileRequest(fileId, sha1Parts);

            return(await _client.FinishLargeFileAsync(request, _cancellationToken));
        }