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);
        }
Exemple #2
0
 private bool CanEditFileCommandExecute(PackagePart file)
 {
     return((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.TrackException(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);
        }
Exemple #4
0
        public void AddDraggedAndDroppedFiles(PackageFolder folder, string[] fileNames)
        {
            if (folder == null)
            {
                bool?rememberedAnswer = null;

                for (int i = 0; i < fileNames.Length; i++)
                {
                    string file = fileNames[i];
                    if (File.Exists(file))
                    {
                        bool movingFile;

                        PackageFolder targetFolder;
                        string        guessFolderName = FileHelper.GuessFolderNameFromFile(file);

                        if (rememberedAnswer == null)
                        {
                            // ask user if he wants to move file
                            Tuple <bool?, bool> answer = UIServices.ConfirmMoveFile(
                                Path.GetFileName(file),
                                guessFolderName, fileNames.Length - i - 1);

                            if (answer.Item1 == null)
                            {
                                // user presses Cancel
                                break;
                            }

                            movingFile = (bool)answer.Item1;
                            if (answer.Item2)
                            {
                                rememberedAnswer = answer.Item1;
                            }
                        }
                        else
                        {
                            movingFile = (bool)rememberedAnswer;
                        }

                        if (movingFile)
                        {
                            if (RootFolder.ContainsFolder(guessFolderName))
                            {
                                targetFolder = (PackageFolder)RootFolder[guessFolderName];
                            }
                            else
                            {
                                targetFolder = RootFolder.AddFolder(guessFolderName);
                            }
                        }
                        else
                        {
                            targetFolder = RootFolder;
                        }

                        targetFolder.AddFile(file, isTempFile: false);
                    }
                    else if (Directory.Exists(file))
                    {
                        RootFolder.AddPhysicalFolder(file);
                    }
                }
            }
            else
            {
                foreach (string file in fileNames)
                {
                    if (File.Exists(file))
                    {
                        folder.AddFile(file, isTempFile: false);
                    }
                    else if (Directory.Exists(file))
                    {
                        folder.AddPhysicalFolder(file);
                    }
                }
            }
        }