mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-07 16:06:08 +00:00
Set modification date from the file in filesystem to the file to upload
This commit is contained in:
parent
0fef17a609
commit
274e0ec47b
@ -52,14 +52,14 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation
|
|||||||
private static final String OC_CHUNK_X_OC_MTIME_HEADER = "X-OC-Mtime";
|
private static final String OC_CHUNK_X_OC_MTIME_HEADER = "X-OC-Mtime";
|
||||||
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, String fileLastModifTimestamp){
|
||||||
super(storagePath, remotePath, mimeType);
|
super(storagePath, remotePath, mimeType, fileLastModifTimestamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ChunkedUploadRemoteFileOperation(
|
public ChunkedUploadRemoteFileOperation(
|
||||||
String storagePath, String remotePath, String mimeType, String requiredEtag
|
String storagePath, String remotePath, String mimeType, String requiredEtag, String fileLastModifTimestamp
|
||||||
){
|
){
|
||||||
super(storagePath, remotePath, mimeType, requiredEtag);
|
super(storagePath, remotePath, mimeType, requiredEtag, fileLastModifTimestamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -101,10 +101,7 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation
|
|||||||
mPutMethod.addRequestHeader(OC_CHUNK_SIZE_HEADER, chunkSizeStr);
|
mPutMethod.addRequestHeader(OC_CHUNK_SIZE_HEADER, chunkSizeStr);
|
||||||
mPutMethod.addRequestHeader(OC_TOTAL_LENGTH_HEADER, totalLengthStr);
|
mPutMethod.addRequestHeader(OC_TOTAL_LENGTH_HEADER, totalLengthStr);
|
||||||
|
|
||||||
// Tell to the server what is the last modification date of the file to upload
|
mPutMethod.addRequestHeader(OC_CHUNK_X_OC_MTIME_HEADER, mFileLastModifTimestamp);
|
||||||
Long timeStampLong = System.currentTimeMillis()/1000;
|
|
||||||
String timeStamp = timeStampLong.toString();
|
|
||||||
mPutMethod.addRequestHeader(OC_CHUNK_X_OC_MTIME_HEADER, timeStamp);
|
|
||||||
|
|
||||||
((ChunkFromFileChannelRequestEntity) mEntity).setOffset(offset);
|
((ChunkFromFileChannelRequestEntity) mEntity).setOffset(offset);
|
||||||
mPutMethod.setRequestEntity(mEntity);
|
mPutMethod.setRequestEntity(mEntity);
|
||||||
|
@ -58,131 +58,130 @@ import com.owncloud.android.lib.common.utils.Log_OC;
|
|||||||
|
|
||||||
public class UploadRemoteFileOperation extends RemoteOperation {
|
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 OC_X_OC_MTIME_HEADER = "X-OC-Mtime";
|
protected static final String OC_X_OC_MTIME_HEADER = "X-OC-Mtime";
|
||||||
protected static final String IF_MATCH_HEADER = "If-Match";
|
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 String mFileLastModifTimestamp;
|
||||||
protected boolean mForbiddenCharsInServer = false;
|
protected PutMethod mPutMethod = null;
|
||||||
protected String mRequiredEtag = null;
|
protected boolean mForbiddenCharsInServer = false;
|
||||||
|
protected String mRequiredEtag = null;
|
||||||
protected final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
|
|
||||||
protected Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
|
|
||||||
|
|
||||||
protected RequestEntity mEntity = null;
|
protected final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
|
||||||
|
protected Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
|
||||||
|
|
||||||
public UploadRemoteFileOperation(String localPath, String remotePath, String mimeType) {
|
protected RequestEntity mEntity = null;
|
||||||
mLocalPath = localPath;
|
|
||||||
mRemotePath = remotePath;
|
|
||||||
mMimeType = mimeType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public UploadRemoteFileOperation(String localPath, String remotePath, String mimeType, String requiredEtag) {
|
public UploadRemoteFileOperation(String localPath, String remotePath, String mimeType, String fileLastModifTimestamp) {
|
||||||
this(localPath, remotePath, mimeType);
|
mLocalPath = localPath;
|
||||||
mRequiredEtag = requiredEtag;
|
mRemotePath = remotePath;
|
||||||
}
|
mMimeType = mimeType;
|
||||||
|
mFileLastModifTimestamp = fileLastModifTimestamp;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
public UploadRemoteFileOperation(String localPath, String remotePath, String mimeType, String requiredEtag, String fileLastModifTimestamp) {
|
||||||
protected RemoteOperationResult run(OwnCloudClient client) {
|
this(localPath, remotePath, mimeType, fileLastModifTimestamp);
|
||||||
RemoteOperationResult result = null;
|
mRequiredEtag = requiredEtag;
|
||||||
DefaultHttpMethodRetryHandler oldRetryHandler =
|
}
|
||||||
(DefaultHttpMethodRetryHandler) client.getParams().getParameter(HttpMethodParams.RETRY_HANDLER);
|
|
||||||
|
|
||||||
try {
|
@Override
|
||||||
// prevent that uploads are retried automatically by network library
|
protected RemoteOperationResult run(OwnCloudClient client) {
|
||||||
client.getParams().setParameter(
|
RemoteOperationResult result = null;
|
||||||
HttpMethodParams.RETRY_HANDLER,
|
DefaultHttpMethodRetryHandler oldRetryHandler =
|
||||||
new DefaultHttpMethodRetryHandler(0, false)
|
(DefaultHttpMethodRetryHandler) client.getParams().getParameter(HttpMethodParams.RETRY_HANDLER);
|
||||||
);
|
|
||||||
|
|
||||||
mPutMethod = new PutMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath));
|
try {
|
||||||
|
// prevent that uploads are retried automatically by network library
|
||||||
|
client.getParams().setParameter(
|
||||||
|
HttpMethodParams.RETRY_HANDLER,
|
||||||
|
new DefaultHttpMethodRetryHandler(0, false)
|
||||||
|
);
|
||||||
|
|
||||||
if (mCancellationRequested.get()) {
|
mPutMethod = new PutMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath));
|
||||||
// the operation was cancelled before getting it's turn to be executed in the queue of uploads
|
|
||||||
result = new RemoteOperationResult(new OperationCancelledException());
|
|
||||||
|
|
||||||
} else {
|
if (mCancellationRequested.get()) {
|
||||||
// perform the upload
|
// the operation was cancelled before getting it's turn to be executed in the queue of uploads
|
||||||
int status = uploadFile(client);
|
result = new RemoteOperationResult(new OperationCancelledException());
|
||||||
if (mForbiddenCharsInServer){
|
|
||||||
result = new RemoteOperationResult(
|
|
||||||
RemoteOperationResult.ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER);
|
|
||||||
} else {
|
|
||||||
result = new RemoteOperationResult(isSuccess(status), status,
|
|
||||||
(mPutMethod != null ? mPutMethod.getResponseHeaders() : null));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
} else {
|
||||||
if (mPutMethod != null && mPutMethod.isAborted()) {
|
// perform the upload
|
||||||
result = new RemoteOperationResult(new OperationCancelledException());
|
int status = uploadFile(client);
|
||||||
|
if (mForbiddenCharsInServer){
|
||||||
|
result = new RemoteOperationResult(
|
||||||
|
RemoteOperationResult.ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER);
|
||||||
|
} else {
|
||||||
|
result = new RemoteOperationResult(isSuccess(status), status,
|
||||||
|
(mPutMethod != null ? mPutMethod.getResponseHeaders() : null));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} catch (Exception e) {
|
||||||
result = new RemoteOperationResult(e);
|
if (mPutMethod != null && mPutMethod.isAborted()) {
|
||||||
}
|
result = new RemoteOperationResult(new OperationCancelledException());
|
||||||
} finally {
|
|
||||||
// reset previous retry handler
|
|
||||||
client.getParams().setParameter(
|
|
||||||
HttpMethodParams.RETRY_HANDLER,
|
|
||||||
oldRetryHandler
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isSuccess(int status) {
|
} else {
|
||||||
return ((status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED ||
|
result = new RemoteOperationResult(e);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
// reset previous retry handler
|
||||||
|
client.getParams().setParameter(
|
||||||
|
HttpMethodParams.RETRY_HANDLER,
|
||||||
|
oldRetryHandler
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isSuccess(int status) {
|
||||||
|
return ((status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED ||
|
||||||
status == HttpStatus.SC_NO_CONTENT));
|
status == HttpStatus.SC_NO_CONTENT));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int uploadFile(OwnCloudClient client) throws IOException {
|
protected int uploadFile(OwnCloudClient client) throws IOException {
|
||||||
int status = -1;
|
int status = -1;
|
||||||
try {
|
try {
|
||||||
File f = new File(mLocalPath);
|
File f = new File(mLocalPath);
|
||||||
mEntity = new FileRequestEntity(f, mMimeType);
|
mEntity = new FileRequestEntity(f, mMimeType);
|
||||||
synchronized (mDataTransferListeners) {
|
synchronized (mDataTransferListeners) {
|
||||||
((ProgressiveDataTransferer)mEntity)
|
((ProgressiveDataTransferer)mEntity)
|
||||||
.addDatatransferProgressListeners(mDataTransferListeners);
|
.addDatatransferProgressListeners(mDataTransferListeners);
|
||||||
}
|
}
|
||||||
if (mRequiredEtag != null && mRequiredEtag.length() > 0) {
|
if (mRequiredEtag != null && mRequiredEtag.length() > 0) {
|
||||||
mPutMethod.addRequestHeader(IF_MATCH_HEADER, "\"" + mRequiredEtag + "\"");
|
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()));
|
||||||
|
|
||||||
// Tell to the server what is the last modification date of the file to upload
|
mPutMethod.addRequestHeader(OC_X_OC_MTIME_HEADER, mFileLastModifTimestamp);
|
||||||
Long timeStampLong = System.currentTimeMillis()/1000;
|
|
||||||
String timeStamp = timeStampLong.toString();
|
|
||||||
mPutMethod.addRequestHeader(OC_X_OC_MTIME_HEADER, timeStamp);
|
|
||||||
|
|
||||||
mPutMethod.setRequestEntity(mEntity);
|
|
||||||
status = client.executeMethod(mPutMethod);
|
|
||||||
|
|
||||||
if (status == 400) {
|
mPutMethod.setRequestEntity(mEntity);
|
||||||
InvalidCharacterExceptionParser xmlParser = new InvalidCharacterExceptionParser();
|
status = client.executeMethod(mPutMethod);
|
||||||
InputStream is = new ByteArrayInputStream(
|
|
||||||
mPutMethod.getResponseBodyAsString().getBytes());
|
|
||||||
try {
|
|
||||||
mForbiddenCharsInServer = xmlParser.parseXMLResponse(is);
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
if (status == 400) {
|
||||||
mForbiddenCharsInServer = false;
|
InvalidCharacterExceptionParser xmlParser = new InvalidCharacterExceptionParser();
|
||||||
Log_OC.e(TAG, "Exception reading exception from server", e);
|
InputStream is = new ByteArrayInputStream(
|
||||||
}
|
mPutMethod.getResponseBodyAsString().getBytes());
|
||||||
}
|
try {
|
||||||
|
mForbiddenCharsInServer = xmlParser.parseXMLResponse(is);
|
||||||
|
|
||||||
client.exhaustResponse(mPutMethod.getResponseBodyAsStream());
|
} catch (Exception e) {
|
||||||
|
mForbiddenCharsInServer = false;
|
||||||
|
Log_OC.e(TAG, "Exception reading exception from server", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
client.exhaustResponse(mPutMethod.getResponseBodyAsStream());
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
mPutMethod.releaseConnection(); // let the connection available for other methods
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
} finally {
|
|
||||||
mPutMethod.releaseConnection(); // let the connection available for other methods
|
|
||||||
}
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Set<OnDatatransferProgressListener> getDataTransferListeners() {
|
public Set<OnDatatransferProgressListener> getDataTransferListeners() {
|
||||||
return mDataTransferListeners;
|
return mDataTransferListeners;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user