Inheritance: PackagePart, IPackageFile
コード例 #1
0
ファイル: ViewContentCommand.cs プロジェクト: grendello/nuget
        private static string ReadFileContent(PackageFile file, out long size) {
            const int MaxLengthToOpen = 10 * 1024;      // limit to 10K 
            const int BufferSize = 2 * 1024;
            char[] buffer = new char[BufferSize];       // read 2K at a time

            StringBuilder sb = new StringBuilder();
            Stream stream = file.GetStream();
            size = stream.Length;
            using (StreamReader reader = new StreamReader(stream)) {
                while (sb.Length < MaxLengthToOpen) {
                    int bytesRead = reader.Read(buffer, 0, BufferSize);
                    if (bytesRead == 0) {
                        break;
                    }
                    else {
                        sb.Append(new string(buffer, 0, bytesRead));
                    }
                }

                // if not reaching the end of the stream yet, append the text "Truncating..."
                if (reader.Peek() > -1) {
                    // continue reading the rest of the current line to avoid dangling line
                    sb.AppendLine(reader.ReadLine());

                    if (reader.Peek() > -1) {
                        sb.AppendLine().AppendLine("*** The rest of the content is truncated. ***");
                    }
                }
            }

            return sb.ToString();
        }
コード例 #2
0
        public static void OpenFileInShellWith(PackageFile file)
        {
            // copy to temporary file
            // create package in the temprary file first in case the operation fails which would
            // override existing file with a 0-byte file.
            string tempFileName = Path.Combine(GetTempFilePath(), file.Name);

            using (Stream tempFileStream = File.Create(tempFileName))
            {
                file.GetStream().CopyTo(tempFileStream);
            }

            if (File.Exists(tempFileName))
            {
                var info = new ProcessStartInfo("rundll32.exe")
                {
                    ErrorDialog     = true,
                    UseShellExecute = false,
                    Arguments       =
                        "shell32.dll,OpenAs_RunDLL " + tempFileName
                };

                Process.Start(info);
            }
        }
コード例 #3
0
        public static void OpenFileInShell(PackageFile file, IUIServices uiServices)
        {
            if (IsExecutableScript(file.Name))
            {
                bool confirm = uiServices.Confirm(
                    String.Format(CultureInfo.CurrentCulture, Resources.OpenExecutableScriptWarning_Title, file.Name),
                    Resources.OpenExecutableScriptWarning,
                    isWarning: true);
                if (!confirm)
                {
                    return;
                }
            }

            // copy to temporary file
            // create package in the temprary file first in case the operation fails which would
            // override existing file with a 0-byte file.
            string tempFileName = Path.Combine(GetTempFilePath(), file.Name);

            using (Stream tempFileStream = File.Create(tempFileName))
            {
                file.GetStream().CopyTo(tempFileStream);
            }

            if (File.Exists(tempFileName))
            {
                Process.Start("explorer.exe", tempFileName);
            }
        }
コード例 #4
0
ファイル: ViewContentCommand.cs プロジェクト: lazlojuly/nuget
        private void ShowFile(PackageFile file)
        {
            bool   isBinary = IsBinaryFile(file.Name);
            long   size;
            string content;

            if (isBinary)
            {
                content = Resources.UnsupportedFormatMessage;
                using (Stream stream = file.GetStream()) {
                    size = stream.Length;
                }
            }
            else
            {
                content = ReadFileContent(file, out size);
            }

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

            ViewModel.ShowFile(fileInfo);
        }
