Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -455,11 +455,26 @@ public B2FileVersion startLargeFile(B2AccountAuthorization accountAuth,
@Override
public B2FileVersion finishLargeFile(B2AccountAuthorization accountAuth,
B2FinishLargeFileRequest request) throws B2Exception {
return webApiClient.postJsonReturnJson(
makeUrl(accountAuth, "b2_finish_large_file"),
makeHeaders(accountAuth),
request,
B2FileVersion.class);
try {
return webApiClient.postJsonReturnJson(
makeUrl(accountAuth, "b2_finish_large_file"),
makeHeaders(accountAuth),
request,
B2FileVersion.class);
} catch (B2BadRequestException original) {
// The service waits for an in-progress finish before returning 400 (#41).
// File info can therefore confirm success after a lost finish response.
try {
final B2FileVersion version = getFileInfo(accountAuth,
B2GetFileInfoRequest.builder(request.getFileId()).build());
if (version.isUpload()) {
return version;
}
} catch (B2Exception ignored) {
// Preserve the finish error if the file's state cannot be checked.
}
throw original;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,56 @@ public void testFinishLargeFile() throws B2Exception {
checkRequestCategory(OTHER, w -> w.finishLargeFile(ACCOUNT_AUTH, request));
}

@Test
public void testFinishLargeFileAlreadyFinished() throws B2Exception {
checkFinishLargeFileAfterBadRequest(true, false);
}

@Test
public void testFinishLargeFileStillUnfinished() throws B2Exception {
checkFinishLargeFileAfterBadRequest(false, false);
}

@Test
public void testFinishLargeFileInfoFails() throws B2Exception {
checkFinishLargeFileAfterBadRequest(false, true);
}

private void checkFinishLargeFileAfterBadRequest(boolean uploaded, boolean infoFails) throws B2Exception {
final B2BadRequestException original = new B2BadRequestException("bad_request", null, "finish failed");
final B2FileVersion version = mock(B2FileVersion.class);
org.mockito.Mockito.when(version.isUpload()).thenReturn(uploaded);
final B2WebApiClient client = new AlwaysThrowsUnauthorizedWebClientApi() {
@Override
public <ResponseType> ResponseType postJsonReturnJson(String url,
B2Headers headersOrNull,
Object request,
Class<ResponseType> responseClass) throws B2Exception {
if (url.endsWith("/b2_finish_large_file")) {
throw original;
}
assertEquals("apiUrl1/b2api/v2/b2_get_file_info", url);
assertEquals(B2GetFileInfoRequest.builder(fileId(1)).build(), request);
if (infoFails) {
throw new B2UnauthorizedException("unauthorized", null, "info failed");
}
return responseClass.cast(version);
}
};
final B2StorageClientWebifierImpl localWebifier = new B2StorageClientWebifierImpl(
client, USER_AGENT, MASTER_URL, null);
final B2FinishLargeFileRequest request = B2FinishLargeFileRequest
.builder(fileId(1), B2Collections.listOf("sha1_1")).build();
try {
final B2FileVersion result = localWebifier.finishLargeFile(ACCOUNT_AUTH, request);
assertTrue(uploaded && !infoFails);
org.junit.Assert.assertSame(version, result);
} catch (B2BadRequestException e) {
assertFalse(uploaded && !infoFails);
org.junit.Assert.assertSame(original, e);
}
}

@Test
public void testCancelLargeFile() throws B2Exception {
final B2CancelLargeFileRequest request = B2CancelLargeFileRequest
Expand Down