Skip to content

Commit

Permalink
Merge pull request #41 from nextcloud/fix_file_modification_date_on_u…
Browse files Browse the repository at this point in the history
…pload

Fix file modification date on upload
  • Loading branch information
AndyScherzinger authored May 13, 2017
2 parents 2c45382 + f40740e commit 4281845
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,30 +146,36 @@ public void onClickHandler(View button) {
Toast.makeText(this, R.string.youre_doing_it_wrong, Toast.LENGTH_SHORT).show();
}
}

private void startRefresh() {
ReadRemoteFolderOperation refreshOperation = new ReadRemoteFolderOperation(FileUtils.PATH_SEPARATOR);
refreshOperation.execute(mClient, this, mHandler);
}

private void startUpload() {
File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
File fileToUpload = upFolder.listFiles()[0];
String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
File fileToUpload = upFolder.listFiles()[0];
String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
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.execute(mClient, this, mHandler);
}

private void startRemoteDeletion() {
File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
File fileToUpload = upFolder.listFiles()[0];
File fileToUpload = upFolder.listFiles()[0];
String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath);
removeOperation.execute(mClient, this, mHandler);
}

private void startDownload() {
File downFolder = new File(getCacheDir(), getString(R.string.download_folder_path));
downFolder.mkdir();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,20 @@ 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 OC_CHUNK_X_OC_MTIME_HEADER = "X-OC-Mtime";
private static final String TAG = ChunkedUploadRemoteFileOperation.class.getSimpleName();
private Context mContext;

public ChunkedUploadRemoteFileOperation(
Context context, String storagePath, String remotePath, String mimeType, String requiredEtag) {
super(storagePath, remotePath, mimeType, requiredEtag);
public ChunkedUploadRemoteFileOperation(Context context, String storagePath, String remotePath,
String mimeType, String requiredEtag, String fileLastModifTimestamp) {
super(storagePath, remotePath, mimeType, requiredEtag, fileLastModifTimestamp);
mContext = context;
}

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

@Override
protected int uploadFile(OwnCloudClient client) throws IOException {
Expand Down Expand Up @@ -112,6 +118,8 @@ protected int uploadFile(OwnCloudClient client) throws IOException {
mPutMethod.addRequestHeader(OC_CHUNKED_HEADER, OC_CHUNKED_HEADER);
mPutMethod.addRequestHeader(OC_CHUNK_SIZE_HEADER, chunkSizeStr);
mPutMethod.addRequestHeader(OC_TOTAL_LENGTH_HEADER, totalLengthStr);
mPutMethod.addRequestHeader(OC_CHUNK_X_OC_MTIME_HEADER, mFileLastModifTimestamp);

((ChunkFromFileChannelRequestEntity) mEntity).setOffset(offset);
mPutMethod.setRequestEntity(mEntity);
if (mCancellationRequested.get()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,32 @@ public class UploadRemoteFileOperation extends RemoteOperation {

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 String mLocalPath;
protected String mRemotePath;
protected String mMimeType;
protected String mFileLastModifTimestamp;
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>();

protected RequestEntity mEntity = null;

public UploadRemoteFileOperation(String localPath, String remotePath, String mimeType) {
public UploadRemoteFileOperation(String localPath, String remotePath, String mimeType,
String fileLastModifTimestamp) {
mLocalPath = localPath;
mRemotePath = remotePath;
mMimeType = mimeType;
mFileLastModifTimestamp = fileLastModifTimestamp;
}

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

Expand Down Expand Up @@ -152,6 +157,7 @@ protected int uploadFile(OwnCloudClient client) throws IOException {
mPutMethod.addRequestHeader(IF_MATCH_HEADER, "\"" + mRequiredEtag + "\"");
}
mPutMethod.addRequestHeader(OC_TOTAL_LENGTH_HEADER, String.valueOf(f.length()));
mPutMethod.addRequestHeader(OC_X_OC_MTIME_HEADER, mFileLastModifTimestamp);
mPutMethod.setRequestEntity(mEntity);
status = client.executeMethod(mPutMethod);

Expand All @@ -175,7 +181,7 @@ protected int uploadFile(OwnCloudClient client) throws IOException {
}
return status;
}

public Set<OnDatatransferProgressListener> getDataTransferListeners() {
return mDataTransferListeners;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,52 +245,49 @@ public RemoteOperationResult downloadFile(RemoteFile remoteFile, String temporal
*
* @return
*/
public RemoteOperationResult uploadFile(
String storagePath, String remotePath, String mimeType, String requiredEtag
) {
public RemoteOperationResult uploadFile(String storagePath, String remotePath, String mimeType,
String requiredEtag) {
return TestActivity.uploadFile(this, storagePath, remotePath, mimeType, mClient, requiredEtag);
}
/** Access to the library method to Upload a File


/** Access to the library method to Upload a File
*
* @param context
* @param storagePath
* @param remotePath
* @param mimeType
* @param client Client instance configured to access the target OC server.
*
* @param client Client instance configured to access the target OC server.
* @param requiredEtag
* @return
*/
public static RemoteOperationResult uploadFile(
Context context, String storagePath, String remotePath, String mimeType, OwnCloudClient client,
String requiredEtag
) {
public static RemoteOperationResult uploadFile(Context context, String storagePath, String remotePath,
String mimeType, OwnCloudClient client, String requiredEtag) {

String fileLastModifTimestamp = getFileLastModifTimeStamp(storagePath);

UploadRemoteFileOperation uploadOperation;

if ((new File(storagePath)).length() > ChunkedUploadRemoteFileOperation.CHUNK_SIZE) {
uploadOperation = new ChunkedUploadRemoteFileOperation(
context, storagePath, remotePath, mimeType, requiredEtag
context, storagePath, remotePath, mimeType, requiredEtag, fileLastModifTimestamp
);
} else {
uploadOperation = new UploadRemoteFileOperation(
storagePath, remotePath, mimeType
storagePath, remotePath, mimeType, requiredEtag, fileLastModifTimestamp
);
}

RemoteOperationResult result = uploadOperation.execute(client);
return result;

return uploadOperation.execute(client);
}

/** Access to the library method to Get Shares
*
* @return
*/
public RemoteOperationResult getShares(){

public RemoteOperationResult getShares() {
GetRemoteSharesOperation getOperation = new GetRemoteSharesOperation();
RemoteOperationResult result = getOperation.execute(mClient);

return result;
return getOperation.execute(mClient);
}

/** Access to the library method to Create Share
Expand All @@ -315,10 +312,9 @@ public RemoteOperationResult getShares(){
public RemoteOperationResult createShare(String path, ShareType shareType, String shareWith, boolean publicUpload,
String password, int permissions){

CreateRemoteShareOperation createOperation = new CreateRemoteShareOperation(path, shareType, shareWith, publicUpload, password, permissions);
RemoteOperationResult result = createOperation.execute(mClient);

return result;
CreateRemoteShareOperation createOperation = new CreateRemoteShareOperation(path, shareType, shareWith,
publicUpload, password, permissions);
return createOperation.execute(mClient);
}


Expand All @@ -330,10 +326,7 @@ public RemoteOperationResult createShare(String path, ShareType shareType, Strin

public RemoteOperationResult removeShare(int idShare) {
RemoveRemoteShareOperation removeOperation = new RemoveRemoteShareOperation(idShare);
RemoteOperationResult result = removeOperation.execute(mClient);

return result;

return removeOperation.execute(mClient);
}


Expand Down Expand Up @@ -373,5 +366,9 @@ public static File extractAsset(String fileName, Context context) throws IOExcep
return extractedFile;
}


private static String getFileLastModifTimeStamp (String storagePath) {
File file = new File(storagePath);
Long timeStampLong = file.lastModified()/1000;
return timeStampLong.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected void setUp() throws Exception {
* Test Upload File without chunks
*/
public void testUploadFile() {

String fullPath2Upload = mBaseFolderPath + UPLOAD_PATH;
RemoteOperationResult result = mActivity.uploadFile(
mFileToUpload.getAbsolutePath(),
Expand Down Expand Up @@ -121,5 +121,5 @@ protected void tearDown() throws Exception {
}
super.tearDown();
}

}

0 comments on commit 4281845

Please sign in to comment.