コード例 #5
0
        private static string ReadFileContent(PackageFile file, out bool truncated)
        {
            var buffer = new char[1024 * 32];

            truncated = false;
            using (var stream = file.GetStream())
                using (var reader = new StreamReader(stream))
                {
                    // Read 500 kb
                    const int maxBytes = 500 * 1024;
                    var       sb       = new StringBuilder();

                    int bytesRead;

                    while ((bytesRead = reader.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        sb.Append(buffer, 0, bytesRead);
                        if (sb.Length >= maxBytes)
                        {
                            truncated = true;
                            break;
                        }
                    }

                    return(sb.ToString());
                }
        }
コード例 #6
0
        internal void ReplaceFile(PackageFile oldFile, string newFilePath)
        {
            bool showingFile = PackageViewModel.IsShowingFileContent(oldFile);

            // temporarily remove the old file in order to add a new file
            Children.Remove(oldFile);

            PackageFile newFile = AddFile(newFilePath, isTempFile: false);

            if (newFile != null)
            {
                // new file added successfully, officially delete the old file by disposing it
                oldFile.Dispose();

                if (showingFile)
                {
                    PackageViewModel.ShowFileContent(newFile);
                }
            }
            else
            {
                // otherwise, if the adding failed, restore the old file
                Children.Add(oldFile);
            }
        }
コード例 #7
0
        public static void OpenFileInShellWith(PackageFile file)
        {
            // copy to temporary file
            // create package in the temprary file first in case the operation fails which would
            // override existing file with a 0-byte file.
            string tempFileName = Path.Combine(GetTempFilePath(), file.Name);

            using (Stream tempFileStream = File.Create(tempFileName))
            {
                file.GetStream().CopyTo(tempFileStream);
            }

            if (File.Exists(tempFileName))
            {
                var info = new ProcessStartInfo("rundll32.exe")
                           {
                               ErrorDialog = true,
                               UseShellExecute = false,
                               Arguments =
                                   "shell32.dll,OpenAs_RunDLL " + tempFileName
                           };

                Process.Start(info);
            }
        }
コード例 #8
0
        public static void OpenFileInShell(PackageFile file, IUIServices uiServices)
        {
            if (IsExecutableScript(file.Name))
            {
                bool confirm = uiServices.Confirm(
                    String.Format(CultureInfo.CurrentCulture, Resources.OpenExecutableScriptWarning_Title, file.Name),
                    Resources.OpenExecutableScriptWarning,
                    isWarning: true);
                if (!confirm)
                {
                    return;
                }
            }

            // copy to temporary file
            // create package in the temprary file first in case the operation fails which would
            // override existing file with a 0-byte file.
            string tempFileName = Path.Combine(GetTempFilePath(), file.Name);
            using (Stream tempFileStream = File.Create(tempFileName))
            {
                file.GetStream().CopyTo(tempFileStream);
            }

            if (File.Exists(tempFileName))
            {
                Process.Start("explorer.exe", tempFileName);
            }
        }
コード例 #9
0
        public static void OpenFileInShellWith(PackageFile file)
        {
            DiagnosticsClient.TrackEvent("FileHelper_OpenFileInShellWith");

            // copy to temporary file
            // create package in the temprary file first in case the operation fails which would
            // override existing file with a 0-byte file.
            var tempFileName = Path.Combine(GetTempFilePath(), file.Name);

            using (Stream tempFileStream = File.Create(tempFileName))
                using (var packageStream = file.GetStream())
                {
                    packageStream.CopyTo(tempFileStream);
                }

            if (File.Exists(tempFileName))
            {
                var info = new ProcessStartInfo("rundll32.exe")
                {
                    ErrorDialog     = true,
                    UseShellExecute = false,
                    Arguments       =
                        "shell32.dll,OpenAs_RunDLL " + tempFileName
                };

                try
                {
                    Process.Start(info);
                }
                catch // Possible Win32 exception, nothing we can do
                {
                }
            }
        }
コード例 #10
0
ファイル: ViewContentCommand.cs プロジェクト: grendello/nuget
        private void ShowFile(PackageFile file) {
            bool isBinary = IsBinaryFile(file.Name);
            long size;
            string content;

            if (isBinary) {
                content = Resources.UnsupportedFormatMessage;
                using (Stream stream = file.GetStream()) {
                    size = stream.Length;
                }
            }
            else {
                content = ReadFileContent(file, out size);
            }

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

            ViewModel.ShowFile(fileInfo);
        }
コード例 #11
0
 public FileContentInfo(PackageFile file, string name, object content, bool isTextFile, long size)
 {
     File = file;
     Name = name;
     Content = content;
     IsTextFile = isTextFile;
     Size = size;
 }
コード例 #12
0
        public PackageFile AddFile(string filePath, bool isTempFile)
        {
            if (!File.Exists(filePath))
            {
                throw new ArgumentException("File does not exist.", "filePath");
            }

            string newFileName = System.IO.Path.GetFileName(filePath);

            if (ContainsFolder(newFileName))
            {
                PackageViewModel.UIServices.Show(Resources.FileNameConflictWithExistingDirectory, MessageLevel.Error);
                return(null);
            }

            bool showingRemovedFile = false;

            if (ContainsFile(newFileName))
            {
                bool confirmed = PackageViewModel.UIServices.Confirm(
                    Resources.ConfirmToReplaceExsitingFile_Title,
                    String.Format(CultureInfo.CurrentCulture, Resources.ConfirmToReplaceExsitingFile, newFileName),
                    isWarning: true);

                if (confirmed)
                {
                    var part = this[newFileName] as PackageFile;
                    showingRemovedFile = PackageViewModel.IsShowingFileContent(part);

                    // remove the existing file before adding the new one
                    RemoveChildByName(newFileName);
                }
                else
                {
                    return(null);
                }
            }

            string newTargetPath = this.Path + "\\" + newFileName;
            var    physicalFile  = new PhysicalPackageFile
            {
                SourcePath = filePath,
                TargetPath = newTargetPath
            };
            var newFile = new PackageFile(physicalFile, newFileName, this);

            Children.Add(newFile);
            newFile.IsSelected = true;
            IsExpanded         = true;
            PackageViewModel.NotifyChanges();

            if (showingRemovedFile)
            {
                PackageViewModel.ShowFileContent(newFile);
            }

            return(newFile);
        }
コード例 #13
0
ファイル: PackageFolder.cs プロジェクト: lazlojuly/nuget
        public PackageFile AddFile(string filePath)
        {
            if (!File.Exists(filePath))
            {
                throw new ArgumentException("File does not exist.", "filePath");
            }

            string newFileName = System.IO.Path.GetFileName(filePath);

            if (ContainsFolder(newFileName))
            {
                PackageViewModel.UIServices.Show(Resources.FileNameConflictWithExistingDirectory, Types.MessageLevel.Error);
                return(null);
            }

            bool showingRemovedFile = false;

            if (ContainsFile(newFileName))
            {
                bool confirmed = PackageViewModel.UIServices.Confirm(Resources.ConfirmToReplaceExsitingFile, true);
                if (confirmed)
                {
                    // check if we are currently showing the content of the file to be removed.
                    // if we are, we'll need to show the new content after replacing the file.
                    if (PackageViewModel.ShowContentViewer)
                    {
                        PackagePart part = this[newFileName];
                        if (PackageViewModel.CurrentFileInfo.File == part)
                        {
                            showingRemovedFile = true;
                        }
                    }

                    // remove the existing file before adding the new one
                    RemoveChildByName(newFileName);
                }
                else
                {
                    return(null);
                }
            }

            var physicalFile = new PhysicalFile(filePath);
            var newFile      = new PackageFile(physicalFile, newFileName, this);

            Children.Add(newFile);
            newFile.IsSelected = true;
            this.IsExpanded    = true;
            PackageViewModel.NotifyChanges();

            if (showingRemovedFile)
            {
                ICommand command = PackageViewModel.ViewContentCommand;
                command.Execute(newFile);
            }

            return(newFile);
        }
コード例 #14
0
        private IEnumerable <IPackageContentViewer> FindContentViewer(PackageFile file)
        {
            string extension = Path.GetExtension(file.Name);

            return(from p in ViewModel.ContentViewerMetadata
                   where p.Metadata.SupportedExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase)
                   orderby p.Metadata.Priority
                   select p.Value);
        }
