示例#1
0
 private bool CanEditFileCommandExecute(PackagePart file)
 {
     return(!IsSigned &&
            (file is PackageFile) &&
            !IsInEditFileMode &&
            !FileHelper.IsBinaryFile(file.Path));
 }
        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)
            {
                isBinary = true;
                try
                {
                    // iterate over all plugins, looking for the first one that return non-null content
                    foreach (var viewer in contentViewers)
                    {
                        // Get peer files
                        var peerFiles = file.Parent !.GetFiles()
                                        .Select(pf => new PackageFile(pf, Path.GetFileName(pf.Path), file.Parent !))
                                        .ToList();

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

                if (content is string)
                {
                    isBinary = false;
                }
            }

            // 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;

            using (var str = file.GetStream())
                using (var tempFile = new TemporaryFile(str, Path.GetExtension(file.Name)))
                {
                    var extractor = new FileInspector(tempFile.FileName);

                    sigs       = extractor.GetSignatures().ToList();
                    isValidSig = extractor.Validate();

                    size = tempFile.Length;
                }

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

            ViewModel.ShowFile(fileInfo);
        }
        private void ShowFile(PackageFile file)
        {
            long   size     = -1;
            object content  = null;
            bool   isBinary = false;

            // find all plugins which can handle this file's extension
            IEnumerable <IPackageContentViewer> contentViewers = FindContentViewer(file);

            if (contentViewers != null)
            {
                isBinary = true;
                try
                {
                    // iterate over all plugins, looking for the first one that return non-null content
                    foreach (IPackageContentViewer viewer in contentViewers)
                    {
                        using (Stream stream = file.GetStream())
                        {
                            if (size == -1)
                            {
                                size = stream.Length;
                            }
                            content = viewer.GetView(Path.GetExtension(file.Name), stream);
                            if (content != null)
                            {
                                // found a plugin that can read this file, stop
                                break;
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    // don't let plugin crash the app
                    content = Resources.PluginFailToReadContent;
                }

                if (content is string)
                {
                    isBinary = false;
                }
            }

            // if plugins fail to read this file, fall back to the default viewer
            if (content == null)
            {
                isBinary = FileHelper.IsBinaryFile(file.Name);
                if (isBinary)
                {
                    // don't calculate the size again if we already have it
                    if (size == -1)
                    {
                        using (Stream stream = file.GetStream())
                        {
                            size = stream.Length;
                        }
                    }
                    content = Resources.UnsupportedFormatMessage;
                }
                else
                {
                    content = ReadFileContent(file, out size);
                }
            }

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

            ViewModel.ShowFile(fileInfo);
        }
示例#4
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)
            {
                isBinary = true;
                try
                {
                    // iterate over all plugins, looking for the first one that return non-null content
                    foreach (var viewer in contentViewers)
                    {
                        using (var stream = file.GetStream())
                        {
                            content = viewer.GetView(Path.GetExtension(file.Name), stream);
                            if (content != null)
                            {
                                // found a plugin that can read this file, stop
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    // don't let plugin crash the app
                    content = Resources.PluginFailToReadContent + Environment.NewLine + ex.ToString();
                }

                if (content is string)
                {
                    isBinary = false;
                }
            }

            // if plugins fail to read this file, fall back to the default viewer
            long size = -1;

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

            if (size == -1)
            {
                // This is inefficient but cn be cleaned up later
                using (var str = file.GetStream())
                    using (var ms = new MemoryStream())
                    {
                        str.CopyTo(ms);
                        size = ms.Length;
                    }
            }

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

            ViewModel.ShowFile(fileInfo);
        }
示例#5
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);
        }