Strip away index.php/apps/files to allow copy pasting from the server
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / RefreshFolderOperation.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 package com.owncloud.android.operations;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Vector;
34
35 import org.apache.http.HttpStatus;
36 import android.accounts.Account;
37 import android.content.Context;
38 import android.content.Intent;
39 import android.util.Log;
40 //import android.support.v4.content.LocalBroadcastManager;
41
42 import com.owncloud.android.MainApp;
43 import com.owncloud.android.datamodel.FileDataStorageManager;
44 import com.owncloud.android.datamodel.OCFile;
45
46 import com.owncloud.android.lib.common.OwnCloudClient;
47 import com.owncloud.android.lib.resources.shares.OCShare;
48 import com.owncloud.android.lib.common.operations.RemoteOperation;
49 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
50 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
51 import com.owncloud.android.lib.common.utils.Log_OC;
52 import com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation;
53 import com.owncloud.android.lib.resources.files.FileUtils;
54 import com.owncloud.android.lib.resources.files.ReadRemoteFileOperation;
55 import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation;
56 import com.owncloud.android.lib.resources.files.RemoteFile;
57
58 import com.owncloud.android.syncadapter.FileSyncAdapter;
59 import com.owncloud.android.utils.FileStorageUtils;
60
61
62
63 /**
64 * Remote operation performing the synchronization of the list of files contained
65 * in a folder identified with its remote path.
66 *
67 * Fetches the list and properties of the files contained in the given folder, including their
68 * properties, and updates the local database with them.
69 *
70 * Does NOT enter in the child folders to synchronize their contents also.
71 */
72 public class RefreshFolderOperation extends RemoteOperation {
73
74 private static final String TAG = RefreshFolderOperation.class.getSimpleName();
75
76 public static final String EVENT_SINGLE_FOLDER_CONTENTS_SYNCED =
77 RefreshFolderOperation.class.getName() + ".EVENT_SINGLE_FOLDER_CONTENTS_SYNCED";
78 public static final String EVENT_SINGLE_FOLDER_SHARES_SYNCED =
79 RefreshFolderOperation.class.getName() + ".EVENT_SINGLE_FOLDER_SHARES_SYNCED";
80
81 /** Time stamp for the synchronization process in progress */
82 private long mCurrentSyncTime;
83
84 /** Remote folder to synchronize */
85 private OCFile mLocalFolder;
86
87 /** Access to the local database */
88 private FileDataStorageManager mStorageManager;
89
90 /** Account where the file to synchronize belongs */
91 private Account mAccount;
92
93 /** Android context; necessary to send requests to the download service */
94 private Context mContext;
95
96 /** Files and folders contained in the synchronized folder after a successful operation */
97 private List<OCFile> mChildren;
98
99 /** Counter of conflicts found between local and remote files */
100 private int mConflictsFound;
101
102 /** Counter of failed operations in synchronization of kept-in-sync files */
103 private int mFailsInFavouritesFound;
104
105 /**
106 * Map of remote and local paths to files that where locally stored in a location
107 * out of the ownCloud folder and couldn't be copied automatically into it
108 **/
109 private Map<String, String> mForgottenLocalFiles;
110
111 /** 'True' means that this operation is part of a full account synchronization */
112 private boolean mSyncFullAccount;
113
114 /** 'True' means that Share resources bound to the files into should be refreshed also */
115 private boolean mIsShareSupported;
116
117 /** 'True' means that the remote folder changed and should be fetched */
118 private boolean mRemoteFolderChanged;
119
120 /** 'True' means that Etag will be ignored */
121 private boolean mIgnoreETag;
122
123
124 /**
125 * Creates a new instance of {@link RefreshFolderOperation}.
126 *
127 * @param folder Folder to synchronize.
128 * @param currentSyncTime Time stamp for the synchronization process in progress.
129 * @param syncFullAccount 'True' means that this operation is part of a full account
130 * synchronization.
131 * @param isShareSupported 'True' means that the server supports the sharing API.
132 * @param ignoreETag 'True' means that the content of the remote folder should
133 * be fetched and updated even though the 'eTag' did not
134 * change.
135 * @param dataStorageManager Interface with the local database.
136 * @param account ownCloud account where the folder is located.
137 * @param context Application context.
138 */
139 public RefreshFolderOperation(OCFile folder,
140 long currentSyncTime,
141 boolean syncFullAccount,
142 boolean isShareSupported,
143 boolean ignoreETag,
144 FileDataStorageManager dataStorageManager,
145 Account account,
146 Context context) {
147 mLocalFolder = folder;
148 mCurrentSyncTime = currentSyncTime;
149 mSyncFullAccount = syncFullAccount;
150 mIsShareSupported = isShareSupported;
151 mStorageManager = dataStorageManager;
152 mAccount = account;
153 mContext = context;
154 mForgottenLocalFiles = new HashMap<String, String>();
155 mRemoteFolderChanged = false;
156 mIgnoreETag = ignoreETag;
157 }
158
159
160 public int getConflictsFound() {
161 return mConflictsFound;
162 }
163
164 public int getFailsInFavouritesFound() {
165 return mFailsInFavouritesFound;
166 }
167
168 public Map<String, String> getForgottenLocalFiles() {
169 return mForgottenLocalFiles;
170 }
171
172 /**
173 * Returns the list of files and folders contained in the synchronized folder,
174 * if called after synchronization is complete.
175 *
176 * @return List of files and folders contained in the synchronized folder.
177 */
178 public List<OCFile> getChildren() {
179 return mChildren;
180 }
181
182 /**
183 * Performs the synchronization.
184 *
185 * {@inheritDoc}
186 */
187 @Override
188 protected RemoteOperationResult run(OwnCloudClient client) {
189 RemoteOperationResult result = null;
190 mFailsInFavouritesFound = 0;
191 mConflictsFound = 0;
192 mForgottenLocalFiles.clear();
193
194 if (FileUtils.PATH_SEPARATOR.equals(mLocalFolder.getRemotePath()) && !mSyncFullAccount) {
195 updateOCVersion(client);
196 }
197
198 result = checkForChanges(client);
199
200 if (result.isSuccess()) {
201 if (mRemoteFolderChanged) {
202 result = fetchAndSyncRemoteFolder(client);
203 } else {
204 // TODO Enable when "On Device" is recovered ?
205 mChildren = mStorageManager.getFolderContent(mLocalFolder/*, false*/);
206 }
207 }
208
209 if (!mSyncFullAccount) {
210 sendLocalBroadcast(
211 EVENT_SINGLE_FOLDER_CONTENTS_SYNCED, mLocalFolder.getRemotePath(), result
212 );
213 }
214
215 if (result.isSuccess() && mIsShareSupported && !mSyncFullAccount) {
216 refreshSharesForFolder(client); // share result is ignored
217 }
218
219 if (!mSyncFullAccount) {
220 sendLocalBroadcast(
221 EVENT_SINGLE_FOLDER_SHARES_SYNCED, mLocalFolder.getRemotePath(), result
222 );
223 }
224
225 return result;
226
227 }
228
229
230 private void updateOCVersion(OwnCloudClient client) {
231 UpdateOCVersionOperation update = new UpdateOCVersionOperation(mAccount, mContext);
232 RemoteOperationResult result = update.execute(client);
233 if (result.isSuccess()) {
234 mIsShareSupported = update.getOCVersion().isSharedSupported();
235 }
236 }
237
238
239 private RemoteOperationResult checkForChanges(OwnCloudClient client) {
240 mRemoteFolderChanged = true;
241 RemoteOperationResult result = null;
242 String remotePath = null;
243
244 remotePath = mLocalFolder.getRemotePath();
245 Log_OC.d(TAG, "Checking changes in " + mAccount.name + remotePath);
246
247 // remote request
248 ReadRemoteFileOperation operation = new ReadRemoteFileOperation(remotePath);
249 result = operation.execute(client);
250 if (result.isSuccess()){
251 OCFile remoteFolder = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0));
252
253 if (!mIgnoreETag) {
254 // check if remote and local folder are different
255 String remoteFolderETag = remoteFolder.getEtag();
256 if (remoteFolderETag != null) {
257 mRemoteFolderChanged =
258 !(remoteFolderETag.equalsIgnoreCase(mLocalFolder.getEtag()));
259 } else {
260 Log_OC.e(TAG, "Checked " + mAccount.name + remotePath + " : " +
261 "No ETag received from server");
262 }
263 }
264
265 result = new RemoteOperationResult(ResultCode.OK);
266
267 Log_OC.i(TAG, "Checked " + mAccount.name + remotePath + " : " +
268 (mRemoteFolderChanged ? "changed" : "not changed"));
269
270 } else {
271 // check failed
272 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
273 removeLocalFolder();
274 }
275 if (result.isException()) {
276 Log_OC.e(TAG, "Checked " + mAccount.name + remotePath + " : " +
277 result.getLogMessage(), result.getException());
278 } else {
279 Log_OC.e(TAG, "Checked " + mAccount.name + remotePath + " : " +
280 result.getLogMessage());
281 }
282 }
283
284 return result;
285 }
286
287
288 private RemoteOperationResult fetchAndSyncRemoteFolder(OwnCloudClient client) {
289 String remotePath = mLocalFolder.getRemotePath();
290 ReadRemoteFolderOperation operation = new ReadRemoteFolderOperation(remotePath);
291 RemoteOperationResult result = operation.execute(client);
292 Log_OC.d(TAG, "Synchronizing " + mAccount.name + remotePath);
293
294 if (result.isSuccess()) {
295 synchronizeData(result.getData(), client);
296 if (mConflictsFound > 0 || mFailsInFavouritesFound > 0) {
297 result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
298 // should be a different result code, but will do the job
299 }
300 } else {
301 if (result.getCode() == ResultCode.FILE_NOT_FOUND)
302 removeLocalFolder();
303 }
304
305 return result;
306 }
307
308
309 private void removeLocalFolder() {
310 if (mStorageManager.fileExists(mLocalFolder.getFileId())) {
311 String currentSavePath = FileStorageUtils.getSavePath(mAccount.name);
312 mStorageManager.removeFolder(
313 mLocalFolder,
314 true,
315 ( mLocalFolder.isDown() &&
316 mLocalFolder.getStoragePath().startsWith(currentSavePath)
317 )
318 );
319 }
320 }
321
322
323 /**
324 * Synchronizes the data retrieved from the server about the contents of the target folder
325 * with the current data in the local database.
326 *
327 * Grants that mChildren is updated with fresh data after execution.
328 *
329 * @param folderAndFiles Remote folder and children files in Folder
330 *
331 * @param client Client instance to the remote server where the data were
332 * retrieved.
333 * @return 'True' when any change was made in the local data, 'false' otherwise
334 */
335 private void synchronizeData(ArrayList<Object> folderAndFiles, OwnCloudClient client) {
336 // get 'fresh data' from the database
337 mLocalFolder = mStorageManager.getFileByPath(mLocalFolder.getRemotePath());
338
339 // parse data from remote folder
340 OCFile remoteFolder = fillOCFile((RemoteFile)folderAndFiles.get(0));
341 remoteFolder.setParentId(mLocalFolder.getParentId());
342 remoteFolder.setFileId(mLocalFolder.getFileId());
343
344 Log_OC.d(TAG, "Remote folder " + mLocalFolder.getRemotePath()
345 + " changed - starting update of local data ");
346
347 List<OCFile> updatedFiles = new Vector<OCFile>(folderAndFiles.size() - 1);
348 List<SynchronizeFileOperation> filesToSyncContents = new Vector<SynchronizeFileOperation>();
349
350 // get current data about local contents of the folder to synchronize
351 // TODO Enable when "On Device" is recovered ?
352 List<OCFile> localFiles = mStorageManager.getFolderContent(mLocalFolder/*, false*/);
353 Map<String, OCFile> localFilesMap = new HashMap<String, OCFile>(localFiles.size());
354 for (OCFile file : localFiles) {
355 localFilesMap.put(file.getRemotePath(), file);
356 }
357
358 // loop to update every child
359 OCFile remoteFile = null, localFile = null;
360 for (int i=1; i<folderAndFiles.size(); i++) {
361 /// new OCFile instance with the data from the server
362 remoteFile = fillOCFile((RemoteFile)folderAndFiles.get(i));
363 remoteFile.setParentId(mLocalFolder.getFileId());
364
365 /// retrieve local data for the read file
366 // localFile = mStorageManager.getFileByPath(remoteFile.getRemotePath());
367 localFile = localFilesMap.remove(remoteFile.getRemotePath());
368
369 /// add to the remoteFile (the new one) data about LOCAL STATE (not existing in server)
370 remoteFile.setLastSyncDateForProperties(mCurrentSyncTime);
371 if (localFile != null) {
372 // some properties of local state are kept unmodified
373 remoteFile.setFileId(localFile.getFileId());
374 remoteFile.setFavorite(localFile.isFavorite());
375 remoteFile.setLastSyncDateForData(localFile.getLastSyncDateForData());
376 remoteFile.setModificationTimestampAtLastSyncForData(
377 localFile.getModificationTimestampAtLastSyncForData()
378 );
379 remoteFile.setStoragePath(localFile.getStoragePath());
380 // eTag will not be updated unless contents are synchronized
381 // (Synchronize[File|Folder]Operation with remoteFile as parameter)
382 remoteFile.setEtag(localFile.getEtag());
383 if (remoteFile.isFolder()) {
384 remoteFile.setFileLength(localFile.getFileLength());
385 // TODO move operations about size of folders to FileContentProvider
386 } else if (mRemoteFolderChanged && remoteFile.isImage() &&
387 remoteFile.getModificationTimestamp() !=
388 localFile.getModificationTimestamp()) {
389 remoteFile.setNeedsUpdateThumbnail(true);
390 Log.d(TAG, "Image " + remoteFile.getFileName() + " updated on the server");
391 }
392 remoteFile.setPublicLink(localFile.getPublicLink());
393 remoteFile.setShareByLink(localFile.isShareByLink());
394 } else {
395 // remote eTag will not be updated unless contents are synchronized
396 // (Synchronize[File|Folder]Operation with remoteFile as parameter)
397 remoteFile.setEtag("");
398 }
399
400 /// check and fix, if needed, local storage path
401 checkAndFixForeignStoragePath(remoteFile); // policy - local files are COPIED
402 // into the ownCloud local folder;
403 searchForLocalFileInDefaultPath(remoteFile); // legacy
404
405 /// prepare content synchronization for kept-in-sync files
406 if (remoteFile.isFavorite()) {
407 SynchronizeFileOperation operation = new SynchronizeFileOperation( localFile,
408 remoteFile,
409 mAccount,
410 true,
411 mContext
412 );
413
414 filesToSyncContents.add(operation);
415 }
416
417 updatedFiles.add(remoteFile);
418 }
419
420 // save updated contents in local database
421 mStorageManager.saveFolder(remoteFolder, updatedFiles, localFilesMap.values());
422
423 // request for the synchronization of file contents AFTER saving current remote properties
424 startContentSynchronizations(filesToSyncContents, client);
425
426 mChildren = updatedFiles;
427 }
428
429 /**
430 * Performs a list of synchronization operations, determining if a download or upload is needed
431 * or if exists conflict due to changes both in local and remote contents of the each file.
432 *
433 * If download or upload is needed, request the operation to the corresponding service and goes
434 * on.
435 *
436 * @param filesToSyncContents Synchronization operations to execute.
437 * @param client Interface to the remote ownCloud server.
438 */
439 private void startContentSynchronizations(
440 List<SynchronizeFileOperation> filesToSyncContents, OwnCloudClient client
441 ) {
442 RemoteOperationResult contentsResult = null;
443 for (SynchronizeFileOperation op: filesToSyncContents) {
444 contentsResult = op.execute(mStorageManager, mContext); // async
445 if (!contentsResult.isSuccess()) {
446 if (contentsResult.getCode() == ResultCode.SYNC_CONFLICT) {
447 mConflictsFound++;
448 } else {
449 mFailsInFavouritesFound++;
450 if (contentsResult.getException() != null) {
451 Log_OC.e(TAG, "Error while synchronizing favourites : "
452 + contentsResult.getLogMessage(), contentsResult.getException());
453 } else {
454 Log_OC.e(TAG, "Error while synchronizing favourites : "
455 + contentsResult.getLogMessage());
456 }
457 }
458 } // won't let these fails break the synchronization process
459 }
460 }
461
462
463 public boolean isMultiStatus(int status) {
464 return (status == HttpStatus.SC_MULTI_STATUS);
465 }
466
467 /**
468 * Creates and populates a new {@link OCFile} object with the data read from the server.
469 *
470 * @param remote remote file read from the server (remote file or folder).
471 * @return New OCFile instance representing the remote resource described by we.
472 */
473 private OCFile fillOCFile(RemoteFile remote) {
474 OCFile file = new OCFile(remote.getRemotePath());
475 file.setCreationTimestamp(remote.getCreationTimestamp());
476 file.setFileLength(remote.getLength());
477 file.setMimetype(remote.getMimeType());
478 file.setModificationTimestamp(remote.getModifiedTimestamp());
479 file.setEtag(remote.getEtag());
480 file.setPermissions(remote.getPermissions());
481 file.setRemoteId(remote.getRemoteId());
482 return file;
483 }
484
485
486 /**
487 * Checks the storage path of the OCFile received as parameter.
488 * If it's out of the local ownCloud folder, tries to copy the file inside it.
489 *
490 * If the copy fails, the link to the local file is nullified. The account of forgotten
491 * files is kept in {@link #mForgottenLocalFiles}
492 *)
493 * @param file File to check and fix.
494 */
495 private void checkAndFixForeignStoragePath(OCFile file) {
496 String storagePath = file.getStoragePath();
497 String expectedPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, file);
498 if (storagePath != null && !storagePath.equals(expectedPath)) {
499 /// fix storagePaths out of the local ownCloud folder
500 File originalFile = new File(storagePath);
501 if (FileStorageUtils.getUsableSpace(mAccount.name) < originalFile.length()) {
502 mForgottenLocalFiles.put(file.getRemotePath(), storagePath);
503 file.setStoragePath(null);
504
505 } else {
506 InputStream in = null;
507 OutputStream out = null;
508 try {
509 File expectedFile = new File(expectedPath);
510 File expectedParent = expectedFile.getParentFile();
511 expectedParent.mkdirs();
512 if (!expectedParent.isDirectory()) {
513 throw new IOException(
514 "Unexpected error: parent directory could not be created"
515 );
516 }
517 expectedFile.createNewFile();
518 if (!expectedFile.isFile()) {
519 throw new IOException("Unexpected error: target file could not be created");
520 }
521 in = new FileInputStream(originalFile);
522 out = new FileOutputStream(expectedFile);
523 byte[] buf = new byte[1024];
524 int len;
525 while ((len = in.read(buf)) > 0){
526 out.write(buf, 0, len);
527 }
528 file.setStoragePath(expectedPath);
529
530 } catch (Exception e) {
531 Log_OC.e(TAG, "Exception while copying foreign file " + expectedPath, e);
532 mForgottenLocalFiles.put(file.getRemotePath(), storagePath);
533 file.setStoragePath(null);
534
535 } finally {
536 try {
537 if (in != null) in.close();
538 } catch (Exception e) {
539 Log_OC.d(TAG, "Weird exception while closing input stream for "
540 + storagePath + " (ignoring)", e);
541 }
542 try {
543 if (out != null) out.close();
544 } catch (Exception e) {
545 Log_OC.d(TAG, "Weird exception while closing output stream for "
546 + expectedPath + " (ignoring)", e);
547 }
548 }
549 }
550 }
551 }
552
553
554 private RemoteOperationResult refreshSharesForFolder(OwnCloudClient client) {
555 RemoteOperationResult result = null;
556
557 // remote request
558 GetRemoteSharesForFileOperation operation =
559 new GetRemoteSharesForFileOperation(mLocalFolder.getRemotePath(), false, true);
560 result = operation.execute(client);
561
562 if (result.isSuccess()) {
563 // update local database
564 ArrayList<OCShare> shares = new ArrayList<OCShare>();
565 for(Object obj: result.getData()) {
566 shares.add((OCShare) obj);
567 }
568 mStorageManager.saveSharesInFolder(shares, mLocalFolder);
569 }
570
571 return result;
572 }
573
574
575 /**
576 * Scans the default location for saving local copies of files searching for
577 * a 'lost' file with the same full name as the {@link OCFile} received as
578 * parameter.
579 *
580 * @param file File to associate a possible 'lost' local file.
581 */
582 private void searchForLocalFileInDefaultPath(OCFile file) {
583 if (file.getStoragePath() == null && !file.isFolder()) {
584 File f = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file));
585 if (f.exists()) {
586 file.setStoragePath(f.getAbsolutePath());
587 file.setLastSyncDateForData(f.lastModified());
588 }
589 }
590 }
591
592
593 /**
594 * Sends a message to any application component interested in the progress
595 * of the synchronization.
596 *
597 * @param event
598 * @param dirRemotePath Remote path of a folder that was just synchronized
599 * (with or without success)
600 * @param result
601 */
602 private void sendLocalBroadcast(
603 String event, String dirRemotePath, RemoteOperationResult result
604 ) {
605 Log_OC.d(TAG, "Send broadcast " + event);
606 Intent intent = new Intent(event);
607 intent.putExtra(FileSyncAdapter.EXTRA_ACCOUNT_NAME, mAccount.name);
608 if (dirRemotePath != null) {
609 intent.putExtra(FileSyncAdapter.EXTRA_FOLDER_PATH, dirRemotePath);
610 }
611 intent.putExtra(FileSyncAdapter.EXTRA_RESULT, result);
612 mContext.sendStickyBroadcast(intent);
613 //LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
614 }
615
616
617 public boolean getRemoteFolderChanged() {
618 return mRemoteFolderChanged;
619 }
620
621 }