コード例 #15
0
 internal void ReplaceFile(PackageFile oldFile)
 {
     var result = PackageViewModel.UIServices.OpenFileDialog("Select New File", "All files (*.*)|*.*",
                                                              out var selectedFileName);
     if (result)
     {
         ReplaceFile(oldFile, selectedFileName);
     }
 }
コード例 #16
0
        private IEnumerable <IPackageContentViewer> FindContentViewer(PackageFile file)
        {
            var extension = Path.GetExtension(file.Name);

            return(from p in ViewModel.ContentViewerMetadata
                   where AppCompat.IsWindows10S ? p.Metadata.SupportsWindows10S : true // Filter out incompatible addins on 10s
                   where p.Metadata.SupportedExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase)
                   orderby p.Metadata.Priority
                   select p.Value);
        }
コード例 #17
0
        private void OpenContentFileExecute(object parameter)
        {
            parameter = parameter ?? SelectedItem;
            PackageFile file = parameter as PackageFile;

            if (file != null)
            {
                FileHelper.OpenFileInShell(file, UIServices);
            }
        }
コード例 #18
0
        internal void ReplaceFile(PackageFile oldFile)
        {
            string?selectedFileName = null;
            var    result           = PackageViewModel?.UIServices.OpenFileDialog("Select New File", "All files (*.*)|*.*",
                                                                                  out selectedFileName) ?? false;

            if (result)
            {
                ReplaceFile(oldFile, selectedFileName !);
            }
        }
