package com.zhangmeng.minio.controller; import com.google.common.reflect.ClassPath; import com.zhangmeng.minio.model.BucketFile; import com.zhangmeng.minio.utils.AlertUtils; import com.zhangmeng.minio.utils.EventBusUtils; import com.zhangmeng.minio.utils.MinioUtils; import io.minio.messages.Bucket; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.collections.ObservableMap; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.control.*; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyCodeCombination; import javafx.scene.input.KeyCombination; import javafx.scene.layout.AnchorPane; import javafx.stage.FileChooser; import javafx.stage.Stage; import org.apache.commons.lang3.ClassPathUtils; import java.io.*; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Properties; /** * @author zhangmeng * @version 1.0 * @date 2024-03-07 15:29 */ public class MinioController { @FXML public AnchorPane center; private final ObservableList list = FXCollections.observableArrayList(); @FXML public MenuItem config_file; @FXML public MenuItem bucket_choose; @FXML public MenuItem reload_config; @FXML public Button upload; @FXML public Button choose_upload_file; @FXML public TextField upload_path; @FXML public TextField bucket_name; @FXML public Button bucket_btn; @FXML public TextField endpoint; @FXML public TextField accessKey; @FXML public TextField secretKey; @FXML public Button save_config; @FXML public ComboBox backet_list; private final ObservableList backet_item_list = FXCollections.observableArrayList(); public TableView tableview; @FXML public TableColumn file_name; @FXML public TableColumn file_size; @FXML public TableColumn file_url; @FXML public TableColumn file_bucket; private File upload_file; public MinioController() { EventBusUtils.getDefault().register(this); } @FXML public void initialize() { tableview.setItems(list); file_name.setCellValueFactory(new PropertyValueFactory("fileName")); file_size.setCellValueFactory(new PropertyValueFactory("size")); file_url.setCellValueFactory(new PropertyValueFactory("url")); file_bucket.setCellValueFactory(new PropertyValueFactory("bucketName")); file_name.prefWidthProperty().bind(tableview.widthProperty().divide(tableview.getColumns().size())); file_size.prefWidthProperty().bind(tableview.widthProperty().divide(tableview.getColumns().size())); file_url.prefWidthProperty().bind(tableview.widthProperty().divide(tableview.getColumns().size())); file_bucket.prefWidthProperty().bind(tableview.widthProperty().divide(tableview.getColumns().size())); // 创建上下文菜单 final ContextMenu contextMenu = new ContextMenu(); MenuItem menuItem = new MenuItem("删除"); menuItem.setOnAction(event -> { list.remove(tableview.getSelectionModel().getSelectedItem()); }); contextMenu.getItems().add(menuItem); // 添加事件监听器来处理上下文菜单的显示 tableview.setOnContextMenuRequested(event -> { // 只有当表格有选中项时才显示上下文菜单 if (!tableview.getSelectionModel().isEmpty()) { contextMenu.show(tableview, event.getScreenX(), event.getScreenY()); } }); tableview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue observable, BucketFile oldValue, BucketFile newValue) { if (newValue != null) { System.out.println(newValue.getFileName()); } } }); //绑定快捷键 KeyCombination kc1 = KeyCombination.valueOf("ctrl+q"); config_file.setAccelerator(kc1); config_file.setOnAction(event -> { FileInputStream fileInputStream = null; try { FXMLLoader fxmlLoader = new FXMLLoader(this.getClass().getResource("/fxml/config.fxml")); AnchorPane root = fxmlLoader.load(); ObservableMap namespace = fxmlLoader.getNamespace(); TextArea text_area = (TextArea) namespace.get("text_area"); Button save_p = (Button) namespace.get("save_p"); URL resource = this.getClass().getResource("/minio.properties"); fileInputStream = new FileInputStream(new File(resource.getFile())); byte[] bytes = fileInputStream.readAllBytes(); text_area.setText(new String(bytes, StandardCharsets.UTF_8)); Stage stage = AlertUtils.alert("minio 配置文件",root,600,410,(Stage) MinioUtils.objectMap.get(MinioUtils.primaryStage)); save_p.setOnAction(event1 -> { stage.close(); try { FileWriter fileWriter = new FileWriter(resource.getFile()); fileWriter.write(text_area.getText()); fileWriter.flush(); fileWriter.close(); load_property(); } catch (IOException e) { e.printStackTrace(); } AlertUtils.alert_msg("保存成功!"); }); } catch (IOException e) { e.printStackTrace(); } finally { if (fileInputStream != null){ try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }); KeyCombination kc2 = KeyCombination.valueOf("ctrl+r"); reload_config.setAccelerator(kc2); KeyCombination kc3 = KeyCombination.valueOf("ctrl+b"); bucket_choose.setAccelerator(kc3); MinioUtils.objectMap.put(MinioUtils.minioController,this); choose_upload_file.setOnAction(event -> { //打开文件 Stage stage = new Stage(); FileChooser dc = new FileChooser(); dc.setTitle("文件选择"); dc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("文件类型", "*.*")); File file = dc.showOpenDialog(stage); if (file != null){ //展示路径 upload_file = file; upload_path.setText(file.getAbsolutePath()); } }); upload.setOnAction(event -> { if (upload_file == null){ AlertUtils.alert_warning("请选择上传的文件后再试!"); return; } MinioUtils.upload_file(upload_file); }); bucket_btn.setOnAction(event -> { if (endpoint.getText().length() == 0 ){ AlertUtils.alert_warning("endpoint 不能为空!"); return; } if (accessKey.getText().length() == 0 ){ AlertUtils.alert_warning("accessKey 不能为空!"); return; } if (secretKey.getText().length() == 0 ){ AlertUtils.alert_warning("secretKey 不能为空!"); return; } if (bucket_name.getText().length() == 0 ){ AlertUtils.alert_warning("bucket_name 不能为空!"); return; } MinioUtils.createBucket(bucket_name.getText()); }); load_property(); save_config.setOnAction(event -> save_properties()); backet_list.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue observable, String oldValue, String newValue) { if (newValue != null){ MinioUtils.objectMap.put(MinioUtils.current_bucket,newValue); reload(); } } }); } public void save_properties(){ if (endpoint.getText().length() == 0 ){ AlertUtils.alert_warning("endpoint 不能为空!"); return; } if (accessKey.getText().length() == 0 ){ AlertUtils.alert_warning("accessKey 不能为空!"); return; } if (secretKey.getText().length() == 0 ){ AlertUtils.alert_warning("secretKey 不能为空!"); return; } // 创建一个Properties对象 Properties properties = new Properties(); URL resource = this.getClass().getResource("/minio.properties"); if (resource != null){ InputStream input = null; OutputStream output = null; try { String file = resource.getFile(); input = new FileInputStream(new File(file)); output = new FileOutputStream(new File(file)); // load a properties file properties.load(input); properties.put("endpoint",this.endpoint.getText()); properties.put("accessKey",this.accessKey.getText()); properties.put("secretKey",this.secretKey.getText()); properties.store(output,"save"); } catch (IOException e) { e.printStackTrace(); } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } if (output != null){ try { output.close(); }catch (IOException e){ e.printStackTrace(); } } } load_property(); AlertUtils.alert_warning("配置文件保存成功!"); } } public void load_property(){ Properties prop = new Properties(); URL resource = this.getClass().getResource("/minio.properties"); if (resource != null){ String file = resource.getFile(); InputStream input = null; try { input = new FileInputStream(new File(file)); // load a properties file prop.load(input); // get the property value and print it out String endpoint = prop.getProperty("endpoint"); this.endpoint.setText(endpoint); String accessKey = prop.getProperty("accessKey"); this.accessKey.setText(accessKey); String secretKey = prop.getProperty("secretKey"); this.secretKey.setText(secretKey); MinioUtils.objectMap.put("endpoint",endpoint); MinioUtils.objectMap.put("accessKey",accessKey); MinioUtils.objectMap.put("secretKey",secretKey); } catch (IOException ex) { ex.printStackTrace(); } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } } } //获取所有储存桶 List buckets = MinioUtils.getAllBuckets(); if (buckets.size()>0){ for (Bucket bucket : buckets) { backet_item_list.add(bucket.name()); } } this.backet_list.setItems(backet_item_list); } @FXML private void bucket_choose_action(){ Stage primaryStage = (Stage) this.choose_upload_file.getScene().getWindow(); FXMLLoader fxmlLoader =new FXMLLoader( getClass().getResource("/fxml/bucket_choose.fxml")); AnchorPane bucket_choose = null; try { bucket_choose = fxmlLoader.load(); } catch (IOException e) { e.printStackTrace(); } Stage stage = AlertUtils.alert("设置", bucket_choose, primaryStage); MinioUtils.objectMap.put(MinioUtils.bucket_choose_action_stage,stage); } @FXML public void reload(){ Object obj = MinioUtils.objectMap.get(MinioUtils.current_bucket); if (obj == null){ AlertUtils.alert_warning("请选择bucket再试"); return; } String bucketName = obj.toString(); List bucketFiles = MinioUtils.getFileList(bucketName); if (bucketFiles.size()>0){ list.clear(); list.addAll(bucketFiles); } } }