Skip to content

add base64Decode and fix PutPolicy Expire #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 6, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/main/java/com/qiniu/api/net/EncodeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,19 @@ public static byte[] urlsafeEncodeBytes(byte[] src) {
}
return b2;
}


public static byte[] urlsafeBase64Decode(String encoded){
byte[] rawbs = encoded.getBytes();
for(int i=0;i<rawbs.length;i++){
if(rawbs[i] == '_'){
rawbs[i] = '/';
}else if(rawbs[i] == '-'){
rawbs[i] = '+';
}
}
return Base64.decodeBase64(rawbs);
}

public static String urlsafeEncodeString(byte[] src) {
return new String(urlsafeEncodeBytes(src));
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/qiniu/api/rs/PutPolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class PutPolicy {
public String endUser;
/** 可选 */
public long expires;

public long deadline;

public PutPolicy(String scope) {
this.scope = scope;
Expand Down Expand Up @@ -59,7 +61,7 @@ private String marshal() throws JSONException {
if (this.endUser != null && this.endUser.length() > 0) {
stringer.key("endUser").value(this.endUser);
}
stringer.key("deadline").value(this.expires);
stringer.key("deadline").value(this.deadline);
stringer.endObject();

return stringer.toString();
Expand All @@ -78,7 +80,7 @@ public String token(Mac mac) throws AuthException, JSONException {
if (this.expires == 0) {
this.expires = 3600; // 3600s, default.
}
this.expires = System.currentTimeMillis() / 1000 + expires;
this.deadline = System.currentTimeMillis() / 1000 + this.expires;
byte[] data = this.marshal().getBytes();
return DigestAuth.signWithData(mac, data);
}
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/qiniu/testing/UtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.qiniu.testing;


import junit.framework.TestCase;

import com.qiniu.api.net.EncodeUtils;

public class UtilTest extends TestCase {

// just upload an image in testdata.
public void test() throws Exception {
String expectString = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+}{:?><-=,./;'[]";
String encodedString = "MTIzNDU2Nzg5MGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVp-IUAjJCVeJiooKV8rfXs6Pz48LT0sLi87J1td";
byte[] rawBytes = EncodeUtils.urlsafeBase64Decode(encodedString);
String decoded = new String(rawBytes);
assertTrue(expectString.equals(decoded));
}

@Override
public void tearDown() {
// do nothing here.
}
}