void UseStream(string input, string output, long startOffset, long endOffset, string customMessage, CancellationToken token, bool isStream) { token.ThrowIfCancellationRequested(); long contentLength; FileInfo fileinfo = new FileInfo(output); long existingLength = isStream ? localStream.Length : fileinfo.Exists ? fileinfo.Length : 0; HttpRequestMessage request = new HttpRequestMessage() { RequestUri = new Uri(input) }; request.Headers.TryAddWithoutValidation("User-Agent", App.UserAgent); request.Headers.Range = (startOffset != -1 && endOffset != -1) ? new RangeHeaderValue(startOffset, endOffset): new RangeHeaderValue(existingLength, null); HttpResponseMessage response = httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, token).GetAwaiter().GetResult(); if (!((response.Content.Headers.ContentRange.Length ?? 0) == existingLength)) { using (ThrowUnacceptableStatusCode(response)) { contentLength = (startOffset != -1 && endOffset != -1) ? endOffset - startOffset : existingLength + (response.Content.Headers.ContentRange.Length - response.Content.Headers.ContentRange.From) ?? 0; resumabilityStatus = new DownloadStatusChanged((int)response.StatusCode == 206); if (!isStream) { localStream = fileinfo.Open(resumabilityStatus.ResumeSupported ? FileMode.Append : FileMode.Create, FileAccess.Write); } OnResumabilityChanged(resumabilityStatus); ReadRemoteStream( response, localStream, existingLength, contentLength, customMessage, token ); response.Dispose(); } } else { #if DEBUG Console.WriteLine($"File download for {input} is already completed! Skipping..."); #endif } }
protected virtual void OnResumabilityChanged(DownloadStatusChanged e) => ResumablityChanged?.Invoke(this, e);