コード例 #19
0
        private void SaveContentExecute(PackageFile file)
        {
            string selectedFileName;
            string title  = "Save " + file.Name;
            string filter = "All files (*.*)|*.*";

            if (UIServices.OpenSaveFileDialog(title, file.Name, filter, out selectedFileName))
            {
                using (FileStream fileStream = File.OpenWrite(selectedFileName)) {
                    file.GetStream().CopyTo(fileStream);
                }
            }
        }
コード例 #20
0
        private static string ReadFileContent(PackageFile file, out long size)
        {
            using (var stream = file.GetStream())
                using (var ms = new MemoryStream())
                    using (var reader = new StreamReader(ms))
                    {
                        stream.CopyTo(ms);
                        size        = ms.Length;
                        ms.Position = 0;

                        return(reader.ReadToEnd());
                    }
        }
コード例 #21
0
        private void SaveContentExecute(PackageFile file)
        {
            var          title  = "Save " + file.Name;
            const string filter = "All files (*.*)|*.*";

            if (UIServices.OpenSaveFileDialog(title, file.Name, /* initial directory */ null, filter, /* overwritePrompt */ true,
                                              out var selectedFileName, out var filterIndex))
            {
                using (var fileStream = File.OpenWrite(selectedFileName))
                {
                    file.GetStream().CopyTo(fileStream);
                }
            }
        }
コード例 #22
0
        public void AddFile(PackageFile file, bool makeCopy = false)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            if (Contains(file))
            {
                return;
            }

            PackagePart newFile;

            if (makeCopy)
            {
                string fileCopyPath;
                using (Stream originalFileStream = file.GetStream())
                {
                    fileCopyPath = FileHelper.CreateTempFile(file.Name, originalFileStream);
                }

                string newTargetPath = this.Path + "\\" + file.Name;
                var    physicalFile  = new PhysicalPackageFile
                {
                    SourcePath = fileCopyPath,
                    TargetPath = newTargetPath
                };

                newFile = new PackageFile(physicalFile, file.Name, this);
            }
            else
            {
                // detach from current parent
                if (file.Parent != null)
                {
                    file.Parent.Detach(file);
                }

                newFile = file;
            }

            Attach(newFile);
            newFile.IsSelected = true;
            IsExpanded         = true;
            PackageViewModel.NotifyChanges();
        }
コード例 #23
0
        private void AddBuildFileCommandExecute(string extension)
        {
            string fileName   = PackageMetadata.Id + extension;
            string sourcePath = FileHelper.CreateTempFile(fileName, Constants.ContentForBuildFile);

            var selectedFolder = SelectedItem as PackageFolder;

            if (selectedFolder != null)
            {
                PackageFile file = selectedFolder.AddFile(sourcePath, isTempFile: true);
                // file can be null if it collides with other files in the same directory
                if (file != null)
                {
                    EditFileCommandExecute(file);
                }
            }
        }
コード例 #24
0
        public static void OpenFileInShell(PackageFile file, IUIServices uiServices)
        {
            if (file is null)
            {
                throw new ArgumentNullException(nameof(file));
            }
            if (uiServices is null)
            {
                throw new ArgumentNullException(nameof(uiServices));
            }

            if (IsExecutableScript(file.Name))
            {
                var confirm = uiServices.Confirm(
                    string.Format(CultureInfo.CurrentCulture, Resources.OpenExecutableScriptWarning_Title, file.Name),
                    Resources.OpenExecutableScriptWarning,
                    isWarning: true);
                if (!confirm)
                {
                    return;
                }
            }

            // copy to temporary file
            // create package in the temprary file first in case the operation fails which would
            // override existing file with a 0-byte file.
            var tempFileName = Path.Combine(GetTempFilePath(), file.Name);

            using (Stream tempFileStream = File.Create(tempFileName))
                using (var packageStream = file.GetStream())
                {
                    packageStream.CopyTo(tempFileStream);
                }

            if (File.Exists(tempFileName))
            {
                try
                {
                    Process.Start("explorer.exe", tempFileName);
                }
                catch // Possible Win32 exception, nothing we can do
                {
                }
            }
        }
