示例#1
0
 private void SetupMime(IReportContext context)
 {
     if (string.IsNullOrWhiteSpace(MimeType))
     {
         MimeType = "text/html";
     }
     if ((MimeType.Contains("text") || MimeType.Contains("json")) && !MimeType.Contains("charset"))
     {
         MimeType += "; charset=utf-8";
     }
     context.SetHeader("Content-Type", MimeType);
 }
示例#2
0
        async Task <bool> OpenConnection()
        {
            Uri url = null;

            try
            {
                Debug.WriteLine($"Opening Connection {TrackId}");
                State = DownloadState.Downloading;
                cancelSource.Token.ThrowIfCancellationRequested();
                if (DownloadStream != null && DownloadStream.CanRead)
                {
                    return(true);
                }
                if (response != null && response.IsSuccessStatusCode)
                {
                    return(true);
                }
                Debug.WriteLine($"Requesting Playback Url {TrackId}");
                url = Uri ?? await MusicManager.Shared.GeTrackPlaybackUri(TrackId);

                if (url == null)
                {
                    NotificationManager.Shared.ProcFailedDownload(SongId);
                    return(false);
                }
                var request = new HttpRequestMessage(HttpMethod.Get, url);
                if (Stream.WritePosition > 0)
                {
                    request.Headers.Range = new RangeHeaderValue(Stream.WritePosition, Stream.FinalLength);
                }

                var time = TimeSpan.FromSeconds(30);
                var cancelationSource = new CancellationTokenSource();

                var respTask = client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancelationSource.Token);
                if (await Task.WhenAny(respTask, Task.Delay(time)) != respTask)
                {
                    throw new TimeoutException();
                }
                response = respTask.Result;
                if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    NotificationManager.Shared.ProcFailedToDownloadTrack(TrackId);
                    return(false);
                }
                response.EnsureSuccessStatusCode();

                if (Stream.FinalLength == 0)
                {
                    Stream.FinalLength = response?.Content?.Headers?.ContentLength ?? 0;
                }
                if (Stream.FinalLength == 0)
                {
                    IEnumerable <string> estimated = null;
                    if (response?.Content?.Headers.TryGetValues("x-estimated-content-length", out estimated) == true)
                    {
                        Stream.EstimatedLength = long.Parse(estimated.First());
                    }
                }
                if (string.IsNullOrWhiteSpace(MimeType))
                {
                    MimeType = response?.Content?.Headers?.ContentType?.MediaType;
                    if (string.IsNullOrEmpty(MimeType) || MimeType.Contains("octet"))
                    {
                        MimeType = "audio/mpeg";
                    }
                }

                DownloadStream = await response.Content.ReadAsStreamAsync();

                return(true);
            }
            catch (OperationCanceledException ex)
            {
                Debug.WriteLine(ex);
            }
            catch (Exception ex)
            {
                if (url != null)
                {
                    ex.Data["Url"] = url.AbsoluteUri;
                }
                if (!(ex is TimeoutException))
                {
                    LogManager.Shared.Report(ex);
                }
                else
                {
                    Console.WriteLine(ex);
                }
            }
            return(false);
        }