6
6
import com .qiniu .http .Response ;
7
7
import com .qiniu .storage .BucketManager ;
8
8
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 ;
13
9
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
+
22
11
import java .util .Map ;
23
12
24
13
/**
@@ -31,20 +20,25 @@ public class SdkManager {
31
20
/**
32
21
* 自定义私有链接过期时间
33
22
*/
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 ;
35
29
36
30
37
31
/**
38
32
* 获取空间带宽统计
39
33
*/
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 {
41
35
return SdkConfigurer .getCdnManager ().getBandwidthData (domains , startDate , endDate , "day" );
42
36
}
43
37
44
38
/**
45
39
* 获取空间的流量统计
46
40
*/
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 {
48
42
return SdkConfigurer .getCdnManager ().getFluxData (domains , startDate , endDate , "day" );
49
43
}
50
44
@@ -73,168 +67,65 @@ public String getPrivateUrl(String publicUrl) {
73
67
/**
74
68
* 更新镜像源
75
69
*/
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 );
85
72
}
86
73
87
74
/**
88
75
* 设置文件生存时间
89
76
*/
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 );
99
79
}
100
80
101
81
/**
102
- * 重命令文件
82
+ * 重命名文件
103
83
*/
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 );
106
86
}
107
87
108
88
/**
109
89
* 移动文件
110
90
*/
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 );
113
93
}
114
94
115
95
/**
116
96
* 移动或复制文件
117
97
*/
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 );
136
104
}
137
- return false ;
138
105
}
139
106
140
107
/**
141
108
* 修改文件类型
142
109
*/
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 );
157
112
}
158
113
159
114
/**
160
115
* 批量删除文件,单次批量请求的文件数量不得超过1000
161
116
*/
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 );
202
122
}
203
123
204
124
/**
205
- * 获取空间文件列表,并映射到FileInfo
125
+ * 获取空间文件列表
206
126
*/
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 , "" );
238
129
}
239
130
240
131
public enum FileAction {
0 commit comments