public HttpResponseMessage Get(string name)
        {
            var ip = WebApiApplication.GetClientIp();

            if (!RequestProtection.AddRequest(ip, RequestType.TrackInfoRequest))
            {
                return(Request.CreateResponse(HttpStatusCode.Forbidden, "Usage limit exceeded"));
            }

            try
            {
                return(Request.CreateResponse(TrackInformationFetcher.GetTrackInformation(name)));
            }
            catch
            {
                // ignored
            }

            return(Request.CreateResponse(HttpStatusCode.InternalServerError));
        }
Ejemplo n.º 2
0
        private void WebClientOnOpenReadCompleted(object sender, OpenReadCompletedEventArgs openReadCompletedEventArgs)
        {
            _webClient.Dispose();

            if (openReadCompletedEventArgs.Cancelled)
            {
                OnDownloadItemDownloadCompleted(new DownloadCompletedEventArgs(true, true, openReadCompletedEventArgs.Error));
                return;
            }

            if (!Overwrite && File.Exists(SavePath))
            {
                return;
            }

            var totalLength = 0;

            try
            {
                totalLength = int.Parse(((WebClient)sender).ResponseHeaders["Content-Length"]);
            }
            catch (Exception)
            {
                // ignored
            }

            try
            {
                long processed = 0;
                var  tmpPath   = Path.GetTempFileName();

                using (var stream = openReadCompletedEventArgs.Result)
                    using (var fs = File.Create(tmpPath))
                    {
                        var buffer = new byte[32 * 1024];
                        int read;

                        while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            fs.Write(buffer, 0, read);

                            processed += read;
                            OnDownloadItemDownloadProgressChanged(new DownloadProgressChangedEventArgs(processed, totalLength));
                        }
                    }

                switch (DownloadFormat)
                {
                case DownloadFormat.M4A:
                {
                    File.Move(tmpPath, SavePath);
                } break;

                case DownloadFormat.MP3:
                {
                    OnDownloadItemConvertionStarted(null);
                    using (var reader = new MediaFoundationReader(tmpPath))
                    {
                        MediaFoundationEncoder.EncodeToMp3(reader, SavePath);
                    }

                    File.Delete(tmpPath);
                } break;
                }

                var information = TrackInformationFetcher.GetTrackInformation(Item);
                using (var file = TagLib.File.Create(SavePath))
                {
                    file.Tag.Title        = information.Name;
                    file.Tag.AlbumArtists = new[] { information.Artist };
                    file.Tag.Album        = information.Album;

                    if (!string.IsNullOrEmpty(information.CoverUrl))
                    {
                        byte[] result;

                        using (var client = new WebClient())
                        {
                            client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
                            result = client.DownloadData(information.CoverUrl);
                        }

                        file.Tag.Pictures = new IPicture[]
                        {
                            new Picture(new ByteVector(result))
                            {
                                Type        = PictureType.FrontCover,
                                Description = "Cover"
                            }
                        };
                    }

                    file.Save();
                }

                GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
                GC.Collect();

                OnDownloadItemDownloadCompleted(new DownloadCompletedEventArgs(false));
            }
            catch (Exception ex)
            {
                OnDownloadItemDownloadCompleted(new DownloadCompletedEventArgs(true, false, ex));
            }
        }