Skip to content
This repository was archived by the owner on Dec 6, 2024. It is now read-only.

Commit 06c73a5

Browse files
SynapticloopSynapticloop
authored andcommitted
documentation and refactoring of the file download
1 parent 0794f0d commit 06c73a5

8 files changed

Lines changed: 74 additions & 47 deletions

File tree

src/main/java/synapticloop/b2/B2ApiClient.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
package synapticloop.b2;
22

33
import java.io.File;
4+
import java.io.IOException;
5+
import java.io.InputStream;
46
import java.util.List;
57

8+
import org.apache.commons.io.FileUtils;
9+
import org.apache.commons.io.IOUtils;
10+
611
import synapticloop.b2.exception.B2ApiException;
712
import synapticloop.b2.request.B2AuthorizeAccountRequest;
813
import synapticloop.b2.request.B2CreateBucketRequest;
914
import synapticloop.b2.request.B2DeleteBucketRequest;
1015
import synapticloop.b2.request.B2DeleteFileVersionRequest;
16+
import synapticloop.b2.request.B2DownloadFileByIdRequest;
17+
import synapticloop.b2.request.B2DownloadFileByNameRequest;
1118
import synapticloop.b2.request.B2GetFileInfoRequest;
1219
import synapticloop.b2.request.B2GetUploadUrlRequest;
1320
import synapticloop.b2.request.B2ListBucketsRequest;
@@ -18,6 +25,7 @@
1825
import synapticloop.b2.response.B2AuthorizeAccountResponse;
1926
import synapticloop.b2.response.B2BucketResponse;
2027
import synapticloop.b2.response.B2DeleteFileVersionResponse;
28+
import synapticloop.b2.response.B2DownloadFileResponse;
2129
import synapticloop.b2.response.B2FileResponse;
2230
import synapticloop.b2.response.B2GetUploadUrlResponse;
2331
import synapticloop.b2.response.B2ListFilesResponse;
@@ -119,6 +127,45 @@ public B2ListFilesResponse listFileVersions(String bucketId, String startFileNam
119127
return(new B2ListFileVersionsRequest(getB2AuthorizeAccountResponse(), bucketId, startFileName, startFileId, maxFileCount).getResponse());
120128
}
121129

