Skip to content

Commit

Permalink
Merge pull request #1876 in ITERATE/cyberduck from bugfix/TRAC-10859 …
Browse files Browse the repository at this point in the history
…to master

* commit '7afc5915392fb64d28ab7344e5f7abaf9a2cf8f3':
  Retry upload of single segment.
  Review.
  • Loading branch information
automerge committed Nov 25, 2019
2 parents fe26d11 + 7afc591 commit 06c0d62
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
Expand Up @@ -71,10 +71,10 @@ public BackgroundException map(final Throwable failure, final StringBuilder buff
return new InteroperabilityException(buffer.toString(), failure);
case HttpStatus.SC_REQUEST_TIMEOUT:
case HttpStatus.SC_GATEWAY_TIMEOUT:
case HttpStatus.SC_BAD_GATEWAY:
return new ConnectionTimeoutException(buffer.toString(), failure);
case HttpStatus.SC_LOCKED:
return new LockedException(buffer.toString(), failure);
case HttpStatus.SC_BAD_GATEWAY:
case HttpStatus.SC_INTERNAL_SERVER_ERROR:
case HttpStatus.SC_SERVICE_UNAVAILABLE:
case 429:
Expand Down
Expand Up @@ -34,6 +34,8 @@
import ch.cyberduck.core.preferences.PreferencesFactory;
import ch.cyberduck.core.shared.DefaultAttributesFinderFeature;
import ch.cyberduck.core.shared.DefaultFindFeature;
import ch.cyberduck.core.threading.BackgroundExceptionCallable;
import ch.cyberduck.core.threading.DefaultRetryCallable;
import ch.cyberduck.core.transfer.TransferStatus;

import org.apache.log4j.Logger;
Expand Down Expand Up @@ -114,15 +116,15 @@ public boolean random() {
private final class ChunkedOutputStream extends OutputStream {
private final OneDriveUploadSession upload;
private final Path file;
private final TransferStatus status;
private final TransferStatus overall;
private final AtomicBoolean close = new AtomicBoolean();

private Long offset = 0L;

public ChunkedOutputStream(final OneDriveUploadSession upload, final Path file, final TransferStatus status) {
this.upload = upload;
this.file = file;
this.status = status;
this.overall = status;
}

@Override
Expand All @@ -135,17 +137,36 @@ public void write(final byte[] b, final int off, final int len) throws IOExcepti
final byte[] content = Arrays.copyOfRange(b, off, len);
final HttpRange range = HttpRange.byLength(offset, content.length);
final String header;
if(status.getLength() == -1L) {
if(overall.getLength() == -1L) {
header = String.format("%d-%d/*", range.getStart(), range.getEnd());
}
else {
header = String.format("%d-%d/%d", range.getStart(), range.getEnd(), status.getOffset() + status.getLength());
header = String.format("%d-%d/%d", range.getStart(), range.getEnd(), overall.getOffset() + overall.getLength());
}
if(upload.uploadFragment(header, content) instanceof OneDriveFile.Metadata) {
log.info(String.format("Completed upload for %s", file));
try {
new DefaultRetryCallable<Void>(session.getHost(), new BackgroundExceptionCallable<Void>() {
@Override
public Void call() throws BackgroundException {
try {
if(upload.uploadFragment(header, content) instanceof OneDriveFile.Metadata) {
log.info(String.format("Completed upload for %s", file));
}
else {
log.debug(String.format("Uploaded fragment %s for file %s", header, file));
}
}
catch(OneDriveAPIException e) {
throw new GraphExceptionMappingService().map("Upload {0} failed", e, file);
}
catch(IOException e) {
throw new DefaultIOExceptionMappingService().map("Upload {0} failed", e, file);
}
return null;
}
}, overall).call();
}
else {
log.debug(String.format("Uploaded fragment %s for file %s", header, file));
catch(BackgroundException e) {
throw new IOException(e.getMessage(), e);
}
offset += content.length;
}
Expand Down
Expand Up @@ -16,6 +16,7 @@
*/

import ch.cyberduck.core.AlphanumericRandomStringService;
import ch.cyberduck.core.DefaultIOExceptionMappingService;
import ch.cyberduck.core.DisabledConnectionCallback;
import ch.cyberduck.core.DisabledLoginCallback;
import ch.cyberduck.core.Path;
Expand All @@ -35,9 +36,9 @@
import org.apache.commons.lang3.RandomUtils;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.nuxeo.onedrive.client.OneDriveAPIException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.EnumSet;
Expand Down Expand Up @@ -158,8 +159,8 @@ public void testWriteUnknownLength() throws Exception {
try {
assertEquals(content.length, IOUtils.copyLarge(in, out, buffer));
}
catch(OneDriveAPIException e) {
final BackgroundException failure = new GraphExceptionMappingService().map(e);
catch(IOException e) {
final BackgroundException failure = new DefaultIOExceptionMappingService().map(e);
assertTrue(failure.getDetail().contains("Invalid Content-Range header value.")
|| failure.getDetail().contains("Bad Request. The Content-Range header is missing or malformed."));
throw failure;
Expand Down

0 comments on commit 06c0d62

Please sign in to comment.