Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit eb4f8a72 authored by David A. Velasco's avatar David A. Velasco
Browse files

Added constructors to grant that upload operations only succeed if the remote...

Added constructors to grant that upload operations only succeed if the remote copy keeps a given ETag
parent f61b4661
Loading
Loading
Loading
Loading
+20 −5
Original line number Diff line number Diff line
@@ -46,12 +46,19 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation
    
    public static final long CHUNK_SIZE = 1024000;
    private static final String OC_CHUNKED_HEADER = "OC-Chunked";
    private static final String OC_CHUNK_SIZE_HEADER = "OC-Chunk-Size";
    private static final String TAG = ChunkedUploadRemoteFileOperation.class.getSimpleName();

    public ChunkedUploadRemoteFileOperation(String storagePath, String remotePath, String mimeType){
        super(storagePath, remotePath, mimeType);
    }

    public ChunkedUploadRemoteFileOperation(
            String storagePath, String remotePath, String mimeType, String requiredEtag
    ){
		 super(storagePath, remotePath, mimeType, requiredEtag);
	}
    
    @Override
    protected int uploadFile(OwnCloudClient client) throws IOException {
        int status = -1;
@@ -63,8 +70,6 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation
            raf = new RandomAccessFile(file, "r");
            channel = raf.getChannel();
            mEntity = new ChunkFromFileChannelRequestEntity(channel, mMimeType, CHUNK_SIZE, file);
            //((ProgressiveDataTransferer)mEntity).
            // addDatatransferProgressListeners(getDataTransferListeners());
            synchronized (mDataTransferListeners) {
				((ProgressiveDataTransferer)mEntity)
                        .addDatatransferProgressListeners(mDataTransferListeners);
@@ -73,15 +78,25 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation
            long offset = 0;
            String uriPrefix = client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath) +
                    "-chunking-" + Math.abs((new Random()).nextInt(9000)+1000) + "-" ;
            long chunkCount = (long) Math.ceil((double)file.length() / CHUNK_SIZE);
            long totalLength = file.length();
            long chunkCount = (long) Math.ceil((double)totalLength / CHUNK_SIZE);
            String chunkSizeStr = String.valueOf(CHUNK_SIZE);
            String totalLengthStr = String.valueOf(file.length());
            for (int chunkIndex = 0; chunkIndex < chunkCount ; chunkIndex++, offset += CHUNK_SIZE) {
                if (chunkIndex == chunkCount - 1) {
                    chunkSizeStr = String.valueOf(CHUNK_SIZE * chunkCount - totalLength);
                }
                if (mPutMethod != null) {
                    mPutMethod.releaseConnection();     // let the connection available
                                                        // for other methods
                }
                mPutMethod = new PutMethod(uriPrefix + chunkCount + "-" + chunkIndex);
                if (mRequiredEtag != null && mRequiredEtag.length() > 0) {
                    mPutMethod.addRequestHeader(IF_MATCH_HEADER, "\"" + mRequiredEtag + "\"");
                }
                mPutMethod.addRequestHeader(OC_CHUNKED_HEADER, OC_CHUNKED_HEADER);
                mPutMethod.addRequestHeader(OC_TOTAL_LENGTH_HEADER, String.valueOf(file.length()));
                mPutMethod.addRequestHeader(OC_CHUNK_SIZE_HEADER, chunkSizeStr);
                mPutMethod.addRequestHeader(OC_TOTAL_LENGTH_HEADER, totalLengthStr);
                ((ChunkFromFileChannelRequestEntity) mEntity).setOffset(offset);
                mPutMethod.setRequestEntity(mEntity);
                if (mCancellationRequested.get()) {
+11 −2
Original line number Diff line number Diff line
@@ -32,7 +32,6 @@ import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.http.HttpStatus;
@@ -60,12 +59,14 @@ public class UploadRemoteFileOperation extends RemoteOperation {
	private static final String TAG = UploadRemoteFileOperation.class.getSimpleName();

	protected static final String OC_TOTAL_LENGTH_HEADER = "OC-Total-Length";
	protected static final String IF_MATCH_HEADER = "If-Match";

	protected String mLocalPath;
	protected String mRemotePath;
	protected String mMimeType;
	protected PutMethod mPutMethod = null;
	protected boolean mForbiddenCharsInServer = false;
	protected String mRequiredEtag = null;
	
	protected final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
	protected Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
@@ -78,6 +79,11 @@ public class UploadRemoteFileOperation extends RemoteOperation {
		mMimeType = mimeType;
	}

	public UploadRemoteFileOperation(String localPath, String remotePath, String mimeType, String requiredEtag) {
		this(localPath, remotePath, mimeType);
		mRequiredEtag = requiredEtag;
	}

	@Override
	protected RemoteOperationResult run(OwnCloudClient client) {
		RemoteOperationResult result = null;
@@ -126,6 +132,9 @@ public class UploadRemoteFileOperation extends RemoteOperation {
				((ProgressiveDataTransferer)mEntity)
                        .addDatatransferProgressListeners(mDataTransferListeners);
			}
			if (mRequiredEtag != null && mRequiredEtag.length() > 0) {
				mPutMethod.addRequestHeader(IF_MATCH_HEADER, "\"" + mRequiredEtag + "\"");
			}
			mPutMethod.addRequestHeader(OC_TOTAL_LENGTH_HEADER, String.valueOf(f.length()));
			mPutMethod.setRequestEntity(mEntity);
			status = client.executeMethod(mPutMethod);