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

Commit daa83a9

Browse files
committed
重构文件操作部分
1 parent bda746f commit daa83a9

File tree

3 files changed

+189
-159
lines changed

3 files changed

+189
-159
lines changed

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

Lines changed: 34 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,8 @@
66
import com.qiniu.http.Response;
77
import com.qiniu.storage.BucketManager;
88
import com.qiniu.storage.model.BatchStatus;
9-
import com.zhazhapan.util.Checker;
10-
import com.zhazhapan.util.Formatter;
11-
import javafx.collections.FXCollections;
12-
import javafx.collections.ObservableList;
139
import org.apache.log4j.Logger;
14-
import org.code4everything.qiniu.QiniuApplication;
15-
import org.code4everything.qiniu.constant.QiniuValueConsts;
16-
import org.code4everything.qiniu.controller.MainWindowController;
17-
import org.code4everything.qiniu.model.FileBean;
18-
import org.code4everything.qiniu.util.DialogUtils;
19-
import org.code4everything.qiniu.util.QiniuUtils;
20-
21-
import java.util.ArrayList;
10+
2211
import java.util.Map;
2312

2413
/**
@@ -31,20 +20,25 @@ public class SdkManager {
3120
/**
3221
* 自定义私有链接过期时间
3322
*/
34-
private static final long EXPIRE_IN_SECONDS = 24 * 60 * 60;
23+
private static final long EXPIRE_IN_SECONDS = 24 * 60 * 60L;
24+
25+
/**
26+
* 文件列表大小
27+
*/
28+
private static final int LIST_SIZE = 1000;
3529

3630

