CNUploadActivity.java
18.4 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
package libs.cnlive.com.uploadsdk.SDK;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.ksyun.ks3.exception.Ks3Error;
import com.ksyun.ks3.model.PartETag;
import com.ksyun.ks3.model.result.CompleteMultipartUploadResult;
import com.ksyun.ks3.model.result.InitiateMultipartUploadResult;
import com.ksyun.ks3.model.result.ListPartsResult;
import com.ksyun.ks3.services.Ks3Client;
import com.ksyun.ks3.services.Ks3ClientConfiguration;
import com.ksyun.ks3.services.handler.AbortMultipartUploadResponseHandler;
import com.ksyun.ks3.services.handler.CompleteMultipartUploadResponseHandler;
import com.ksyun.ks3.services.handler.InitiateMultipartUploadResponceHandler;
import com.ksyun.ks3.services.handler.ListPartsResponseHandler;
import com.ksyun.ks3.services.handler.UploadPartResponceHandler;
import com.ksyun.ks3.services.request.AbortMultipartUploadRequest;
import com.ksyun.ks3.services.request.CompleteMultipartUploadRequest;
import com.ksyun.ks3.services.request.InitiateMultipartUploadRequest;
import com.ksyun.ks3.services.request.ListPartsRequest;
import com.ksyun.ks3.services.request.UploadPartRequest;
import org.apache.http.Header;
import java.util.ArrayList;
import java.util.List;
import libs.cnlive.com.uploadsdk.database.SQLiteDAOImpl;
import static libs.cnlive.com.uploadsdk.SDK.UploadState.LIST_PART_FINISH;
import static libs.cnlive.com.uploadsdk.SDK.UploadState.UPDATE_SINGLE_UPLOAD_STATUS;
import static libs.cnlive.com.uploadsdk.SDK.UploadState.UPLOAD_NEXT_PART;
import static libs.cnlive.com.uploadsdk.SDK.UploadState.UPLOAD_PART_FINISH;
/**
* Created by Administrator on 2016/11/4.
*/
public class CNUploadActivity extends AppCompatActivity {
public static final long PART_SIZE = 5 * 1024 * 1024;
private Ks3Client client;
private Ks3ClientConfiguration configuration;
// private Map<String, List<UploadFile>> dataSource;
// private File currentDir;
private SQLiteDAOImpl dao = new SQLiteDAOImpl(CNUploadActivity.this);
private MyHandler myHandler = new MyHandler();
private List<UploadFile> uploadFileList = new ArrayList<>();
private String mBucketname = "trans-test";
// private String mBucketKey = "androidtest";
// public UploadActivity(Map<String, List<UploadFile>> dataSource, File currentDir) {
// this.dataSource = dataSource;
// this.currentDir = currentDir;
// }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
//初始化Ks3Client
public void initUploadPart(String accessKey, String accessSecret, Context context, String endPoint) {
configuration = Ks3ClientConfiguration.getDefaultConfiguration();
client = new Ks3Client(accessKey, accessSecret, context);
client.setEndpoint(endPoint);
client.setConfiguration(configuration);
}
//多块上传
public void uploadFile(UploadFile item) {
InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(mBucketname, item.file.getName());
if (dao.find(item.file.getPath()) == null || dao.find(item.file.getPath()).getFilePath().isEmpty()) {
initiateMultipartUpload(request, item);
} else {
item.itemUploadPartFactory.partNumber = item.partNumber;
item.itemUploadPartFactory.offset = item.offset;
item.itemUploadPartFactory.remainingBytes = item.file.length() - (item.itemUploadPartFactory.partNumber - 1) * PART_SIZE;
item.status = UploadFile.STATUS_CONTINUE_UPLOAD_PART;
uploadpart(item);
}
}
//初始化多块上传
private void initiateMultipartUpload(InitiateMultipartUploadRequest request, final UploadFile item) {
client.initiateMultipartUpload(request,
new InitiateMultipartUploadResponceHandler() {
@Override
public void onSuccess(int statesCode,
Header[] responceHeaders,
InitiateMultipartUploadResult result) {
// List<UploadFile> uploadFiles = dataSource
// .get(currentDir.getPath());
// for (UploadFile file : uploadFiles) {
// if (file.file.getPath().equalsIgnoreCase(item.file.getPath())) {
// file.status = UploadFile.STATUS_INIT;
// item.status = UploadFile.STATUS_INIT;
// }
// }
item.status = UploadFile.STATUS_INIT;
Log.e("++++++init", "---->" + item.file.getName() + " " + result.getUploadId());
if (dao.find(item.file.getPath()) == null || dao.find(item.file.getPath()).getFilePath().isEmpty()) {
dao.save(item.file.getPath(), result.getUploadId(), result.getKey(), 1, 0);
}
Log.e("---->", String.valueOf(dao.findAll().size()) + " " + dao.find(item.file.getPath()).getFilePath());
uploadFileList.add(item);
beginMultiUpload(result, item);
myHandler.sendEmptyMessage(UploadState.UPDATE_SINGLE_UPLOAD_STATUS);
}
@Override
public void onFailure(int statesCode, Ks3Error error,
Header[] responceHeaders, String response,
Throwable paramThrowable) {
// List<UploadFile> uploadFiles = dataSource
// .get(currentDir.getPath());
// for (UploadFile file : uploadFiles) {
// if (file.file.getPath().equalsIgnoreCase(
// item.file.getPath())) {
// file.status = UploadFile.STATUS_INIT_FAIL;
// item.status = UploadFile.STATUS_INIT_FAIL;
// }
// }
item.status = UploadFile.STATUS_INIT_FAIL;
Log.e("initFaliure", "---->" + statesCode);
myHandler.sendEmptyMessage(UploadState.UPDATE_SINGLE_UPLOAD_STATUS);
}
});
}
private void beginMultiUpload(InitiateMultipartUploadResult initResult, UploadFile item) {
item.itemUploadPartFactory = new UploadPartFactory(
initResult.getBucket(), initResult.getKey(),
initResult.getUploadId(), item.file, PART_SIZE);
Message message = myHandler.obtainMessage();
message.what = UPLOAD_NEXT_PART;
Bundle bundle = new Bundle();
bundle.putSerializable("uploadFile", item);
message.setData(bundle);
myHandler.sendMessage(message);
}
private void uploadpart(final UploadFile item) {
if (item.itemUploadPartFactory.hasMoreRequests()) {
final UploadPartRequest request = item.itemUploadPartFactory.getNextUploadPartRequest();
client.uploadPart(request, new UploadPartResponceHandler() {
double progressInFile = 0;
@Override
public void onTaskProgress(double progress) {
long uploadedInpart = (long) (progress / 100 * request.contentLength);
long uploadedInFile = uploadedInpart + item.itemUploadPartFactory.getUploadSize();
progressInFile = Double
.valueOf(request.getFile().length() > 0 ? uploadedInFile
* 1.0D
/ request.getFile().length()
* 100.0D
: -1.0D);
// List<UploadFile> uploadFiles = dataSource.get(currentDir
// .getPath());
// for (UploadFile file : uploadFiles) {
// if (file.file.getPath().equalsIgnoreCase(item.file.getPath())) {
// if (item.isPaused) {
// Log.e("upload", "---->" + item.progress + "暂停");
// item.status = UploadFile.STATUS_PAUSED;
// file.status = UploadFile.STATUS_PAUSED;
// } else if (!item.isPaused && progressInFile > item.progress) {
// Log.e("upload", "---->" + item.progress + "上传");
// file.status = UploadFile.STATUS_UPLOADPART;
// file.progress = (int) progressInFile;
// item.status = UploadFile.STATUS_UPLOADPART;
// item.progress = (int) progressInFile;
// }
// }
// }
// item.progressInFile = progressInFile;
// item.status = UploadFile.STATUS_UPLOADPART;
if (item.isPaused) {
// item.progressInFile = progressInFile;
item.status = UploadFile.STATUS_PAUSED;
} else if (!item.isPaused && progressInFile > item.progress) {
item.progress = progressInFile;
item.status = UploadFile.STATUS_UPLOADPART;
}
Log.e("upload", "---->" + progressInFile + " " + item.progress);
myHandler.sendEmptyMessage(UploadState.UPDATE_SINGLE_UPLOAD_STATUS);
}
@Override
public void onSuccess(int statesCode, Header[] responceHeaders,
PartETag result) {
if (!item.isPaused) {
Message message = myHandler.obtainMessage();
message.what = UploadState.UPLOAD_NEXT_PART;
Bundle bundle = new Bundle();
bundle.putSerializable("uploadFile", item);
message.setData(bundle);
myHandler.sendMessage(message);
}
}
@Override
public void onFailure(int statesCode, Ks3Error error,
Header[] responceHeaders, String response,
Throwable throwable) {
// List<UploadFile> uploadFiles = dataSource.get(currentDir
// .getPath());
// for (UploadFile file : uploadFiles) {
// if (file.file.getPath().equalsIgnoreCase(
// item.file.getPath())) {
// file.status = UploadFile.STATUS_UPLOADPART_FAIL;
// file.progress = (int) progressInFile;
// item.status = UploadFile.STATUS_UPLOADPART_FAIL;
// item.progress = (int) progressInFile;
// }
// }
item.status = UploadFile.STATUS_UPLOADPART_FAIL;
item.progress = (int) progressInFile;
Log.e("upload failure", "---->" + request.getPartNumber() + " " + request.getFileOffset() + " " + request.getContentLength());
myHandler.sendEmptyMessage(UploadState.UPDATE_SINGLE_UPLOAD_STATUS);
Log.e("Upload failure", "");
}
});
Log.e("---", " " + item.file.getPath() + " " + request.getFileOffset());
item.partNumber = request.getPartNumber();
item.offset = request.getFileOffset();
dao.update(item.itemUploadPartFactory.getUploadId(), request.getPartNumber(), request.getFileOffset());
} else {
Message message = myHandler.obtainMessage();
message.what = UploadState.UPLOAD_PART_FINISH;
Bundle bundle = new Bundle();
bundle.putSerializable("uploadFile", item);
message.setData(bundle);
myHandler.sendMessage(message);
}
}
private void listParts(ListPartsRequest request, final UploadFile item) {
client.listParts(request, new ListPartsResponseHandler() {
@Override
public void onFailure(int i, Ks3Error ks3Error, Header[] headers, String s, Throwable throwable) {
// List<UploadFile> uploadFiles = dataSource.get(currentDir.getPath());
// for (UploadFile file : uploadFiles) {
// if (file.file.getPath().equalsIgnoreCase(item.file.getPath())) {
// file.status = UploadFile.STATUS_LISTING_FAIL;
// item.status = UploadFile.STATUS_LISTING_FAIL;
// }
// }
item.status = UploadFile.STATUS_LISTING_FAIL;
myHandler.sendEmptyMessage(UPDATE_SINGLE_UPLOAD_STATUS);
uploadFileList.remove(item);
dao.delete(item.file.getPath());
}
@Override
public void onSuccess(int i, Header[] headers, ListPartsResult listPartsResult) {
Message message = myHandler.obtainMessage();
message.what = LIST_PART_FINISH;
message.obj = listPartsResult;
Bundle bundle = new Bundle();
bundle.putSerializable("uploadFile", item);
message.setData(bundle);
myHandler.sendMessage(message);
}
});
}
private void completeUploadPart(CompleteMultipartUploadRequest request, final UploadFile item) {
client.completeMultipartUpload(request, new CompleteMultipartUploadResponseHandler() {
@Override
public void onFailure(int i, Ks3Error ks3Error, Header[] headers, String s, Throwable throwable) {
// List<UploadFile> uploadFiles = dataSource.get(currentDir.getPath());
// for (UploadFile file : uploadFiles) {
// if (file.file.getPath().equalsIgnoreCase(item.file.getPath())) {
// file.status = UploadFile.STATUS_COMPLETE_FAIL;
// file.progress = 100;
// item.status = UploadFile.STATUS_COMPLETE_FAIL;
// item.progress = 100;
// }
// }
item.status = UploadFile.STATUS_COMPLETE_FAIL;
item.progress = 100;
myHandler.sendEmptyMessage(0);
}
@Override
public void onSuccess(int i, Header[] headers, CompleteMultipartUploadResult completeMultipartUploadResult) {
// List<UploadFile> uploadFiles = dataSource.get(currentDir.getPath());
// for (UploadFile file : uploadFiles) {
// if (file.file.getPath().equalsIgnoreCase(item.file.getPath())) {
// file.status = UploadFile.STATUS_COMPLETE;
// item.status = UploadFile.STATUS_COMPLETE;
// }
// }
item.status = UploadFile.STATUS_COMPLETE;
myHandler.sendEmptyMessage(0);
}
});
uploadFileList.remove(item);
dao.delete(item.file.getPath());
}
//取消多块上传
public void abortMultipartUpload(final UploadFile item) {
AbortMultipartUploadRequest request = new AbortMultipartUploadRequest(item.itemUploadPartFactory.getBucketName(), item.itemUploadPartFactory.getObjectKey(), item.itemUploadPartFactory.getUploadId());
client.abortMultipartUpload(request, new AbortMultipartUploadResponseHandler() {
@Override
public void onFailure(int i, Ks3Error ks3Error, Header[] headers, String s, Throwable throwable) {
}
@Override
public void onSuccess(int i, Header[] headers) {
uploadFileList.remove(item);
dao.delete(item.file.getPath());
}
});
}
//暂停
public void pause(UploadFile item) {
client.pause(CNUploadActivity.this);
for (UploadFile uploadFile : uploadFileList) {
if (uploadFile.file.getPath().equals(item.file.getPath())) {
uploadFile.status = UploadFile.STATUS_PAUSED;
} else if (!uploadFile.file.getPath().equalsIgnoreCase(item.file.getPath())) {
uploadFile.itemUploadPartFactory.partNumber = uploadFile.partNumber;
uploadFile.itemUploadPartFactory.offset = uploadFile.offset;
uploadFile.itemUploadPartFactory.remainingBytes = uploadFile.file.length() - (uploadFile.partNumber - 1) * PART_SIZE;
uploadpart(uploadFile);
}
}
}
class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
int what = msg.what;
Bundle bundle;
UploadFile item;
switch (what) {
case UPDATE_SINGLE_UPLOAD_STATUS:
// adapter.updateCurrent();
updateCurrent();
break;
case UPLOAD_NEXT_PART:
bundle = msg.getData();
item = (UploadFile) bundle.get("uploadFile");
uploadpart(item);
break;
case UPLOAD_PART_FINISH:
bundle = msg.getData();
item = (UploadFile) bundle.get("uploadFile");
ListPartsRequest listPartsRequest = new ListPartsRequest(
item.itemUploadPartFactory.getBucketName(),
item.itemUploadPartFactory.getObjectKey(),
item.itemUploadPartFactory.getUploadId());
listParts(listPartsRequest, item);
break;
case LIST_PART_FINISH:
ListPartsResult listResult = (ListPartsResult) msg.obj;
bundle = msg.getData();
item = (UploadFile) bundle.get("uploadFile");
CompleteMultipartUploadRequest comRequest = new CompleteMultipartUploadRequest(listResult);
completeUploadPart(comRequest, item);
break;
default:
break;
}
}
}
public void updateCurrent() {
}
}