コード例 #25
0
        private void AddScriptCommandExecute(string scriptName)
        {
            string content = scriptName.Equals("init.ps1", StringComparison.OrdinalIgnoreCase)
                                 ? Constants.ContentForInit
                                 : Constants.ContentForInstall;
            string sourcePath = FileHelper.CreateTempFile(scriptName, content);

            var selectedFolder = SelectedItem as PackageFolder;

            if (selectedFolder != null)
            {
                PackageFile file = selectedFolder.AddFile(sourcePath, isTempFile: true);
                // file can be null if it collides with other files in the same directory
                if (file != null)
                {
                    EditFileCommandExecute(file);
                }
            }
        }
コード例 #26
0
        private void AddNewFileToFolder(PackageFolder folder)
        {
            string newName;
            bool   result = UIServices.OpenRenameDialog(
                "NewFile.txt",
                "Provide name for the new file.",
                out newName);

            if (result)
            {
                string      sourcePath = FileHelper.CreateTempFile(newName);
                PackageFile file       = folder.AddFile(sourcePath, isTempFile: true);
                // file can be null if it collides with other files in the same directory
                if (file != null)
                {
                    EditFileCommandExecute(file);
                }
            }
        }
コード例 #27
0
 private void SaveContentExecute(PackageFile file)
 {
     try
     {
         var          title  = "Save " + file.Name;
         const string filter = "All files (*.*)|*.*";
         if (UIServices.OpenSaveFileDialog(title, file.Name, /* initial directory */ null, filter, /* overwritePrompt */ true,
                                           out var selectedFileName, out var filterIndex))
         {
             using (var fileStream = File.Open(selectedFileName, FileMode.Create, FileAccess.Write, FileShare.Read))
                 using (var packageStream = file.GetStream())
                 {
                     packageStream.CopyTo(fileStream);
                 }
         }
     }
     catch (Exception e)
     {
         UIServices.Show(e.Message, MessageLevel.Error);
     }
 }
コード例 #28
0
        private static bool IsMicrosoftFile(PackageFile file)
        {
            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();

                    if (isValidSig == SignatureCheckResult.Valid && sigs.Count > 0)
                    {
                        return(sigs[0].SigningCertificate.Subject.EndsWith(", O=Microsoft Corporation, L=Redmond, S=Washington, C=US", StringComparison.OrdinalIgnoreCase));
                    }
                }

            return(false);
        }
コード例 #29
0
ファイル: ViewContentCommand.cs プロジェクト: lazlojuly/nuget
        private static string ReadFileContent(PackageFile file, out long size)
        {
            const int MaxLengthToOpen = 10 * 1024;      // limit to 10K
            const int BufferSize      = 2 * 1024;

            char[] buffer = new char[BufferSize];       // read 2K at a time

            StringBuilder sb     = new StringBuilder();
            Stream        stream = file.GetStream();

            size = stream.Length;
            using (StreamReader reader = new StreamReader(stream)) {
                while (sb.Length < MaxLengthToOpen)
                {
                    int bytesRead = reader.Read(buffer, 0, BufferSize);
                    if (bytesRead == 0)
                    {
                        break;
                    }
                    else
                    {
                        sb.Append(new string(buffer, 0, bytesRead));
                    }
                }

                // if not reaching the end of the stream yet, append the text "Truncating..."
                if (reader.Peek() > -1)
                {
                    // continue reading the rest of the current line to avoid dangling line
                    sb.AppendLine(reader.ReadLine());

                    if (reader.Peek() > -1)
                    {
                        sb.AppendLine().AppendLine("*** The rest of the content is truncated. ***");
                    }
                }
            }

            return(sb.ToString());
        }
