Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
添加文件统计
Browse files Browse the repository at this point in the history
  • Loading branch information
easepan committed Oct 17, 2017
1 parent b746004 commit 6efee01
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 15 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,17 @@

其中 `config.json` 为配置文件

- 文件上传

除了可上传本地文件外,还可抓取网络文件到空间中,如:

![上传网络文件](http://img.blog.csdn.net/20171017152757227?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjY5NTQ3NzM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)

然后点击开始上传即可

**2. 资源管理界面:**

![资源管理界面](http://img.blog.csdn.net/20171017111653241?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjY5NTQ3NzM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
![资源管理界面](http://img.blog.csdn.net/20171017153112198?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjY5NTQ3NzM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)

- 刷新列表

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/zhazhapan/qiniu/QiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,14 @@ public void listFileOfBucket() {
Values.BUCKET_LIST_LIMIT_SIZE, "");
ArrayList<FileInfo> files = new ArrayList<FileInfo>();
logger.info("get file list of bucket: " + bucket);
QiniuApplication.totalLength = 0;
QiniuApplication.totalSize = 0;
// 处理获取的file list结果
while (iterator.hasNext()) {
com.qiniu.storage.model.FileInfo[] items = iterator.next();
for (com.qiniu.storage.model.FileInfo item : items) {
QiniuApplication.totalLength++;
QiniuApplication.totalSize += item.fsize;
// 将七牛的时间单位(100纳秒)转换成毫秒,然后转换成时间
String time = Formatter.timeStampToString(item.putTime / 10000);
String size = Formatter.formatSize(item.fsize);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/zhazhapan/qiniu/QiniuApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ public class QiniuApplication extends Application {

public static CdnManager cdnManager = null;

/**
* 空间总文件数
*/
public static int totalLength = 0;

/**
* 空间使用总大小
*/
public static long totalSize = 0;

/**
* 主程序入口
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/zhazhapan/qiniu/config/ConfigLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public static void writeConfig(String configJson) {
ThreadPool.executor.submit(() -> {
try {
BufferedWriter out = new BufferedWriter(new FileWriter(configPath, false));
out.write(Formatter.jsonFormat(configJson));
out.write(Formatter.formatJson(configJson));
out.close();
logger.info("rewrite configuration success");
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import javafx.scene.control.ButtonType;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
Expand Down Expand Up @@ -114,7 +115,13 @@ public class MainWindowController {
private Hyperlink toIntro;

@FXML
Hyperlink toIntro1;
private Hyperlink toIntro1;

@FXML
private Label totalSizeLabel;

@FXML
private Label totalLengthLabel;

private static MainWindowController mainWindowController = null;

Expand Down Expand Up @@ -371,20 +378,33 @@ public void searchFile(KeyEvent event) {
ArrayList<FileInfo> files = new ArrayList<FileInfo>();
String search = Checker.checkNull(searchTextField.getText());
logger.info("search file: " + search);
QiniuApplication.totalLength = 0;
QiniuApplication.totalSize = 0;
try {
// 正则匹配查询
Pattern pattern = Pattern.compile(search, Pattern.CASE_INSENSITIVE);
for (FileInfo file : QiniuApplication.data) {
if (pattern.matcher(file.getName()).find()) {
files.add(file);
QiniuApplication.totalLength++;
QiniuApplication.totalSize += Formatter.sizeToLong(file.getSize());
}
}
} catch (Exception e) {
logger.warn("pattern '" + search + "' compile error, message: " + e.getMessage());
}
setBucketCount();
resTable.setItems(FXCollections.observableArrayList(files));
}

/**
* 统计空间文件的数量以及大小
*/
public void setBucketCount() {
totalLengthLabel.setText(Formatter.customFormatDecimal(QiniuApplication.totalLength, ",###") + " 个文件");
totalSizeLabel.setText(Formatter.formatSize(QiniuApplication.totalSize));
}

/**
* 刷新资源列表
*/
Expand All @@ -403,8 +423,12 @@ public void refreshResTable() {
public void setResTableData() {
ThreadPool.executor.submit(() -> {
new QiManager().listFileOfBucket();
Platform.runLater(() -> resTable.setItems(QiniuApplication.data));
Platform.runLater(() -> {
resTable.setItems(QiniuApplication.data);
setBucketCount();
});
});

}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/zhazhapan/qiniu/util/Checker.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ public class Checker {

public static final Pattern NUMBER_PATTERN = Pattern.compile("^[0-9]+$");

public static final Pattern DECIMAL_PATTERN = Pattern.compile("^[0-9]+(\\.[0-9]+)?$");

public static boolean isDate(String date) {
return isNull(date) ? false : DATE_PATTERN.matcher(date).matches();
}

public static boolean isDecimal(String string) {
return isNull(string) ? false : DECIMAL_PATTERN.matcher(string).matches();
}

public static boolean isNumber(String string) {
return isNull(string) ? false : NUMBER_PATTERN.matcher(string).matches();
}
Expand Down
63 changes: 54 additions & 9 deletions src/main/java/com/zhazhapan/qiniu/util/Formatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static int stringToInt(String string) {
if (Checker.isNumber(string)) {
return Integer.parseInt(string);
}
return 0;
return -1;
}

public static final Pattern FILE_NAME_PATTERN = Pattern.compile("([^/\\\\:*\"<>|?]+\\.)*[^/\\\\:*\"<>|?]+(\\?.*)?$",
Expand All @@ -42,29 +42,74 @@ public static String formatSize(long size) {
if (size < Values.KB) {
return size + " B";
} else if (size < Values.MB) {
return decimalFormat((double) size / Values.KB) + " KB";
return formatDecimal((double) size / Values.KB) + " KB";
} else if (size < Values.GB) {
return decimalFormat((double) size / Values.MB) + " MB";
return formatDecimal((double) size / Values.MB) + " MB";
} else if (size < Values.TB) {
return decimalFormat((double) size / Values.GB) + " GB";
return formatDecimal((double) size / Values.GB) + " GB";
} else {
return decimalFormat((double) size / Values.TB) + " TB";
return formatDecimal((double) size / Values.TB) + " TB";
}
}

public static String decimalFormat(double number) {
return decimalFormat(number, "#0.00");
/**
* 将格式化后的大小转换成long型
*
* @param size
* 格式:34.12 MB
* @return long
*/
public static long sizeToLong(String size) {
if (Checker.isNotEmpty(size)) {
String num = size.split(" ")[0];
double result = 0;
if (size.contains("TB")) {
result = stringToDouble(num) * Values.TB;
} else if (size.contains("GB")) {
result = stringToDouble(num) * Values.GB;
} else if (size.contains("MB")) {
result = stringToDouble(num) * Values.MB;
} else if (size.contains("KB")) {
result = stringToDouble(num) * Values.KB;
} else {
result = stringToDouble(num);
}
return (long) result;
}
return -1;
}

public static double stringToDouble(String s) {
if (Checker.isDecimal(s)) {
return Double.parseDouble(s);
}
return -1;
}

public static long stringToLong(String s) {
if (Checker.isNumber(s)) {
return Long.parseLong(s);
}
return -1;
}

public static String customFormatDecimal(double number, String format) {
return formatDecimal(number, format);
}

public static String formatDecimal(double number) {
return formatDecimal(number, "#0.00");
}

public static String decimalFormat(double number, String format) {
public static String formatDecimal(double number, String format) {
return new DecimalFormat(format).format(number);
}

public static String timeStampToString(long time) {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
}

public static String jsonFormat(String string) {
public static String formatJson(String string) {
String json;
try {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/zhazhapan/qiniu/view/MainWindow.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@
<Insets left="10.0" />
</HBox.margin>
</TextField>
<Label fx:id="totalLengthLabel">
<HBox.margin>
<Insets left="10.0" />
</HBox.margin>
</Label>
</children>
<padding>
<Insets left="10.0" />
Expand Down Expand Up @@ -208,6 +213,11 @@
<Insets top="10.0" />
</VBox.margin>
</Button>
<Label fx:id="totalSizeLabel">
<VBox.margin>
<Insets top="10.0" />
</VBox.margin>
</Label>
</children>
</VBox>
</children>
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/zhazhapan/qiniu/QiniuApplicationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class QiniuApplicationTest {

@Test
public void contextLoads() {
}
public void testContext() {

}
}
23 changes: 23 additions & 0 deletions src/test/java/com/zhazhapan/qiniu/TestFormatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
*
*/
package com.zhazhapan.qiniu;

import org.junit.Test;

import com.zhazhapan.qiniu.util.Formatter;

/**
* @author pantao
*
*/
public class TestFormatter {

@Test
public void testSizeToLong() {
String[] sizes = { "23.12 MB", "12.89 KB", "23 B", "23.77 GB", "89.12 TB" };
for (String size : sizes) {
System.out.println(Formatter.sizeToLong(size));
}
}
}

0 comments on commit 6efee01

Please sign in to comment.