SynchronizeFileOperation.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * @author masensio
  6. * Copyright (C) 2012 Bartek Przybylski
  7. * Copyright (C) 2015 ownCloud Inc.
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.operations;
  22. import android.content.Context;
  23. import android.text.TextUtils;
  24. import com.nextcloud.client.account.User;
  25. import com.nextcloud.client.files.downloader.FilesDownloadHelper;
  26. import com.owncloud.android.datamodel.FileDataStorageManager;
  27. import com.owncloud.android.datamodel.OCFile;
  28. import com.owncloud.android.files.services.FileUploader;
  29. import com.owncloud.android.files.services.NameCollisionPolicy;
  30. import com.owncloud.android.lib.common.OwnCloudClient;
  31. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  32. import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
  33. import com.owncloud.android.lib.common.utils.Log_OC;
  34. import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation;
  35. import com.owncloud.android.lib.resources.files.model.RemoteFile;
  36. import com.owncloud.android.operations.common.SyncOperation;
  37. import com.owncloud.android.utils.FileStorageUtils;
  38. /**
  39. * Remote operation performing the read of remote file in the ownCloud server.
  40. */
  41. public class SynchronizeFileOperation extends SyncOperation {
  42. private static final String TAG = SynchronizeFileOperation.class.getSimpleName();
  43. private OCFile mLocalFile;
  44. private String mRemotePath;
  45. private OCFile mServerFile;
  46. private User mUser;
  47. private boolean mSyncFileContents;
  48. private Context mContext;
  49. private boolean mTransferWasRequested;
  50. /**
  51. * When 'false', uploads to the server are not done; only downloads or conflict detection.
  52. * This is a temporal field.
  53. * TODO Remove when 'folder synchronization' replaces 'folder download'.
  54. */
  55. private boolean mAllowUploads;
  56. /**
  57. * Constructor for "full synchronization mode".
  58. * <p/>
  59. * Uses remotePath to retrieve all the data both in local cache and in the remote OC server
  60. * when the operation is executed, instead of reusing {@link OCFile} instances.
  61. * <p/>
  62. * Useful for direct synchronization of a single file.
  63. *
  64. * @param remotePath remote path of the file
  65. * @param user Nextcloud user owning the file.
  66. * @param syncFileContents When 'true', transference of data will be started by the
  67. * operation if needed and no conflict is detected.
  68. * @param context Android context; needed to start transfers.
  69. */
  70. public SynchronizeFileOperation(
  71. String remotePath,
  72. User user,
  73. boolean syncFileContents,
  74. Context context,
  75. FileDataStorageManager storageManager) {
  76. super(storageManager);
  77. mRemotePath = remotePath;
  78. mLocalFile = null;
  79. mServerFile = null;
  80. mUser = user;
  81. mSyncFileContents = syncFileContents;
  82. mContext = context;
  83. mAllowUploads = true;
  84. }
  85. /**
  86. * Constructor allowing to reuse {@link OCFile} instances just queried from local cache or
  87. * from remote OC server.
  88. *
  89. * Useful to include this operation as part of the synchronization of a folder
  90. * (or a full account), avoiding the repetition of fetch operations (both in local database
  91. * or remote server).
  92. *
  93. * At least one of localFile or serverFile MUST NOT BE NULL. If you don't have none of them,
  94. * use the other constructor.
  95. *
  96. * @param localFile Data of file (just) retrieved from local cache/database.
  97. * @param serverFile Data of file (just) retrieved from a remote server. If null,
  98. * will be retrieved from network by the operation when executed.
  99. * @param user Nextcloud user owning the file.
  100. * @param syncFileContents When 'true', transference of data will be started by the
  101. * operation if needed and no conflict is detected.
  102. * @param context Android context; needed to start transfers.
  103. */
  104. public SynchronizeFileOperation(
  105. OCFile localFile,
  106. OCFile serverFile,
  107. User user,
  108. boolean syncFileContents,
  109. Context context,
  110. FileDataStorageManager storageManager) {
  111. super(storageManager);
  112. mLocalFile = localFile;
  113. mServerFile = serverFile;
  114. if (mLocalFile != null) {
  115. mRemotePath = mLocalFile.getRemotePath();
  116. if (mServerFile != null && !mServerFile.getRemotePath().equals(mRemotePath)) {
  117. throw new IllegalArgumentException("serverFile and localFile do not correspond" +
  118. " to the same OC file");
  119. }
  120. } else if (mServerFile != null) {
  121. mRemotePath = mServerFile.getRemotePath();
  122. } else {
  123. throw new IllegalArgumentException("Both serverFile and localFile are NULL");
  124. }
  125. mUser = user;
  126. mSyncFileContents = syncFileContents;
  127. mContext = context;
  128. mAllowUploads = true;
  129. }
  130. /**
  131. * Temporal constructor.
  132. *
  133. * Extends the previous one to allow constrained synchronizations where uploads are never
  134. * performed - only downloads or conflict detection.
  135. *
  136. * Do not use unless you are involved in 'folder synchronization' or 'folder download' work
  137. * in progress.
  138. *
  139. * TODO Remove when 'folder synchronization' replaces 'folder download'.
  140. *
  141. * @param localFile Data of file (just) retrieved from local cache/database.
  142. * MUSTN't be null.
  143. * @param serverFile Data of file (just) retrieved from a remote server.
  144. * If null, will be retrieved from network by the operation
  145. * when executed.
  146. * @param user Nextcloud user owning the file.
  147. * @param syncFileContents When 'true', transference of data will be started by the
  148. * operation if needed and no conflict is detected.
  149. * @param allowUploads When 'false', uploads to the server are not done;
  150. * only downloads or conflict detection.
  151. * @param context Android context; needed to start transfers.
  152. */
  153. public SynchronizeFileOperation(
  154. OCFile localFile,
  155. OCFile serverFile,
  156. User user,
  157. boolean syncFileContents,
  158. boolean allowUploads,
  159. Context context,
  160. FileDataStorageManager storageManager) {
  161. this(localFile, serverFile, user, syncFileContents, context, storageManager);
  162. mAllowUploads = allowUploads;
  163. }
  164. @Override
  165. protected RemoteOperationResult run(OwnCloudClient client) {
  166. RemoteOperationResult result = null;
  167. mTransferWasRequested = false;
  168. if (mLocalFile == null) {
  169. // Get local file from the DB
  170. mLocalFile = getStorageManager().getFileByPath(mRemotePath);
  171. }
  172. if (!mLocalFile.isDown()) {
  173. /// easy decision
  174. requestForDownload(mLocalFile);
  175. result = new RemoteOperationResult(ResultCode.OK);
  176. } else {
  177. /// local copy in the device -> need to think a bit more before do anything
  178. if (mServerFile == null) {
  179. ReadFileRemoteOperation operation = new ReadFileRemoteOperation(mRemotePath);
  180. result = operation.execute(client);
  181. if (result.isSuccess()) {
  182. mServerFile = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0));
  183. mServerFile.setLastSyncDateForProperties(System.currentTimeMillis());
  184. } else if (result.getCode() != ResultCode.FILE_NOT_FOUND) {
  185. return result;
  186. }
  187. }
  188. if (mServerFile != null) {
  189. /// check changes in server and local file
  190. boolean serverChanged;
  191. if (TextUtils.isEmpty(mLocalFile.getEtag())) {
  192. // file uploaded (null) or downloaded ("") before upgrade to version 1.8.0; check the old condition
  193. serverChanged = mServerFile.getModificationTimestamp() !=
  194. mLocalFile.getModificationTimestampAtLastSyncForData();
  195. } else {
  196. serverChanged = !mServerFile.getEtag().equals(mLocalFile.getEtag());
  197. }
  198. boolean localChanged =
  199. mLocalFile.getLocalModificationTimestamp() > mLocalFile.getLastSyncDateForData();
  200. /// decide action to perform depending upon changes
  201. //if (!mLocalFile.getEtag().isEmpty() && localChanged && serverChanged) {
  202. if (localChanged && serverChanged) {
  203. result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
  204. getStorageManager().saveConflict(mLocalFile, mServerFile.getEtag());
  205. } else if (localChanged) {
  206. if (mSyncFileContents && mAllowUploads) {
  207. requestForUpload(mLocalFile);
  208. // the local update of file properties will be done by the FileUploader
  209. // service when the upload finishes
  210. } else {
  211. // NOTHING TO DO HERE: updating the properties of the file in the server
  212. // without uploading the contents would be stupid;
  213. // So, an instance of SynchronizeFileOperation created with
  214. // syncFileContents == false is completely useless when we suspect
  215. // that an upload is necessary (for instance, in FileObserverService).
  216. Log_OC.d(TAG, "Nothing to do here");
  217. }
  218. result = new RemoteOperationResult(ResultCode.OK);
  219. } else if (serverChanged) {
  220. mLocalFile.setRemoteId(mServerFile.getRemoteId());
  221. if (mSyncFileContents) {
  222. requestForDownload(mLocalFile); // local, not server; we won't to keep
  223. // the value of favorite!
  224. // the update of local data will be done later by the FileUploader
  225. // service when the upload finishes
  226. } else {
  227. // TODO CHECK: is this really useful in some point in the code?
  228. mServerFile.setFavorite(mLocalFile.isFavorite());
  229. mServerFile.setHidden(mLocalFile.shouldHide());
  230. mServerFile.setLastSyncDateForData(mLocalFile.getLastSyncDateForData());
  231. mServerFile.setStoragePath(mLocalFile.getStoragePath());
  232. mServerFile.setParentId(mLocalFile.getParentId());
  233. mServerFile.setEtag(mLocalFile.getEtag());
  234. getStorageManager().saveFile(mServerFile);
  235. }
  236. result = new RemoteOperationResult(ResultCode.OK);
  237. } else {
  238. // nothing changed, nothing to do
  239. result = new RemoteOperationResult(ResultCode.OK);
  240. }
  241. // safe blanket: sync'ing a not in-conflict file will clean wrong conflict markers in ancestors
  242. if (result.getCode() != ResultCode.SYNC_CONFLICT) {
  243. getStorageManager().saveConflict(mLocalFile, null);
  244. }
  245. } else {
  246. // remote file does not exist, deleting local copy
  247. boolean deleteResult = getStorageManager().removeFile(mLocalFile, true, true);
  248. if (deleteResult) {
  249. result = new RemoteOperationResult(ResultCode.FILE_NOT_FOUND);
  250. } else {
  251. Log_OC.e(TAG, "Removal of local copy failed (remote file does not exist any longer).");
  252. }
  253. }
  254. }
  255. Log_OC.i(TAG, "Synchronizing " + mUser.getAccountName() + ", file " + mLocalFile.getRemotePath() +
  256. ": " + result.getLogMessage());
  257. return result;
  258. }
  259. /**
  260. * Requests for an upload to the FileUploader service
  261. *
  262. * @param file OCFile object representing the file to upload
  263. */
  264. private void requestForUpload(OCFile file) {
  265. FileUploader.uploadUpdateFile(
  266. mContext,
  267. mUser,
  268. file,
  269. FileUploader.LOCAL_BEHAVIOUR_MOVE,
  270. NameCollisionPolicy.OVERWRITE
  271. );
  272. mTransferWasRequested = true;
  273. }
  274. /**
  275. * Requests for a download to the FileDownloader service
  276. *
  277. * @param file OCFile object representing the file to download
  278. */
  279. private void requestForDownload(OCFile file) {
  280. FilesDownloadHelper downloadHelper = new FilesDownloadHelper();
  281. downloadHelper.downloadFile(
  282. mUser,
  283. file);
  284. mTransferWasRequested = true;
  285. }
  286. public boolean transferWasRequested() {
  287. return mTransferWasRequested;
  288. }
  289. public OCFile getLocalFile() {
  290. return mLocalFile;
  291. }
  292. }