コード例 #30
0
        public void AddFile(PackageFile file, bool makeCopy = false)
        {
            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            if (Contains(file))
            {
                return;
            }

            PackagePart newFile;

            if (makeCopy)
            {
                string fileCopyPath;
                using (var originalFileStream = file.GetStream())
                {
                    fileCopyPath = FileHelper.CreateTempFile(file.Name, originalFileStream);
                }

                var newTargetPath = Path + "\\" + file.Name;
                var physicalFile  = new DiskPackageFile(newTargetPath, fileCopyPath);

                newFile = new PackageFile(physicalFile, file.Name, this);
            }
            else
            {
                ((PackageFolder?)file.Parent)?.Detach(file);

                newFile = file;
            }

            Attach(newFile);
            newFile.IsSelected = true;
            IsExpanded         = true;
            PackageViewModel?.NotifyChanges();
        }
コード例 #31
0
 internal bool IsShowingFileContent(PackageFile file)
 {
     return(ShowContentViewer && CurrentFileInfo.File == file);
 }
コード例 #32
0
 private void SaveContentExecute(PackageFile file)
 {
     string selectedFileName;
     string title = "Save " + file.Name;
     const string filter = "All files (*.*)|*.*";
     int filterIndex;
     if (UIServices.OpenSaveFileDialog(title, file.Name, /* initial directory */ null, filter, /* overwritePrompt */ true,
                                       out selectedFileName, out filterIndex))
     {
         using (FileStream fileStream = File.OpenWrite(selectedFileName))
         {
             file.GetStream().CopyTo(fileStream);
         }
     }
 }
コード例 #33
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)
                    {
                        // 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);
        }
コード例 #34
0
 internal bool IsShowingFileContent(PackageFile file)
 {
     return ShowContentViewer && CurrentFileInfo.File == file;
 }
コード例 #35
0
 private IEnumerable<IPackageContentViewer> FindContentViewer(PackageFile file)
 {
     string extension = Path.GetExtension(file.Name);
     return from p in ViewModel.ContentViewerMetadata
            where p.Metadata.SupportedExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase)
            orderby p.Metadata.Priority
            select p.Value;
 }
コード例 #36
0
 private bool SaveContentCanExecute(PackageFile file)
 {
     return(!IsInEditFileMode);
 }
コード例 #37
0
        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 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
            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);
        }
コード例 #38
0
 internal void ShowFileContent(PackageFile file)
 {
     ViewContentCommand.Execute(file);
 }
コード例 #39
0
ファイル: PackageViewModel.cs プロジェクト: grendello/nuget
 private void SaveContentExecute(PackageFile file)
 {
     string selectedFileName;
     string title = "Save " + file.Name;
     string filter = "All files (*.*)|*.*";
     if (UIServices.OpenSaveFileDialog(title, file.Name, filter, out selectedFileName)) {
         using (FileStream fileStream = File.OpenWrite(selectedFileName)) {
             file.GetStream().CopyTo(fileStream);
         }
     }
 }
コード例 #40
0
        internal void ReplaceFile(PackageFile oldFile, string newFilePath)
        {
            bool showingFile = PackageViewModel.IsShowingFileContent(oldFile);

            // temporarily remove the old file in order to add a new file
            Children.Remove(oldFile);

            PackageFile newFile = AddFile(newFilePath, isTempFile: false);
            if (newFile != null)
            {
                // new file added successfully, officially delete the old file by disposing it
                oldFile.Dispose();

                if (showingFile)
                {
                    PackageViewModel.ShowFileContent(newFile);
                }
            }
            else
            {
                // otherwise, if the adding failed, restore the old file
                Children.Add(oldFile);
            }
        }
コード例 #41
0
 internal void ReplaceFile(PackageFile oldFile)
 {
     string selectedFileName;
     bool result = PackageViewModel.UIServices.OpenFileDialog("Select New File", "All files (*.*)|*.*",
                                                              out selectedFileName);
     if (result)
     {
         ReplaceFile(oldFile, selectedFileName);
     }
 }
