コード例 #1
0
        protected string CalculateHash(long count)
        {
            using (var fileStream = File.OpenRead())
            {
                do
                {
                    var buffer = new byte[65536];

                    if (count < buffer.Length)
                    {
                        buffer = new byte[count];
                    }

                    var bytesRead = fileStream.Read(buffer, 0, buffer.Length);

                    if (bytesRead > 0)
                    {
                        HashProvider.Append(buffer, 0, buffer.Length);
                    }

                    count -= bytesRead;
                } while (count > 0);
            }

            return(HashProvider.GetComputedHashAsString());
        }
コード例 #2
0
        protected Uri GetFinishUriForThreadedUploads()
        {
            var finishUri = new StringBuilder(string.Format("{0}&respformat=json", UploadSpecification.FinishUri.AbsoluteUri));

            if (File.Length > 0)
            {
                HashProvider.Finalize(new byte[1], 0, 0);
                finishUri.AppendFormat("&filehash={0}", HashProvider.GetComputedHashAsString());
            }

            if (!string.IsNullOrEmpty(UploadSpecificationRequest.Details))
            {
                finishUri.AppendFormat("&details={0}", Uri.EscapeDataString(UploadSpecificationRequest.Details));
            }
            if (!string.IsNullOrEmpty(UploadSpecificationRequest.Title))
            {
                finishUri.AppendFormat("&title={0}", Uri.EscapeDataString(UploadSpecificationRequest.Title));
            }
            if (UploadSpecificationRequest.ForceUnique)
            {
                finishUri.Append("&forceunique=1");
            }

            return(new Uri(finishUri.ToString()));
        }
コード例 #3
0
        private UploadResponse FinishUpload()
        {
            var finishUri = GetFinishUriForThreadedUploads();
            var client    = GetHttpClient();

            var requestMessage = new HttpRequestMessage(HttpMethod.Get, finishUri);

            requestMessage.Headers.Add("Accept", "application/json");
            requestMessage.AddDefaultHeaders(Client);

            var response = RequestExecutor.Send(client, requestMessage, HttpCompletionOption.ResponseContentRead);

            return(GetUploadResponse(response, HashProvider.GetComputedHashAsString()));
        }
コード例 #4
0
        private async Task <UploadResponse> FinishUploadAsync()
        {
            var client         = GetHttpClient();
            var finishUri      = this.GetFinishUriForThreadedUploads();
            var requestMessage = new HttpRequestMessage(HttpMethod.Get, finishUri);

            requestMessage.Headers.Add("Accept", "application/json");
            requestMessage.AddDefaultHeaders(Client);

            var response = await RequestExecutor.SendAsync(
                client,
                requestMessage,
                HttpCompletionOption.ResponseContentRead,
                default(CancellationToken)).ConfigureAwait(false);

            return(await GetUploadResponseAsync(response, HashProvider.GetComputedHashAsString()).ConfigureAwait(false));
        }
コード例 #5
0
        private async Task <UploadResponse> FinishUpload(CancellationToken cancellationToken)
        {
            HashProvider.Finalize(new byte[0], 0, 0);
            var finishUri      = GetFinishUriForThreadedUploads();
            var requestMessage = new HttpRequestMessage(HttpMethod.Get, finishUri);

            var responseMessage = await SendHttpRequest(requestMessage, cancellationToken).ConfigureAwait(false);

            string localHash      = HashProvider.GetComputedHashAsString();
            var    uploadResponse = await GetUploadResponseAsync(responseMessage, localHash).ConfigureAwait(false);

            if (uploadResponse.Count == 1)
            {
                string serverHash = uploadResponse.Single().Hash;
                if (!string.IsNullOrEmpty(serverHash) && !localHash.Equals(serverHash, StringComparison.OrdinalIgnoreCase))
                {
                    throw new UploadException($"File hash mismatch! Client: {localHash} Server: {serverHash}", Enums.UploadStatusCode.Unknown);
                }
            }

            return(uploadResponse);
        }
コード例 #6
0
        protected string CalculateHash(long count)
        {
            do
            {
                var buffer = new byte[Configuration.BufferSize];

                if (count < buffer.Length)
                {
                    buffer = new byte[count];
                }

                var bytesRead = FileStream.Read(buffer, 0, buffer.Length);

                if (bytesRead > 0)
                {
                    HashProvider.Append(buffer, 0, buffer.Length);
                }

                count -= bytesRead;
            } while (count > 0);

            FileStream.Seek(0, SeekOrigin.Begin);
            return(HashProvider.GetComputedHashAsString());
        }