Skip to content

Commit 54bc7c4

Browse files
committed
Merge pull request #84 from icattlecoder/feature/base64decode
add base64Decode and fix PutPolicy Expire
2 parents 6b43af2 + 90a76b0 commit 54bc7c4

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

src/main/java/com/qiniu/api/net/EncodeUtils.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,19 @@ public static byte[] urlsafeEncodeBytes(byte[] src) {
3636
}
3737
return b2;
3838
}
39-
39+
40+
public static byte[] urlsafeBase64Decode(String encoded){
41+
byte[] rawbs = encoded.getBytes();
42+
for(int i=0;i<rawbs.length;i++){
43+
if(rawbs[i] == '_'){
44+
rawbs[i] = '/';
45+
}else if(rawbs[i] == '-'){
46+
rawbs[i] = '+';
47+
}
48+
}
49+
return Base64.decodeBase64(rawbs);
50+
}
51+
4052
public static String urlsafeEncodeString(byte[] src) {
4153
return new String(urlsafeEncodeBytes(src));
4254
}

src/main/java/com/qiniu/api/rs/PutPolicy.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class PutPolicy {
3232
public String endUser;
3333
/** 可选 */
3434
public long expires;
35+
36+
public long deadline;
3537

3638
public PutPolicy(String scope) {
3739
this.scope = scope;
@@ -59,7 +61,7 @@ private String marshal() throws JSONException {
5961
if (this.endUser != null && this.endUser.length() > 0) {
6062
stringer.key("endUser").value(this.endUser);
6163
}
62-
stringer.key("deadline").value(this.expires);
64+
stringer.key("deadline").value(this.deadline);
6365
stringer.endObject();
6466

6567
return stringer.toString();
@@ -78,7 +80,7 @@ public String token(Mac mac) throws AuthException, JSONException {
7880
if (this.expires == 0) {
7981
this.expires = 3600; // 3600s, default.
8082
}
81-
this.expires = System.currentTimeMillis() / 1000 + expires;
83+
this.deadline = System.currentTimeMillis() / 1000 + this.expires;
8284
byte[] data = this.marshal().getBytes();
8385
return DigestAuth.signWithData(mac, data);
8486
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.qiniu.testing;
2+
3+
4+
import junit.framework.TestCase;
5+
6+
import com.qiniu.api.net.EncodeUtils;
7+
8+
public class UtilTest extends TestCase {
9+
10+
// just upload an image in testdata.
11+
public void test() throws Exception {
12+
String expectString = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+}{:?><-=,./;'[]";
13+
String encodedString = "MTIzNDU2Nzg5MGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVp-IUAjJCVeJiooKV8rfXs6Pz48LT0sLi87J1td";
14+
byte[] rawBytes = EncodeUtils.urlsafeBase64Decode(encodedString);
15+
String decoded = new String(rawBytes);
16+
assertTrue(expectString.equals(decoded));
17+
}
18+
19+
@Override
20+
public void tearDown() {
21+
// do nothing here.
22+
}
23+
}

0 commit comments

Comments
 (0)