예제 #1
0
        private void DownloadWorker(BlockingCollection <TrackDto> queue, PageInfo pageInfo, String path)
        {
            bool     hasTracks = true;
            TrackDto track     = null;

            while (hasTracks)
            {
                if (cts.Token.IsCancellationRequested)
                {
                    cts.Token.ThrowIfCancellationRequested();
                }

                try
                {
                    track = queue.Take();
                }
                catch (InvalidOperationException e)
                {
                    hasTracks = false;
                }

                if (hasTracks && track != null)
                {
                    String dest = GetFilePath(pageInfo, track, path);
                    if (fileSystem.IsDownloadedTrack(dest, track))
                    {
                        OnDownloadProgress(this, new TrackDownloadProgressEventArgs(track, track.FileSize));
                    }
                    else
                    {
                        String url = apiClient.GetTrackUrl(track.Id);
                        fileSystem.CreateDirectoryForFilePath(dest);
                        fileDownloader.Download(url, dest, (o, e) =>
                        {
                            if (!CallHandlerOnlyOnFinish || e.BytesDownloaded >= track.FileSize)
                            {
                                OnDownloadProgress(this, new TrackDownloadProgressEventArgs(track, e.BytesDownloaded));
                            }
                        });

                        AddTagsToFile(dest, track);
                    }
                }
            }
        }