柬埔寨通 v6.1.5版本的 MD5 值为:9b68581ac30883219d6a25858a099d6b
以下内容为反编译后的 QiNiuUploader.java 源代码,内容仅作参考
package net.duohuo.magappx.common.service.file;
import android.text.TextUtils;
import android.widget.Toast;
import com.baidu.platform.comapi.map.MapBundleKey;
import com.qiniu.android.common.AutoZone;
import com.qiniu.android.common.FixedZone;
import com.qiniu.android.common.Zone;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.Configuration;
import com.qiniu.android.storage.UpCancellationSignal;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UploadManager;
import com.qiniu.android.storage.UploadOptions;
import com.umeng.analytics.pro.bg;
import com.umeng.socialize.net.dplus.CommonNetImpl;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import net.duohuo.core.dialog.IDialog;
import net.duohuo.core.ioc.Ioc;
import net.duohuo.core.net.Net;
import net.duohuo.core.net.Result;
import net.duohuo.core.net.Task;
import net.duohuo.core.util.FileUtil;
import net.duohuo.core.util.LogUtil;
import net.duohuo.core.util.PhotoUtil;
import net.duohuo.core.util.SafeJsonUtil;
import net.duohuo.magappx.API;
import net.duohuo.magappx.SiteConfig;
import net.duohuo.magappx.common.util.Constants;
import net.duohuo.magappx.common.view.RichContent;
import net.duohuo.magappx.video.util.Config;
import org.json.JSONObject;
public class QiNiuUploader implements FileUploader {
static String token;
Configuration config;
List<RichContent.Pic> pics;
SiteConfig siteConfig;
UploadCallBack uploadCallBack;
long lastTokenTime = 0;
String[] httpZones = {"http://upload.qiniu.com", "http://upload-z1.qiniu.com", "http://upload-z2.qiniu.com", "http://upload-na0.qiniu.com"};
String[] httpsZones = {"https://upload.qbox.me", "https://upload-z1.qbox.me", "https://upload-z2.qbox.me", "https://upload-na0.qbox.me"};
Zone[] zones = {FixedZone.zone0, FixedZone.zone1, FixedZone.zone2, FixedZone.zoneNa0};
String configZoneStr = "";
boolean isHttpZone = false;
Zone configZone = null;
boolean isGettingToken = false;
List<RichContent.Pic> failPics = new ArrayList();
@Override
public void uploadPic(RichContent.Pic pic) {
if (this.pics == null) {
this.pics = new ArrayList();
}
this.pics.add(pic);
if (this.isGettingToken) {
return;
}
checkToken();
}
public void checkToken() {
if (System.currentTimeMillis() - this.lastTokenTime > 120000) {
this.isGettingToken = true;
updateToken(new Task<Object>() {
@Override
public void onResult(Object obj) {
QiNiuUploader.this.uploadTask();
}
});
} else {
uploadTask();
}
}
private void updateToken(final Task task) {
if (this.siteConfig == null) {
this.siteConfig = (SiteConfig) Ioc.get(SiteConfig.class);
}
String str = this.siteConfig.qiNiuUploadUrl;
this.configZoneStr = str;
if (str != null) {
int i = 0;
while (true) {
String[] strArr = this.httpZones;
if (i >= strArr.length) {
break;
}
if (this.configZoneStr.contains(strArr[i])) {
this.configZone = this.zones[i];
this.isHttpZone = true;
break;
}
i++;
}
if (!this.isHttpZone) {
int i2 = 0;
while (true) {
String[] strArr2 = this.httpsZones;
if (i2 >= strArr2.length) {
break;
}
if (this.configZoneStr.contains(strArr2[i2])) {
this.configZone = this.zones[i2];
break;
}
i2++;
}
}
}
if (this.configZone == null) {
this.configZone = new AutoZone();
}
Net.url(API.Common.qiniu_token).showProgress(false).get(new Task<Result>() {
@Override
public void onError() {
super.onError();
QiNiuUploader.this.isGettingToken = false;
}
@Override
public void onResult(Result result) {
QiNiuUploader.this.isGettingToken = false;
QiNiuUploader.token = result.json().getString("data");
QiNiuUploader.this.lastTokenTime = System.currentTimeMillis();
QiNiuUploader.this.config = new Configuration.Builder().zone(QiNiuUploader.this.configZone).build();
Task task2 = task;
if (task2 != null) {
task2.onResult(null);
}
}
});
}
public void uploadTask() {
if (this.pics.size() == 0) {
if (this.uploadCallBack != null) {
if (this.failPics.size() > 0) {
final int size = this.failPics.size();
Ioc.getCurrentActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
((IDialog) Ioc.get(IDialog.class)).showToastShort(Ioc.getCurrentActivity(), size + "张图片上传失败");
}
});
this.uploadCallBack.onPicFail(this.failPics);
this.failPics.clear();
return;
}
this.uploadCallBack.onUploaded();
return;
}
return;
}
for (int i = 0; i < this.pics.size(); i++) {
onUpload(this.pics.get(i));
}
this.pics.clear();
}
private void onUpload(final RichContent.Pic pic) {
final String sb;
UploadManager uploadManager = new UploadManager(this.config);
if (pic.isFlashPic) {
if (pic.url.toLowerCase().endsWith(".gif") || PhotoUtil.isGifFile(new File(pic.url))) {
sb = "chat/flashpic/" + new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/" + System.currentTimeMillis() + ((int) (Math.random() * 1000.0d)) + "_" + ((int) (Math.random() * 1000.0d)) + ".gif";
} else {
sb = "chat/flashpic/" + new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/" + System.currentTimeMillis() + ((int) (Math.random() * 1000.0d)) + "_" + ((int) (Math.random() * 1000.0d)) + PhotoUtil.getPicSuffix(pic.url);
}
} else {
if (pic.isPic) {
if (pic.url.toLowerCase().endsWith(".gif") || PhotoUtil.isGifFile(new File(pic.url))) {
StringBuilder sb2 = new StringBuilder();
sb2.append(pic.isChat ? "chat/" : "");
sb2.append("pic/");
sb2.append(new SimpleDateFormat("yyyyMMdd").format(new Date()));
sb2.append("/");
sb2.append(System.currentTimeMillis());
sb2.append((int) (Math.random() * 1000.0d));
sb2.append("_");
sb2.append((int) (Math.random() * 1000.0d));
sb2.append(".gif");
sb = sb2.toString();
} else {
StringBuilder sb3 = new StringBuilder();
sb3.append(pic.isChat ? "chat/" : "");
sb3.append("pic/");
sb3.append(new SimpleDateFormat("yyyyMMdd").format(new Date()));
sb3.append("/");
sb3.append(System.currentTimeMillis());
sb3.append((int) (Math.random() * 1000.0d));
sb3.append("_");
sb3.append((int) (Math.random() * 1000.0d));
sb3.append(PhotoUtil.getPicSuffix(pic.url));
sb = sb3.toString();
}
} else if (pic.isAudio) {
if (pic.moduleType == 1) {
sb = "comment/audio/" + new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/" + System.currentTimeMillis() + ((int) (Math.random() * 1000.0d)) + "_" + ((int) (Math.random() * 1000.0d)) + ".amr";
} else {
StringBuilder sb4 = new StringBuilder();
sb4.append(pic.isChat ? "chat/" : "");
sb4.append("audio/");
sb4.append(new SimpleDateFormat("yyyyMMdd").format(new Date()));
sb4.append("/");
sb4.append(System.currentTimeMillis());
sb4.append((int) (Math.random() * 1000.0d));
sb4.append("_");
sb4.append((int) (Math.random() * 1000.0d));
sb4.append(".amr");
sb = sb4.toString();
}
} else if (pic.isAttachment) {
sb = "attach/" + new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/" + System.currentTimeMillis() + ((int) (Math.random() * 1000.0d)) + "_" + ((int) (Math.random() * 1000.0d)) + "." + FileUtil.getFileType(pic.url);
} else {
StringBuilder sb5 = new StringBuilder();
sb5.append(pic.isChat ? "chat/" : "");
sb5.append("video/");
sb5.append(new SimpleDateFormat("yyyyMMdd").format(new Date()));
sb5.append("/");
sb5.append(System.currentTimeMillis());
sb5.append((int) (Math.random() * 1000.0d));
sb5.append("_");
sb5.append((int) (Math.random() * 1000.0d));
sb5.append(".mp4");
sb = sb5.toString();
}
}
pic.uploadkey = sb;
UpCompletionHandler upCompletionHandler = new UpCompletionHandler() {
@Override
public void complete(String str, ResponseInfo responseInfo, JSONObject jSONObject) {
if (responseInfo.isCancelled()) {
return;
}
if (responseInfo.isOK()) {
LogUtil.i("zmh", "suceess");
if (pic.isPic) {
try {
JSONObject jSONObject2 = jSONObject.getJSONObject("data");
pic.width = jSONObject2.getInt("w");
pic.height = jSONObject2.getInt(bg.aG);
pic.size = jSONObject2.getInt(MapBundleKey.OfflineMapKey.OFFLINE_TOTAL_SIZE);
QiNiuUploader.this.getAid(pic);
return;
} catch (Exception e) {
e.printStackTrace();
return;
}
}
if (pic.isAudio) {
QiNiuUploader.this.getAudioUrl(pic);
return;
} else if (pic.isAttachment) {
QiNiuUploader.this.setAttachMentAid(pic);
return;
} else {
QiNiuUploader.this.beginVideothumb(str, pic);
return;
}
}
LogUtil.i("zmh", "fail");
if (responseInfo.statusCode == 413) {
if (pic.isPic) {
pic.customErrorMsg = "图片太大了";
Toast.makeText(Ioc.getApplicationContext(), pic.customErrorMsg, 0).show();
} else if (!pic.isAudio) {
pic.customErrorMsg = "视频太大了";
Toast.makeText(Ioc.getApplicationContext(), pic.customErrorMsg, 0).show();
}
}
if (pic.isPic) {
QiNiuUploader.this.taskFail(pic);
}
}
};
if (pic.isUri) {
uploadManager.put(pic.uri, Ioc.getApplicationContext().getContentResolver(), pic.uploadkey, token, upCompletionHandler, new UploadOptions(null, null, false, null, new UpCancellationSignal() {
@Override
public boolean isCancelled() {
return !sb.equals(pic.uploadkey);
}
}));
} else {
uploadManager.put((pic.isPic || pic.isAudio || pic.isAttachment) ? pic.url : pic.videoFile.getAbsolutePath(), pic.uploadkey, token, upCompletionHandler, new UploadOptions(null, null, false, null, new UpCancellationSignal() {
@Override
public boolean isCancelled() {
return !sb.equals(pic.uploadkey);
}
}));
}
}
public void setAttachMentAid(final RichContent.Pic pic) {
Net url = Net.url(API.Common.qiniu_attachment_save);
url.showProgress(false);
url.param("attach_key", pic.uploadkey);
url.param("attach_size", Long.valueOf(pic.size));
url.post(new Task<Result>() {
@Override
public void onResult(Result result) {
if (result.success()) {
pic.attachment_url = SafeJsonUtil.getString(result.json(), "attach_url");
pic.attachmentAid = SafeJsonUtil.getString(result.json(), "attach_aid");
if (pic.callback != null) {
pic.callback.sucess(pic);
}
}
}
});
}
public void getAudioUrl(final RichContent.Pic pic) {
Net url = Net.url(API.Common.qiniu_audio_save);
url.showProgress(false);
url.param("audio_key", pic.uploadkey);
url.post(new Task<Result>() {
@Override
public void onResult(Result result) {
if (result.success()) {
pic.audio_url = SafeJsonUtil.getString(result.json(), "audio_url");
pic.aid = SafeJsonUtil.getString(result.json(), "audio_aid");
pic.isUpload = true;
if (pic.callback == null || TextUtils.isEmpty(pic.audio_url)) {
return;
}
pic.callback.sucess(pic);
}
}
});
}
public void getAid(final RichContent.Pic pic) {
Net url = Net.url(API.Common.qiniu_save);
url.showProgress(false);
url.param("key", pic.uploadkey);
url.param("width", Integer.valueOf(pic.width));
url.param("height", Integer.valueOf(pic.height));
url.param(MapBundleKey.OfflineMapKey.OFFLINE_TOTAL_SIZE, Long.valueOf(pic.size));
url.param("time_stamp", pic.time_stamp);
url.param(Constants.LATITUDE, pic.latitude);
url.param(Constants.LONGITUDE, pic.longitude);
url.post(new Task<Result>() {
@Override
public void onResult(Result result) {
if (!result.success()) {
QiNiuUploader.this.taskFail(pic);
return;
}
pic.aid = result.getData().getString(CommonNetImpl.AID);
pic.pic_url = result.getData().getString("url");
pic.isUpload = true;
if (pic.callback != null) {
pic.callback.sucess(pic);
}
}
});
}
public void taskFail(RichContent.Pic pic) {
if (pic.callback != null) {
pic.callback.failed(pic);
}
this.failPics.add(pic);
}
public void beginVideothumb(final String str, final RichContent.Pic pic) {
UploadManager uploadManager = new UploadManager(new Configuration.Builder().zone(new AutoZone()).build());
StringBuilder sb = new StringBuilder();
sb.append(pic.isChat ? "chat/" : "");
sb.append("pic/");
sb.append(new SimpleDateFormat("yyyyMMdd").format(new Date()));
sb.append("/");
sb.append(System.currentTimeMillis());
sb.append((int) (Math.random() * 1000.0d));
sb.append("_");
sb.append((int) (Math.random() * 1000.0d));
sb.append(Config.COVER_PATH_SUFFIX);
final String sb2 = sb.toString();
uploadManager.put(pic.thumbFile, sb2, token, new UpCompletionHandler() {
@Override
public void complete(String str2, ResponseInfo responseInfo, JSONObject jSONObject) {
if (responseInfo.isOK()) {
try {
JSONObject jSONObject2 = jSONObject.getJSONObject("data");
pic.width = jSONObject2.getInt("w");
pic.height = jSONObject2.getInt(bg.aG);
QiNiuUploader.this.getVideoAid(pic, str, sb2, jSONObject2.optString("w"), jSONObject2.optString(bg.aG), jSONObject2.optString(MapBundleKey.OfflineMapKey.OFFLINE_TOTAL_SIZE));
return;
} catch (Exception unused) {
return;
}
}
if (pic.callback != null) {
pic.callback.failed(pic);
}
}
}, (UploadOptions) null);
}
public void getVideoAid(final RichContent.Pic pic, String str, String str2, String str3, String str4, String str5) {
Net url = Net.url(API.Common.qiniu_video_save);
url.showProgress(false);
url.param("video_key", str);
url.param("pic_key", str2);
url.param("pic_width", str3);
url.param("pic_height", str4);
url.param("pic_size", str5);
url.post(new Task<Result>() {
@Override
public void onResult(Result result) {
if (result.success()) {
if (result.json().containsKey("video_aid")) {
pic.videoAid = result.json().getString("video_aid");
}
if (result.json().containsKey("pic_aid")) {
pic.thumbAid = result.json().getString("pic_aid");
}
if (result.json().containsKey("video_url")) {
pic.video_url = result.json().getString("video_url");
}
if (result.json().containsKey("pic_url")) {
pic.pic_url = result.json().getString("pic_url");
}
pic.isUpload = true;
if (pic.callback != null) {
pic.callback.sucess(pic);
return;
}
return;
}
if (pic.callback != null) {
pic.callback.failed(pic);
}
}
});
}
}