Popcorn.Services.Movie.MovieService.DownloadCoverImageAsync C# (CSharp) Method

DownloadCoverImageAsync() public method

Download cover image for each of the movies provided
public DownloadCoverImageAsync ( IEnumerable movies, CancellationTokenSource ct ) : Task
movies IEnumerable The movies to process
ct System.Threading.CancellationTokenSource Used to cancel task
return Task
        public async Task DownloadCoverImageAsync(IEnumerable<MovieShort> movies, CancellationTokenSource ct)
        {
            var watch = Stopwatch.StartNew();

            var moviesToProcess = movies.ToList();
            try
            {
                await
                    moviesToProcess.ForEachAsync(
                        movie =>
                            DownloadFileHelper.DownloadFileTaskAsync(movie.MediumCoverImage,
                                Constants.CoverMoviesDirectory + movie.ImdbCode + Constants.ImageFileExtension, ct: ct),
                        (movie, t) =>
                        {
                            if (t.Item3 == null && !string.IsNullOrEmpty(t.Item2))
                                movie.CoverImagePath = t.Item2;
                        });
            }
            catch (Exception exception) when (exception is TaskCanceledException)
            {
                Logger.Debug(
                    "DownloadCoverImageAsync cancelled.");
            }
            catch (Exception exception)
            {
                Logger.Error(
                    $"DownloadCoverImageAsync: {exception.Message}");
                throw;
            }
            finally
            {
                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Logger.Debug(
                    $"DownloadCoverImageAsync ({string.Join(";", moviesToProcess.Select(movie => movie.ImdbCode))}) in {elapsedMs} milliseconds.");
            }
        }