<commit_before>﻿using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.Packaging.Core;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using NuGetPackageExplorer.Types;
using NuGetPe;
using Ookii.Dialogs.Wpf;
using PackageExplorerViewModel;

namespace PackageExplorer
{
    [Export(typeof(INuGetPackageDownloader))]
    internal class PackageDownloader : INuGetPackageDownloader
    {
        private static readonly FileSizeConverter FileSizeConverter = new FileSizeConverter();

        [Import]
        public Lazy<MainWindow> MainWindow { get; set; }

        [Import]
        public IUIServices UIServices { get; set; }

        #region IPackageDownloader Members

        public async Task Download(string targetFilePath, SourceRepository sourceRepository, PackageIdentity packageIdentity)
        {
            var sourceFilePath = await DownloadWithProgress(sourceRepository, packageIdentity);
            if (!string.IsNullOrEmpty(sourceFilePath))
            {
                File.Copy(sourceFilePath, targetFilePath, overwrite: true);
            }
        }

        public async Task<ISignaturePackage> Download(SourceRepository sourceRepository, PackageIdentity packageIdentity)
        {
            var tempFilePath = await DownloadWithProgress(sourceRepository, packageIdentity);
            try
            {
                return (tempFilePath == null) ? null : new ZipPackage(tempFilePath);
            }
            catch (Exception e)
            {
                DiagnosticsClient.Notify(e);
                UIServices.Show(e.Message, MessageLevel.Error);
                return null;
            }

        }

        private Task<string> DownloadWithProgress(SourceRepository sourceRepository, PackageIdentity packageIdentity)
        {
            var progressDialogText = Resources.Dialog_DownloadingPackage;
            if (packageIdentity.HasVersion)
            {
                progressDialogText = string.Format(CultureInfo.CurrentCulture, progressDialogText, packageIdentity.Id, packageIdentity.Version);
            }
            else
            {
                progressDialogText = string.Format(CultureInfo.CurrentCulture, progressDialogText, packageIdentity.Id, string.Empty);
            }
<commit_msg>Don't send telemetry error for bad package<commit_after>﻿using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.Packaging.Core;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using NuGetPackageExplorer.Types;
using NuGetPe;
using Ookii.Dialogs.Wpf;
using PackageExplorerViewModel;

namespace PackageExplorer
{
    [Export(typeof(INuGetPackageDownloader))]
    internal class PackageDownloader : INuGetPackageDownloader
    {
        private static readonly FileSizeConverter FileSizeConverter = new FileSizeConverter();

        [Import]
        public Lazy<MainWindow> MainWindow { get; set; }

        [Import]
        public IUIServices UIServices { get; set; }

        #region IPackageDownloader Members

        public async Task Download(string targetFilePath, SourceRepository sourceRepository, PackageIdentity packageIdentity)
        {
            var sourceFilePath = await DownloadWithProgress(sourceRepository, packageIdentity);
            if (!string.IsNullOrEmpty(sourceFilePath))
            {
                File.Copy(sourceFilePath, targetFilePath, overwrite: true);
            }
        }

        public async Task<ISignaturePackage> Download(SourceRepository sourceRepository, PackageIdentity packageIdentity)
        {
            var tempFilePath = await DownloadWithProgress(sourceRepository, packageIdentity);
            try
            {
                return (tempFilePath == null) ? null : new ZipPackage(tempFilePath);
            }
            catch (Exception e)
            {
                // Don't send telemetry error for bad package
                if(!(e is InvalidDataException))
                {
                    DiagnosticsClient.Notify(e);
                }
                
                UIServices.Show(e.Message, MessageLevel.Error);
                return null;
            }

        }

        private Task<string> DownloadWithProgress(SourceRepository sourceRepository, PackageIdentity packageIdentity)
        {
            var progressDialogText = Resources.Dialog_DownloadingPackage;
            if (packageIdentity.HasVersion)
            {
                progressDialogText = string.Format(CultureInfo.CurrentCulture, progressDialogText, packageIdentity.Id, packageIdentity.Version);
            }
            else
            {
                progressDialogText = string.Format(CultureInfo.CurrentCulture, progressDialogText, packageIdentity.Id, string.Empty);
            }

{'fraction_non_alphanumeric': 0.06994768455725635, 'fraction_numerical': 0.0, 'mean_word_length': 2.6022330774598745, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6264021', 'n_tokens_mistral': 1238, 'n_tokens_neox': 1205, 'n_words': 284}