Esempio n. 1
0
        private void ShowFile(PackageFile file)
        {
            object?content  = null;
            var    isBinary = false;

            // find all plugins which can handle this file's extension
            var contentViewers = FindContentViewer(file);

            if (contentViewers != null)
            {
                try
                {
                    // iterate over all plugins, looking for the first one that return non-null content
                    foreach (var viewer in contentViewers)
                    {
                        var files = file.GetAssociatedPackageFiles().ToList();

                        content = viewer.GetView(file, files);
                        if (content != null)
                        {
                            // found a plugin that can read this file, stop
                            break;
                        }
                    }
                }
                catch (Exception ex) when(!(ex is FileNotFoundException))
                {
                    DiagnosticsClient.TrackException(ex, ViewModel.Package, ViewModel.PublishedOnNuGetOrg);
                    // don't let plugin crash the app
                    content = Resources.PluginFailToReadContent + Environment.NewLine + ex.ToString();
                }

                isBinary = content is not string;
            }

            // if plugins fail to read this file, fall back to the default viewer
            var truncated = false;

            if (content == null)
            {
                isBinary = FileHelper.IsBinaryFile(file.Name);
                if (isBinary)
                {
                    content = Resources.UnsupportedFormatMessage;
                }
                else
                {
                    content = ReadFileContent(file, out truncated);
                }
            }

            long size = -1;
            IReadOnlyList <AuthenticodeSignature> sigs;
            SignatureCheckResult isValidSig;
            {
                // note: later, throught binding converter, SigningCertificate's CN value is extracted through native api
                if (AppCompat.IsSupported(RuntimeFeature.Cryptography, RuntimeFeature.NativeMethods))
                {
                    using var stream   = file.GetStream();
                    using var tempFile = new TemporaryFile(stream, Path.GetExtension(file.Name));
                    var extractor = new FileInspector(tempFile.FileName);

                    sigs       = extractor.GetSignatures().ToList();
                    isValidSig = extractor.Validate();
                    size       = tempFile.Length;
                }
                else
                {
                    using var stream = StreamUtility.MakeSeekable(file.GetStream(), disposeOriginal: true);
                    var peFile      = new PeFile(stream);
                    var certificate = CryptoUtility.GetSigningCertificate(peFile);

                    if (certificate is not null)
                    {
                        sigs       = new List <AuthenticodeSignature>(0);
                        isValidSig = SignatureCheckResult.UnknownProvider;
                    }
                    else
                    {
                        sigs       = new List <AuthenticodeSignature>(0);
                        isValidSig = SignatureCheckResult.NoSignature;
                    }
                    size = peFile.FileSize;
                }
            }

            var fileInfo = new FileContentInfo(
                file,
                file.Path,
                content,
                !isBinary,
                size,
                truncated,
                sigs,
                isValidSig);

            ViewModel.ShowFile(fileInfo);
        }