private void Encrypt(Dictionary <string, string> files, string encryptionKey) { Workspace.Instance.SetStatus(TaskStatusType.Working, "Encrypting resources..."); int processedCount = 0; foreach (var pair in files) { try { int progressValue = (processedCount / files.Count) * 100; processedCount++; CryptographyProvider.EncryptHeader(pair.Key, pair.Value, encryptionKey); Workspace.Instance.SetStatus(TaskStatusType.Working, $"Encrypting resources...({processedCount}/{files.Count})"); } catch (Exception ex) { Workspace.Instance.Report.AddReportWithIdentifier($"{ex.Message}\r\n{ex.StackTrace}", ReportType.Warning); } } Workspace.Instance.SetStatus(TaskStatusType.Completed, "Completed."); }
private async void OnClickEncrypt() { // Check options. if (string.IsNullOrEmpty(_saveDirectory)) { MessageBox.Show("Save directory path cannot be null or empty.", "MView", MessageBoxButton.OK, MessageBoxImage.Warning); return; } if (!Directory.Exists(_saveDirectory)) { MessageBox.Show("Save directory does not exist.", "MView", MessageBoxButton.OK, MessageBoxImage.Warning); return; } if (string.IsNullOrEmpty(_encryptionKey) || !Regex.IsMatch(_encryptionKey, @"^[a-zA-Z0-9]{32,32}$")) { MessageBox.Show("Invalid encryption key is inputted.", "MView", MessageBoxButton.OK, MessageBoxImage.Warning); return; } if (IsBackupFiles) { if (string.IsNullOrEmpty(_backupDirectory)) { MessageBox.Show("Backup directory path cannot be null or empty.", "MView", MessageBoxButton.OK, MessageBoxImage.Warning); return; } if (!Directory.Exists(_backupDirectory)) { MessageBox.Show("Backup directory does not exist.", "MView", MessageBoxButton.OK, MessageBoxImage.Warning); return; } } // Check files. if (ToolHostWithExplorerViewModel.Instance.IsUseCurrentFile) { if (Workspace.Instance.ActiveDocument != null) { FileViewModelBase file = Workspace.Instance.ActiveDocument; if (file.GetType() != typeof(AudioFileViewModel) && file.GetType() != typeof(ImageFileViewModel)) { MessageBox.Show("You cannot encrypt the file that is currently open.", "MView", MessageBoxButton.OK, MessageBoxImage.Warning); return; } } } if (ToolHostWithExplorerViewModel.Instance.SelectedItems.Count == 0) { MessageBox.Show("Select a file to proceed.", "MView", MessageBoxButton.OK, MessageBoxImage.Warning); return; } var task = Task.Run(() => { // TODO : 백업 기능 추가, 확장자 변경 시키기. if (ToolHostWithExplorerViewModel.Instance.IsUseCurrentFile) { Workspace.Instance.SetStatus(TaskStatusType.Working, "Encrypting resource..."); string filePath = Workspace.Instance.ActiveDocument.FilePath; string savePath = Path.Combine(_saveDirectory, Path.GetFileName(filePath)); CryptographyProvider.EncryptHeader(filePath, savePath, _encryptionKey); Workspace.Instance.SetStatus(TaskStatusType.Completed, "Completed."); } else { Workspace.Instance.SetStatus(TaskStatusType.Loading, "Loading resources..."); // Get selected files. string[] extensions = new string[] { ".ogg", ".m4a", ".wav", ".png" }; List <string> files = Workspace.Instance.FileExplorer.GetSelectedFiles(ToolHostWithExplorerViewModel.Instance.SelectedItems.ToList(), extensions.ToList()); // Make file dictionary. string baseDirectory = ToolHostWithExplorerViewModel.Instance.Items[0].FullName; Dictionary <string, string> fileDictionary = Workspace.Instance.FileExplorer.IndexFromList(files, baseDirectory, _saveDirectory); Dictionary <string, string> modifiedDictionary = new Dictionary <string, string>(); foreach (var pair in fileDictionary) { modifiedDictionary.Add(pair.Key, GetModifiedFilePath(pair.Value)); } Workspace.Instance.SetStatus(TaskStatusType.Ready, "Resources are loaded successfully."); Encrypt(fileDictionary, _encryptionKey); } }); await task; }