3731
/**
3832
* 获取空间带宽统计
3933
*/
40-
public CdnResult.BandwidthResult getBucketBandwidth(String[] domains, String startDate, String endDate) throws QiniuException {
34+
public CdnResult.BandwidthResult getBandwidthData(String[] domains, String startDate, String endDate) throws QiniuException {
4135
return SdkConfigurer.getCdnManager().getBandwidthData(domains, startDate, endDate, "day");
4236
}
4337

4438
/**
4539
* 获取空间的流量统计
4640
*/
47-
public CdnResult.FluxResult getBucketFlux(String[] domains, String startDate, String endDate) throws QiniuException {
41+
public CdnResult.FluxResult getFluxData(String[] domains, String startDate, String endDate) throws QiniuException {
4842
return SdkConfigurer.getCdnManager().getFluxData(domains, startDate, endDate, "day");
4943
}
5044

@@ -73,168 +67,65 @@ public String getPrivateUrl(String publicUrl) {
7367
/**
7468
* 更新镜像源
7569
*/
76-
public void updateFile(String bucket, String key) {
77-
String log = "update file '" + key + "' on bucket '" + bucket;
78-
try {
79-
SdkConfigurer.getBucketManager().prefetch(bucket, key);
80-
LOGGER.info(log + "' success");
81-
} catch (QiniuException e) {
82-
LOGGER.error(log + "' error, message: " + e.getMessage());
83-
DialogUtils.showException(QiniuValueConsts.UPDATE_ERROR, e);
84-
}
70+
public void prefetch(String bucket, String key) throws QiniuException {
71+
SdkConfigurer.getBucketManager().prefetch(bucket, key);
8572
}
8673

8774
/**
8875
* 设置文件生存时间
8976
*/
90-
public void setFileLife(String bucket, String key, int days) {
91-
String log = "set file of '" + key + "' life to " + days + " day(s) ";
92-
try {
93-
SdkConfigurer.getBucketManager().deleteAfterDays(bucket, key, days);
94-
LOGGER.info(log + "success");
95-
} catch (QiniuException e) {
96-
LOGGER.error(log + "error, message: " + e.getMessage());
97-
DialogUtils.showException(QiniuValueConsts.MOVE_OR_RENAME_ERROR, e);
98-
}
77+
public void deleteAfterDays(String bucket, String key, int days) throws QiniuException {
78+
SdkConfigurer.getBucketManager().deleteAfterDays(bucket, key, days);
9979
}
10080

10181
/**
102-
* 重命令文件
82+
* 重命名文件
10383
*/
104-
public Boolean renameFile(String bucket, String oldName, String newName) {
105-
return moveFile(bucket, oldName, bucket, newName);
84+
public void renameFile(String bucket, String oldName, String newName) throws QiniuException {
85+
moveFile(bucket, oldName, bucket, newName);
10686
}
10787

10888
/**
10989
* 移动文件
11090
*/
111-
private boolean moveFile(String fromBucket, String fromKey, String toBucket, String toKey) {
112-
return moveOrCopyFile(fromBucket, fromKey, toBucket, toKey, FileAction.MOVE);
91+
public void moveFile(String srcBucket, String fromKey, String destBucket, String toKey) throws QiniuException {
92+
moveOrCopyFile(srcBucket, fromKey, destBucket, toKey, FileAction.MOVE);
11393
}
11494

11595
/**
11696
* 移动或复制文件
11797
*/
118-
public boolean moveOrCopyFile(String fromBucket, String fromKey, String toBucket, String toKey, FileAction action) {
119-
if (QiniuUtils.checkNet()) {
120-
String log = "move file '" + fromKey + "' from bucket '" + fromBucket + "' to bucket '" + toBucket + "', "
121-
+ "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "and rename file '" + toKey + "'";
122-
try {
123-
if (action == FileAction.COPY) {
124-
log = log.replaceAll("^move", "copy");
125-
SdkConfigurer.getBucketManager().copy(fromBucket, fromKey, toBucket, toKey, true);
126-
} else {
127-
SdkConfigurer.getBucketManager().move(fromBucket, fromKey, toBucket, toKey, true);
128-
}
129-
LOGGER.info(log + " success");
130-
return true;
131-
} catch (QiniuException e) {
132-
LOGGER.error(log + " failed, message: " + e.getMessage());
133-
DialogUtils.showException(QiniuValueConsts.MOVE_OR_RENAME_ERROR, e);
134-
return false;
135-
}
98+
public void moveOrCopyFile(String srcBucket, String srcKey, String destBucket, String destKey,
99+
FileAction fileAction) throws QiniuException {
100+
if (FileAction.COPY == fileAction) {
101+
SdkConfigurer.getBucketManager().copy(srcBucket, srcKey, destBucket, destKey, true);
102+
} else {
103+
SdkConfigurer.getBucketManager().move(srcBucket, srcKey, destBucket, destKey, true);
136104
}
137-
return false;
138105
}
139106

140107
/**
141108
* 修改文件类型
142109
*/
143-
public boolean changeType(String fileName, String newType, String bucket) {
144-
if (QiniuUtils.checkNet()) {
145-
String log = "change file '" + fileName + "' type '" + newType + "' on bucket '" + bucket;
146-
try {
147-
SdkConfigurer.getBucketManager().changeMime(bucket, fileName, newType);
148-
LOGGER.info(log + "' success");
149-
return true;
150-
} catch (QiniuException e) {
151-
LOGGER.error(log + "' failed, message: " + e.getMessage());
152-
DialogUtils.showException(QiniuValueConsts.CHANGE_FILE_TYPE_ERROR, e);
153-
return false;
154-
}
155-
}
156-
return false;
110+
public void changeMime(String fileName, String newType, String bucket) throws QiniuException {
111+
SdkConfigurer.getBucketManager().changeMime(bucket, fileName, newType);
157112
}
158113

159114
/**
160115
* 批量删除文件,单次批量请求的文件数量不得超过1000
161116
*/
162-
public void deleteFiles(ObservableList<FileBean> fileInfos, String bucket) {
163-
if (Checker.isNotEmpty(fileInfos) && QiniuUtils.checkNet()) {
164-
// 生成待删除的文件列表
165-
String[] files = new String[fileInfos.size()];
166-
ArrayList<FileBean> seletecFileInfos = new ArrayList<>();
167-
int i = 0;
168-
for (FileBean fileInfo : fileInfos) {
169-
files[i++] = fileInfo.getName();
170-
seletecFileInfos.add(fileInfo);
171-
}
172-
try {
173-
BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
174-
batchOperations.addDeleteOp(bucket, files);
175-
Response response = SdkConfigurer.getBucketManager().batch(batchOperations);
176-
BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
177-
MainWindowController main = MainWindowController.getInstance();
178-
// 文件列表是否为搜索后结果
179-
boolean sear = Checker.isNotEmpty(main.searchTextField.getText());
180-
ObservableList<FileBean> currentRes = main.resTable.getItems();
181-
for (i = 0; i < files.length; i++) {
182-
BatchStatus status = batchStatusList[i];
183-
String file = files[i];
184-
if (status.code == 200) {
185-
LOGGER.info("delete file '" + file + "' success");
186-
QiniuApplication.data.remove(seletecFileInfos.get(i));
187-
QiniuApplication.totalLength--;
188-
QiniuApplication.totalSize -= Formatter.sizeToLong(seletecFileInfos.get(i).getSize());
189-
if (sear) {
190-
currentRes.remove(seletecFileInfos.get(i));
191-
}
192-
} else {
193-
LOGGER.error("delete file '" + file + "' failed, message: " + status.data.error);
194-
DialogUtils.showError("删除文件:" + file + " 失败");
195-
}
196-
}
197-
} catch (QiniuException e) {
198-
DialogUtils.showException(QiniuValueConsts.DELETE_ERROR, e);
199-
}
200-
MainWindowController.getInstance().setBucketCount();
201-
}
117+
public BatchStatus[] batchDelete(String bucket, String[] keys) throws QiniuException {
118+
BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
119+
batchOperations.addDeleteOp(bucket, keys);
120+
Response response = SdkConfigurer.getBucketManager().batch(batchOperations);
121+
return response.jsonToObject(BatchStatus[].class);
202122
}
203123

204124
/**
205-
* 获取空间文件列表,并映射到FileInfo
125+
* 获取空间文件列表
206126
*/
207-
public void listFileOfBucket() {
208-
MainWindowController main = MainWindowController.getInstance();
209-
// 列举空间文件列表
210-
String bucket = main.bucketChoiceCombo.getValue();
211-
BucketManager.FileListIterator iterator = SdkConfigurer.getBucketManager().createFileListIterator(bucket, "",
212-
QiniuValueConsts.BUCKET_LIST_LIMIT_SIZE, "");
213-
ArrayList<FileBean> files = new ArrayList<>();
214-
LOGGER.info("get file list of bucket: " + bucket);
215-
QiniuApplication.totalLength = 0;
216-
QiniuApplication.totalSize = 0;
217-
// 处理获取的file list结果
218-
while (iterator.hasNext()) {
219-
com.qiniu.storage.model.FileInfo[] items = iterator.next();
220-
for (com.qiniu.storage.model.FileInfo item : items) {
221-
QiniuApplication.totalLength++;
222-
QiniuApplication.totalSize += item.fsize;
223-
// 将七牛的时间单位(100纳秒)转换成毫秒,然后转换成时间
224-
String time = Formatter.timeStampToString(item.putTime / 10000);
225-
String size = Formatter.formatSize(item.fsize);
226-
FileBean file = new FileBean(item.key, item.mimeType, size, time);
227-
files.add(file);
228-
LOGGER.info("file name: " + item.key + ", file type: " + item.mimeType + ", file size: " + size + ", "
229-
+ "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "file time: " + time);
230-
}
231-
}
232-
QiniuApplication.data = FXCollections.observableArrayList(files);
233-
}
234-
235-
private void urlError(Exception e) {
236-
LOGGER.error("generate url error: " + e.getMessage());
237-
DialogUtils.showException(QiniuValueConsts.GENERATE_URL_ERROR, e);
127+
public BucketManager.FileListIterator getFileListIterator(String bucket) {
128+
return SdkConfigurer.getBucketManager().createFileListIterator(bucket, "", LIST_SIZE, "");
238129
}
239130

240131
public enum FileAction {

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ private void initialize() {
166166
nameCol.setOnEditCommit(v -> {
167167
String name;
168168
FileBean fileInfo = v.getTableView().getItems().get(v.getTablePosition().getRow());
169-
if (new SdkManager().renameFile(bucketChoiceCombo.getValue(), v.getOldValue(), v.getNewValue())) {
169+
if (service.renameFile(bucketChoiceCombo.getValue(), v.getOldValue(), v.getNewValue())) {
170170
name = v.getNewValue();
171171
} else {
172172
name = v.getOldValue();
@@ -182,7 +182,7 @@ private void initialize() {
182182
typeCol.setOnEditCommit(v -> {
183183
FileBean fileInfo = v.getTableView().getItems().get(v.getTablePosition().getRow());
184184
String type;
185-
if (new SdkManager().changeType(fileInfo.getName(), v.getNewValue(), bucketChoiceCombo.getValue())) {
185+
if (service.changeType(fileInfo.getName(), v.getNewValue(), bucketChoiceCombo.getValue())) {
186186
type = v.getNewValue();
187187
} else {
188188
type = v.getOldValue();
@@ -371,7 +371,7 @@ public void downloadCdnLog() {
371371
* 文件刷新,cdn相关
372372
*/
373373
public void refreshFile() {
374-
service.refreshFile(resTable.getSelectionModel().getSelectedItems(), bucketDomainTextField.getText());
374+
service.refreshFiles(resTable.getSelectionModel().getSelectedItems(), bucketDomainTextField.getText());
375375
}
376376

377377
/**
@@ -411,7 +411,7 @@ private void download(DownloadWay way) {
411411
} else {
412412
logger.debug("start to private download");
413413
for (FileBean fileInfo : selectedItems) {
414-
service.publicDownload(fileInfo.getName(), bucketDomainTextField.getText());
414+
service.privateDownload(fileInfo.getName(), bucketDomainTextField.getText());
415415
}
416416
}
417417
}
@@ -430,9 +430,8 @@ public void publicDownload() {
430430
public void updateFile() {
431431
ObservableList<FileBean> selectedItems = resTable.getSelectionModel().getSelectedItems();
432432
if (Checker.isNotEmpty(selectedItems)) {
433-
SdkManager manager = new SdkManager();
434433
for (FileBean fileInfo : selectedItems) {
435-
manager.updateFile(bucketChoiceCombo.getValue(), fileInfo.getName());
434+
service.updateFiles(bucketChoiceCombo.getValue(), fileInfo.getName());
436435
}
437436
}
438437
}
@@ -447,9 +446,8 @@ public void setLife() {
447446
QiniuValueConsts.DEFAULT_FILE_LIFE);
448447
if (Checker.isNumber(lifeStr)) {
449448
int life = Formatter.stringToInt(lifeStr);
450-
SdkManager manager = new SdkManager();
451449
for (FileBean fileInfo : selectedItems) {
452-
manager.setFileLife(bucketChoiceCombo.getValue(), fileInfo.getName(), life);
450+
service.setFilesLife(bucketChoiceCombo.getValue(), fileInfo.getName(), life);
453451
}
454452
}
455453
}
@@ -473,12 +471,11 @@ public void showFileMovableDialog() {
473471
if (Checker.isNotNull(pair)) {
474472
boolean useNewKey = Checker.isNotEmpty(pair.getValue()[1]);
475473
ObservableList<FileBean> resData = resTable.getItems();
476-
SdkManager manager = new SdkManager();
477474
for (FileBean fileInfo : selectedItems) {
478475
String fb = bucketChoiceCombo.getValue();
479476
String tb = pair.getValue()[0];
480477
String name = useNewKey ? pair.getValue()[1] : fileInfo.getName();
481-
boolean move = manager.moveOrCopyFile(fb, fileInfo.getName(), tb, name, pair.getKey());
478+
boolean move = service.moveOrCopyFile(fb, fileInfo.getName(), tb, name, pair.getKey());
482479
if (pair.getKey() == SdkManager.FileAction.MOVE && move) {
483480
boolean sear = Checker.isNotEmpty(searchTextField.getText());
484481
if (fb.equals(tb)) {
@@ -506,8 +503,7 @@ public void showFileMovableDialog() {
506503
* 删除文件
507504
*/
508505
public void deleteFiles() {
509-
ObservableList<FileBean> fileInfos = resTable.getSelectionModel().getSelectedItems();
510-
new SdkManager().deleteFiles(fileInfos, bucketChoiceCombo.getValue());
506+
service.deleteFiles(resTable.getSelectionModel().getSelectedItems(), bucketChoiceCombo.getValue());
511507
}
512508

513509
/**
@@ -572,7 +568,7 @@ public void refreshResTable() {
572568
*/
573569
private void setResTableData() {
574570
ThreadPool.executor.submit(() -> {
575-
new SdkManager().listFileOfBucket();
571+
service.listFile();
576572
Platform.runLater(() -> {
577573
resTable.setItems(QiniuApplication.data);
578574
setBucketCount();

0 commit comments

Comments
 (0)