Search in sources :

Example 1 with B2GetUploadPartUrlResponse

use of synapticloop.b2.response.B2GetUploadPartUrlResponse in project cyberduck by iterate-ch.

the class B2WriteFeature method write.

@Override
public HttpResponseOutputStream<BaseB2Response> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
    // Submit store call to background thread
    final DelayedHttpEntityCallable<BaseB2Response> command = new DelayedHttpEntityCallable<BaseB2Response>() {

        /**
         * @return The SHA-1 returned by the server for the uploaded object
         */
        @Override
        public BaseB2Response call(final AbstractHttpEntity entity) throws BackgroundException {
            try {
                final Checksum checksum = status.getChecksum();
                if (status.isSegment()) {
                    final B2GetUploadPartUrlResponse uploadUrl = session.getClient().getUploadPartUrl(status.getParameters().get("fileId"));
                    return session.getClient().uploadLargeFilePart(uploadUrl, status.getPart(), entity, checksum.hash);
                } else {
                    if (null == urls.get()) {
                        final B2GetUploadUrlResponse uploadUrl = session.getClient().getUploadUrl(fileid.getVersionId(containerService.getContainer(file), new DisabledListProgressListener()));
                        if (log.isDebugEnabled()) {
                            log.debug(String.format("Obtained upload URL %s for file %s", uploadUrl, file));
                        }
                        urls.set(uploadUrl);
                        return this.upload(uploadUrl, entity, checksum);
                    } else {
                        final B2GetUploadUrlResponse uploadUrl = urls.get();
                        if (log.isDebugEnabled()) {
                            log.debug(String.format("Use cached upload URL %s for file %s", uploadUrl, file));
                        }
                        try {
                            return this.upload(uploadUrl, entity, checksum);
                        } catch (IOException | B2ApiException e) {
                            // Upload many files to the same upload_url until that URL gives an error
                            log.warn(String.format("Remove cached upload URL after failure %s", e));
                            urls.remove();
                            // Retry
                            return this.upload(uploadUrl, entity, checksum);
                        }
                    }
                }
            } catch (B2ApiException e) {
                throw new B2ExceptionMappingService(fileid).map("Upload {0} failed", e, file);
            } catch (IOException e) {
                throw new DefaultIOExceptionMappingService().map("Upload {0} failed", e, file);
            }
        }

        private BaseB2Response upload(final B2GetUploadUrlResponse uploadUrl, final AbstractHttpEntity entity, final Checksum checksum) throws B2ApiException, IOException {
            final Map<String, String> fileinfo = new HashMap<>(status.getMetadata());
            if (null != status.getTimestamp()) {
                fileinfo.put(X_BZ_INFO_SRC_LAST_MODIFIED_MILLIS, String.valueOf(status.getTimestamp()));
            }
            final B2FileResponse response = session.getClient().uploadFile(uploadUrl, containerService.getKey(file), entity, checksum.algorithm == HashAlgorithm.sha1 ? checksum.hash : "do_not_verify", status.getMime(), fileinfo);
            fileid.cache(file, response.getFileId());
            return response;
        }

        @Override
        public long getContentLength() {
            return status.getLength();
        }
    };
    return this.write(file, status, command);
}
Also used : DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) HashMap(java.util.HashMap) B2GetUploadUrlResponse(synapticloop.b2.response.B2GetUploadUrlResponse) B2ApiException(synapticloop.b2.exception.B2ApiException) IOException(java.io.IOException) DelayedHttpEntityCallable(ch.cyberduck.core.http.DelayedHttpEntityCallable) B2GetUploadPartUrlResponse(synapticloop.b2.response.B2GetUploadPartUrlResponse) Checksum(ch.cyberduck.core.io.Checksum) BaseB2Response(synapticloop.b2.response.BaseB2Response) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) B2FileResponse(synapticloop.b2.response.B2FileResponse)

Aggregations

DefaultIOExceptionMappingService (ch.cyberduck.core.DefaultIOExceptionMappingService)1 DisabledListProgressListener (ch.cyberduck.core.DisabledListProgressListener)1 DelayedHttpEntityCallable (ch.cyberduck.core.http.DelayedHttpEntityCallable)1 Checksum (ch.cyberduck.core.io.Checksum)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 AbstractHttpEntity (org.apache.http.entity.AbstractHttpEntity)1 B2ApiException (synapticloop.b2.exception.B2ApiException)1 B2FileResponse (synapticloop.b2.response.B2FileResponse)1 B2GetUploadPartUrlResponse (synapticloop.b2.response.B2GetUploadPartUrlResponse)1 B2GetUploadUrlResponse (synapticloop.b2.response.B2GetUploadUrlResponse)1 BaseB2Response (synapticloop.b2.response.BaseB2Response)1