MainController.java 24.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
package org.code4everything.qiniu.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.qiniu.common.QiniuException;
import com.zhazhapan.util.Checker;
import com.zhazhapan.util.Formatter;
import com.zhazhapan.util.ThreadPool;
import com.zhazhapan.util.Utils;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.chart.AreaChart;
import javafx.scene.control.*;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.input.DragEvent;
import javafx.scene.input.TransferMode;
import javafx.stage.FileChooser;
import javafx.util.Pair;
import org.code4everything.qiniu.QiniuApplication;
import org.code4everything.qiniu.api.SdkConfigurer;
import org.code4everything.qiniu.api.SdkManager;
import org.code4everything.qiniu.constant.QiniuValueConsts;
import org.code4everything.qiniu.model.FileBean;
import org.code4everything.qiniu.service.QiniuService;
import org.code4everything.qiniu.util.ConfigUtils;
import org.code4everything.qiniu.util.DialogUtils;
import org.code4everything.qiniu.util.QiniuDialog;
import org.code4everything.qiniu.util.QiniuUtils;

import java.awt.*;
import java.io.File;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;

/**
 * 界面控制类
 *
 * @author pantao
 */
public class MainController {

    private static final String UPLOAD_STATUS_TEMPLATE = "{}\tsuccess\t{}{}\t{}";

    private static MainController mainController = null;

    private final QiniuService service = new QiniuService();

    private final QiniuDialog dialog = new QiniuDialog();

    @FXML
    public ComboBox<String> bucketCB;

    @FXML
    public TextField zoneTF;

    @FXML
    public TextArea uploadStatusTA;

    @FXML
    public ComboBox<String> prefixCB;

    @FXML
    public TableView<FileBean> resTV;

    @FXML
    public TextField searchTF;

    @FXML
    public CheckBox recursiveCB;

    @FXML
    public CheckBox keepPathCB;

    private ObservableList<FileBean> resData = null;

    /**
     * 空间总文件数
     */
    private int dataLength = 0;

    /**
     * 空间使用总大小
     */
    private long dataSize = 0;

    @FXML
    private TextArea selectedFileTA;

    @FXML
    private TextField domainTF;

    @FXML
    private TableColumn<FileBean, String> nameTC;

    @FXML
    private TableColumn<FileBean, String> typeTC;

    @FXML
    private TableColumn<FileBean, String> sizeTC;

    @FXML
    private TableColumn<FileBean, String> timeTC;

    @FXML
    private Label sizeLabel;

    @FXML
    private Label lengthLabel;

    @FXML
    private AreaChart<String, Long> fluxAC;

    @FXML
    private AreaChart<String, Long> bandwidthAC;

    @FXML
    private DatePicker startDP;

    @FXML
    private DatePicker endDP;

    @FXML
    private ComboBox<String> fluxUnitCB;

    @FXML
    private ComboBox<String> bandwidthUnitCB;

    private String status = "";

    /**
     * 父文件夹路径
     */
    private List<String> rootPath = new ArrayList<>();

    public static MainController getInstance() {
        return mainController;
    }

    public ObservableList<FileBean> getResData() {
        return resData;
    }

    public void setResData(ObservableList<FileBean> resData) {
        this.resData = resData;
    }

    public int getDataLength() {
        return dataLength;
    }

    public void setDataLength(int dataLength) {
        this.dataLength = dataLength;
    }

    public long getDataSize() {
        return dataSize;
    }

    public void setDataSize(long dataSize) {
        this.dataSize = dataSize;
    }

