1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-08 00:16:09 +00:00

Added constructors to grant that upload operations only succeed if the remote copy keeps a given ETag

This commit is contained in:
David A. Velasco 2015-09-28 11:13:44 +02:00
parent f61b466124
commit eb4f8a7234
2 changed files with 31 additions and 7 deletions

View File

@ -46,10 +46,17 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation
public static final long CHUNK_SIZE = 1024000; public static final long CHUNK_SIZE = 1024000;
private static final String OC_CHUNKED_HEADER = "OC-Chunked"; 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(); private static final String TAG = ChunkedUploadRemoteFileOperation.class.getSimpleName();
public ChunkedUploadRemoteFileOperation(String storagePath, String remotePath, String mimeType){ public ChunkedUploadRemoteFileOperation(String storagePath, String remotePath, String mimeType){
super(storagePath, remotePath, mimeType); super(storagePath, remotePath, mimeType);
}
public ChunkedUploadRemoteFileOperation(
String storagePath, String remotePath, String mimeType, String requiredEtag
){
super(storagePath, remotePath, mimeType, requiredEtag);
} }
@Override @Override
@ -63,8 +70,6 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation
raf = new RandomAccessFile(file, "r"); raf = new RandomAccessFile(file, "r");
channel = raf.getChannel(); channel = raf.getChannel();
mEntity = new ChunkFromFileChannelRequestEntity(channel, mMimeType, CHUNK_SIZE, file); mEntity = new ChunkFromFileChannelRequestEntity(channel, mMimeType, CHUNK_SIZE, file);
//((ProgressiveDataTransferer)mEntity).
// addDatatransferProgressListeners(getDataTransferListeners());
synchronized (mDataTransferListeners) { synchronized (mDataTransferListeners) {
((ProgressiveDataTransferer)mEntity) ((ProgressiveDataTransferer)mEntity)
.addDatatransferProgressListeners(mDataTransferListeners); .addDatatransferProgressListeners(mDataTransferListeners);
@ -73,15 +78,25 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation
long offset = 0; long offset = 0;
String uriPrefix = client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath) + String uriPrefix = client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath) +
"-chunking-" + Math.abs((new Random()).nextInt(9000)+1000) + "-" ; "-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) { for (int chunkIndex = 0; chunkIndex < chunkCount ; chunkIndex++, offset += CHUNK_SIZE) {
if (chunkIndex == chunkCount - 1) {
chunkSizeStr = String.valueOf(CHUNK_SIZE * chunkCount - totalLength);
}
if (mPutMethod != null) { if (mPutMethod != null) {
mPutMethod.releaseConnection(); // let the connection available mPutMethod.releaseConnection(); // let the connection available
// for other methods // for other methods
} }
mPutMethod = new PutMethod(uriPrefix + chunkCount + "-" + chunkIndex); 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_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); ((ChunkFromFileChannelRequestEntity) mEntity).setOffset(offset);
mPutMethod.setRequestEntity(mEntity); mPutMethod.setRequestEntity(mEntity);
if (mCancellationRequested.get()) { if (mCancellationRequested.get()) {

View File

@ -32,7 +32,6 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean; 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.PutMethod;
import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
@ -60,12 +59,14 @@ public class UploadRemoteFileOperation extends RemoteOperation {
private static final String TAG = UploadRemoteFileOperation.class.getSimpleName(); private static final String TAG = UploadRemoteFileOperation.class.getSimpleName();
protected static final String OC_TOTAL_LENGTH_HEADER = "OC-Total-Length"; 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 mLocalPath;
protected String mRemotePath; protected String mRemotePath;
protected String mMimeType; protected String mMimeType;
protected PutMethod mPutMethod = null; protected PutMethod mPutMethod = null;
protected boolean mForbiddenCharsInServer = false; protected boolean mForbiddenCharsInServer = false;
protected String mRequiredEtag = null;
protected final AtomicBoolean mCancellationRequested = new AtomicBoolean(false); protected final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
protected Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>(); protected Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
@ -78,6 +79,11 @@ public class UploadRemoteFileOperation extends RemoteOperation {
mMimeType = mimeType; mMimeType = mimeType;
} }
public UploadRemoteFileOperation(String localPath, String remotePath, String mimeType, String requiredEtag) {
this(localPath, remotePath, mimeType);
mRequiredEtag = requiredEtag;
}
@Override @Override
protected RemoteOperationResult run(OwnCloudClient client) { protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result = null; RemoteOperationResult result = null;
@ -126,6 +132,9 @@ public class UploadRemoteFileOperation extends RemoteOperation {
((ProgressiveDataTransferer)mEntity) ((ProgressiveDataTransferer)mEntity)
.addDatatransferProgressListeners(mDataTransferListeners); .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.addRequestHeader(OC_TOTAL_LENGTH_HEADER, String.valueOf(f.length()));
mPutMethod.setRequestEntity(mEntity); mPutMethod.setRequestEntity(mEntity);
status = client.executeMethod(mPutMethod); status = client.executeMethod(mPutMethod);