Skip to content

Commit 4281845

Browse files
Merge pull request #41 from nextcloud/fix_file_modification_date_on_upload
Fix file modification date on upload
2 parents 2c45382 + f40740e commit 4281845

File tree

5 files changed

+66
-49
lines changed

5 files changed

+66
-49
lines changed

sample_client/src/com/owncloud/android/lib/sampleclient/MainActivity.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,30 +146,36 @@ public void onClickHandler(View button) {
146146
Toast.makeText(this, R.string.youre_doing_it_wrong, Toast.LENGTH_SHORT).show();
147147
}
148148
}
149-
149+
150150
private void startRefresh() {
151151
ReadRemoteFolderOperation refreshOperation = new ReadRemoteFolderOperation(FileUtils.PATH_SEPARATOR);
152152
refreshOperation.execute(mClient, this, mHandler);
153153
}
154-
154+
155155
private void startUpload() {
156156
File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
157-
File fileToUpload = upFolder.listFiles()[0];
158-
String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
157+
File fileToUpload = upFolder.listFiles()[0];
158+
String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
159159
String mimeType = getString(R.string.sample_file_mimetype);
160-
UploadRemoteFileOperation uploadOperation = new UploadRemoteFileOperation(fileToUpload.getAbsolutePath(), remotePath, mimeType);
160+
161+
// Get the last modification date of the file from the file system
162+
Long timeStampLong = fileToUpload.lastModified() / 1000;
163+
String timeStamp = timeStampLong.toString();
164+
165+
UploadRemoteFileOperation uploadOperation =
166+
new UploadRemoteFileOperation(fileToUpload.getAbsolutePath(), remotePath, mimeType, timeStamp);
161167
uploadOperation.addDatatransferProgressListener(this);
162168
uploadOperation.execute(mClient, this, mHandler);
163169
}
164-
170+
165171
private void startRemoteDeletion() {
166172
File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
167-
File fileToUpload = upFolder.listFiles()[0];
173+
File fileToUpload = upFolder.listFiles()[0];
168174
String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
169175
RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath);
170176
removeOperation.execute(mClient, this, mHandler);
171177
}
172-
178+
173179
private void startDownload() {
174180
File downFolder = new File(getCacheDir(), getString(R.string.download_folder_path));
175181
downFolder.mkdir();

src/com/owncloud/android/lib/resources/files/ChunkedUploadRemoteFileOperation.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,20 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation
5454
public static final long CHUNK_SIZE = 1024000;
5555
private static final String OC_CHUNKED_HEADER = "OC-Chunked";
5656
private static final String OC_CHUNK_SIZE_HEADER = "OC-Chunk-Size";
57+
private static final String OC_CHUNK_X_OC_MTIME_HEADER = "X-OC-Mtime";
5758
private static final String TAG = ChunkedUploadRemoteFileOperation.class.getSimpleName();
5859
private Context mContext;
5960

60-
public ChunkedUploadRemoteFileOperation(
61-
Context context, String storagePath, String remotePath, String mimeType, String requiredEtag) {
62-
super(storagePath, remotePath, mimeType, requiredEtag);
61+
public ChunkedUploadRemoteFileOperation(Context context, String storagePath, String remotePath,
62+
String mimeType, String requiredEtag, String fileLastModifTimestamp) {
63+
super(storagePath, remotePath, mimeType, requiredEtag, fileLastModifTimestamp);
6364
mContext = context;
6465
}
66+
67+
public ChunkedUploadRemoteFileOperation(String storagePath, String remotePath, String mimeType,
68+
String requiredEtag, String fileLastModifTimestamp) {
69+
super(storagePath, remotePath, mimeType, requiredEtag, fileLastModifTimestamp);
70+
}
6571

6672
@Override
6773
protected int uploadFile(OwnCloudClient client) throws IOException {
@@ -112,6 +118,8 @@ protected int uploadFile(OwnCloudClient client) throws IOException {
112118
mPutMethod.addRequestHeader(OC_CHUNKED_HEADER, OC_CHUNKED_HEADER);
113119
mPutMethod.addRequestHeader(OC_CHUNK_SIZE_HEADER, chunkSizeStr);
114120
mPutMethod.addRequestHeader(OC_TOTAL_LENGTH_HEADER, totalLengthStr);
121+
mPutMethod.addRequestHeader(OC_CHUNK_X_OC_MTIME_HEADER, mFileLastModifTimestamp);
122+
115123
((ChunkFromFileChannelRequestEntity) mEntity).setOffset(offset);
116124
mPutMethod.setRequestEntity(mEntity);
117125
if (mCancellationRequested.get()) {

src/com/owncloud/android/lib/resources/files/UploadRemoteFileOperation.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,32 @@ public class UploadRemoteFileOperation extends RemoteOperation {
6262

6363
protected static final String OC_TOTAL_LENGTH_HEADER = "OC-Total-Length";
6464
protected static final String IF_MATCH_HEADER = "If-Match";
65+
protected static final String OC_X_OC_MTIME_HEADER = "X-OC-Mtime";
6566

6667
protected String mLocalPath;
6768
protected String mRemotePath;
6869
protected String mMimeType;
70+
protected String mFileLastModifTimestamp;
6971
protected PutMethod mPutMethod = null;
7072
protected boolean mForbiddenCharsInServer = false;
7173
protected String mRequiredEtag = null;
72-
74+
7375
protected final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
7476
protected Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
7577

7678
protected RequestEntity mEntity = null;
7779

78-
public UploadRemoteFileOperation(String localPath, String remotePath, String mimeType) {
80+
public UploadRemoteFileOperation(String localPath, String remotePath, String mimeType,
81+
String fileLastModifTimestamp) {
7982
mLocalPath = localPath;
8083
mRemotePath = remotePath;
8184
mMimeType = mimeType;
85+
mFileLastModifTimestamp = fileLastModifTimestamp;
8286
}
8387

84-
public UploadRemoteFileOperation(String localPath, String remotePath, String mimeType, String requiredEtag) {
85-
this(localPath, remotePath, mimeType);
88+
public UploadRemoteFileOperation(String localPath, String remotePath, String mimeType, String requiredEtag,
89+
String fileLastModifTimestamp) {
90+
this(localPath, remotePath, mimeType, fileLastModifTimestamp);
8691
mRequiredEtag = requiredEtag;
8792
}
8893

@@ -152,6 +157,7 @@ protected int uploadFile(OwnCloudClient client) throws IOException {
152157
mPutMethod.addRequestHeader(IF_MATCH_HEADER, "\"" + mRequiredEtag + "\"");
153158
}
154159
mPutMethod.addRequestHeader(OC_TOTAL_LENGTH_HEADER, String.valueOf(f.length()));
160+
mPutMethod.addRequestHeader(OC_X_OC_MTIME_HEADER, mFileLastModifTimestamp);
155161
mPutMethod.setRequestEntity(mEntity);
156162
status = client.executeMethod(mPutMethod);
157163

@@ -175,7 +181,7 @@ protected int uploadFile(OwnCloudClient client) throws IOException {
175181
}
176182
return status;
177183
}
178-
184+
179185
public Set<OnDatatransferProgressListener> getDataTransferListeners() {
180186
return mDataTransferListeners;
181187
}

test_client/src/com/owncloud/android/lib/test_project/TestActivity.java

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -245,52 +245,49 @@ public RemoteOperationResult downloadFile(RemoteFile remoteFile, String temporal
245245
*
246246
* @return
247247
*/
248-
public RemoteOperationResult uploadFile(
249-
String storagePath, String remotePath, String mimeType, String requiredEtag
250-
) {
248+
public RemoteOperationResult uploadFile(String storagePath, String remotePath, String mimeType,
249+
String requiredEtag) {
251250
return TestActivity.uploadFile(this, storagePath, remotePath, mimeType, mClient, requiredEtag);
252251
}
253-
254-
255-
/** Access to the library method to Upload a File
252+
253+
254+
/** Access to the library method to Upload a File
256255
*
257256
* @param context
258257
* @param storagePath
259258
* @param remotePath
260259
* @param mimeType
261-
* @param client Client instance configured to access the target OC server.
262-
*
260+
* @param client Client instance configured to access the target OC server.
261+
* @param requiredEtag
263262
* @return
264263
*/
265-
public static RemoteOperationResult uploadFile(
266-
Context context, String storagePath, String remotePath, String mimeType, OwnCloudClient client,
267-
String requiredEtag
268-
) {
264+
public static RemoteOperationResult uploadFile(Context context, String storagePath, String remotePath,
265+
String mimeType, OwnCloudClient client, String requiredEtag) {
266+
267+
String fileLastModifTimestamp = getFileLastModifTimeStamp(storagePath);
268+
269269
UploadRemoteFileOperation uploadOperation;
270+
270271
if ((new File(storagePath)).length() > ChunkedUploadRemoteFileOperation.CHUNK_SIZE) {
271272
uploadOperation = new ChunkedUploadRemoteFileOperation(
272-
context, storagePath, remotePath, mimeType, requiredEtag
273+
context, storagePath, remotePath, mimeType, requiredEtag, fileLastModifTimestamp
273274
);
274275
} else {
275276
uploadOperation = new UploadRemoteFileOperation(
276-
storagePath, remotePath, mimeType
277+
storagePath, remotePath, mimeType, requiredEtag, fileLastModifTimestamp
277278
);
278279
}
279-
280-
RemoteOperationResult result = uploadOperation.execute(client);
281-
return result;
280+
281+
return uploadOperation.execute(client);
282282
}
283283

284284
/** Access to the library method to Get Shares
285285
*
286286
* @return
287287
*/
288-
public RemoteOperationResult getShares(){
289-
288+
public RemoteOperationResult getShares() {
290289
GetRemoteSharesOperation getOperation = new GetRemoteSharesOperation();
291-
RemoteOperationResult result = getOperation.execute(mClient);
292-
293-
return result;
290+
return getOperation.execute(mClient);
294291
}
295292

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

318-
CreateRemoteShareOperation createOperation = new CreateRemoteShareOperation(path, shareType, shareWith, publicUpload, password, permissions);
319-
RemoteOperationResult result = createOperation.execute(mClient);
320-
321-
return result;
315+
CreateRemoteShareOperation createOperation = new CreateRemoteShareOperation(path, shareType, shareWith,
316+
publicUpload, password, permissions);
317+
return createOperation.execute(mClient);
322318
}
323319

324320

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

331327
public RemoteOperationResult removeShare(int idShare) {
332328
RemoveRemoteShareOperation removeOperation = new RemoveRemoteShareOperation(idShare);
333-
RemoteOperationResult result = removeOperation.execute(mClient);
334-
335-
return result;
336-
329+
return removeOperation.execute(mClient);
337330
}
338331

339332

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

376-
369+
private static String getFileLastModifTimeStamp (String storagePath) {
370+
File file = new File(storagePath);
371+
Long timeStampLong = file.lastModified()/1000;
372+
return timeStampLong.toString();
373+
}
377374
}

test_client/tests/src/com/owncloud/android/lib/test_project/test/UploadFileTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected void setUp() throws Exception {
6969
* Test Upload File without chunks
7070
*/
7171
public void testUploadFile() {
72-
72+
7373
String fullPath2Upload = mBaseFolderPath + UPLOAD_PATH;
7474
RemoteOperationResult result = mActivity.uploadFile(
7575
mFileToUpload.getAbsolutePath(),
@@ -121,5 +121,5 @@ protected void tearDown() throws Exception {
121121
}
122122
super.tearDown();
123123
}
124-
124+
125125
}

0 commit comments

Comments
 (0)