    /**
     * 初始化
     */
    @FXML
    private void initialize() {
        mainController = this;
        nameTC.setCellValueFactory(new PropertyValueFactory<>("name"));
        // 设置文件名可编辑
        nameTC.setCellFactory(TextFieldTableCell.forTableColumn());
        nameTC.setOnEditCommit(value -> {
            String name;
            FileBean fileBean = value.getTableView().getItems().get(value.getTablePosition().getRow());
            // 编辑后重命名文件
            if (service.renameFile(bucketCB.getValue(), value.getOldValue(), value.getNewValue())) {
                name = value.getNewValue();
            } else {
                name = value.getOldValue();
            }
            if (Checker.isNotEmpty(searchTF.getText())) {
                resData.get(resData.indexOf(fileBean)).setName(name);
            }
            fileBean.setName(name);
        });
        typeTC.setCellValueFactory(new PropertyValueFactory<>("type"));
        // 设置文件类型可编辑
        typeTC.setCellFactory(TextFieldTableCell.forTableColumn());
        typeTC.setOnEditCommit(value -> {
            String type;
            FileBean fileBean = value.getTableView().getItems().get(value.getTablePosition().getRow());
            // 编辑后更新文件类型
            if (service.changeType(fileBean.getName(), value.getNewValue(), bucketCB.getValue())) {
                type = value.getNewValue();
            } else {
                type = value.getOldValue();
            }
            if (Checker.isNotEmpty(searchTF.getText())) {
                resData.get(resData.indexOf(fileBean)).setType(type);
            }
            fileBean.setType(type);
        });
        sizeTC.setCellValueFactory(new PropertyValueFactory<>("size"));
        timeTC.setCellValueFactory(new PropertyValueFactory<>("time"));
        // 设置表格允许多选
        resTV.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        // 设置默认的开始和结束日期,并事件刷新数据
        endDP.setValue(LocalDate.now());
        long startTime = System.currentTimeMillis() - QiniuValueConsts.DATE_SPAN_OF_THIRTY_ONE;
        LocalDate localEndDate = Formatter.dateToLocalDate(new Date(startTime));
        startDP.setValue(localEndDate);
        // 设置桶下拉框改变事件,改变后配置新的上传环境
        bucketCB.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            zoneTF.setText(QiniuApplication.getConfigBean().getZone(newValue));
            searchTF.clear();
            String url = QiniuApplication.getConfigBean().getUrl(newValue);
            if (Checker.isHyperLink(url)) {
                domainTF.setText(url);
            } else {
                domainTF.setText(QiniuValueConsts.DOMAIN_CONFIG_ERROR);
            }
            ThreadPool.executor.submit(() -> {
                if (SdkConfigurer.configUploadEnv(QiniuApplication.getConfigBean().getZone(newValue), newValue)) {
                    // 加载文件列表
                    mapResourceData();
                    // 刷新流量带宽统计
                    dateChange();
                }
            });
        });
        // 初始化统计单位选择框
        fluxUnitCB.getItems().addAll("KB", "MB", "GB", "TB");
        fluxUnitCB.setValue("KB");
        fluxUnitCB.getSelectionModel().selectedItemProperty().addListener((obs, o, n) -> drawChart(true, false));
        bandwidthUnitCB.getItems().addAll(fluxUnitCB.getItems());
        bandwidthUnitCB.setValue("KB");
        bandwidthUnitCB.getSelectionModel().selectedItemProperty().addListener((obs, o, n) -> drawChart(false, true));
    }

    /**
     * 开始拖曳文件
     */
    public void dragFileOver(DragEvent event) {
        event.acceptTransferModes(TransferMode.ANY);
    }

    /**
     * 拖曳文件松开鼠标
     */
    public void dragFileDropped(DragEvent event) {
        appendFile(event.getDragboard().getFiles());
    }

    /**
     * 开始日期或结束日期改变,刷新流量、带宽统计
     */
    public void dateChange() {
        drawChart(true, true);
    }

    /**
     * 绘制数据统计图表
     */
    private void drawChart(boolean isFluxUnitChange, boolean isBandwidthUnitChange) {
        Date localStartDate = Formatter.localDateToDate(startDP.getValue());
        Date localEndDate = Formatter.localDateToDate(endDP.getValue());
        // 将本地日期装换成字符串
        String fromDate = Formatter.dateToString(localStartDate);
        String toDate = Formatter.dateToString(localEndDate);
        // 获取开始日期和结束日期的时间差
        long timeSpan = localEndDate.getTime() - localStartDate.getTime();
        if (Checker.isNotEmpty(domainTF.getText()) && timeSpan >= 0 && timeSpan <= QiniuValueConsts.DATE_SPAN_OF_THIRTY_ONE) {
            Platform.runLater(() -> {
                String[] domains = {domainTF.getText()};
                if (isFluxUnitChange) {
                    // 获取流量数据
                    String fluxUnit = fluxUnitCB.getValue();
                    fluxAC.getData().clear();
                    fluxAC.getData().add(service.getBucketFlux(domains, fromDate, toDate, fluxUnit));
                }
                if (isBandwidthUnitChange) {
                    // 获取带宽数据
                    String bandUnit = bandwidthUnitCB.getValue();
                    bandwidthAC.getData().clear();
                    bandwidthAC.getData().add(service.getBucketBandwidth(domains, fromDate, toDate, bandUnit));
                }
            });
        }
    }

    /**
     * 下载日志
     */
    public void downloadCdnLog() {
        String date = DialogUtils.showInputDialog(null, QiniuValueConsts.INPUT_LOG_DATE,
                Formatter.dateToString(new Date()));
        service.downloadCdnLog(date);
    }

    /**
     * 刷新文件
     */
    public void refreshFile() {
        service.refreshFile(resTV.getSelectionModel().getSelectedItems(), domainTF.getText());
    }

    /**
     * 用浏览器打开文件
     */
    public void openFile() {
        ObservableList<FileBean> selectedItems = resTV.getSelectionModel().getSelectedItems();
        if (Checker.isNotEmpty(selectedItems)) {
            String filename = selectedItems.get(0).getName();
            QiniuUtils.openLink(QiniuUtils.buildUrl(filename, domainTF.getText()));
        }
    }

    /**
     * 私有下载
     */
    public void privateDownload() {
        download(DownloadWay.PRIVATE);
    }

    /**
     * 下载文件
     */
    private void download(DownloadWay way) {
        ObservableList<FileBean> selectedItems = resTV.getSelectionModel().getSelectedItems();
        if (Checker.isNotEmpty(selectedItems)) {
            if (way == DownloadWay.PUBLIC) {
                selectedItems.forEach(bean -> service.publicDownload(bean.getName(), domainTF.getText()));
            } else {
                selectedItems.forEach(bean -> service.privateDownload(bean.getName(), domainTF.getText()));
            }
        }
    }

    /**
     * 公有下载
     */
    public void publicDownload() {
        download(DownloadWay.PUBLIC);
    }

    /**
     * 更新镜像源
     */
    public void updateFile() {
        ObservableList<FileBean> selectedItems = resTV.getSelectionModel().getSelectedItems();
        if (Checker.isNotEmpty(selectedItems)) {
            selectedItems.forEach(bean -> service.updateFile(bucketCB.getValue(), bean.getName()));
        }
    }

    /**
     * 设置文件生存时间
     */
    public void setLife() {
        ObservableList<FileBean> selectedItems = resTV.getSelectionModel().getSelectedItems();
        if (Checker.isNotEmpty(selectedItems)) {
            // 弹出输入框
            String fileLife = DialogUtils.showInputDialog(null, QiniuValueConsts.FILE_LIFE,
                    QiniuValueConsts.DEFAULT_FILE_LIFE);
            if (Checker.isNumber(fileLife)) {
                int life = Formatter.stringToInt(fileLife);
                selectedItems.forEach(bean -> service.setFileLife(bucketCB.getValue(), bean.getName(), life));
            }
        }
    }

    /**
     * 显示移动或复制文件的弹窗
     */
    public void showFileMovableDialog() {
        ObservableList<FileBean> selectedItems = resTV.getSelectionModel().getSelectedItems();
        if (Checker.isEmpty(selectedItems)) {
            // 没有选择文件,结束方法
            return;
        }
        Pair<SdkManager.FileAction, String[]> resultPair;
        String bucket = bucketCB.getValue();
        if (selectedItems.size() > 1) {
            resultPair = dialog.showFileDialog(bucket, "", false);
        } else {
            resultPair = dialog.showFileDialog(bucket, selectedItems.get(0).getName(), true);
        }
        if (Checker.isNotNull(resultPair)) {
            boolean useNewKey = Checker.isNotEmpty(resultPair.getValue()[1]);
            ObservableList<FileBean> fileBeans = resTV.getItems();
            for (FileBean fileBean : selectedItems) {
                String fromBucket = bucketCB.getValue();
                String toBucket = resultPair.getValue()[0];
                String name = useNewKey ? resultPair.getValue()[1] : fileBean.getName();
                boolean isSuccess = service.moveOrCopyFile(fromBucket, fileBean.getName(), toBucket, name,
                        resultPair.getKey());
                if (resultPair.getKey() == SdkManager.FileAction.MOVE && isSuccess) {
                    boolean isInSearch = Checker.isNotEmpty(searchTF.getText());
                    if (fromBucket.equals(toBucket)) {
                        // 更新文件名
                        fileBean.setName(name);
                        if (isInSearch) {
                            fileBeans.get(fileBeans.indexOf(fileBean)).setName(name);
                        }
                    } else {
                        // 删除数据源
                        fileBeans.remove(fileBean);
                        dataLength--;
                        dataSize -= Formatter.sizeToLong(fileBean.getSize());
                        if (isInSearch) {
                            fileBeans.remove(fileBean);
                        }
                    }
                }
            }
            countBucket();
        }
    }

    /**
     * 删除文件
     */
    public void deleteFile() {
        service.deleteFile(resTV.getSelectionModel().getSelectedItems(), bucketCB.getValue());
    }

    /**
     * 复制链接
     */
    public void copyLink() {
        ObservableList<FileBean> fileBeans = resTV.getSelectionModel().getSelectedItems();
        if (Checker.isNotEmpty(fileBeans)) {
            // 只复制选中的第一个文件的链接
            Utils.copyToClipboard(QiniuUtils.buildUrl(fileBeans.get(0).getName(), domainTF.getText()));
        }
    }

    /**
     * 搜索资源文件,忽略大小写
     */
    public void searchFile() {
        ArrayList<FileBean> files = new ArrayList<>();
        String search = Checker.checkNull(searchTF.getText());
        dataLength = 0;
        dataSize = 0;
        // 正则匹配查询
        Pattern pattern = Pattern.compile(search, Pattern.CASE_INSENSITIVE);
        for (FileBean file : resData) {
            if (pattern.matcher(file.getName()).find()) {
                files.add(file);
                dataLength++;
                dataSize += Formatter.sizeToLong(file.getSize());
            }
        }
        countBucket();
        resTV.setItems(FXCollections.observableArrayList(files));
    }

    /**
     * 统计空间文件的数量以及大小
     */
    public void countBucket() {
        lengthLabel.setText(Formatter.customFormatDecimal(dataLength, ",###") + " 个文件");
        sizeLabel.setText(Formatter.formatSize(dataSize));
    }

    /**
     * 刷新资源列表
     */
    public void refreshResourceData() {
        mapResourceData();
        DialogUtils.showInformation(QiniuValueConsts.REFRESH_SUCCESS);
    }

    /**
     * 将从存储空间获取的文件列表映射到表中
     */
    private void mapResourceData() {
        ThreadPool.executor.submit(() -> {
            // 列出资源文件
            service.listFile();
            Platform.runLater(() -> {
                resTV.setItems(resData);
                countBucket();
            });
        });

    }

    /**
     * 添加桶至下拉框
     */
    public void appendBucket(String bucket) {
        if (!bucketCB.getItems().contains(bucket)) {
            bucketCB.getItems().add(bucket);
        }
    }

    /**
     * 保存文件的上传状态
     */
    public void saveUploadStatus() {
        FileChooser chooser = new FileChooser();
        chooser.setTitle(QiniuValueConsts.FILE_CHOOSER_TITLE);
        chooser.setInitialDirectory(new File(Utils.getCurrentWorkDir()));
        File file = chooser.showSaveDialog(QiniuApplication.getStage());
        QiniuUtils.saveFile(file, uploadStatusTA.getText());
    }

    /**
     * 复制文件上传状态至剪贴板
     */
    public void copyUploadStatus() {
        Utils.copyToClipboard(uploadStatusTA.getText());
    }

    /**
     * 清空文件的上传状态
     */
    public void clearUploadStatus() {
        uploadStatusTA.clear();
    }

    /**
     * 显示选择文件的弹窗
     */
    public void showOpenFileDialog() {
        FileChooser chooser = new FileChooser();
        chooser.setTitle(QiniuValueConsts.FILE_CHOOSER_TITLE);
        chooser.setInitialDirectory(new File(Utils.getCurrentWorkDir()));
        appendFile(chooser.showOpenMultipleDialog(QiniuApplication.getStage()));
    }

    /**
     * 添加上传的文件,支持拖曳文件夹
     */
    private void appendFile(List<File> files) {
        if (Checker.isNotEmpty(files)) {
            File[] fileArray = new File[files.size()];
            appendFile(files.toArray(fileArray), false);
        }
    }

    /**
     * 添加上传的文件,支持拖曳文件夹
     */
    private void appendFile(File[] files, boolean isRecursive) {
        if (Checker.isNotNull(files)) {
            for (File file : files) {
                if (file.isDirectory()) {
                    if (isRecursive) {
                        // 递归添加文件
                        if (recursiveCB.isSelected()) {
                            appendFile(file.listFiles(), true);
                        }
                    } else {
                        rootPath.add(file.getAbsolutePath());
                        appendFile(file.listFiles(), true);
                    }
                } else if (!selectedFileTA.getText().contains(file.getAbsolutePath())) {
                    selectedFileTA.insertText(0, file.getAbsolutePath() + "\r\n");
                }
            }
        }
    }

    /**
     * 上传选择的文件
     */
    public void uploadFile() {
        if (Checker.isEmpty(zoneTF.getText()) || Checker.isEmpty(selectedFileTA.getText())) {
            // 没有选择存储空间或文件,不能上传文件
            DialogUtils.showWarning(QiniuValueConsts.NEED_CHOOSE_BUCKET_OR_FILE);
            return;
        }
        // 新建一个线程上传文件的线程
        ThreadPool.executor.submit(() -> {
            Platform.runLater(() -> uploadStatusTA.insertText(0, QiniuValueConsts.CONFIG_UPLOAD_ENVIRONMENT));
            String bucket = bucketCB.getValue();
            // 默认不指定KEY的情况下,以文件内容的哈希值作为文件名
            String key = Checker.checkNull(prefixCB.getValue());
            String[] paths = selectedFileTA.getText().split("\n");
            // 去掉\r\n的长度
            int endIndex = QiniuValueConsts.UPLOADING.length() - 2;
            Platform.runLater(() -> uploadStatusTA.deleteText(0,
                    QiniuValueConsts.CONFIG_UPLOAD_ENVIRONMENT.length() - 1));
            // 总文件数
            for (String path : paths) {
                if (Checker.isNotEmpty(path)) {
                    Platform.runLater(() -> uploadStatusTA.insertText(0, QiniuValueConsts.UPLOADING));
                    String filename = "";
                    String url = "http://" + QiniuApplication.getConfigBean().getUrl(bucket) + "/";
                    File file = new File(path);
                    try {
                        // 判断文件是否存在
                        if (file.exists()) {
                            // 保持文件相对父文件夹的路径
                            if (keepPathCB.isSelected() && Checker.isNotEmpty(rootPath)) {
                                for (String root : rootPath) {
                                    if (file.getAbsolutePath().startsWith(root)) {
                                        String postKey = root.substring(root.lastIndexOf(File.separator) + 1);
                                        filename = key + postKey + file.getAbsolutePath().substring(root.length());
                                        break;
                                    }
                                }
                            }
                            if (Checker.isEmpty(filename)) {
                                filename = key + file.getName();
                            }
                            service.uploadFile(bucket, path, filename);
                            String now = DateUtil.formatDate(new Date());
                            status = StrUtil.format(UPLOAD_STATUS_TEMPLATE, now, url, filename, path);
                        } else if (Checker.isHyperLink(path)) {
                            // 抓取网络文件到空间中
                            filename = key + QiniuUtils.getFileName(path);
                            SdkConfigurer.getBucketManager().fetch(path, bucket, filename);
                            String now = DateUtil.formatDate(new Date());
                            status = StrUtil.format(UPLOAD_STATUS_TEMPLATE, now, url, filename, path);
                        } else {
                            // 文件不存在
                            status = DateUtil.formatDate(new Date()) + "\tfailed\t" + path;
                        }
                    } catch (QiniuException e) {
                        status = DateUtil.formatDate(new Date()) + "\terror\t" + path;
                        Platform.runLater(() -> DialogUtils.showException(QiniuValueConsts.UPLOAD_ERROR, e));
                    }
                    Platform.runLater(() -> {
                        uploadStatusTA.deleteText(0, endIndex);
                        uploadStatusTA.insertText(0, status);
                    });
                }
                Platform.runLater(() -> selectedFileTA.deleteText(0, path.length() + (paths.length > 1 ? 1 : 0)));
            }
            rootPath.clear();
            Platform.runLater(() -> {
                // 将光标移到最前面
                uploadStatusTA.positionCaret(0);
                // 清空待上传的文件列表
                selectedFileTA.clear();
            });
            mapResourceData();
            // 添加文件前缀到配置文件
            savePrefix(key);
        });
    }

    /**
     * 保存前缀
     */
    private void savePrefix(String key) {
        if (Checker.isNotEmpty(key) && !QiniuApplication.getConfigBean().getPrefixes().contains(key)) {
            Platform.runLater(() -> prefixCB.getItems().add(key));
            QiniuApplication.getConfigBean().getPrefixes().add(key);
            ConfigUtils.writeConfig();
        }
    }

    /**
     * 打开配置文件
     */
    public void openConfigFile() {
        try {
            Desktop.getDesktop().open(new File(QiniuValueConsts.CONFIG_PATH));
            // 用户触发是否重载配置文件
            Optional<ButtonType> result = DialogUtils.showConfirmation(QiniuValueConsts.RELOAD_CONFIG);
            if (result.isPresent() && result.get() == ButtonType.OK) {
                // 重新载入配置文件
                bucketCB.getItems().clear();
                prefixCB.getItems().clear();
                ConfigUtils.loadConfig();
            }
        } catch (Exception e) {
            DialogUtils.showException(QiniuValueConsts.OPEN_FILE_ERROR, e);
        }
    }

    /**
     * 显示重置密钥的弹窗
     */
    public void showKeyDialog() {
        boolean ok = dialog.showKeyDialog();
        if (ok && Checker.isNotEmpty(zoneTF.getText())) {
            // 配置新的环境
            SdkConfigurer.configUploadEnv(zoneTF.getText(), bucketCB.getValue());
        }
    }

    /**
     * 显示添加存储空间的弹窗
     */
    public void showBucketDialog() {
        dialog.showBucketDialog();
    }

    public enum DownloadWay {
        // 下载的方式,包括私有和公有
        PRIVATE, PUBLIC
    }
}