Skip to content

Commit

Permalink
fix: fix download for empty files
Browse files Browse the repository at this point in the history
  • Loading branch information
mwwoda committed Jan 31, 2024
1 parent 9a94a1a commit ff36f13
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
31 changes: 31 additions & 0 deletions src/intTest/java/com/box/sdk/BoxFileIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,37 @@ public void setsAndRetrievesDispositionAt() throws ParseException {
}
}

@Test
public void uploadAndDownloadEmptyFileSucceeds() throws IOException {
BoxAPIConnection api = jwtApiForServiceAccount();
BoxAPIConnection api = new BoxAPIConnection(accessToken);
BoxFolder folder = getUniqueFolder(api);
String fileName = "empty_file";
URL fileURL = this.getClass().getResource("/sample-files/" + fileName);
String filePath = URLDecoder.decode(fileURL.getFile(), "utf-8");
long fileSize = new File(filePath).length();
byte[] fileContent = readAllBytes(filePath);
BoxFile uploadedFile = null;
try {
InputStream uploadStream = Files.newInputStream(Paths.get(filePath));
ProgressListener mockUploadListener = mock(ProgressListener.class);
BoxFile.Info uploadedFileInfo = folder.uploadFile(
uploadStream, BoxFileIT.generateString(), fileSize, mockUploadListener
);
uploadedFile = uploadedFileInfo.getResource();

ByteArrayOutputStream downloadStream = new ByteArrayOutputStream();
uploadedFile.download(downloadStream);
byte[] downloadedFileContent = downloadStream.toByteArray();

assertThat(downloadedFileContent, is(equalTo(fileContent)));
assertThat(folder, hasItem(Matchers.<BoxItem.Info>hasProperty("ID", equalTo(uploadedFile.getID()))));
} finally {
deleteFile(uploadedFile);
}

}

private byte[] readFileContent(String fileName) throws IOException {
URL fileURL = this.getClass().getResource("/sample-files/" + fileName);
String filePath = URLDecoder.decode(fileURL.getFile(), "utf-8");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/box/sdk/BoxAPIResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ static BoxAPIResponse toBoxResponse(Response response) {
);
}
ResponseBody responseBody = response.body();
if (responseBody.contentLength() == 0 || responseBody.contentType() == null) {
if (responseBody.contentType() == null) {
try {
return new BoxAPIResponse(response.code(),
response.request().method(),
Expand Down
Empty file.

0 comments on commit ff36f13

Please sign in to comment.