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

Commit bda746f

Browse files
committed
重构文件下载部分
1 parent 3b569ff commit bda746f

File tree

4 files changed

+111
-86
lines changed

4 files changed

+111
-86
lines changed

src/main/java/org/code4everything/qiniu/api/SdkManager.java

Lines changed: 15 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import org.code4everything.qiniu.util.DialogUtils;
1919
import org.code4everything.qiniu.util.QiniuUtils;
2020

21-
import java.io.UnsupportedEncodingException;
22-
import java.net.URLEncoder;
2321
import java.util.ArrayList;
2422
import java.util.Map;
2523

@@ -30,6 +28,12 @@ public class SdkManager {
3028

3129
private static final Logger LOGGER = Logger.getLogger(SdkManager.class);
3230

31+
/**
32+
* 自定义私有链接过期时间
33+
*/
34+
private static final long EXPIRE_IN_SECONDS = 24 * 60 * 60;
35+
36+
3337
/**
3438
* 获取空间带宽统计
3539
*/
@@ -45,85 +49,25 @@ public CdnResult.FluxResult getBucketFlux(String[] domains, String startDate, St
4549
}
4650

4751
/**
48-
* 日志下载,cdn相关
49-
*/
50-
public void downloadCdnLog(String logDate) {
51-
if (Checker.isNotEmpty(QiniuApplication.getConfigBean().getBuckets()) && Checker.isDate(logDate)) {
52-
String[] domains = new String[QiniuApplication.getConfigBean().getBuckets().size()];
53-
for (int i = 0; i < QiniuApplication.getConfigBean().getBuckets().size(); i++) {
54-
domains[i] = QiniuApplication.getConfigBean().getBuckets().get(i).getUrl();
55-
}
56-
try {
57-
CdnResult.LogListResult logRes = SdkConfigurer.getCdnManager().getCdnLogList(domains, logDate);
58-
for (Map.Entry<String, LogData[]> logs : logRes.data.entrySet()) {
59-
for (LogData log : logs.getValue()) {
60-
QiniuUtils.download(log.url);
61-
}
62-
}
63-
} catch (QiniuException e) {
64-
LOGGER.error("get cdn log url error, message: " + e.getMessage());
65-
DialogUtils.showException(e);
66-
}
67-
}
68-
}
69-
70-
/**
71-
* 刷新文件,cdn相关
52+
* 日志下载,CDN 相关
7253
*/
73-
public void refreshFile(ObservableList<FileBean> fileInfos, String domain) {
74-
if (Checker.isNotEmpty(fileInfos)) {
75-
String[] files = new String[fileInfos.size()];
76-
int i = 0;
77-
for (FileBean fileInfo : fileInfos) {
78-
files[i++] = getPublicURL(fileInfo.getName(), domain);
79-
}
80-
refreshFile(files);
81-
}
54+
public Map<String, LogData[]> listCdnLog(String[] domains, String logDate) throws QiniuException {
55+
return SdkConfigurer.getCdnManager().getCdnLogList(domains, logDate).data;
8256
}
8357

8458
/**
85-
* 刷新文件,cdn相关
59+
* 刷新文件,CDN 相关
8660
*/
87-
private void refreshFile(String[] files) {
88-
try {
89-
// 单次方法调用刷新的链接不可以超过100个
90-
SdkConfigurer.getCdnManager().refreshUrls(files);
91-
LOGGER.info("refresh files success");
92-
} catch (QiniuException e) {
93-
LOGGER.error("refresh files error, message: " + e.getMessage());
94-
DialogUtils.showException(e);
95-
}
61+
public void refreshFiles(String[] files) throws QiniuException {
62+
// 单次方法调用刷新的链接不可以超过100个
63+
SdkConfigurer.getCdnManager().refreshUrls(files);
9664
}
9765

9866
/**
9967
* 私有下载
10068
*/
101-
public void privateDownload(String fileName, String domain) {
102-
// 自定义链接过期时间(小时)
103-
long expireInSeconds = 24;
104-
String publicURL = getPublicURL(fileName, domain);
105-
QiniuUtils.download(SdkConfigurer.getAuth().privateDownloadUrl(publicURL, expireInSeconds));
106-
}
107-
108-
/**
109-
* 公有下载
110-
*/
111-
public void publicDownload(String fileName, String domain) {
112-
String url = getPublicURL(fileName, domain);
113-
if (Checker.isNotEmpty(url)) {
114-
QiniuUtils.download(url);
115-
}
116-
}
117-
118-
public String getPublicURL(String fileName, String domain) {
119-
fileName = fileName.replaceAll(" ", "qn_code_per_20").replaceAll("/", "qn_code_per_2F");
120-
try {
121-
fileName = URLEncoder.encode(fileName, "utf-8").replaceAll("qn_code_per_2F", "/");
122-
return String.format("%s/%s", domain, fileName.replaceAll("qn_code_per_20", "%20"));
123-
} catch (UnsupportedEncodingException e) {
124-
urlError(e);
125-
return null;
126-
}
69+
public String getPrivateUrl(String publicUrl) {
70+
return SdkConfigurer.getAuth().privateDownloadUrl(publicUrl, EXPIRE_IN_SECONDS);
12771
}
12872

12973
/**

src/main/java/org/code4everything/qiniu/controller/MainWindowController.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,14 @@ private void drawChart(boolean fluxUnitChange, boolean bandUnitChange) {
364364
public void downloadCdnLog() {
365365
String date = DialogUtils.showInputDialog(null, QiniuValueConsts.INPUT_LOG_DATE,
366366
Formatter.dateToString(new Date()));
367-
new SdkManager().downloadCdnLog(date);
367+
service.downloadCdnLog(date);
368368
}
369369

370370
/**
371371
* 文件刷新,cdn相关
372372
*/
373373
public void refreshFile() {
374-
new SdkManager().refreshFile(resTable.getSelectionModel().getSelectedItems(),
375-
"http://" + bucketDomainTextField.getText());
374+
service.refreshFile(resTable.getSelectionModel().getSelectedItems(), bucketDomainTextField.getText());
376375
}
377376

378377
/**
@@ -390,8 +389,7 @@ public void openFile() {
390389
ObservableList<FileBean> selectedItems = resTable.getSelectionModel().getSelectedItems();
391390
if (Checker.isNotEmpty(selectedItems)) {
392391
String filename = selectedItems.get(0).getName();
393-
String url = "http://" + new SdkManager().getPublicURL(filename, bucketDomainTextField.getText());
394-
QiniuUtils.openLink(url);
392+
QiniuUtils.openLink(QiniuUtils.joinUrl(filename, bucketDomainTextField.getText()));
395393
}
396394
}
397395

@@ -405,17 +403,15 @@ public void privateDownload() {
405403
private void download(DownloadWay way) {
406404
ObservableList<FileBean> selectedItems = resTable.getSelectionModel().getSelectedItems();
407405
if (Checker.isNotEmpty(selectedItems)) {
408-
SdkManager manager = new SdkManager();
409-
String domain = "http://" + bucketDomainTextField.getText();
410406
if (way == DownloadWay.PUBLIC) {
411407
logger.debug("start to public download");
412408
for (FileBean fileInfo : selectedItems) {
413-
manager.publicDownload(fileInfo.getName(), domain);
409+
service.publicDownload(fileInfo.getName(), bucketDomainTextField.getText());
414410
}
415411
} else {
416412
logger.debug("start to private download");
417413
for (FileBean fileInfo : selectedItems) {
418-
manager.privateDownload(fileInfo.getName(), domain);
414+
service.publicDownload(fileInfo.getName(), bucketDomainTextField.getText());
419415
}
420416
}
421417
}
@@ -521,10 +517,7 @@ public void copyLink() {
521517
ObservableList<FileBean> fileInfos = resTable.getSelectionModel().getSelectedItems();
522518
if (Checker.isNotEmpty(fileInfos)) {
523519
// 只复制选中的第一个文件的链接
524-
String link = "http://" + new SdkManager().getPublicURL(fileInfos.get(0).getName(),
525-
bucketDomainTextField.getText());
526-
Utils.copyToClipboard(link);
527-
logger.info("copy link: " + link);
520+
Utils.copyToClipboard(QiniuUtils.joinUrl(fileInfos.get(0).getName(), bucketDomainTextField.getText()));
528521
}
529522
}
530523

src/main/java/org/code4everything/qiniu/service/QiniuService.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
import com.zhazhapan.util.Checker;
66
import com.zhazhapan.util.Formatter;
77
import javafx.application.Platform;
8+
import javafx.collections.ObservableList;
89
import javafx.scene.chart.XYChart;
910
import org.apache.log4j.Logger;
11+
import org.code4everything.qiniu.QiniuApplication;
1012
import org.code4everything.qiniu.api.SdkManager;
1113
import org.code4everything.qiniu.constant.QiniuValueConsts;
14+
import org.code4everything.qiniu.model.FileBean;
1215
import org.code4everything.qiniu.util.DialogUtils;
16+
import org.code4everything.qiniu.util.QiniuUtils;
1317

1418
import java.util.Map;
1519

@@ -27,6 +31,70 @@ public class QiniuService {
2731

2832
private final SdkManager sdkManager = new SdkManager();
2933

34+
/**
35+
* 公有下载
36+
*/
37+
public void publicDownload(String fileName, String domain) {
38+
QiniuUtils.download(QiniuUtils.joinUrl(fileName, domain));
39+
}
40+
41+
/**
42+
* 私有下载
43+
*/
44+
public void privateDownload(String fileName, String domain) {
45+
QiniuUtils.download(sdkManager.getPrivateUrl(QiniuUtils.joinUrl(fileName, domain)));
46+
}
47+
48+
/**
49+
* 刷新文件
50+
*/
51+
public void refreshFile(ObservableList<FileBean> fileBeans, String domain) {
52+
if (Checker.isNotEmpty(fileBeans)) {
53+
String[] files = new String[fileBeans.size()];
54+
int i = 0;
55+
// 获取公有链接
56+
for (FileBean fileBean : fileBeans) {
57+
files[i++] = QiniuUtils.joinUrl(fileBean.getName(), domain);
58+
}
59+
try {
60+
// 属性文件
61+
sdkManager.refreshFiles(files);
62+
LOGGER.info("refresh files success");
63+
} catch (QiniuException e) {
64+
LOGGER.error("refresh files error, message -> " + e.getMessage());
65+
DialogUtils.showException(e);
66+
}
67+
}
68+
}
69+
70+
/**
71+
* 日志下载
72+
*/
73+
public void downloadCdnLog(String logDate) {
74+
if (Checker.isNotEmpty(QiniuApplication.getConfigBean().getBuckets()) && Checker.isDate(logDate)) {
75+
// 转换域名成数组格式
76+
String[] domains = new String[QiniuApplication.getConfigBean().getBuckets().size()];
77+
for (int i = 0; i < QiniuApplication.getConfigBean().getBuckets().size(); i++) {
78+
domains[i] = QiniuApplication.getConfigBean().getBuckets().get(i).getUrl();
79+
}
80+
Map<String, CdnResult.LogData[]> cdnLog = null;
81+
try {
82+
cdnLog = sdkManager.listCdnLog(domains, logDate);
83+
} catch (QiniuException e) {
84+
DialogUtils.showException(e);
85+
}
86+
if (Checker.isNotEmpty(cdnLog)) {
87+
// 下载日志
88+
for (Map.Entry<String, CdnResult.LogData[]> logs : cdnLog.entrySet()) {
89+
for (CdnResult.LogData log : logs.getValue()) {
90+
QiniuUtils.download(log.url);
91+
}
92+
}
93+
}
94+
}
95+
}
96+
97+
3098
/**
3199
* 获取空间带宽统计,使用自定义单位
32100
*/

src/main/java/org/code4everything/qiniu/util/QiniuUtils.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,24 @@
99
import com.zhazhapan.util.dialog.Alerts;
1010
import org.apache.log4j.Logger;
1111
import org.code4everything.qiniu.QiniuApplication;
12-
import org.code4everything.qiniu.api.SdkConfigurer;
1312
import org.code4everything.qiniu.constant.QiniuValueConsts;
1413

1514
import java.io.File;
1615
import java.io.IOException;
1716
import java.io.InputStream;
1817
import java.io.UnsupportedEncodingException;
1918
import java.net.URL;
19+
import java.net.URLEncoder;
2020

2121
/**
2222
* @author pantao
2323
* @since 2018/4/14
2424
*/
2525
public class QiniuUtils {
2626

27-
private static final Logger LOGGER = Logger.getLogger(SdkConfigurer.class);
27+
private static final Logger LOGGER = Logger.getLogger(QiniuUtils.class);
28+
29+
private static final String HTTP = "http";
2830

2931
private QiniuUtils() {}
3032

@@ -93,6 +95,24 @@ public static String getFileName(String string) {
9395
try {
9496
return Formatter.getFileName(string);
9597
} catch (UnsupportedEncodingException e) {
98+
LOGGER.error("get file name of url failed, message -> " + e.getMessage());
99+
return "";
100+
}
101+
}
102+
103+
/**
104+
* 拼接链接
105+
*/
106+
public static String joinUrl(String fileName, String domain) {
107+
if (!domain.startsWith(HTTP)) {
108+
domain = HTTP + "://" + domain;
109+
}
110+
fileName = fileName.replaceAll(" ", "qn_code_per_20").replaceAll("/", "qn_code_per_2F");
111+
try {
112+
fileName = URLEncoder.encode(fileName, "utf-8").replaceAll("qn_code_per_2F", "/");
113+
return String.format("%s/%s", domain, fileName.replaceAll("qn_code_per_20", "%20"));
114+
} catch (UnsupportedEncodingException e) {
115+
LOGGER.error("encode url failed, message -> " + e.getMessage());
96116
return "";
97117
}
98118
}

0 commit comments

Comments
 (0)