|
1 |
| -use egui::Ui; |
| 1 | +use crate::app::{encryption, TemplateApp}; |
| 2 | +use egui::{Ui, ComboBox}; |
| 3 | +use rfd::FileDialog; |
| 4 | +use std::fs; |
| 5 | +use encryption::{EncryptionMethod, caesar_encrypt_file, xor_encrypt_file}; |
2 | 6 |
|
3 |
| -pub fn encrypt_file_view(ui: &mut Ui) { |
| 7 | +pub fn encrypt_file_view(ui: &mut Ui, app: &mut TemplateApp) { |
4 | 8 | ui.label("📁 Encrypt File");
|
5 |
| - ui.label("Feature coming soon: allow user to select a file, then encrypt it."); |
| 9 | + |
| 10 | + ui.horizontal(|ui| { |
| 11 | + ui.label("Encryption Key:"); |
| 12 | + ui.text_edit_singleline(&mut app.encryption_key); |
| 13 | + }); |
| 14 | + |
| 15 | + ui.horizontal(|ui| { |
| 16 | + ui.label("Encryption Method"); |
| 17 | + ComboBox::from_id_salt("file_encryption_method_select") |
| 18 | + .selected_text(match app.selected_encryption { |
| 19 | + EncryptionMethod::Caesar => "Caesar Cipher", |
| 20 | + EncryptionMethod::XOR => "XOR Cipher", |
| 21 | + }) |
| 22 | + .show_ui(ui, |ui| { |
| 23 | + ui.selectable_value(&mut app.selected_encryption, EncryptionMethod::Caesar, "Caesar Cipher"); |
| 24 | + ui.selectable_value(&mut app.selected_encryption, EncryptionMethod::XOR, "XOR Cipher"); |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + if ui.button("Select File to Encrypt").clicked() { |
| 29 | + if let Some(path) = FileDialog::new().pick_file() { |
| 30 | + if let Ok(data) = fs::read_to_string(&path) { |
| 31 | + let encrypted = match app.selected_encryption { |
| 32 | + EncryptionMethod::Caesar => caesar_encrypt_file(&data, &app.encryption_key), |
| 33 | + EncryptionMethod::XOR => xor_encrypt_file(&data, &app.encryption_key), |
| 34 | + }; |
| 35 | + |
| 36 | + let output_path = path.with_file_name(format!( |
| 37 | + "encrypted_{}", |
| 38 | + path.file_name().unwrap().to_string_lossy() |
| 39 | + )); |
| 40 | + |
| 41 | + if let Err(err) = fs::write(&output_path, encrypted) { |
| 42 | + app.output_text = format!("Failed to write output: {}", err); |
| 43 | + } else { |
| 44 | + app.output_text = format!("Encryption successful. Saved to:\n{}", output_path.display()); |
| 45 | + } |
| 46 | + } else { |
| 47 | + app.output_text = "Failed to read the selected file.".to_owned(); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + ui.label("Status:"); |
| 53 | + ui.text_edit_multiline(&mut app.output_text); |
6 | 54 | }
|
0 commit comments