Skip to content

Commit

Permalink
Merge pull request #26 from JoshuaYin/master
Browse files Browse the repository at this point in the history
2.1.3
  • Loading branch information
joshua.yin authored Aug 30, 2019
2 parents b1277ae + 1623277 commit 6b1031d
Show file tree
Hide file tree
Showing 26 changed files with 83 additions and 40 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<dependency>
<groupId>cn.ucloud.ufile</groupId>
<artifactId>ufile-client-java</artifactId>
<version>2.1.2</version>
<version>2.1.3</version>
</dependency>
```

Expand All @@ -36,7 +36,7 @@
/*
* your other dependencies
*/
implementation 'cn.ucloud.ufile:ufile-client-java:2.1.2'
implementation 'cn.ucloud.ufile:ufile-client-java:2.1.3'
}
```

Expand Down
2 changes: 1 addition & 1 deletion ufile-sample-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<dependency>
<groupId>cn.ucloud.ufile</groupId>
<artifactId>ufile-client-java</artifactId>
<version>2.1.2</version>
<version>2.1.3</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public class Constants {
static {
// 开启Debug级别日志
JLog.SHOW_TEST = true;
JLog.SHOW_DEBUG = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
import cn.ucloud.ufile.http.UfileCallback;
import cn.ucloud.ufile.sample.Constants;
import cn.ucloud.ufile.util.JLog;
import cn.ucloud.ufile.util.MimeTypeUtil;
import okhttp3.Request;

import java.io.*;
import java.net.URLEncoder;

/**
* @author: joshua
Expand All @@ -22,15 +24,16 @@
*/
public class PutObjectSample {
private static final String TAG = "PutObjectSample";
private static ObjectConfig config = new ObjectConfig("cn-bj", "ufileos.com");
private static ObjectConfig config = new ObjectConfig("cn-sh2", "ufileos.com");

public static void main(String[] args) {
InputStream is = new ByteArrayInputStream(new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07});
// 如果上传File,则文件的MimeType可以使用MimeTypeUtil.getMimeType(File)来获取,MimeTypeUtil可能支持的type类型不全,用户可以按需自行填写
String mimeType = "image/jpeg";
String keyName = "a/b/c.jpg";
String bucketName = "bucket_name";
putStream(is, mimeType, keyName, bucketName);
File file = new File("");
String keyName = "";
String mimeType = MimeTypeUtil.getMimeType(keyName);
String bucketName = "";
putFile(file, mimeType, keyName, bucketName);
}

public static void putFile(File file, String mimeType, String nameAs, String toBucket) {
Expand Down
2 changes: 1 addition & 1 deletion ufile/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>cn.ucloud.ufile</groupId>
<artifactId>ufile</artifactId>
<packaging>pom</packaging>
<version>2.1.2</version>
<version>2.1.3</version>

<modules>
<module>ufile-core</module>
Expand Down
6 changes: 3 additions & 3 deletions ufile/ufile-client-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
<parent>
<artifactId>ufile</artifactId>
<groupId>cn.ucloud.ufile</groupId>
<version>2.1.2</version>
<version>2.1.3</version>
</parent>

<artifactId>ufile-client-java</artifactId>
<version>2.1.2</version>
<version>2.1.3</version>

<dependencies>
<dependency>
Expand All @@ -26,7 +26,7 @@
<dependency>
<groupId>cn.ucloud.ufile</groupId>
<artifactId>ufile-core</artifactId>
<version>2.1.2</version>
<version>2.1.3</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public AppendObjectApi withAuthOptionalData(JsonElement authOptionalData) {
}

@Override
protected void prepareData() throws UfileParamException, UfileIOException, UfileAuthorizationException, UfileSignatureException {
protected void prepareData() throws UfileClientException {
parameterValidat();

List<Parameter<String>> query = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import cn.ucloud.ufile.auth.UfileAuthorizationException;
import cn.ucloud.ufile.auth.sign.UfileSignatureException;
import cn.ucloud.ufile.bean.base.BaseResponseBean;
import cn.ucloud.ufile.exception.UfileClientException;
import cn.ucloud.ufile.exception.UfileParamException;
import cn.ucloud.ufile.exception.UfileRequiredParamNotFoundException;
import cn.ucloud.ufile.http.HttpClient;
Expand Down Expand Up @@ -79,7 +80,7 @@ public DeleteObjectApi withAuthOptionalData(JsonElement authOptionalData) {
}

@Override
protected void prepareData() throws UfileParamException, UfileAuthorizationException, UfileSignatureException {
protected void prepareData() throws UfileClientException {
parameterValidat();
String contentType = "application/json; charset=utf-8";
String date = dateFormat.format(new Date(System.currentTimeMillis()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public DownloadFileApi setBufferSize(int bufferSize) {
}

@Override
protected void prepareData() throws UfileParamException, UfileAuthorizationException, UfileSignatureException {
protected void prepareData() throws UfileClientException {
parameterValidat();

File dir = new File(localPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import cn.ucloud.ufile.util.Parameter;
import com.google.gson.JsonElement;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


/**
* API-生成私有下载URL
Expand Down Expand Up @@ -82,7 +85,7 @@ public GenerateObjectPrivateUrlApi withAuthOptionalData(JsonElement authOptional
* @throws UfileAuthorizationException 授权异常时抛出
* @throws UfileSignatureException 签名异常时抛出
*/
public String createUrl() throws UfileParamException, UfileAuthorizationException, UfileSignatureException {
public String createUrl() throws UfileClientException {
parameterValidat();
long expiresTime = System.currentTimeMillis() / 1000 + expiresDuration;

Expand Down Expand Up @@ -135,22 +138,28 @@ public void run() {
.generateGetUrl(builder.getBaseUrl(), builder.getParams());
if (callback != null)
callback.onSuccess(url);
} catch (UfileParamException | UfileAuthorizationException | UfileSignatureException e) {
} catch (UfileClientException e) {
if (callback != null)
callback.onFailed(e);
}
}
}.start();
}

private String generateFinalHost(String bucketName, String keyName) {
private String generateFinalHost(String bucketName, String keyName) throws UfileClientException {
if (host == null || host.length() == 0)
return host;

if (host.startsWith("http"))
return String.format("%s/%s", host, keyName);

return String.format("http://%s.%s/%s", bucketName, host, keyName);
try {
bucketName = URLEncoder.encode(bucketName, "UTF-8");
keyName = URLEncoder.encode(keyName, "UTF-8");
return String.format("http://%s.%s/%s", bucketName, host, keyName);
} catch (UnsupportedEncodingException e) {
throw new UfileClientException("Occur error during URLEncode bucketName and keyName");
}
}

protected void parameterValidat() throws UfileParamException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package cn.ucloud.ufile.api.object;

import cn.ucloud.ufile.exception.UfileClientException;
import cn.ucloud.ufile.exception.UfileParamException;
import cn.ucloud.ufile.exception.UfileRequiredParamNotFoundException;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
* API-生成公共下载URL
*
Expand Down Expand Up @@ -42,19 +46,25 @@ protected GenerateObjectPublicUrlApi(String host, String keyName, String bucketN
* @return 下载URL
* @throws UfileRequiredParamNotFoundException 必要参数未找到时抛出
*/
public String createUrl() throws UfileParamException {
public String createUrl() throws UfileClientException {
parameterValidat();
return generateFinalHost(bucketName, keyName);
}

private String generateFinalHost(String bucketName, String keyName) {
private String generateFinalHost(String bucketName, String keyName) throws UfileClientException {
if (host == null || host.length() == 0)
return host;

if (host.startsWith("http"))
return String.format("%s/%s", host, keyName);

return String.format("http://%s.%s/%s", bucketName, host, keyName);
try {
bucketName = URLEncoder.encode(bucketName, "UTF-8");
keyName = URLEncoder.encode(keyName, "UTF-8");
return String.format("http://%s.%s/%s", bucketName, host, keyName);
} catch (UnsupportedEncodingException e) {
throw new UfileClientException("Occur error during URLEncode bucketName and keyName");
}
}

protected void parameterValidat() throws UfileParamException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import cn.ucloud.ufile.auth.UfileAuthorizationException;
import cn.ucloud.ufile.auth.sign.UfileSignatureException;
import cn.ucloud.ufile.bean.ObjectListBean;
import cn.ucloud.ufile.exception.UfileClientException;
import cn.ucloud.ufile.exception.UfileParamException;
import cn.ucloud.ufile.exception.UfileRequiredParamNotFoundException;
import cn.ucloud.ufile.http.HttpClient;
Expand Down Expand Up @@ -114,7 +115,7 @@ public ObjectListApi withAuthOptionalData(JsonElement authOptionalData) {
}

@Override
protected void prepareData() throws UfileParamException, UfileAuthorizationException, UfileSignatureException {
protected void prepareData() throws UfileClientException {
parameterValidat();
List<Parameter<String>> query = new ArrayList<>();
if (prefix != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public ObjectProfileApi withAuthOptionalData(JsonElement authOptionalData) {
}

@Override
protected void prepareData() throws UfileParamException, UfileAuthorizationException, UfileSignatureException {
protected void prepareData() throws UfileClientException {
parameterValidat();

String contentType = "application/json; charset=utf-8";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cn.ucloud.ufile.auth.sign.UfileSignatureException;
import cn.ucloud.ufile.bean.ObjectRestoreBean;
import cn.ucloud.ufile.bean.UfileErrorBean;
import cn.ucloud.ufile.exception.UfileClientException;
import cn.ucloud.ufile.exception.UfileParamException;
import cn.ucloud.ufile.exception.UfileRequiredParamNotFoundException;
import cn.ucloud.ufile.exception.UfileServerException;
Expand Down Expand Up @@ -82,7 +83,7 @@ public ObjectRestoreApi withAuthOptionalData(JsonElement authOptionalData) {
}

@Override
protected void prepareData() throws UfileParamException, UfileAuthorizationException, UfileSignatureException {
protected void prepareData() throws UfileClientException {
parameterValidat();

String keyName_tmp = keyName; //参数参与签名
Expand All @@ -92,7 +93,7 @@ protected void prepareData() throws UfileParamException, UfileAuthorizationExcep

call = new PutJsonRequestBuilder()
.setConnTimeOut(connTimeOut).setReadTimeOut(readTimeOut).setWriteTimeOut(writeTimeOut)
.baseUrl(generateFinalHost(bucketName, keyName_tmp + "?restore"))
.baseUrl(generateFinalHost(bucketName, keyName_tmp) + "?restore")
.addHeader("Accpet", "*/*")
.addHeader("Date", date)
.addHeader("authorization", authorization)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public PutFileApi setBufferSize(int bufferSize) {
}

@Override
protected void prepareData() throws UfileParamException, UfileFileException, UfileAuthorizationException, UfileSignatureException {
protected void prepareData() throws UfileClientException {
parameterValidat();
if (!file.exists())
throw new UfileFileException("File is inexistent!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public PutStreamApi withAuthOptionalData(JsonElement authOptionalData) {
}

@Override
protected void prepareData() throws UfileParamException, UfileIOException, UfileAuthorizationException, UfileSignatureException {
protected void prepareData() throws UfileClientException {
try {
parameterValidat();

Expand Down Expand Up @@ -205,6 +205,8 @@ protected void prepareData() throws UfileParamException, UfileIOException, Ufile
call = builder.build(httpClient.getOkHttpClient());
} catch (IOException e) {
throw new UfileIOException(e.getMessage());
} catch (UfileClientException e) {
throw e;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import cn.ucloud.ufile.bean.base.BaseResponseBean;
import cn.ucloud.ufile.compat.base64.Base64UrlEncoderCompat;
import cn.ucloud.ufile.compat.base64.DefaultBase64UrlEncoderCompat;
import cn.ucloud.ufile.exception.UfileClientException;
import cn.ucloud.ufile.exception.UfileIOException;
import cn.ucloud.ufile.exception.UfileParamException;
import cn.ucloud.ufile.exception.UfileRequiredParamNotFoundException;
Expand Down Expand Up @@ -120,7 +121,7 @@ public UploadFileHitApi withAuthOptionalData(JsonElement authOptionalData) {
}

@Override
protected void prepareData() throws UfileParamException, UfileAuthorizationException, UfileSignatureException, UfileIOException {
protected void prepareData() throws UfileClientException {
parameterValidat();

String date = dateFormat.format(new Date(System.currentTimeMillis()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import cn.ucloud.ufile.bean.base.BaseResponseBean;
import cn.ucloud.ufile.compat.base64.Base64UrlEncoderCompat;
import cn.ucloud.ufile.compat.base64.DefaultBase64UrlEncoderCompat;
import cn.ucloud.ufile.exception.UfileClientException;
import cn.ucloud.ufile.exception.UfileIOException;
import cn.ucloud.ufile.exception.UfileParamException;
import cn.ucloud.ufile.exception.UfileRequiredParamNotFoundException;
Expand Down Expand Up @@ -124,7 +125,7 @@ public UploadStreamHitApi withAuthOptionalData(JsonElement authOptionalData) {
}

@Override
protected void prepareData() throws UfileParamException, UfileIOException, UfileAuthorizationException, UfileSignatureException {
protected void prepareData() throws UfileClientException {
parameterValidat();

String date = dateFormat.format(new Date(System.currentTimeMillis()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cn.ucloud.ufile.auth.UfileAuthorizationException;
import cn.ucloud.ufile.auth.sign.UfileSignatureException;
import cn.ucloud.ufile.bean.base.BaseResponseBean;
import cn.ucloud.ufile.exception.UfileClientException;
import cn.ucloud.ufile.exception.UfileParamException;
import cn.ucloud.ufile.exception.UfileRequiredParamNotFoundException;
import cn.ucloud.ufile.http.HttpClient;
Expand Down Expand Up @@ -66,7 +67,7 @@ public AbortMultiUploadApi withAuthOptionalData(JsonElement authOptionalData) {
}

@Override
protected void prepareData() throws UfileParamException, UfileAuthorizationException, UfileSignatureException {
protected void prepareData() throws UfileClientException {
parameterValidat();

String contentType = "application/json; charset=utf-8";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cn.ucloud.ufile.auth.UfileAuthorizationException;
import cn.ucloud.ufile.auth.sign.UfileSignatureException;
import cn.ucloud.ufile.bean.MultiUploadResponse;
import cn.ucloud.ufile.exception.UfileClientException;
import cn.ucloud.ufile.exception.UfileParamException;
import cn.ucloud.ufile.exception.UfileRequiredParamNotFoundException;
import cn.ucloud.ufile.http.HttpClient;
Expand Down Expand Up @@ -100,7 +101,7 @@ public int compare(MultiUploadPartState o1, MultiUploadPartState o2) {
};

@Override
protected void prepareData() throws UfileParamException, UfileAuthorizationException, UfileSignatureException {
protected void prepareData() throws UfileClientException {
parameterValidat();

PostStringRequestBuilder builder = new PostStringRequestBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public InitMultiUploadApi withAuthOptionalData(JsonElement authOptionalData) {
}

@Override
protected void prepareData() throws UfileParamException, UfileAuthorizationException, UfileSignatureException {
protected void prepareData() throws UfileClientException {
parameterValidat();

String contentType = mediaType.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public MultiUploadPartApi withAuthOptionalData(JsonElement authOptionalData) {
}

@Override
protected void prepareData() throws UfileParamException, UfileAuthorizationException, UfileSignatureException {
protected void prepareData() throws UfileClientException {
parameterValidat();
List<Parameter<String>> query = new ArrayList<>();
query.add(new Parameter<>("uploadId", info.getUploadId()));
Expand Down
Loading

0 comments on commit 6b1031d

Please sign in to comment.