130+
public void downloadFileByNameToFile(String bucketName, String fileName, File file) throws B2ApiException {
131+
try {
132+
FileUtils.copyInputStreamToFile(new B2DownloadFileByNameRequest(getB2AuthorizeAccountResponse(), bucketName, fileName).getResponse().getContent(), file);
133+
} catch (IOException ex) {
134+
throw new B2ApiException("Could not download to file", ex);
135+
}
136+
}
137+
138+
public byte[] downloadFileByNameToBytes(String bucketName, String fileName) throws B2ApiException {
139+
try {
140+
return(IOUtils.toByteArray(new B2DownloadFileByNameRequest(getB2AuthorizeAccountResponse(), bucketName, fileName).getResponse().getContent()));
141+
} catch (IOException ex) {
142+
throw new B2ApiException("Could not download to bytes", ex);
143+
}
144+
}
145+
146+
public InputStream downloadFileByNameToStream(String bucketName, String fileName) throws B2ApiException {
147+
return(new B2DownloadFileByNameRequest(getB2AuthorizeAccountResponse(), bucketName, fileName).getResponse().getContent());
148+
}
149+
150+
public B2DownloadFileResponse downloadFileByName(String bucketName, String fileName) throws B2ApiException {
151+
return(new B2DownloadFileByNameRequest(getB2AuthorizeAccountResponse(), bucketName, fileName).getResponse());
152+
}
153+
154+
public void downloadFileByIdToFile(String bucketId, String FileId, File file) {
155+
}
156+
157+
public byte[] downloadByFileIdToBytes(String bucketId, String fileId) {
158+
return(new byte[] {});
159+
}
160+
161+
public InputStream downloadFileByIdToStream(String fileId) throws B2ApiException {
162+
return(new B2DownloadFileByIdRequest(getB2AuthorizeAccountResponse(), fileId).getResponse().getContent());
163+
}
164+
165+
public B2DownloadFileResponse downloadFileById(String fileId) throws B2ApiException {
166+
return(new B2DownloadFileByIdRequest(getB2AuthorizeAccountResponse(), fileId).getResponse());
167+
}
168+
122169
private synchronized B2AuthorizeAccountResponse getB2AuthorizeAccountResponse() throws B2ApiException {
123170
if(null == b2AuthorizeAccountResponse) {
124171
b2AuthorizeAccountResponse = new B2AuthorizeAccountRequest(accountId, applicationKey).getResponse();

src/main/java/synapticloop/b2/request/B2AuthorizeAccountRequest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@
1717
*/
1818

1919
public class B2AuthorizeAccountRequest extends BaseB2Request {
20-
2120
private static final String B2_AUTHORIZE_ACCOUNT = BASE_API + "b2_authorize_account";
2221

2322
public B2AuthorizeAccountRequest(String accountId, String applicationKey) {
2423
super(null);
2524
url = B2_AUTHORIZE_ACCOUNT;
26-
headers.put("Authorization", "Basic " + Base64.getEncoder().encodeToString((accountId + ":" + applicationKey).getBytes()));
25+
headers.put(REQUEST_PROPERTY_AUTHORIZATION, "Basic " + Base64.getEncoder().encodeToString((accountId + ":" + applicationKey).getBytes()));
2726
}
2827

2928
public B2AuthorizeAccountResponse getResponse() throws B2ApiException {

src/main/java/synapticloop/b2/request/B2DownloadFileByIdRequest.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package synapticloop.b2.request;
22

3-
import java.io.File;
4-
53
import synapticloop.b2.exception.B2ApiException;
64
import synapticloop.b2.response.B2AuthorizeAccountResponse;
75
import synapticloop.b2.response.B2DownloadFileResponse;
@@ -21,20 +19,14 @@
2119

2220
public class B2DownloadFileByIdRequest extends BaseB2Request {
2321
private static final String B2_DOWNLOAD_FILE_BY_ID = BASE_API_VERSION + "b2_download_file_by_id";
24-
private File fileTo = null;
2522

2623
public B2DownloadFileByIdRequest(B2AuthorizeAccountResponse b2AuthorizeAccountResponse, String fileId) {
2724
super(b2AuthorizeAccountResponse);
2825
url = b2AuthorizeAccountResponse.getDownloadUrl() + B2_DOWNLOAD_FILE_BY_ID;
2926
parameters.put(KEY_FILE_ID, fileId);
3027
}
3128

32-
public B2DownloadFileByIdRequest(B2AuthorizeAccountResponse b2AuthorizeAccountResponse, String fileId, File fileTo) {
33-
this(b2AuthorizeAccountResponse, fileId);
34-
this.fileTo = fileTo;
35-
}
36-
3729
public B2DownloadFileResponse getResponse() throws B2ApiException {
38-
return(new B2DownloadFileResponse(executeGetWithData(), fileTo));
30+
return(new B2DownloadFileResponse(executeGetWithData()));
3931
}
4032
}

src/main/java/synapticloop/b2/request/B2DownloadFileByNameRequest.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package synapticloop.b2.request;
22

3-
import java.io.File;
4-
53
import synapticloop.b2.exception.B2ApiException;
64
import synapticloop.b2.response.B2AuthorizeAccountResponse;
75
import synapticloop.b2.response.B2DownloadFileResponse;
@@ -21,19 +19,13 @@
2119
*/
2220

2321
public class B2DownloadFileByNameRequest extends BaseB2Request {
24-
private File fileTo = null;
2522

2623
public B2DownloadFileByNameRequest(B2AuthorizeAccountResponse b2AuthorizeAccountResponse, String bucketName, String fileName) {
2724
super(b2AuthorizeAccountResponse);
2825
url = b2AuthorizeAccountResponse.getDownloadUrl() + "/file/" + bucketName + "/" + fileName;
2926
}
3027

31-
public B2DownloadFileByNameRequest(B2AuthorizeAccountResponse b2AuthorizeAccountResponse, String bucketName, String fileName, File fileTo) {
32-
this(b2AuthorizeAccountResponse, bucketName, fileName);
33-
this.fileTo = fileTo;
34-
}
35-
3628
public B2DownloadFileResponse getResponse() throws B2ApiException {
37-
return(new B2DownloadFileResponse(executeGetWithData(), fileTo));
29+
return(new B2DownloadFileResponse(executeGetWithData()));
3830
}
3931
}

src/main/java/synapticloop/b2/request/BaseB2Request.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class BaseB2Request {
3737

3838

3939
private static final String REQUEST_PROPERTY_CHARSET = "charset";
40-
private static final String REQUEST_PROPERTY_AUTHORIZATION = "Authorization";
40+
protected static final String REQUEST_PROPERTY_AUTHORIZATION = "Authorization";
4141
private static final String REQUEST_PROPERTY_CONTENT_TYPE = "Content-Type";
4242

4343
protected static final String KEY_ACCOUNT_ID = "accountId";
@@ -129,7 +129,6 @@ protected String executeGet() throws B2ApiException {
129129
protected CloseableHttpResponse executeGetWithData() throws B2ApiException {
130130
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
131131

132-
133132
try {
134133
URI uri = new URIBuilder(url).build();
135134
LOG.debug("GET request to URL '{}'", url);

src/main/java/synapticloop/b2/response/B2DownloadFileResponse.java

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package synapticloop.b2.response;
22

3-
import java.io.File;
43
import java.io.IOException;
54
import java.io.InputStream;
65
import java.util.HashMap;
76
import java.util.Map;
87

9-
import org.apache.commons.io.FileUtils;
10-
import org.apache.commons.io.IOUtils;
118
import org.apache.http.Header;
129
import org.apache.http.client.methods.CloseableHttpResponse;
1310

@@ -30,8 +27,7 @@ public class B2DownloadFileResponse extends BaseB2Response {
3027
headerLookup.put("X-Bz-Content-Sha1", HEADER_X_BZ_CONTENT_SHA1);
3128
}
3229

33-
private byte[] content = null;
34-
private File fileTo = null;
30+
private InputStream content = null;
3531
private Integer contentLength = null;
3632
private String contentType = null;
3733
private String fileId = null;
@@ -40,24 +36,14 @@ public class B2DownloadFileResponse extends BaseB2Response {
4036

4137
private Map<String, String> fileInfo = new HashMap<String, String>();
4238

43-
public B2DownloadFileResponse(CloseableHttpResponse closeableHttpResponse, File fileTo) throws B2ApiException {
44-
this.fileTo = fileTo;
45-
46-
InputStream inputStream = null;
39+
public B2DownloadFileResponse(CloseableHttpResponse closeableHttpResponse) throws B2ApiException {
4740
try {
48-
inputStream = closeableHttpResponse.getEntity().getContent();
41+
content = closeableHttpResponse.getEntity().getContent();
4942
parseHeaders(closeableHttpResponse);
5043

51-
if(null != fileTo) {
52-
// write the contents to the file
53-
FileUtils.copyInputStreamToFile(inputStream, fileTo);
54-
} else {
55-
content = IOUtils.toByteArray(inputStream);
56-
}
5744
} catch (IllegalStateException | IOException ex) {
5845
throw new B2ApiException("Could not retrieve response", ex);
5946
} finally {
60-
IOUtils.closeQuietly(inputStream);
6147
}
6248
}
6349

@@ -96,18 +82,29 @@ private void parseHeaders(CloseableHttpResponse closeableHttpResponse) throws B2
9682
}
9783
}
9884

99-
public byte[] getContent() {
85+
/**
86+
* Get the content of the downloaded file
87+
*
88+
* @return the downloaded file
89+
*/
90+
public InputStream getContent() {
10091
return this.content;
10192
}
10293

103-
public File getFileTo() {
104-
return this.fileTo;
105-
}
106-
94+
/**
95+
* Get the content length of the downloaded file
96+
*
97+
* @return the length of the content
98+
*/
10799
public Integer getContentLength() {
108100
return this.contentLength;
109101
}
110102

103+
/**
104+
* Get the content type of the downloaded file
105+
*
106+
* @return the content type of the downloaded file
107+
*/
111108
public String getContentType() {
112109
return this.contentType;
113110
}

src/main/java/synapticloop/b2/response/B2HideFileResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public B2HideFileResponse(String string) throws B2ApiException {
1818
this.fileId = jsonObject.optString(KEY_FILE_ID);
1919
this.fileName = jsonObject.optString(KEY_FILE_NAME);
2020
String actionTemp = jsonObject.optString(KEY_ACTION);
21-
if(null != actionTemp && actionTemp.compareTo("hide") == 0) {
21+
if(null != actionTemp && actionTemp.compareTo(ActionType.HIDE.toString()) == 0) {
2222
this.action = ActionType.HIDE;
2323
} else {
2424
this.action = ActionType.UPLOAD;

src/test/java/synapticloop/b2/request/B2DownloadFileRequestTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import java.io.IOException;
55

6+
import org.apache.commons.io.IOUtils;
67
import org.junit.Test;
78

89
import synapticloop.b2.exception.B2ApiException;
@@ -21,10 +22,10 @@ public void testDownloadFileBy() throws B2ApiException, IOException {
2122
b2FileResponse = B2TestHelper.uploadTemporaryFileToBucket(randomPrivateBucket.getBucketId());
2223

2324
B2DownloadFileResponse b2DownloadFileResponse = new B2DownloadFileByNameRequest(B2TestHelper.getB2AuthorizeAccountResponse(), randomPrivateBucket.getBucketName(), b2FileResponse.getFileName()).getResponse();
24-
assertEquals(B2TestHelper.DUMMY_FILE_CONTENT, new String(b2DownloadFileResponse.getContent()));
25+
assertEquals(B2TestHelper.DUMMY_FILE_CONTENT, IOUtils.toString(b2DownloadFileResponse.getContent()));
2526

2627
b2DownloadFileResponse = new B2DownloadFileByIdRequest(B2TestHelper.getB2AuthorizeAccountResponse(), b2FileResponse.getFileId()).getResponse();
27-
assertEquals(B2TestHelper.DUMMY_FILE_CONTENT, new String(b2DownloadFileResponse.getContent()));
28+
assertEquals(B2TestHelper.DUMMY_FILE_CONTENT, IOUtils.toString(b2DownloadFileResponse.getContent()));
2829
}
2930

3031
}

0 commit comments

Comments
 (0)