Skip to content

Commit

Permalink
Merge pull request #106 from simon-liubin/bug_fix/up_is_nullkey
Browse files Browse the repository at this point in the history
 修复:普通流上传,key为空对象导致错误
  • Loading branch information
longbai committed Apr 21, 2014
2 parents 2e6b402 + 8940680 commit 3b9f911
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 40 deletions.
21 changes: 14 additions & 7 deletions src/main/java/com/qiniu/api/io/IoApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ private static void setParam(MultipartEntity requestEntity, Map<String, String>
}
}

private static PutRet putStream(String uptoken, String key, InputStream reader,PutExtra extra) {
private static PutRet putStream(String uptoken, String key, InputStream reader,PutExtra extra, String fileName) {
MultipartEntity requestEntity = new MultipartEntity();
try {
requestEntity.addPart("token", new StringBody(uptoken));
AbstractContentBody inputBody = buildInputStreamBody(reader, extra, key);
AbstractContentBody inputBody = buildInputStreamBody(reader, extra, fileName != null ? fileName : "null");
requestEntity.addPart("file", inputBody);
setKey(requestEntity, key);
setParam(requestEntity, extra.params);
Expand All @@ -109,19 +109,26 @@ private static PutRet putStream(String uptoken, String key, InputStream reader,P
return new PutRet(ret);
}

private static InputStreamBody buildInputStreamBody(InputStream reader,PutExtra extra, String key){
private static InputStreamBody buildInputStreamBody(InputStream reader,PutExtra extra, String fileName){
if(extra.mimeType != null){
return new InputStreamBody(reader, extra.mimeType, key);
return new InputStreamBody(reader, extra.mimeType, fileName);
}else{
return new InputStreamBody(reader, key);
return new InputStreamBody(reader, fileName);
}
}

public static PutRet put(String uptoken,String key,InputStream reader,PutExtra extra){
return putStream(uptoken,key,reader,extra, null);
}

public static PutRet put(String uptoken,String key,InputStream reader,PutExtra extra, String fileName){
return putStream(uptoken,key,reader,extra, fileName);
}


public static PutRet Put(String uptoken,String key,InputStream reader,PutExtra extra)
{
PutRet ret = putStream(uptoken,key,reader,extra);
return ret;
return put(uptoken,key,reader,extra);
}


Expand Down
53 changes: 20 additions & 33 deletions src/test/java/com/qiniu/testing/IOTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.json.JSONObject;

Expand All @@ -13,37 +14,33 @@
import com.qiniu.api.io.IoApi;
import com.qiniu.api.io.PutExtra;
import com.qiniu.api.io.PutRet;
import com.qiniu.api.net.CallRet;
import com.qiniu.api.rs.Entry;
import com.qiniu.api.rs.PutPolicy;
import com.qiniu.api.rs.RSClient;

public class IOTest extends TestCase {

// because all the testcase concurrently executes
// so the key should be different.
public final String key = "IOTest-key";
private String key = "IOTest-key";

public final String key2 = "IOTest-Stream-key";
private String key2 = "IOTest-Stream-key";

public final String expectedHash = "FmDZwqadA4-ib_15hYfQpb7UXUYR";
public final String expectedHash2 = "Fp9UwOPl9G3HmZsVFkJrMtdwSMp8";
private final String expectedHash = "FmDZwqadA4-ib_15hYfQpb7UXUYR";
private final String expectedHash2 = "Fp9UwOPl9G3HmZsVFkJrMtdwSMp8";

public String bucketName;
private String bucketName;

public Mac mac;
private Mac mac;
@Override
public void setUp() {
Config.ACCESS_KEY = System.getenv("QINIU_ACCESS_KEY");
Config.SECRET_KEY = System.getenv("QINIU_SECRET_KEY");
Config.RS_HOST = System.getenv("QINIU_RS_HOST");
bucketName = System.getenv("QINIU_TEST_BUCKET");

assertNotNull(Config.ACCESS_KEY);
assertNotNull(Config.SECRET_KEY);
assertNotNull(Config.RS_HOST);
assertNotNull(bucketName);
mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY);

key = UUID.randomUUID().toString();
key2 = UUID.randomUUID().toString();
}

// just upload an image in testdata.
Expand Down Expand Up @@ -104,27 +101,17 @@ private String getJsonValue(JSONObject jsonObject, String name){

@Override
public void tearDown() {
// delete the metadata from rs
// confirms it exists.
{
RSClient rs = new RSClient(mac);
Entry sr = rs.stat(bucketName, key);
assertTrue(sr.ok());
assertTrue(expectedHash.equals(sr.getHash()));
}

// deletes it from rs
{
RSClient rs = new RSClient(mac);
CallRet cr = rs.delete(bucketName, key);
assertTrue(cr.ok());
RSClient rs = new RSClient(mac);
try{
rs.delete(bucketName, key2);
}catch(Exception e){

}

// confirms that it's deleted
{
RSClient rs = new RSClient(mac);
Entry sr = rs.stat(bucketName, key);
assertTrue(!sr.ok());

try{
rs.delete(bucketName, key);
}catch(Exception e){

}
}
}

0 comments on commit 3b9f911

Please sign in to comment.