コード例 #42
0
        public void AddFile(PackageFile file, bool makeCopy = false)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            if (Contains(file))
            {
                return;
            }

            PackagePart newFile;

            if (makeCopy)
            {
                string fileCopyPath;
                using (Stream originalFileStream = file.GetStream())
                {
                    fileCopyPath = FileHelper.CreateTempFile(file.Name, originalFileStream);
                }

                string newTargetPath = this.Path + "\\" + file.Name;
                var physicalFile = new PhysicalPackageFile(isTempFile: true, originalPath: fileCopyPath, targetPath: newTargetPath);

                newFile = new PackageFile(physicalFile, file.Name, this);
            }
            else
            {
                // detach from current parent
                if (file.Parent != null)
                {
                    file.Parent.Detach(file);
                }

                newFile = file;
            }

            Attach(newFile);
            newFile.IsSelected = true;
            IsExpanded = true;
            PackageViewModel.NotifyChanges();
        }
コード例 #43
0
        public PackageFile AddFile(string filePath, bool isTempFile)
        {
            if (!File.Exists(filePath))
            {
                throw new ArgumentException("File does not exist.", "filePath");
            }

            string newFileName = System.IO.Path.GetFileName(filePath);
            if (ContainsFolder(newFileName))
            {
                PackageViewModel.UIServices.Show(Resources.FileNameConflictWithExistingDirectory, MessageLevel.Error);
                return null;
            }

            bool showingRemovedFile = false;
            if (ContainsFile(newFileName))
            {
                bool confirmed = PackageViewModel.UIServices.Confirm(
                    Resources.ConfirmToReplaceExsitingFile_Title,
                    String.Format(CultureInfo.CurrentCulture, Resources.ConfirmToReplaceExsitingFile, newFileName),
                    isWarning: true);

                if (confirmed)
                {
                    var part = this[newFileName] as PackageFile;
                    showingRemovedFile = PackageViewModel.IsShowingFileContent(part);

                    // remove the existing file before adding the new one
                    RemoveChildByName(newFileName);
                }
                else
                {
                    return null;
                }
            }

            string newTargetPath = this.Path + "\\" + newFileName;
            var physicalFile = new PhysicalPackageFile(isTempFile, filePath, newTargetPath);
            var newFile = new PackageFile(physicalFile, newFileName, this);

            Children.Add(newFile);
            newFile.IsSelected = true;
            IsExpanded = true;
            PackageViewModel.NotifyChanges();

            if (showingRemovedFile)
            {
                PackageViewModel.ShowFileContent(newFile);
            }

            return newFile;
        }
コード例 #44
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);
        }
コード例 #45
0
 private bool SaveContentCanExecute(PackageFile file)
 {
     return !IsInEditFileMode;
 }
コード例 #46
0
 internal void ShowFileContent(PackageFile file)
 {
     ViewContentCommand.Execute(file);
 }
コード例 #47
0
ファイル: PackageFolder.cs プロジェクト: jacksonh/nuget
        public PackageFile AddFile(string filePath)
        {
            if (!File.Exists(filePath)) {
                throw new ArgumentException("File does not exist.", "filePath");
            }

            string newFileName = System.IO.Path.GetFileName(filePath);
            if (ContainsFolder(newFileName)) {
                PackageViewModel.UIServices.Show(Resources.FileNameConflictWithExistingDirectory, Types.MessageLevel.Error);
                return null;
            }

            bool showingRemovedFile = false;
            if (ContainsFile(newFileName)) {
                bool confirmed = PackageViewModel.UIServices.Confirm(Resources.ConfirmToReplaceExsitingFile, true);
                if (confirmed) {
                    // check if we are currently showing the content of the file to be removed.
                    // if we are, we'll need to show the new content after replacing the file.
                    if (PackageViewModel.ShowContentViewer) {
                        PackagePart part = this[newFileName];
                        if (PackageViewModel.CurrentFileInfo.File == part) {
                            showingRemovedFile = true;
                        }
                    }

                    // remove the existing file before adding the new one
                    RemoveChildByName(newFileName);
                }
                else {
                    return null;
                }
            }

            var physicalFile = new PhysicalFile(filePath);
            var newFile = new PackageFile(physicalFile, newFileName, this);
            Children.Add(newFile);
            newFile.IsSelected = true;
            this.IsExpanded = true;
            PackageViewModel.NotifyChanges();

            if (showingRemovedFile) {
                ICommand command = PackageViewModel.ViewContentCommand;
                command.Execute(newFile);
            }

            return newFile;
        }