七牛上传图片使用服务端uploadToken
This commit is contained in:
@@ -86,33 +86,6 @@ public class XChatConstants {
|
||||
*/
|
||||
public static final String AGORA_KEY = "04d48cdbfd4547b3933a59e7acb0ae55"; //已更换
|
||||
|
||||
/**
|
||||
* 七牛 access_url
|
||||
*/
|
||||
public static final String QINIU_ACCESS_URL_DEBUG = "http://image.uat.zhongjialx.com/";//已更换
|
||||
|
||||
public static final String QINIU_ACCESS_URL_RELEASE = "https://image.zhongjialx.com/";//已更换
|
||||
|
||||
|
||||
/**
|
||||
* 七牛 access_bucket(RELEASE)
|
||||
*/
|
||||
public static final String QINIU_ACCESS_BUCKET_RELEASE = "accompnay-image";//已更换
|
||||
/**
|
||||
* 七牛 access_bucket(DEBUG)
|
||||
*/
|
||||
public static final String QINIU_ACCESS_BUCKET_DEBUG = "accompany-image-dev";//已更换
|
||||
|
||||
/**
|
||||
* 七牛 access_key
|
||||
*/
|
||||
public static final String QINIU_ACCESS_KEY = "7c9eC45DhG3qVtyRXuQe17SZfEmELLtGK2umzTc1";//已更换
|
||||
|
||||
/**
|
||||
* 七牛 secret_key
|
||||
*/
|
||||
public static final String QINIU_SECRET_KEY = "t3sAMF3u6kFXJ9PCs3YxRzsu-ZjB03vL3aUAOVpk";//已更换
|
||||
|
||||
/**
|
||||
* 阿里云日志空间名字
|
||||
*/
|
||||
|
@@ -2,28 +2,23 @@ package com.yizhuan.xchat_android_core.file;
|
||||
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.netease.nim.uikit.common.util.file.FileUtil;
|
||||
import com.netease.nim.uikit.common.util.log.LogUtil;
|
||||
import com.qiniu.android.common.FixedZone;
|
||||
import com.qiniu.android.storage.Configuration;
|
||||
import com.qiniu.android.storage.UploadManager;
|
||||
import com.qiniu.android.utils.UrlSafeBase64;
|
||||
import com.yizhuan.xchat_android_constants.XChatConstants;
|
||||
import com.yizhuan.xchat_android_core.Env;
|
||||
import com.yizhuan.xchat_android_core.base.BaseModel;
|
||||
import com.yizhuan.xchat_android_core.bean.response.ServiceResult;
|
||||
import com.yizhuan.xchat_android_core.community.bean.DynamicMedia;
|
||||
import com.yizhuan.xchat_android_core.exception.ErrorThrowable;
|
||||
import com.yizhuan.xchat_android_core.utils.net.RxHelper;
|
||||
import com.yizhuan.xchat_android_library.net.rxnet.RxNet;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
import io.reactivex.Single;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import retrofit2.http.GET;
|
||||
|
||||
public class FileModel extends BaseModel implements IFileModel {
|
||||
|
||||
@@ -43,52 +38,33 @@ public class FileModel extends BaseModel implements IFileModel {
|
||||
}
|
||||
|
||||
private UploadManager uploadManager;
|
||||
public static final String accessUrl = Env.isDebug() ? XChatConstants.QINIU_ACCESS_URL_DEBUG : XChatConstants.QINIU_ACCESS_URL_RELEASE;
|
||||
public static final String accessKey = XChatConstants.QINIU_ACCESS_KEY;
|
||||
public static final String secretKey = XChatConstants.QINIU_SECRET_KEY;
|
||||
public static final String picprocessing = "?imageslim";
|
||||
public static final String BUCKET = Env.isDebug() ? XChatConstants.QINIU_ACCESS_BUCKET_DEBUG : XChatConstants.QINIU_ACCESS_BUCKET_RELEASE;
|
||||
private final Api api = RxNet.create(Api.class);
|
||||
|
||||
@Override
|
||||
public Single<String> uploadFile(String path) {
|
||||
|
||||
|
||||
File file;
|
||||
if (TextUtils.isEmpty(path) || !((file = new File(path)).exists())) {
|
||||
return Single.error(new ErrorThrowable(path + " 为空或者该文件不存在!"));
|
||||
}
|
||||
File finalFile = file;
|
||||
return Single.create(singleEmitter -> {
|
||||
try {
|
||||
// 1 构造上传策略
|
||||
JSONObject json = new JSONObject();
|
||||
long deadline = System.currentTimeMillis() / 1000 + 3600;
|
||||
// 有效时间为一个小时
|
||||
json.put("deadline", deadline);
|
||||
json.put("scope", BUCKET);
|
||||
String encodedPutPolicy = UrlSafeBase64.encodeToString(json.toString().getBytes());
|
||||
byte[] sign = HmacSHA1Encrypt(encodedPutPolicy, secretKey);
|
||||
String encodedSign = UrlSafeBase64.encodeToString(sign);
|
||||
String uploadToken = accessKey + ':' + encodedSign + ':' + encodedPutPolicy;
|
||||
|
||||
uploadManager.put(finalFile, FileUtil.getFileNameFromPath(path), uploadToken,
|
||||
(key, info, response) -> {
|
||||
if (info.isOK()) {
|
||||
try {
|
||||
String imgName = response.getString("key");
|
||||
singleEmitter.onSuccess(accessUrl + imgName + picprocessing);
|
||||
} catch (Exception e) {
|
||||
singleEmitter.onError(e);
|
||||
}
|
||||
} else {
|
||||
singleEmitter.onError(new Throwable(info.error));
|
||||
}
|
||||
}, null);
|
||||
|
||||
} catch (Exception e) {
|
||||
singleEmitter.onError(e);
|
||||
}
|
||||
});
|
||||
return api.getUploadToken()
|
||||
.compose(RxHelper.handleSchedulers())
|
||||
.compose(RxHelper.handleBeanData())
|
||||
.flatMap(uploadToken -> Single.create(singleEmitter ->
|
||||
uploadManager.put(finalFile, uploadToken.getKey(), uploadToken.getToken(),
|
||||
(key, info, response) -> {
|
||||
if (info.isOK()) {
|
||||
try {
|
||||
String imgUrl = response.getString("path");
|
||||
singleEmitter.onSuccess(imgUrl);
|
||||
} catch (Exception e) {
|
||||
singleEmitter.onError(e);
|
||||
}
|
||||
} else {
|
||||
singleEmitter.onError(new Throwable(info.error));
|
||||
}
|
||||
}, null)));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -102,51 +78,31 @@ public class FileModel extends BaseModel implements IFileModel {
|
||||
if (TextUtils.isEmpty(path) || !((file = new File(path)).exists())) {
|
||||
return Single.error(new ErrorThrowable(path + " 为空或者该文件不存在!"));
|
||||
}
|
||||
|
||||
File finalFile = file;
|
||||
Single<DynamicMedia> single = Single.create(singleEmitter -> {
|
||||
try {
|
||||
// 1 构造上传策略
|
||||
JSONObject json = new JSONObject();
|
||||
long deadline = System.currentTimeMillis() / 1000 + 3600;
|
||||
// 有效时间为一个小时
|
||||
json.put("deadline", deadline);
|
||||
json.put("scope", BUCKET);
|
||||
//在这里返回宽高和图片格式
|
||||
json.put("returnBody",
|
||||
"{\"key\":\"$(key)\",\"hash\":\"$(etag)\"," +
|
||||
"\"w\":\"$(imageInfo.width)\",\"h\":\"$(imageInfo.height)\"," +
|
||||
"\"format\":\"$(imageInfo.format)\"}");
|
||||
String encodedPutPolicy = UrlSafeBase64.encodeToString(json.toString().getBytes());
|
||||
byte[] sign = HmacSHA1Encrypt(encodedPutPolicy, secretKey);
|
||||
String encodedSign = UrlSafeBase64.encodeToString(sign);
|
||||
String uploadToken = accessKey + ':' + encodedSign + ':' + encodedPutPolicy;
|
||||
|
||||
uploadManager.put(finalFile, TextUtils.isEmpty(qiniuName) ? null : qiniuName, uploadToken,
|
||||
(key, info, response) -> {
|
||||
LogUtil.print(info);
|
||||
if (info.isOK()) {
|
||||
try {
|
||||
LogUtil.print("上传成功");
|
||||
LogUtil.print(response);
|
||||
DynamicMedia media = responseToMeia(response);
|
||||
if (media != null) {
|
||||
singleEmitter.onSuccess(media);
|
||||
return;
|
||||
return api.getUploadToken()
|
||||
.compose(RxHelper.handleSchedulers())
|
||||
.compose(RxHelper.handleBeanData())
|
||||
.flatMap(uploadToken -> Single.create(singleEmitter ->
|
||||
uploadManager.put(finalFile, uploadToken.getKey(), uploadToken.getToken(),
|
||||
(key, info, response) -> {
|
||||
if (info.isOK()) {
|
||||
try {
|
||||
LogUtil.print("上传成功");
|
||||
LogUtil.print(response);
|
||||
DynamicMedia media = responseToMeia(response);
|
||||
if (media != null) {
|
||||
singleEmitter.onSuccess(media);
|
||||
return;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
singleEmitter.onError(new Throwable("qiniu json error"));
|
||||
} else {
|
||||
singleEmitter.onError(new Throwable(info.error));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
singleEmitter.onError(new Throwable("qiniu json error"));
|
||||
} else {
|
||||
singleEmitter.onError(new Throwable(info.error));
|
||||
}
|
||||
}, null);
|
||||
|
||||
} catch (Exception e) {
|
||||
singleEmitter.onError(e);
|
||||
}
|
||||
});
|
||||
return single.observeOn(AndroidSchedulers.mainThread());
|
||||
}, null)));
|
||||
}
|
||||
|
||||
|
||||
@@ -155,33 +111,11 @@ public class FileModel extends BaseModel implements IFileModel {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用 HMAC-SHA1 签名方法对encryptText进行签名
|
||||
*
|
||||
* @param encryptText 被签名的字符串
|
||||
* @param encryptKey 密钥
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static byte[] HmacSHA1Encrypt(String encryptText, String encryptKey)
|
||||
throws Exception {
|
||||
byte[] data = encryptKey.getBytes("UTF-8");
|
||||
// 根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
|
||||
SecretKey secretKey = new SecretKeySpec(data, "HmacSHA1");
|
||||
// 生成一个指定 Mac 算法 的 Mac 对象
|
||||
Mac mac = Mac.getInstance("HmacSHA1");
|
||||
// 用给定密钥初始化 Mac 对象
|
||||
mac.init(secretKey);
|
||||
byte[] text = encryptText.getBytes("UTF-8");
|
||||
// 完成 Mac 操作
|
||||
return mac.doFinal(text);
|
||||
}
|
||||
|
||||
private DynamicMedia responseToMeia(JSONObject response) {
|
||||
DynamicMedia media = new DynamicMedia();
|
||||
try {
|
||||
String imgName = response.getString("key");
|
||||
media.setResUrl(accessUrl + imgName + picprocessing);
|
||||
String imgNamePath = response.getString("path");
|
||||
media.setResUrl(imgNamePath);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
@@ -196,4 +130,16 @@ public class FileModel extends BaseModel implements IFileModel {
|
||||
LogUtil.print("七牛上传", media);
|
||||
return media;
|
||||
}
|
||||
|
||||
interface Api {
|
||||
|
||||
/**
|
||||
* 获取钱包
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GET("/qiniu/upload/getUploadToken")
|
||||
Single<ServiceResult<UploadToken>> getUploadToken();
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,9 @@
|
||||
package com.yizhuan.xchat_android_core.file;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class UploadToken {
|
||||
private String key;
|
||||
private String token;
|
||||
}
|
Reference in New Issue
Block a user