mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-30 01:48:14 +00:00 
			
		
		
		
	Merge pull request #140 from owncloud/761_file_modification_date_lost
File modification date lost when uploading file
This commit is contained in:
		
						commit
						7647a4974b
					
				| @ -157,7 +157,12 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, | |||||||
|     	File fileToUpload = upFolder.listFiles()[0]; |     	File fileToUpload = upFolder.listFiles()[0]; | ||||||
|     	String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();  |     	String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();  | ||||||
|     	String mimeType = getString(R.string.sample_file_mimetype); |     	String mimeType = getString(R.string.sample_file_mimetype); | ||||||
|     	UploadRemoteFileOperation uploadOperation = new UploadRemoteFileOperation(fileToUpload.getAbsolutePath(), remotePath, mimeType); | 
 | ||||||
|  | 		// Get the last modification date of the file from the file system | ||||||
|  | 		Long timeStampLong = fileToUpload.lastModified()/1000; | ||||||
|  | 		String timeStamp = timeStampLong.toString(); | ||||||
|  | 
 | ||||||
|  |     	UploadRemoteFileOperation uploadOperation = new UploadRemoteFileOperation(fileToUpload.getAbsolutePath(), remotePath, mimeType, timeStamp); | ||||||
|     	uploadOperation.addDatatransferProgressListener(this); |     	uploadOperation.addDatatransferProgressListener(this); | ||||||
|     	uploadOperation.execute(mClient, this, mHandler); |     	uploadOperation.execute(mClient, this, mHandler); | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -49,16 +49,19 @@ 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 OC_CHUNK_SIZE_HEADER = "OC-Chunk-Size"; | ||||||
|  |     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, | ||||||
|         super(storagePath, remotePath, mimeType); |                                             String fileLastModifTimestamp){ | ||||||
|  |         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 | ||||||
| @ -99,6 +102,9 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation | |||||||
|                 mPutMethod.addRequestHeader(OC_CHUNKED_HEADER, OC_CHUNKED_HEADER); |                 mPutMethod.addRequestHeader(OC_CHUNKED_HEADER, OC_CHUNKED_HEADER); | ||||||
|                 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); | ||||||
|  | 
 | ||||||
|  |                 mPutMethod.addRequestHeader(OC_CHUNK_X_OC_MTIME_HEADER, mFileLastModifTimestamp); | ||||||
|  | 
 | ||||||
