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