|                 ((ChunkFromFileChannelRequestEntity) mEntity).setOffset(offset); |                 ((ChunkFromFileChannelRequestEntity) mEntity).setOffset(offset); | ||||||
|                 mPutMethod.setRequestEntity(mEntity); |                 mPutMethod.setRequestEntity(mEntity); | ||||||
|                 if (mCancellationRequested.get()) { |                 if (mCancellationRequested.get()) { | ||||||
|  | |||||||
| @ -58,123 +58,131 @@ 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 IF_MATCH_HEADER = "If-Match"; |     protected static final String OC_X_OC_MTIME_HEADER = "X-OC-Mtime"; | ||||||
|  |     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 final AtomicBoolean mCancellationRequested = new AtomicBoolean(false); | ||||||
| 	protected Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>(); |     protected Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>(); | ||||||
| 
 | 
 | ||||||
| 	protected RequestEntity mEntity = null; |     protected RequestEntity mEntity = null; | ||||||
| 
 | 
 | ||||||
| 	public UploadRemoteFileOperation(String localPath, String remotePath, String mimeType) { |     public UploadRemoteFileOperation(String localPath, String remotePath, String mimeType, | ||||||
| 		mLocalPath = localPath; |                                      String fileLastModifTimestamp) { | ||||||
| 		mRemotePath = remotePath; |         mLocalPath = localPath; | ||||||
| 		mMimeType = mimeType; |         mRemotePath = remotePath; | ||||||
| 	} |         mMimeType = mimeType; | ||||||
|  |         mFileLastModifTimestamp = fileLastModifTimestamp; | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
| 	public UploadRemoteFileOperation(String localPath, String remotePath, String mimeType, String requiredEtag) { |     public UploadRemoteFileOperation(String localPath, String remotePath, String mimeType, | ||||||
| 		this(localPath, remotePath, mimeType); |                                      String requiredEtag, String fileLastModifTimestamp) { | ||||||
| 		mRequiredEtag = requiredEtag; |         this(localPath, remotePath, mimeType, fileLastModifTimestamp); | ||||||
| 	} |         mRequiredEtag = requiredEtag; | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
| 	@Override |     @Override | ||||||
| 	protected RemoteOperationResult run(OwnCloudClient client) { |     protected RemoteOperationResult run(OwnCloudClient client) { | ||||||
| 		RemoteOperationResult result = null; |         RemoteOperationResult result = null; | ||||||
| 		DefaultHttpMethodRetryHandler oldRetryHandler = |         DefaultHttpMethodRetryHandler oldRetryHandler = | ||||||
| 			(DefaultHttpMethodRetryHandler) client.getParams().getParameter(HttpMethodParams.RETRY_HANDLER); |             (DefaultHttpMethodRetryHandler) client.getParams().getParameter(HttpMethodParams.RETRY_HANDLER); | ||||||
| 
 | 
 | ||||||
| 		try { |         try { | ||||||
| 			// prevent that uploads are retried automatically by network library |             // prevent that uploads are retried automatically by network library | ||||||
| 			client.getParams().setParameter( |             client.getParams().setParameter( | ||||||
| 				HttpMethodParams.RETRY_HANDLER, |                 HttpMethodParams.RETRY_HANDLER, | ||||||
| 				new DefaultHttpMethodRetryHandler(0, false) |                 new DefaultHttpMethodRetryHandler(0, false) | ||||||
| 			); |             ); | ||||||
| 
 | 
 | ||||||
| 			mPutMethod = new PutMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath)); |             mPutMethod = new PutMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath)); | ||||||
| 
 | 
 | ||||||
| 			if (mCancellationRequested.get()) { |             if (mCancellationRequested.get()) { | ||||||
| 				// the operation was cancelled before getting it's turn to be executed in the queue of uploads |                 // the operation was cancelled before getting it's turn to be executed in the queue of uploads | ||||||
| 				result = new RemoteOperationResult(new OperationCancelledException()); |                 result = new RemoteOperationResult(new OperationCancelledException()); | ||||||
| 
 | 
 | ||||||
| 			} else { |             } else { | ||||||
| 				// perform the upload |                 // perform the upload | ||||||
| 				int status = uploadFile(client); |                 int status = uploadFile(client); | ||||||
| 				if (mForbiddenCharsInServer){ |                 if (mForbiddenCharsInServer){ | ||||||
| 					result = new RemoteOperationResult( |                     result = new RemoteOperationResult( | ||||||
| 							RemoteOperationResult.ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER); |                             RemoteOperationResult.ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER); | ||||||
| 				} else { |                 } else { | ||||||
| 					result = new RemoteOperationResult(isSuccess(status), status, |                     result = new RemoteOperationResult(isSuccess(status), status, | ||||||
| 							(mPutMethod != null ? mPutMethod.getResponseHeaders() : null)); |                             (mPutMethod != null ? mPutMethod.getResponseHeaders() : null)); | ||||||
| 				} |                 } | ||||||
| 			} |             } | ||||||
| 
 | 
 | ||||||
| 		} catch (Exception e) { |         } catch (Exception e) { | ||||||
| 			if (mPutMethod != null && mPutMethod.isAborted()) { |             if (mPutMethod != null && mPutMethod.isAborted()) { | ||||||
| 				result = new RemoteOperationResult(new OperationCancelledException()); |                 result = new RemoteOperationResult(new OperationCancelledException()); | ||||||
| 
 | 
 | ||||||
| 			} else { |             } else { | ||||||
| 				result = new RemoteOperationResult(e); |                 result = new RemoteOperationResult(e); | ||||||
| 			} |             } | ||||||
| 		} finally { |         } finally { | ||||||
| 			// reset previous retry handler |             // reset previous retry handler | ||||||
| 			client.getParams().setParameter( |             client.getParams().setParameter( | ||||||
| 				HttpMethodParams.RETRY_HANDLER, |                 HttpMethodParams.RETRY_HANDLER, | ||||||
| 				oldRetryHandler |                 oldRetryHandler | ||||||
| 			); |             ); | ||||||
| 		} |         } | ||||||
| 		return result; |         return result; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	public boolean isSuccess(int status) { |     public boolean isSuccess(int status) { | ||||||
| 		return ((status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED || |         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())); | ||||||
| 			mPutMethod.setRequestEntity(mEntity); |  | ||||||
| 			status = client.executeMethod(mPutMethod); |  | ||||||
| 
 | 
 | ||||||
| 			if (status == 400) { |             mPutMethod.addRequestHeader(OC_X_OC_MTIME_HEADER, mFileLastModifTimestamp); | ||||||
| 				InvalidCharacterExceptionParser xmlParser = new InvalidCharacterExceptionParser(); |  | ||||||
| 				InputStream is = new ByteArrayInputStream( |  | ||||||
| 						mPutMethod.getResponseBodyAsString().getBytes()); |  | ||||||
| 				try { |  | ||||||
| 					mForbiddenCharsInServer = xmlParser.parseXMLResponse(is); |  | ||||||
| 
 | 
 | ||||||
| 				} catch (Exception e) { |             mPutMethod.setRequestEntity(mEntity); | ||||||
| 					mForbiddenCharsInServer = false; |             status = client.executeMethod(mPutMethod); | ||||||
| 					Log_OC.e(TAG, "Exception reading exception from server", e); |  | ||||||
| 				} |  | ||||||
| 			} |  | ||||||
| 
 | 
 | ||||||
| 			client.exhaustResponse(mPutMethod.getResponseBodyAsStream()); |             if (status == 400) { | ||||||
|  |                 InvalidCharacterExceptionParser xmlParser = new InvalidCharacterExceptionParser(); | ||||||
|  |                 InputStream is = new ByteArrayInputStream( | ||||||
|  |                         mPutMethod.getResponseBodyAsString().getBytes()); | ||||||
|  |                 try { | ||||||
|  |                     mForbiddenCharsInServer = xmlParser.parseXMLResponse(is); | ||||||
| 
 | 
 | ||||||
| 		} finally { |                 } catch (Exception e) { | ||||||
| 			mPutMethod.releaseConnection(); // let the connection available for other methods |                     mForbiddenCharsInServer = false; | ||||||
| 		} |                     Log_OC.e(TAG, "Exception reading exception from server", e); | ||||||
| 		return status; |                 } | ||||||
| 	} |             } | ||||||
|  | 
 | ||||||
|  |             client.exhaustResponse(mPutMethod.getResponseBodyAsStream()); | ||||||
|  | 
 | ||||||
|  |         } finally { | ||||||
|  |             mPutMethod.releaseConnection(); // let the connection available for other methods | ||||||
|  |         } | ||||||
|  |         return status; | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|     public Set<OnDatatransferProgressListener> getDataTransferListeners() { |     public Set<OnDatatransferProgressListener> getDataTransferListeners() { | ||||||
|         return mDataTransferListeners; |         return mDataTransferListeners; | ||||||
|  | |||||||
| @ -249,10 +249,10 @@ public class TestActivity extends Activity { | |||||||
| 	public RemoteOperationResult uploadFile( | 	public RemoteOperationResult uploadFile( | ||||||
| 			String storagePath, String remotePath, String mimeType | 			String storagePath, String remotePath, String mimeType | ||||||
| 			) { | 			) { | ||||||
|  | 
 | ||||||
| 		return TestActivity.uploadFile(storagePath, remotePath, mimeType, mClient); | 		return TestActivity.uploadFile(storagePath, remotePath, mimeType, mClient); | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	 |  | ||||||
| 	/** Access to the library method to Upload a File  | 	/** Access to the library method to Upload a File  | ||||||
| 	 * @param storagePath | 	 * @param storagePath | ||||||
| 	 * @param remotePath | 	 * @param remotePath | ||||||
| @ -264,14 +264,18 @@ public class TestActivity extends Activity { | |||||||
| 	public static RemoteOperationResult uploadFile( | 	public static RemoteOperationResult uploadFile( | ||||||
| 			String storagePath, String remotePath, String mimeType, OwnCloudClient client | 			String storagePath, String remotePath, String mimeType, OwnCloudClient client | ||||||
| 			) { | 			) { | ||||||
| 		UploadRemoteFileOperation uploadOperation; | 
 | ||||||
|  |         String fileLastModifTimestamp = getFileLastModifTimeStamp(storagePath); | ||||||
|  | 
 | ||||||
|  |         UploadRemoteFileOperation uploadOperation; | ||||||
|  | 
 | ||||||
| 		if ((new File(storagePath)).length() > ChunkedUploadRemoteFileOperation.CHUNK_SIZE ) { | 		if ((new File(storagePath)).length() > ChunkedUploadRemoteFileOperation.CHUNK_SIZE ) { | ||||||
|             uploadOperation = new ChunkedUploadRemoteFileOperation( |             uploadOperation = new ChunkedUploadRemoteFileOperation( | ||||||
|             		storagePath, remotePath, mimeType |             		storagePath, remotePath, mimeType, fileLastModifTimestamp | ||||||
|     		); |     		); | ||||||
|         } else { |         } else { | ||||||
|             uploadOperation = new UploadRemoteFileOperation( |             uploadOperation = new UploadRemoteFileOperation( | ||||||
|             		storagePath, remotePath, mimeType |             		storagePath, remotePath, mimeType, fileLastModifTimestamp | ||||||
|     		); |     		); | ||||||
|         } |         } | ||||||
| 		 | 		 | ||||||
| @ -381,5 +385,9 @@ public class TestActivity extends Activity { | |||||||
| 		return extractedFile; | 		return extractedFile; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 
 |     private static String getFileLastModifTimeStamp (String storagePath) { | ||||||
|  |         File file = new File(storagePath); | ||||||
|  |         Long timeStampLong = file.lastModified()/1000; | ||||||
|  |         return timeStampLong.toString(); | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -121,5 +121,4 @@ public class UploadFileTest extends RemoteTest { | |||||||
| 		} | 		} | ||||||
| 		super.tearDown(); | 		super.tearDown(); | ||||||
| 	} | 	} | ||||||
| 	 |  | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user